Skip to content

Commit

Permalink
Adds --os-cache to replace old --no-cache.
Browse files Browse the repository at this point in the history
Deprecates the old --no-cache option in favor of --os-cache.

The old CLI args (--no_cache and --no-cache) and ENV option
(OS_NO_CACHE) are still supported but no longer show up
in help.

The new option for --os-cache can also be set via the OS_CACHE ENV
variable... which now defaults to False. This should be much more user friendly.

Fixes LP Bug #1087776.

Change-Id: I3cea089c7e11ce75f22c2d7f3242b02b80441323
  • Loading branch information
dprince committed Dec 11, 2012
1 parent f37bbae commit e483455
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 38 deletions.
12 changes: 6 additions & 6 deletions novaclient/client.py
Expand Up @@ -86,7 +86,8 @@ def __init__(self, user, password, projectid, auth_url=None,
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())
Expand All @@ -106,7 +107,7 @@ def __init__(self, user, password, projectid, auth_url=None,
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), ...]
Expand All @@ -130,8 +131,7 @@ def __init__(self, user, password, projectid, auth_url=None,
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."""
Expand Down Expand Up @@ -316,7 +316,7 @@ def authenticate(self):
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
Expand Down Expand Up @@ -388,7 +388,7 @@ def authenticate(self):
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)
Expand Down
20 changes: 14 additions & 6 deletions novaclient/shell.py
Expand Up @@ -93,12 +93,20 @@ def get_base_parser(self):
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.")
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,
action='store_true',
Expand Down Expand Up @@ -384,15 +392,15 @@ def main(self, argv):
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,
args.endpoint_type, args.insecure, args.service_type,
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
Expand Down Expand Up @@ -463,7 +471,7 @@ def main(self, argv):
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):
Expand Down
7 changes: 4 additions & 3 deletions novaclient/v1_1/client.py
Expand Up @@ -53,8 +53,8 @@ def __init__(self, username, api_key, project_id, auth_url=None,
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
Expand Down Expand Up @@ -92,6 +92,7 @@ def __init__(self, username, api_key, project_id, auth_url=None,
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:
Expand All @@ -116,7 +117,7 @@ def __init__(self, username, api_key, project_id, auth_url=None,
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):
Expand Down
12 changes: 4 additions & 8 deletions tests/test_auth_plugins.py
Expand Up @@ -89,8 +89,7 @@ def mock_iter_entry_points(_type):
@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)
Expand All @@ -113,8 +112,7 @@ def mock_iter_entry_points(_t):
@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)

Expand Down Expand Up @@ -151,8 +149,7 @@ def mock_iter_entry_points(_type):
@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")

Expand All @@ -176,7 +173,6 @@ def mock_iter_entry_points(_type):
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()
24 changes: 24 additions & 0 deletions tests/test_client.py
Expand Up @@ -24,3 +24,27 @@ def test_get_client_class_v1_1(self):
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)
23 changes: 8 additions & 15 deletions tests/v1_1/test_auth.py
Expand Up @@ -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": {
Expand Down Expand Up @@ -82,7 +81,7 @@ def test_auth_call():

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,
Expand All @@ -100,8 +99,7 @@ def test_auth_call():

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": {
Expand Down Expand Up @@ -182,8 +180,7 @@ def test_auth_call():

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": {
Expand Down Expand Up @@ -235,8 +232,7 @@ def test_auth_call():

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,
Expand Down Expand Up @@ -265,8 +261,7 @@ def test_auth_call():
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))

Expand All @@ -277,8 +272,7 @@ def test_auth_call():
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))
Expand All @@ -293,8 +287,7 @@ def test_auth_call(m):
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):
Expand Down

0 comments on commit e483455

Please sign in to comment.