From 6ae0aa52728311158c4556ac38caffc717af21dc Mon Sep 17 00:00:00 2001 From: Oleksandr Anosov Date: Mon, 16 May 2022 23:15:34 +0300 Subject: [PATCH] Add ability to list ECS services by launch type (#5138) --- moto/ecs/models.py | 21 +++++++++----- moto/ecs/responses.py | 5 +++- tests/test_ecs/test_ecs_boto3.py | 49 +++++++++++++++++++++----------- 3 files changed, 51 insertions(+), 24 deletions(-) diff --git a/moto/ecs/models.py b/moto/ecs/models.py index 47eb42039..4fd9976eb 100644 --- a/moto/ecs/models.py +++ b/moto/ecs/models.py @@ -1278,16 +1278,23 @@ class EC2ContainerServiceBackend(BaseBackend): return service - def list_services(self, cluster_str, scheduling_strategy=None): + def list_services(self, cluster_str, scheduling_strategy=None, launch_type=None): cluster_name = cluster_str.split("/")[-1] service_arns = [] for key, service in self.services.items(): - if cluster_name + ":" in key: - if ( - scheduling_strategy is None - or service.scheduling_strategy == scheduling_strategy - ): - service_arns.append(service.arn) + if cluster_name + ":" not in key: + continue + + if ( + scheduling_strategy is not None + and service.scheduling_strategy != scheduling_strategy + ): + continue + + if launch_type is not None and service.launch_type != launch_type: + continue + + service_arns.append(service.arn) return sorted(service_arns) diff --git a/moto/ecs/responses.py b/moto/ecs/responses.py index ef568b8d5..66d5c7a28 100644 --- a/moto/ecs/responses.py +++ b/moto/ecs/responses.py @@ -228,7 +228,10 @@ class EC2ContainerServiceResponse(BaseResponse): def list_services(self): cluster_str = self._get_param("cluster", "default") scheduling_strategy = self._get_param("schedulingStrategy") - service_arns = self.ecs_backend.list_services(cluster_str, scheduling_strategy) + launch_type = self._get_param("launchType") + service_arns = self.ecs_backend.list_services( + cluster_str, scheduling_strategy, launch_type=launch_type + ) return json.dumps( { "serviceArns": service_arns diff --git a/tests/test_ecs/test_ecs_boto3.py b/tests/test_ecs/test_ecs_boto3.py index 41c2439a7..0f459e2ff 100644 --- a/tests/test_ecs/test_ecs_boto3.py +++ b/tests/test_ecs/test_ecs_boto3.py @@ -641,7 +641,8 @@ def test_create_service_scheduling_strategy(): @mock_ecs def test_list_services(): client = boto3.client("ecs", region_name="us-east-1") - _ = client.create_cluster(clusterName="test_ecs_cluster") + _ = client.create_cluster(clusterName="test_ecs_cluster1") + _ = client.create_cluster(clusterName="test_ecs_cluster2") _ = client.register_task_definition( family="test_ecs_task", containerDefinitions=[ @@ -659,41 +660,57 @@ def test_list_services(): ], ) _ = client.create_service( - cluster="test_ecs_cluster", + cluster="test_ecs_cluster1", serviceName="test_ecs_service1", taskDefinition="test_ecs_task", schedulingStrategy="REPLICA", + launchType="EC2", desiredCount=2, ) _ = client.create_service( - cluster="test_ecs_cluster", + cluster="test_ecs_cluster1", serviceName="test_ecs_service2", taskDefinition="test_ecs_task", schedulingStrategy="DAEMON", + launchType="FARGATE", desiredCount=2, ) - unfiltered_response = client.list_services(cluster="test_ecs_cluster") - len(unfiltered_response["serviceArns"]).should.equal(2) - unfiltered_response["serviceArns"][0].should.equal( - "arn:aws:ecs:us-east-1:{}:service/test_ecs_cluster/test_ecs_service1".format( + _ = client.create_service( + cluster="test_ecs_cluster2", + serviceName="test_ecs_service3", + taskDefinition="test_ecs_task", + schedulingStrategy="REPLICA", + launchType="FARGATE", + desiredCount=2, + ) + + test_ecs_service1_arn = ( + "arn:aws:ecs:us-east-1:{}:service/test_ecs_cluster1/test_ecs_service1".format( ACCOUNT_ID ) ) - unfiltered_response["serviceArns"][1].should.equal( - "arn:aws:ecs:us-east-1:{}:service/test_ecs_cluster/test_ecs_service2".format( + test_ecs_service2_arn = ( + "arn:aws:ecs:us-east-1:{}:service/test_ecs_cluster1/test_ecs_service2".format( ACCOUNT_ID ) ) - filtered_response = client.list_services( - cluster="test_ecs_cluster", schedulingStrategy="REPLICA" + cluster1_services = client.list_services(cluster="test_ecs_cluster1") + len(cluster1_services["serviceArns"]).should.equal(2) + cluster1_services["serviceArns"][0].should.equal(test_ecs_service1_arn) + cluster1_services["serviceArns"][1].should.equal(test_ecs_service2_arn) + + cluster1_replica_services = client.list_services( + cluster="test_ecs_cluster1", schedulingStrategy="REPLICA" ) - len(filtered_response["serviceArns"]).should.equal(1) - filtered_response["serviceArns"][0].should.equal( - "arn:aws:ecs:us-east-1:{}:service/test_ecs_cluster/test_ecs_service1".format( - ACCOUNT_ID - ) + len(cluster1_replica_services["serviceArns"]).should.equal(1) + cluster1_replica_services["serviceArns"][0].should.equal(test_ecs_service1_arn) + + cluster1_fargate_services = client.list_services( + cluster="test_ecs_cluster1", launchType="FARGATE" ) + len(cluster1_fargate_services["serviceArns"]).should.equal(1) + cluster1_fargate_services["serviceArns"][0].should.equal(test_ecs_service2_arn) @mock_ecs