From 71e8f6ef5b3a8e64a3bdbe506a16d6b3c82d9044 Mon Sep 17 00:00:00 2001 From: GuyTempleton Date: Thu, 13 Apr 2017 08:54:25 +0100 Subject: [PATCH] First cut of container instance deregistration --- moto/ecs/models.py | 26 +++++++++++++++++++++++++- moto/ecs/responses.py | 11 +++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/moto/ecs/models.py b/moto/ecs/models.py index aadc76bce..e4258cee1 100644 --- a/moto/ecs/models.py +++ b/moto/ecs/models.py @@ -716,7 +716,31 @@ class EC2ContainerServiceBackend(BaseBackend): else: resource["stringSetValue"].append(str(port)) - def deregister_container_instance(self, cluster_str, container_instance_str): + def deregister_container_instance(self, cluster_str, container_instance_str, force): + cluster_name = cluster_str.split('/')[-1] + if cluster_name not in self.clusters: + raise Exception("{0} is not a cluster".format(cluster_name)) + container_instance_id = container_instance_str.split('/')[-1] + container_instance = self.container_instances[cluster_name].get(container_instance_id) + if container_instance is None: + raise Exception("{0} is not a container id in the cluster") + if not force and container_instance.running_tasks_count > 0: + raise Exception("Found running tasks on the instance.") + # Currently assume that people might want to do something based around deregistered instances + # with tasks left running on them - but nothing if deregistration is forced or no tasks were + # running already + elif force and container_instance.running_tasks_count > 0: + if not self.container_instances.get('orphaned'): + self.container_instances['orphaned'] = {} + self.container_instances['orphaned'][container_instance_id] = container_instance + del(self.container_instances[cluster_name][container_instance_id]) + self._respond_to_cluster_state_update(cluster_str) + pass + + def _respond_to_cluster_state_update(self, cluster_str): + cluster_name = cluster_str.split('/')[-1] + if cluster_name not in self.clusters: + raise Exception("{0} is not a cluster".format(cluster_name)) pass diff --git a/moto/ecs/responses.py b/moto/ecs/responses.py index 9c4524816..6c8fd8e2d 100644 --- a/moto/ecs/responses.py +++ b/moto/ecs/responses.py @@ -203,6 +203,17 @@ class EC2ContainerServiceResponse(BaseResponse): 'containerInstance': container_instance.response_object }) + def deregister_container_instance(self): + cluster_str = self._get_param('cluster', 'default'), + container_instance_str = self._get_param('containerInstance') + force = self._get_param('force', False) + container_instance = self.ecs_backend.deregister_container_instance( + cluster_str, container_instance_str, force + ) + 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(