Add support for new location APIs
Related blueprint new-location-apis Change-Id: Id378cd5b7d20775b5117ee3f509bd6bdd61702e3
This commit is contained in:
@@ -560,3 +560,33 @@ class Controller(object):
|
||||
req_id_hdr = {'x-openstack-request-id': response.request_ids[0]}
|
||||
|
||||
return self._get(image_id, req_id_hdr)
|
||||
|
||||
def add_image_location(self, image_id, location_url, validation_data={}):
|
||||
"""Add a new location to an image.
|
||||
|
||||
:param image_id: ID of image to which the location is to be added.
|
||||
:param location_url: URL of the location to add.
|
||||
:param validation_data: Validation data for the image.
|
||||
"""
|
||||
if not utils.has_version(self.http_client, 'v2.17'):
|
||||
raise exc.HTTPNotImplemented(
|
||||
'This operation is not supported by Glance.')
|
||||
|
||||
url = '/v2/images/%s/locations' % image_id
|
||||
data = {'url': location_url,
|
||||
'validation_data': validation_data}
|
||||
resp, body = self.http_client.post(url, data=data)
|
||||
return self._get(image_id)
|
||||
|
||||
@utils.add_req_id_to_object()
|
||||
def get_image_locations(self, image_id):
|
||||
"""Fetch list of locations associated to the Image.
|
||||
|
||||
:param image_id: ID of image to which the location is to be fetched.
|
||||
"""
|
||||
if not utils.has_version(self.http_client, 'v2.17'):
|
||||
raise exc.HTTPNotImplemented(
|
||||
'This operation is not supported by Glance.')
|
||||
url = '/v2/images/%s/locations' % (image_id)
|
||||
resp, locations = self.http_client.get(url)
|
||||
return locations, resp
|
||||
|
||||
@@ -1012,6 +1012,43 @@ def do_location_update(gc, args):
|
||||
utils.print_dict(image)
|
||||
|
||||
|
||||
@utils.arg('--url', metavar='<URL>', required=True,
|
||||
help=_('URL of location to add.'))
|
||||
@utils.arg('--validation-data', metavar='<STRING>', default='{}',
|
||||
help=_('Validation data containing os_hash_algo and os_hash_value '
|
||||
'only associated to the image. Must be a valid JSON object '
|
||||
'(default: %(default)s)'))
|
||||
@utils.arg('id', metavar='<IMAGE_ID>',
|
||||
help=_('ID of image whose location is to be added.'))
|
||||
def do_add_location(gc, args):
|
||||
"""Add location to an image which is in `queued` state only. """
|
||||
try:
|
||||
invalid_val_data = None
|
||||
validation_data = json.loads(args.validation_data)
|
||||
accepted_values = ['os_hash_algo', 'os_hash_value']
|
||||
invalid_val_data = list(set(validation_data.keys()).difference(
|
||||
accepted_values))
|
||||
if invalid_val_data:
|
||||
utils.exit('Validation Data should contain only os_hash_algo '
|
||||
'and os_hash_value. `%s` is not allowed' %
|
||||
(*invalid_val_data,))
|
||||
|
||||
allowed_hash_algo = ['sha512', 'sha256', 'sha1', 'md5']
|
||||
if validation_data and \
|
||||
validation_data['os_hash_algo'] not in allowed_hash_algo:
|
||||
raise utils.exit('os_hash_algo: `%s` is incorrect, '
|
||||
'allowed hashing algorithms: %s' %
|
||||
(validation_data['os_hash_algo'],
|
||||
allowed_hash_algo))
|
||||
|
||||
except ValueError:
|
||||
utils.exit('validation-data is not a valid JSON object.')
|
||||
else:
|
||||
image = gc.images.add_image_location(args.id, args.url,
|
||||
validation_data=validation_data)
|
||||
utils.print_image(image)
|
||||
|
||||
|
||||
# Metadata - catalog
|
||||
NAMESPACE_SCHEMA = None
|
||||
|
||||
|
||||
Reference in New Issue
Block a user