Add ability to list ECS services by launch type (#5138)
This commit is contained in:
parent
6f4ca14da8
commit
6ae0aa5272
@ -1278,15 +1278,22 @@ class EC2ContainerServiceBackend(BaseBackend):
|
|||||||
|
|
||||||
return service
|
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]
|
cluster_name = cluster_str.split("/")[-1]
|
||||||
service_arns = []
|
service_arns = []
|
||||||
for key, service in self.services.items():
|
for key, service in self.services.items():
|
||||||
if cluster_name + ":" in key:
|
if cluster_name + ":" not in key:
|
||||||
|
continue
|
||||||
|
|
||||||
if (
|
if (
|
||||||
scheduling_strategy is None
|
scheduling_strategy is not None
|
||||||
or service.scheduling_strategy == scheduling_strategy
|
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)
|
service_arns.append(service.arn)
|
||||||
|
|
||||||
return sorted(service_arns)
|
return sorted(service_arns)
|
||||||
|
@ -228,7 +228,10 @@ class EC2ContainerServiceResponse(BaseResponse):
|
|||||||
def list_services(self):
|
def list_services(self):
|
||||||
cluster_str = self._get_param("cluster", "default")
|
cluster_str = self._get_param("cluster", "default")
|
||||||
scheduling_strategy = self._get_param("schedulingStrategy")
|
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(
|
return json.dumps(
|
||||||
{
|
{
|
||||||
"serviceArns": service_arns
|
"serviceArns": service_arns
|
||||||
|
@ -641,7 +641,8 @@ def test_create_service_scheduling_strategy():
|
|||||||
@mock_ecs
|
@mock_ecs
|
||||||
def test_list_services():
|
def test_list_services():
|
||||||
client = boto3.client("ecs", region_name="us-east-1")
|
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(
|
_ = client.register_task_definition(
|
||||||
family="test_ecs_task",
|
family="test_ecs_task",
|
||||||
containerDefinitions=[
|
containerDefinitions=[
|
||||||
@ -659,41 +660,57 @@ def test_list_services():
|
|||||||
],
|
],
|
||||||
)
|
)
|
||||||
_ = client.create_service(
|
_ = client.create_service(
|
||||||
cluster="test_ecs_cluster",
|
cluster="test_ecs_cluster1",
|
||||||
serviceName="test_ecs_service1",
|
serviceName="test_ecs_service1",
|
||||||
taskDefinition="test_ecs_task",
|
taskDefinition="test_ecs_task",
|
||||||
schedulingStrategy="REPLICA",
|
schedulingStrategy="REPLICA",
|
||||||
|
launchType="EC2",
|
||||||
desiredCount=2,
|
desiredCount=2,
|
||||||
)
|
)
|
||||||
_ = client.create_service(
|
_ = client.create_service(
|
||||||
cluster="test_ecs_cluster",
|
cluster="test_ecs_cluster1",
|
||||||
serviceName="test_ecs_service2",
|
serviceName="test_ecs_service2",
|
||||||
taskDefinition="test_ecs_task",
|
taskDefinition="test_ecs_task",
|
||||||
schedulingStrategy="DAEMON",
|
schedulingStrategy="DAEMON",
|
||||||
|
launchType="FARGATE",
|
||||||
desiredCount=2,
|
desiredCount=2,
|
||||||
)
|
)
|
||||||
unfiltered_response = client.list_services(cluster="test_ecs_cluster")
|
_ = client.create_service(
|
||||||
len(unfiltered_response["serviceArns"]).should.equal(2)
|
cluster="test_ecs_cluster2",
|
||||||
unfiltered_response["serviceArns"][0].should.equal(
|
serviceName="test_ecs_service3",
|
||||||
"arn:aws:ecs:us-east-1:{}:service/test_ecs_cluster/test_ecs_service1".format(
|
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
|
ACCOUNT_ID
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
unfiltered_response["serviceArns"][1].should.equal(
|
test_ecs_service2_arn = (
|
||||||
"arn:aws:ecs:us-east-1:{}:service/test_ecs_cluster/test_ecs_service2".format(
|
"arn:aws:ecs:us-east-1:{}:service/test_ecs_cluster1/test_ecs_service2".format(
|
||||||
ACCOUNT_ID
|
ACCOUNT_ID
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
filtered_response = client.list_services(
|
cluster1_services = client.list_services(cluster="test_ecs_cluster1")
|
||||||
cluster="test_ecs_cluster", schedulingStrategy="REPLICA"
|
len(cluster1_services["serviceArns"]).should.equal(2)
|
||||||
)
|
cluster1_services["serviceArns"][0].should.equal(test_ecs_service1_arn)
|
||||||
len(filtered_response["serviceArns"]).should.equal(1)
|
cluster1_services["serviceArns"][1].should.equal(test_ecs_service2_arn)
|
||||||
filtered_response["serviceArns"][0].should.equal(
|
|
||||||
"arn:aws:ecs:us-east-1:{}:service/test_ecs_cluster/test_ecs_service1".format(
|
cluster1_replica_services = client.list_services(
|
||||||
ACCOUNT_ID
|
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
|
@mock_ecs
|
||||||
|
Loading…
Reference in New Issue
Block a user