From 137791e9606b87ef0de31f5cdac0ef016d799e20 Mon Sep 17 00:00:00 2001 From: "Riccardo M. Cefala" Date: Tue, 14 Jun 2016 17:58:11 +0200 Subject: [PATCH] add ECS describe_container_instances --- moto/ecs/models.py | 30 ++++++++++++++++++++++-- moto/ecs/responses.py | 9 ++++++++ tests/test_ecs/test_ecs_boto3.py | 39 ++++++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+), 2 deletions(-) diff --git a/moto/ecs/models.py b/moto/ecs/models.py index 45ced304e..30d7c21e3 100644 --- a/moto/ecs/models.py +++ b/moto/ecs/models.py @@ -108,6 +108,19 @@ class ContainerInstance(BaseObject): return response_object +class Failure(BaseObject): + def __init__(self, reason, arn): + self.reason = reason + self.arn = arn + + @property + def response_object(self): + response_object = self.gen_response_object() + response_object['reason'] = self.reason + response_object['arn'] = self.arn + return response_object + + class EC2ContainerServiceBackend(BaseBackend): def __init__(self): self.clusters = {} @@ -252,8 +265,21 @@ class EC2ContainerServiceBackend(BaseBackend): 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 describe_container_instances(self, cluster_str, list_container_instance_ids): + cluster_name = cluster_str.split('/')[-1] + if cluster_name not in self.clusters: + raise Exception("{0} is not a cluster".format(cluster_name)) + failures = [] + container_instance_objects = [] + for container_instance_id in list_container_instance_ids: + container_instance_arn = 'arn:aws:ecs:us-east-1:012345678910:container-instance/{0}'.format(container_instance_id) + container_instance = self.container_instances[cluster_name].get(container_instance_arn, None) + if container_instance is not None: + container_instance_objects.append(container_instance) + else: + failures.append(Failure(reason='MISSING', arn=container_instance_arn)) + + return container_instance_objects, failures def deregister_container_instance(self, cluster_str, container_instance_str): pass diff --git a/moto/ecs/responses.py b/moto/ecs/responses.py index 0adf7813c..b9770a435 100644 --- a/moto/ecs/responses.py +++ b/moto/ecs/responses.py @@ -130,3 +130,12 @@ class EC2ContainerServiceResponse(BaseResponse): return json.dumps({ 'containerInstanceArns': container_instance_arns }) + + def describe_container_instances(self): + cluster_str = self._get_param('cluster') + list_container_instance_arns = self._get_param('containerInstances') + container_instances, failures = self.ecs_backend.describe_container_instances(cluster_str, list_container_instance_arns) + return json.dumps({ + 'failures': [ci.response_object for ci in failures], + 'containerInstances': [ci.response_object for ci in container_instances] + }) diff --git a/tests/test_ecs/test_ecs_boto3.py b/tests/test_ecs/test_ecs_boto3.py index d671fde7d..9ca367e0a 100644 --- a/tests/test_ecs/test_ecs_boto3.py +++ b/tests/test_ecs/test_ecs_boto3.py @@ -414,3 +414,42 @@ def test_list_container_instances(): len(response['containerInstanceArns']).should.equal(instance_to_create) for arn in test_instance_arns: response['containerInstanceArns'].should.contain(arn) + + +@mock_ec2 +@mock_ecs +def test_describe_container_instances(): + ecs_client = boto3.client('ecs', region_name='us-east-1') + ec2 = boto3.resource('ec2', region_name='us-east-1') + + test_cluster_name = 'test_ecs_cluster' + _ = ecs_client.create_cluster( + clusterName=test_cluster_name + ) + + instance_to_create = 3 + test_instance_arns = [] + for i in range(0, instance_to_create): + test_instance = ec2.create_instances( + ImageId="ami-1234abcd", + MinCount=1, + MaxCount=1, + )[0] + + instance_id_document = json.dumps( + ec2_utils.generate_instance_identity_document(test_instance) + ) + + response = ecs_client.register_container_instance( + cluster=test_cluster_name, + instanceIdentityDocument=instance_id_document) + + test_instance_arns.append(response['containerInstance']['containerInstanceArn']) + + test_instance_ids = map((lambda x: x.split('/')[1]), test_instance_arns) + response = ecs_client.describe_container_instances(cluster=test_cluster_name, containerInstances=test_instance_ids) + len(response['failures']).should.equal(0) + len(response['containerInstances']).should.equal(instance_to_create) + response_arns = [ci['containerInstanceArn'] for ci in response['containerInstances']] + for arn in test_instance_arns: + response_arns.should.contain(arn)