Add CLI for V2 image create, update, and upload

Provides command line support for image-create, image-update,
and image-upload using the Glance V2 API. This includes building
help text for create and update based on the image jsonschema
as fetched from the server.

Also fixes bug caused by default warlock patch generation not
matching what Glance expects when updating a core property
which had not originally been set when the image was created.

Related to bp glance-client-v2

Change-Id: I841f9e3d05802f4b794cb6f4849abe03ff0324d9
This commit is contained in:
eddie-sheffield
2013-08-19 17:27:07 -04:00
parent 7a4a8a0979
commit 32d9c42816
9 changed files with 585 additions and 65 deletions
+86
View File
@@ -18,6 +18,11 @@ import os
import sys
import uuid
if os.name == 'nt':
import msvcrt
else:
msvcrt = None
import prettytable
from glanceclient import exc
@@ -35,6 +40,61 @@ def arg(*args, **kwargs):
return _decorator
def schema_args(schema_getter, omit=[]):
typemap = {
'string': str,
'integer': int,
'boolean': string_to_bool,
'array': list
}
def _decorator(func):
schema = schema_getter()
if schema is None:
param = '<unavailable>'
kwargs = {
'help': ("Please run with connection parameters set to "
"retrieve the schema for generating help for this "
"command")
}
func.__dict__.setdefault('arguments', []).insert(0, ((param, ),
kwargs))
else:
properties = schema.get('properties', {})
for name, property in properties.iteritems():
if name in omit:
continue
param = '--' + name.replace('_', '-')
kwargs = {}
type_str = property.get('type', 'string')
if type_str == 'array':
items = property.get('items')
kwargs['type'] = typemap.get(items.get('type'))
kwargs['nargs'] = '+'
else:
kwargs['type'] = typemap.get(type_str)
if type_str == 'boolean':
kwargs['metavar'] = '[True|False]'
else:
kwargs['metavar'] = '<%s>' % name.upper()
description = property.get('description', "")
if 'enum' in property:
if len(description):
description += " "
description += ("Valid values: " +
', '.join(property.get('enum')))
kwargs['help'] = description
func.__dict__.setdefault('arguments',
[]).insert(0, ((param, ), kwargs))
return func
return _decorator
def pretty_choice_list(l):
return ', '.join("'%s'" % i for i in l)
@@ -224,3 +284,29 @@ def get_file_size(file_obj):
return
else:
raise
def get_data_file(args):
if args.file:
return open(args.file, 'rb')
else:
# distinguish cases where:
# (1) stdin is not valid (as in cron jobs):
# glance ... <&-
# (2) image data is provided through standard input:
# glance ... < /tmp/file or cat /tmp/file | glance ...
# (3) no image data provided:
# glance ...
try:
os.fstat(0)
except OSError:
# (1) stdin is not valid (closed...)
return None
if not sys.stdin.isatty():
# (2) image data is provided through standard input
if msvcrt:
msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY)
return sys.stdin
else:
# (3) no image data provided
return None