From 0b7534c723e97a1ab7eacdb2a25519487fa2cbde Mon Sep 17 00:00:00 2001 From: Chi Lo Date: Mon, 5 Aug 2019 15:26:58 -0700 Subject: [PATCH] Update image using output from Get image This patch allows the output from GET Image API to be used as input json body of UPDATE Image API. Note that the created_at and updated_at parameters, if present in the input json body, will be ignored. Change-Id: I6e21de50d3835338cd4b6c5bcdff396733e9367e --- .../ims/persistency/wsme/models.py | 51 ++++++++++++------- 1 file changed, 32 insertions(+), 19 deletions(-) diff --git a/orm/services/image_manager/ims/persistency/wsme/models.py b/orm/services/image_manager/ims/persistency/wsme/models.py index 11c609eb..7849f472 100755 --- a/orm/services/image_manager/ims/persistency/wsme/models.py +++ b/orm/services/image_manager/ims/persistency/wsme/models.py @@ -141,9 +141,9 @@ class Image(Model): # Output-only fields status = wsme.wsattr(wsme.types.text, mandatory=False) - created_at = wsme.wsattr(wsme.types.IntegerType(minimum=0), + created_at = wsme.wsattr(wsme.types.text, mandatory=False, name='created-at') - updated_at = wsme.wsattr(wsme.types.IntegerType(minimum=0), + updated_at = wsme.wsattr(wsme.types.text, mandatory=False, name='updated-at') locations = wsme.wsattr(wsme.types.ArrayType(str), mandatory=False) self_link = wsme.wsattr(wsme.types.text, mandatory=False, name='self') @@ -165,8 +165,8 @@ class Image(Model): regions=[], customers=[], status='', - created_at=0, - updated_at=0, + created_at='', + updated_at='', locations=[], self_link='', protected=default_protected, @@ -235,8 +235,9 @@ class Image(Model): 'Visibility is private but no customers were' ' specified!') elif self.visibility not in ["private", "public"]: - raise ErrorStatus(400, - "Image visibility can only be 'public' or 'private'") + raise ErrorStatus( + 400, + "Image visibility can only be 'public' or 'private'") # Validate disk format valid_disk_formats = ('ami', 'ari', 'aki', 'vhd', 'vmdk', 'raw', @@ -248,21 +249,28 @@ class Image(Model): customer_input = set() for customer in self.customers: if customer in customer_input: - raise ErrorStatus(400, "customer {} exist more than one".format(customer)) + raise ErrorStatus( + 400, "customer {} exist more than one".format(customer)) customer_input.add(customer) # Validate container format valid_container_formats = ('ami', 'ari', 'aki', 'bare', 'ovf', 'ova', 'docker') if self.container_format not in valid_container_formats: - raise ErrorStatus(400, 'Invalid container format! {}'.format(self.container_format)) + raise ErrorStatus( + 400, + 'Invalid container format! {}'.format(self.container_format)) if int(self.min_ram) not in range(0, self.min_ram + 1, 1024): - raise ErrorStatus(400, "mininum RAM value must be a multiple of 1024") + raise ErrorStatus( + 400, "mininum RAM value must be a multiple of 1024") if context == "update": for region in self.regions: if region.type == "group": - raise ErrorStatus(400, "region {} type is invalid for update, \'group\' can be only in create".format(region.name)) + raise ErrorStatus( + 400, + "region {} type is invalid for update, \'group\' can" + " be only in create".format(region.name)) def to_db_model(self): image = db_models.Image() @@ -332,14 +340,14 @@ class Image(Model): image.protected = sql_image.protected == 1 image.owner = sql_image.owner image.schema = sql_image.schema - image.created_at = sql_image.created_at - image.updated_at = sql_image.updated_at + image.created_at = str(sql_image.created_at) + image.updated_at = str(sql_image.updated_at) for attribute in ('status', 'self_link', 'file'): setattr(image, attribute, getattr(sql_image, attribute, '')) for attribute in ('created_at', 'updated_at'): - setattr(image, attribute, getattr(sql_image, attribute, 0)) + setattr(image, attribute, str(getattr(sql_image, attribute, ''))) setattr(image, 'locations', getattr(sql_image, 'locations', [])) setattr(image, 'links', getattr(sql_image, 'links', {})) @@ -370,16 +378,20 @@ class Image(Model): def handle_region_group(self): regions_to_add = [] - for region in self.regions[:]: # get copy of it to be able to delete from the origin + # get copy of it to be able to delete from the origin + for region in self.regions[:]: if region.type == "group": group_regions = self.get_regions_for_group(region.name) if group_regions is None: - raise ErrorStatus(404, "Group {} does not exist".format(region.name)) + raise ErrorStatus( + 404, "Group {} does not exist".format(region.name)) for group_region in group_regions: - regions_to_add.append(Region(name=group_region, type='single')) + regions_to_add.append( + Region(name=group_region, type='single')) self.regions.remove(region) - self.regions.extend(set(regions_to_add)) # remove duplicates if exist + # remove duplicates if exist + self.regions.extend(set(regions_to_add)) def get_regions_for_group(self, group_name): set_utils_conf(conf) @@ -418,8 +430,9 @@ class ImageWrapper(Model): "existing image") if sql_image.disk_format != new_image.disk_format: - raise ErrorStatus(400, - "Cannot change disk format of existing image") + raise ErrorStatus( + 400, + "Cannot change disk format of existing image") if sql_image.min_ram != new_image.min_ram: raise ErrorStatus(400,