From 2504a398f9ac9659986abaeb0ba19d6550eeb309 Mon Sep 17 00:00:00 2001 From: Larry Aiello Date: Sun, 26 Jul 2020 08:51:26 -0400 Subject: [PATCH] Implement ec2.register_image() (#3177) * implement register_image * format code --- moto/ec2/models.py | 13 +++++++++++++ moto/ec2/responses/amis.py | 11 ++++++++++- tests/test_ec2/test_amis.py | 10 ++++++++++ 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/moto/ec2/models.py b/moto/ec2/models.py index 6e320c0ae..ad9ae3b1b 100644 --- a/moto/ec2/models.py +++ b/moto/ec2/models.py @@ -1516,6 +1516,19 @@ class AmiBackend(object): return True + def register_image(self, name=None, description=None): + ami_id = random_ami_id() + ami = Ami( + self, + ami_id, + instance=None, + source_ami=None, + name=name, + description=description, + ) + self.amis[ami_id] = ami + return ami + def remove_launch_permission(self, ami_id, user_ids=None, group=None): ami = self.describe_images(ami_ids=[ami_id])[0] self.validate_permission_targets(user_ids=user_ids, group=group) diff --git a/moto/ec2/responses/amis.py b/moto/ec2/responses/amis.py index 0e70182bb..178d583e0 100755 --- a/moto/ec2/responses/amis.py +++ b/moto/ec2/responses/amis.py @@ -73,8 +73,12 @@ class AmisResponse(BaseResponse): return MODIFY_IMAGE_ATTRIBUTE_RESPONSE def register_image(self): + name = self.querystring.get("Name")[0] + description = self._get_param("Description", if_none="") if self.is_not_dryrun("RegisterImage"): - raise NotImplementedError("AMIs.register_image is not yet implemented") + image = self.ec2_backend.register_image(name, description) + template = self.response_template(REGISTER_IMAGE_RESPONSE) + return template.render(image=image) def reset_image_attribute(self): if self.is_not_dryrun("ResetImageAttribute"): @@ -190,3 +194,8 @@ MODIFY_IMAGE_ATTRIBUTE_RESPONSE = """ true """ + +REGISTER_IMAGE_RESPONSE = """ + 59dbff89-35bd-4eac-99ed-be587EXAMPLE + {{ image.id }} +""" diff --git a/tests/test_ec2/test_amis.py b/tests/test_ec2/test_amis.py index 275b12905..220dd143c 100644 --- a/tests/test_ec2/test_amis.py +++ b/tests/test_ec2/test_amis.py @@ -773,6 +773,16 @@ def test_ami_describe_non_existent(): img.load() +@mock_ec2 +def test_ami_registration(): + ec2 = boto3.client("ec2", region_name="us-east-1") + image_id = ec2.register_image(Name="test-register-image").get("ImageId", "") + images = ec2.describe_images(ImageIds=[image_id]).get("Images", []) + assert images[0]["Name"] == "test-register-image", "No image was registered." + assert images[0]["RootDeviceName"] == "/dev/sda1", "Wrong root device name." + assert images[0]["State"] == "available", "State should be available." + + @mock_ec2 def test_ami_filter_wildcard(): ec2_resource = boto3.resource("ec2", region_name="us-west-1")