diff --git a/novaclient/client.py b/novaclient/client.py index f66dcfe5c..9bd838742 100644 --- a/novaclient/client.py +++ b/novaclient/client.py @@ -86,7 +86,8 @@ class HTTPClient(httplib2.Http): proxy_token=None, region_name=None, endpoint_type='publicURL', service_type=None, service_name=None, volume_service_name=None, - timings=False, bypass_url=None, no_cache=False, + timings=False, bypass_url=None, + os_cache=False, no_cache=True, http_log_debug=False, auth_system='keystone'): super(HTTPClient, self).__init__(timeout=timeout, proxy_info=_get_proxy_info()) @@ -106,7 +107,7 @@ class HTTPClient(httplib2.Http): self.volume_service_name = volume_service_name self.timings = timings self.bypass_url = bypass_url - self.no_cache = no_cache + self.os_cache = os_cache or not no_cache self.http_log_debug = http_log_debug self.times = [] # [("item", starttime, endtime), ...] @@ -130,8 +131,7 @@ class HTTPClient(httplib2.Http): self._logger.addHandler(ch) def use_token_cache(self, use_it): - # One day I'll stop using negative naming. - self.no_cache = not use_it + self.os_cache = use_it def unauthenticate(self): """Forget all of our authentication information.""" @@ -316,7 +316,7 @@ class HTTPClient(httplib2.Http): if key is None: keys[index] = '?' keyring_key = "/".join(keys) - if not self.no_cache and not self.used_keyring: + if self.os_cache and not self.used_keyring: # Lookup the token/mgmt url from the keyring first time # through. # If we come through again, it's because the old token @@ -388,7 +388,7 @@ class HTTPClient(httplib2.Http): self.set_management_url(self.bypass_url) # Store the token/mgmt url in the keyring for later requests. - if has_keyring and not self.no_cache: + if has_keyring and self.os_cache: try: keyring_value = "%s|%s" % (self.auth_token, self.management_url) diff --git a/novaclient/shell.py b/novaclient/shell.py index c15893f0e..98df2c0ab 100644 --- a/novaclient/shell.py +++ b/novaclient/shell.py @@ -93,11 +93,19 @@ class OpenStackComputeShell(object): help="Print debugging output") parser.add_argument('--no-cache', - default=utils.env('OS_NO_CACHE', default=False), - action='store_true', - help="Don't use the auth token cache.") - parser.add_argument('--no_cache', + default=utils.env('OS_NO_CACHE', default=True), + action='store_false', + dest='os_cache', help=argparse.SUPPRESS) + parser.add_argument('--no_cache', + action='store_false', + dest='os_cache', + help=argparse.SUPPRESS) + + parser.add_argument('--os-cache', + default=utils.env('OS_CACHE', default=False), + action='store_true', + help="Use the auth token cache.") parser.add_argument('--timings', default=False, @@ -384,7 +392,7 @@ class OpenStackComputeShell(object): os_region_name, os_auth_system, endpoint_type, insecure, service_type, service_name, volume_service_name, username, apikey, projectid, url, region_name, - bypass_url, no_cache) = ( + bypass_url, os_cache) = ( args.os_username, args.os_password, args.os_tenant_name, args.os_auth_url, args.os_region_name, args.os_auth_system, @@ -392,7 +400,7 @@ class OpenStackComputeShell(object): args.service_name, args.volume_service_name, args.username, args.apikey, args.projectid, args.url, args.region_name, - args.bypass_url, args.no_cache) + args.bypass_url, args.os_cache) if not endpoint_type: endpoint_type = DEFAULT_NOVA_ENDPOINT_TYPE @@ -463,7 +471,7 @@ class OpenStackComputeShell(object): service_name=service_name, auth_system=os_auth_system, volume_service_name=volume_service_name, timings=args.timings, bypass_url=bypass_url, - no_cache=no_cache, http_log_debug=options.debug) + os_cache=os_cache, http_log_debug=options.debug) try: if not utils.isunauthenticated(args.func): diff --git a/novaclient/v1_1/client.py b/novaclient/v1_1/client.py index d9e8720ca..6b2d3d3fa 100644 --- a/novaclient/v1_1/client.py +++ b/novaclient/v1_1/client.py @@ -53,8 +53,8 @@ class Client(object): endpoint_type='publicURL', extensions=None, service_type='compute', service_name=None, volume_service_name=None, timings=False, - bypass_url=None, no_cache=False, http_log_debug=False, - auth_system='keystone'): + bypass_url=None, os_cache=False, no_cache=True, + http_log_debug=False, auth_system='keystone'): # FIXME(comstud): Rename the api_key argument above when we # know it's not being used as keyword argument password = api_key @@ -92,6 +92,7 @@ class Client(object): self.services = services.ServiceManager(self) self.fixed_ips = fixed_ips.FixedIPsManager(self) self.floating_ips_bulk = floating_ips_bulk.FloatingIPBulkManager(self) + self.os_cache = os_cache or not no_cache # Add in any extensions... if extensions: @@ -116,7 +117,7 @@ class Client(object): volume_service_name=volume_service_name, timings=timings, bypass_url=bypass_url, - no_cache=no_cache, + os_cache=self.os_cache, http_log_debug=http_log_debug) def set_management_url(self, url): diff --git a/tests/test_auth_plugins.py b/tests/test_auth_plugins.py index 8a3a53238..c20ea62de 100644 --- a/tests/test_auth_plugins.py +++ b/tests/test_auth_plugins.py @@ -89,8 +89,7 @@ class AuthPluginTest(utils.TestCase): @mock.patch.object(httplib2.Http, "request", mock_request) def test_auth_call(): cs = client.Client("username", "password", "project_id", - "auth_url/v2.0", auth_system="fake", - no_cache=True) + "auth_url/v2.0", auth_system="fake") cs.client.authenticate() headers = requested_headers(cs) @@ -113,8 +112,7 @@ class AuthPluginTest(utils.TestCase): @mock.patch.object(httplib2.Http, "request", mock_request) def test_auth_call(): cs = client.Client("username", "password", "project_id", - "auth_url/v2.0", auth_system="notexists", - no_cache=True) + "auth_url/v2.0", auth_system="notexists") self.assertRaises(exceptions.AuthSystemNotFound, cs.client.authenticate) @@ -151,8 +149,7 @@ class AuthPluginTest(utils.TestCase): @mock.patch.object(httplib2.Http, "request", mock_request) def test_auth_call(): cs = client.Client("username", "password", "project_id", - auth_system="fakewithauthurl", - no_cache=True) + auth_system="fakewithauthurl") cs.client.authenticate() self.assertEquals(cs.client.auth_url, "http://faked/v2.0") @@ -176,7 +173,6 @@ class AuthPluginTest(utils.TestCase): def test_auth_call(): with self.assertRaises(exceptions.EndpointNotFound): cs = client.Client("username", "password", "project_id", - auth_system="fakewithauthurl", - no_cache=True) + auth_system="fakewithauthurl") test_auth_call() diff --git a/tests/test_client.py b/tests/test_client.py index a1a454655..e2fc9d965 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -24,3 +24,27 @@ class ClientTest(utils.TestCase): def test_get_client_class_unknown(self): self.assertRaises(novaclient.exceptions.UnsupportedVersion, novaclient.client.get_client_class, '0') + + def test_client_with_os_cache_enabled(self): + cs = novaclient.v1_1.client.Client("user", "password", "project_id", + auth_url="foo/v2", os_cache=True) + self.assertEqual(True, cs.os_cache) + self.assertEqual(True, cs.client.os_cache) + + def test_client_with_os_cache_disabled(self): + cs = novaclient.v1_1.client.Client("user", "password", "project_id", + auth_url="foo/v2", os_cache=False) + self.assertEqual(False, cs.os_cache) + self.assertEqual(False, cs.client.os_cache) + + def test_client_with_no_cache_enabled(self): + cs = novaclient.v1_1.client.Client("user", "password", "project_id", + auth_url="foo/v2", no_cache=True) + self.assertEqual(False, cs.os_cache) + self.assertEqual(False, cs.client.os_cache) + + def test_client_with_no_cache_disabled(self): + cs = novaclient.v1_1.client.Client("user", "password", "project_id", + auth_url="foo/v2", no_cache=False) + self.assertEqual(True, cs.os_cache) + self.assertEqual(True, cs.client.os_cache) diff --git a/tests/v1_1/test_auth.py b/tests/v1_1/test_auth.py index f1ae081f8..5f312d637 100644 --- a/tests/v1_1/test_auth.py +++ b/tests/v1_1/test_auth.py @@ -18,8 +18,7 @@ def to_http_response(resp_dict): class AuthenticateAgainstKeystoneTests(utils.TestCase): def test_authenticate_success(self): cs = client.Client("username", "password", "project_id", - "auth_url/v2.0", service_type='compute', - no_cache=True) + "auth_url/v2.0", service_type='compute') resp = { "access": { "token": { @@ -82,7 +81,7 @@ class AuthenticateAgainstKeystoneTests(utils.TestCase): def test_authenticate_failure(self): cs = client.Client("username", "password", "project_id", - "auth_url/v2.0", no_cache=True) + "auth_url/v2.0") resp = {"unauthorized": {"message": "Unauthorized", "code": "401"}} auth_response = httplib2.Response({ "status": 401, @@ -100,8 +99,7 @@ class AuthenticateAgainstKeystoneTests(utils.TestCase): def test_auth_redirect(self): cs = client.Client("username", "password", "project_id", - "auth_url/v1.0", service_type='compute', - no_cache=True) + "auth_url/v1.0", service_type='compute') dict_correct_response = { "access": { "token": { @@ -182,8 +180,7 @@ class AuthenticateAgainstKeystoneTests(utils.TestCase): def test_ambiguous_endpoints(self): cs = client.Client("username", "password", "project_id", - "auth_url/v2.0", service_type='compute', - no_cache=True) + "auth_url/v2.0", service_type='compute') resp = { "access": { "token": { @@ -235,8 +232,7 @@ class AuthenticateAgainstKeystoneTests(utils.TestCase): class AuthenticationTests(utils.TestCase): def test_authenticate_success(self): - cs = client.Client("username", "password", "project_id", "auth_url", - no_cache=True) + cs = client.Client("username", "password", "project_id", "auth_url") management_url = 'https://localhost/v1.1/443470' auth_response = httplib2.Response({ 'status': 204, @@ -265,8 +261,7 @@ class AuthenticationTests(utils.TestCase): test_auth_call() def test_authenticate_failure(self): - cs = client.Client("username", "password", "project_id", "auth_url", - no_cache=True) + cs = client.Client("username", "password", "project_id", "auth_url") auth_response = httplib2.Response({'status': 401}) mock_request = mock.Mock(return_value=(auth_response, None)) @@ -277,8 +272,7 @@ class AuthenticationTests(utils.TestCase): test_auth_call() def test_auth_automatic(self): - cs = client.Client("username", "password", "project_id", "auth_url", - no_cache=True) + cs = client.Client("username", "password", "project_id", "auth_url") http_client = cs.client http_client.management_url = '' mock_request = mock.Mock(return_value=(None, None)) @@ -293,8 +287,7 @@ class AuthenticationTests(utils.TestCase): test_auth_call() def test_auth_manual(self): - cs = client.Client("username", "password", "project_id", "auth_url", - no_cache=True) + cs = client.Client("username", "password", "project_id", "auth_url") @mock.patch.object(cs.client, 'authenticate') def test_auth_call(m):