Add ability to list ECS services by launch type (#5138)

This commit is contained in:
Oleksandr Anosov 2022-05-16 23:15:34 +03:00 committed by GitHub
parent 6f4ca14da8
commit 6ae0aa5272
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 51 additions and 24 deletions

View File

@ -1278,15 +1278,22 @@ 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 cluster_name + ":" not in key:
continue
if (
scheduling_strategy is None
or service.scheduling_strategy == scheduling_strategy
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)

View File

@ -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

View File

@ -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"
)
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
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(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