ECS - create_cluster with tags (#4654)

This commit is contained in:
Bert Blommers 2021-12-04 21:05:05 -01:00 committed by GitHub
parent afc467785d
commit 695a3ca3d3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 55 additions and 5 deletions

View File

@ -12,6 +12,7 @@ from moto.core import BaseBackend, BaseModel, CloudFormationModel, ACCOUNT_ID
from moto.core.exceptions import JsonRESTError
from moto.core.utils import unix_time, pascal_to_camelcase, remap_nested_keys
from moto.ec2 import ec2_backends
from moto.utilities.tagging_service import TaggingService
from .exceptions import (
EcsClientException,
ServiceNotFoundException,
@ -719,6 +720,9 @@ class EC2ContainerServiceBackend(BaseBackend):
self.container_instances = {}
self.task_sets = {}
self.region_name = region_name
self.tagger = TaggingService(
tag_name="tags", key_name="key", value_name="value"
)
def reset(self):
region_name = self.region_name
@ -759,9 +763,11 @@ class EC2ContainerServiceBackend(BaseBackend):
else:
raise Exception("{0} is not a task_definition".format(task_definition_name))
def create_cluster(self, cluster_name):
def create_cluster(self, cluster_name, tags=None):
cluster = Cluster(cluster_name, self.region_name)
self.clusters[cluster_name] = cluster
if tags:
self.tagger.tag_resource(cluster.arn, tags)
return cluster
def list_clusters(self):
@ -770,7 +776,10 @@ class EC2ContainerServiceBackend(BaseBackend):
"""
return [cluster.arn for cluster in self.clusters.values()]
def describe_clusters(self, list_clusters_name=None):
def describe_clusters(self, list_clusters_name=None, include=None):
"""
Only include=TAGS is currently supported.
"""
list_clusters = []
failures = []
if list_clusters_name is None:
@ -785,6 +794,14 @@ class EC2ContainerServiceBackend(BaseBackend):
failures.append(
ClusterFailure("MISSING", cluster_name, self.region_name)
)
if "TAGS" in (include or []):
for cluster in list_clusters:
cluster_arn = cluster["clusterArn"]
if self.tagger.has_tags(cluster_arn):
cluster_tags = self.tagger.list_tags_for_resource(cluster_arn)
cluster.update(cluster_tags)
return list_clusters, failures
def delete_cluster(self, cluster_str):

View File

@ -27,9 +27,10 @@ class EC2ContainerServiceResponse(BaseResponse):
def create_cluster(self):
cluster_name = self._get_param("clusterName")
tags = self._get_param("tags")
if cluster_name is None:
cluster_name = "default"
cluster = self.ecs_backend.create_cluster(cluster_name)
cluster = self.ecs_backend.create_cluster(cluster_name, tags)
return json.dumps({"cluster": cluster.response_object})
def list_clusters(self):
@ -42,8 +43,9 @@ class EC2ContainerServiceResponse(BaseResponse):
)
def describe_clusters(self):
list_clusters_name = self._get_param("clusters")
clusters, failures = self.ecs_backend.describe_clusters(list_clusters_name)
names = self._get_param("clusters")
include = self._get_param("include")
clusters, failures = self.ecs_backend.describe_clusters(names, include)
return json.dumps(
{
"clusters": clusters,

View File

@ -55,6 +55,37 @@ def test_list_clusters():
@mock_ecs
def test_describe_clusters():
client = boto3.client("ecs", region_name="us-east-1")
tag_list = [{"key": "tagName", "value": "TagValue"}]
_ = client.create_cluster(clusterName="c_with_tags", tags=tag_list)
_ = client.create_cluster(clusterName="c_without")
clusters = client.describe_clusters(clusters=["c_with_tags"], include=["TAGS",])[
"clusters"
]
clusters.should.have.length_of(1)
cluster = clusters[0]
cluster["clusterName"].should.equal("c_with_tags")
cluster.should.have.key("tags")
cluster["tags"].should.equal(tag_list)
clusters = client.describe_clusters(clusters=["c_without"], include=["TAGS",])[
"clusters"
]
clusters.should.have.length_of(1)
cluster = clusters[0]
cluster["clusterName"].should.equal("c_without")
cluster.shouldnt.have.key("tags")
clusters = client.describe_clusters(clusters=["c_with_tags", "c_without"])[
"clusters"
]
clusters.should.have.length_of(2)
clusters[0].shouldnt.have.key("tags")
clusters[1].shouldnt.have.key("tags")
@mock_ecs
def test_describe_clusters_missing():
client = boto3.client("ecs", region_name="us-east-1")
response = client.describe_clusters(clusters=["some-cluster"])
response["failures"].should.contain(