From 115f9513f6ed1c2d5472493dde7fa66f9a966e7c Mon Sep 17 00:00:00 2001 From: "Riccardo M. Cefala" Date: Sat, 11 Jun 2016 12:52:19 +0200 Subject: [PATCH] add ECS ContainerInstance register and list actions --- moto/ecs/models.py | 23 +++++++++++++++++++++++ moto/ecs/responses.py | 17 +++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/moto/ecs/models.py b/moto/ecs/models.py index f8f8f201a..45ced304e 100644 --- a/moto/ecs/models.py +++ b/moto/ecs/models.py @@ -113,6 +113,7 @@ class EC2ContainerServiceBackend(BaseBackend): self.clusters = {} self.task_definitions = {} self.services = {} + self.container_instances = {} def fetch_task_definition(self, task_definition_str): task_definition_components = task_definition_str.split(':') @@ -235,6 +236,28 @@ class EC2ContainerServiceBackend(BaseBackend): else: raise Exception("cluster {0} or service {1} does not exist".format(cluster_name, service_name)) + def register_container_instance(self, cluster_str, ec2_instance_id): + cluster_name = cluster_str.split('/')[-1] + if cluster_name in self.clusters: + cluster = self.clusters[cluster_name] + else: + raise Exception("{0} is not a cluster".format(cluster.name)) + container_instance = ContainerInstance(ec2_instance_id) + if not self.container_instances.get(cluster_name): + self.container_instances[cluster_name] = {} + self.container_instances[cluster_name][container_instance.containerInstanceArn] = container_instance + return container_instance + + def list_container_instances(self, cluster_str): + cluster_name = cluster_str.split('/')[-1] + return sorted(self.container_instances[cluster_name].keys()) + + def describe_container_instances(self, cluster_str, list_container_instances_str): + pass + + def deregister_container_instance(self, cluster_str, container_instance_str): + pass + ecs_backends = {} for region, ec2_backend in ec2_backends.items(): diff --git a/moto/ecs/responses.py b/moto/ecs/responses.py index 1cf88bd4d..0adf7813c 100644 --- a/moto/ecs/responses.py +++ b/moto/ecs/responses.py @@ -113,3 +113,20 @@ class EC2ContainerServiceResponse(BaseResponse): return json.dumps({ 'service': service.response_object }) + + def register_container_instance(self): + cluster_str = self._get_param('cluster') + instance_identity_document_str = self._get_param('instanceIdentityDocument') + instance_identity_document = json.loads(instance_identity_document_str) + ec2_instance_id = instance_identity_document["instanceId"] + container_instance = self.ecs_backend.register_container_instance(cluster_str, ec2_instance_id) + return json.dumps({ + 'containerInstance' : container_instance.response_object + }) + + def list_container_instances(self): + cluster_str = self._get_param('cluster') + container_instance_arns = self.ecs_backend.list_container_instances(cluster_str) + return json.dumps({ + 'containerInstanceArns': container_instance_arns + })