summaryrefslogtreecommitdiff
path: root/glanceclient
diff options
context:
space:
mode:
Diffstat (limited to 'glanceclient')
-rw-r--r--glanceclient/common/utils.py4
-rwxr-xr-xglanceclient/shell.py24
-rw-r--r--glanceclient/tests/unit/test_shell.py19
-rw-r--r--glanceclient/tests/unit/test_utils.py1
-rw-r--r--glanceclient/v1/shell.py2
-rw-r--r--glanceclient/v2/shell.py6
6 files changed, 38 insertions, 18 deletions
diff --git a/glanceclient/common/utils.py b/glanceclient/common/utils.py
index c056063..955a365 100644
--- a/glanceclient/common/utils.py
+++ b/glanceclient/common/utils.py
@@ -307,8 +307,10 @@ def save_image(data, path):
def make_size_human_readable(size):
suffix = ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB']
base = 1024.0
-
index = 0
+
+ if size is None:
+ size = 0
while size >= base:
index = index + 1
size = size / base
diff --git a/glanceclient/shell.py b/glanceclient/shell.py
index 8d51003..06b2c46 100755
--- a/glanceclient/shell.py
+++ b/glanceclient/shell.py
@@ -225,7 +225,7 @@ class OpenStackImagesShell(object):
parser.add_argument('-v', '--verbose',
default=False, action="store_true",
- help="Print more verbose output")
+ help="Print more verbose output.")
parser.add_argument('--get-schema',
default=False, action="store_true",
@@ -236,7 +236,7 @@ class OpenStackImagesShell(object):
parser.add_argument('--timeout',
default=600,
- help='Number of seconds to wait for a response')
+ help='Number of seconds to wait for a response.')
parser.add_argument('--no-ssl-compression',
dest='ssl_compression',
@@ -443,18 +443,13 @@ class OpenStackImagesShell(object):
return ks_session
def _get_endpoint_and_token(self, args):
- image_url = self._get_image_url(args)
+ endpoint = self._get_image_url(args)
auth_token = args.os_auth_token
- auth_reqd = (not (auth_token and image_url) or
- (hasattr(args, 'func') and
- utils.is_authentication_required(args.func)))
-
- if not auth_reqd:
- endpoint = image_url
- token = args.os_auth_token
- else:
+ auth_req = (hasattr(args, 'func') and
+ utils.is_authentication_required(args.func))
+ if auth_req and not (endpoint and auth_token):
if not args.os_username:
raise exc.CommandError(
_("You must provide a username via"
@@ -527,7 +522,7 @@ class OpenStackImagesShell(object):
'key': args.os_key
}
ks_session = self._get_keystone_session(**kwargs)
- token = args.os_auth_token or ks_session.get_token()
+ auth_token = args.os_auth_token or ks_session.get_token()
endpoint_type = args.os_endpoint_type or 'public'
service_type = args.os_service_type or 'image'
@@ -536,7 +531,7 @@ class OpenStackImagesShell(object):
interface=endpoint_type,
region_name=args.os_region_name)
- return endpoint, token
+ return endpoint, auth_token
def _get_versioned_client(self, api_version, args):
endpoint, token = self._get_endpoint_and_token(args)
@@ -580,6 +575,9 @@ class OpenStackImagesShell(object):
schema = client.schemas.get(resource)
with open(schema_file_path, 'w') as f:
f.write(json.dumps(schema.raw()))
+ except exc.Unauthorized:
+ raise exc.CommandError(
+ "Invalid OpenStack Identity credentials.")
except Exception:
# NOTE(esheffield) do nothing here, we'll get a message
# later if the schema is missing
diff --git a/glanceclient/tests/unit/test_shell.py b/glanceclient/tests/unit/test_shell.py
index 6c37462..3247d05 100644
--- a/glanceclient/tests/unit/test_shell.py
+++ b/glanceclient/tests/unit/test_shell.py
@@ -295,6 +295,25 @@ class ShellTest(testutils.TestCase):
glance_shell.main(args.split())
self._assert_auth_plugin_args()
+ @mock.patch('glanceclient.Client')
+ def test_endpoint_token_no_auth_req(self, mock_client):
+
+ def verify_input(version=None, endpoint=None, *args, **kwargs):
+ self.assertIn('token', kwargs)
+ self.assertEqual(TOKEN_ID, kwargs['token'])
+ self.assertEqual(DEFAULT_IMAGE_URL, endpoint)
+ return mock.MagicMock()
+
+ mock_client.side_effect = verify_input
+ glance_shell = openstack_shell.OpenStackImagesShell()
+ args = ['--os-image-api-version', '2',
+ '--os-auth-token', TOKEN_ID,
+ '--os-image-url', DEFAULT_IMAGE_URL,
+ 'image-list']
+
+ glance_shell.main(args)
+ self.assertEqual(1, mock_client.call_count)
+
@mock.patch('sys.stdin', side_effect=mock.MagicMock)
@mock.patch('getpass.getpass', return_value='password')
@mock.patch('glanceclient.v2.client.Client')
diff --git a/glanceclient/tests/unit/test_utils.py b/glanceclient/tests/unit/test_utils.py
index a51548c..0b2d6d9 100644
--- a/glanceclient/tests/unit/test_utils.py
+++ b/glanceclient/tests/unit/test_utils.py
@@ -33,6 +33,7 @@ class TestUtils(testtools.TestCase):
self.assertEqual("1MB", utils.make_size_human_readable(1048576))
self.assertEqual("1.4GB", utils.make_size_human_readable(1476395008))
self.assertEqual("9.3MB", utils.make_size_human_readable(9761280))
+ self.assertEqual("0B", utils.make_size_human_readable(None))
def test_get_new_file_size(self):
size = 98304
diff --git a/glanceclient/v1/shell.py b/glanceclient/v1/shell.py
index c28dc23..3372951 100644
--- a/glanceclient/v1/shell.py
+++ b/glanceclient/v1/shell.py
@@ -410,7 +410,7 @@ def do_member_list(gc, args):
@utils.arg('image', metavar='<IMAGE>',
help='Image to add member to.')
@utils.arg('tenant_id', metavar='<TENANT_ID>',
- help='Tenant to add as member')
+ help='Tenant to add as member.')
@utils.arg('--can-share', action='store_true', default=False,
help='Allow the specified tenant to share this image.')
def do_member_create(gc, args):
diff --git a/glanceclient/v2/shell.py b/glanceclient/v2/shell.py
index 08271e0..593630d 100644
--- a/glanceclient/v2/shell.py
+++ b/glanceclient/v2/shell.py
@@ -353,7 +353,7 @@ def do_image_tag_delete(gc, args):
help='URL of location to add.')
@utils.arg('--metadata', metavar='<STRING>', default='{}',
help=('Metadata associated with the location. '
- 'Must be a valid JSON object (default: %(default)s)'))
+ 'Must be a valid JSON object (default: %(default)s).'))
@utils.arg('id', metavar='<ID>',
help='ID of image to which the location is to be added.')
def do_location_add(gc, args):
@@ -380,7 +380,7 @@ def do_location_delete(gc, args):
help='URL of location to update.')
@utils.arg('--metadata', metavar='<STRING>', default='{}',
help=('Metadata associated with the location. '
- 'Must be a valid JSON object (default: %(default)s)'))
+ 'Must be a valid JSON object (default: %(default)s).'))
@utils.arg('id', metavar='<ID>',
help='ID of image whose location is to be updated.')
def do_location_update(gc, args):
@@ -940,7 +940,7 @@ def do_task_show(gc, args):
help='Type of Task. Please refer to Glance schema or documentation'
' to see which tasks are supported.')
@utils.arg('--input', metavar='<STRING>', default='{}',
- help='Parameters of the task to be launched')
+ help='Parameters of the task to be launched.')
def do_task_create(gc, args):
"""Create a new task."""
if not (args.type and args.input):