ECS - create_cluster with tags (#4654)
This commit is contained in:
parent
afc467785d
commit
695a3ca3d3
@ -12,6 +12,7 @@ from moto.core import BaseBackend, BaseModel, CloudFormationModel, ACCOUNT_ID
|
|||||||
from moto.core.exceptions import JsonRESTError
|
from moto.core.exceptions import JsonRESTError
|
||||||
from moto.core.utils import unix_time, pascal_to_camelcase, remap_nested_keys
|
from moto.core.utils import unix_time, pascal_to_camelcase, remap_nested_keys
|
||||||
from moto.ec2 import ec2_backends
|
from moto.ec2 import ec2_backends
|
||||||
|
from moto.utilities.tagging_service import TaggingService
|
||||||
from .exceptions import (
|
from .exceptions import (
|
||||||
EcsClientException,
|
EcsClientException,
|
||||||
ServiceNotFoundException,
|
ServiceNotFoundException,
|
||||||
@ -719,6 +720,9 @@ class EC2ContainerServiceBackend(BaseBackend):
|
|||||||
self.container_instances = {}
|
self.container_instances = {}
|
||||||
self.task_sets = {}
|
self.task_sets = {}
|
||||||
self.region_name = region_name
|
self.region_name = region_name
|
||||||
|
self.tagger = TaggingService(
|
||||||
|
tag_name="tags", key_name="key", value_name="value"
|
||||||
|
)
|
||||||
|
|
||||||
def reset(self):
|
def reset(self):
|
||||||
region_name = self.region_name
|
region_name = self.region_name
|
||||||
@ -759,9 +763,11 @@ class EC2ContainerServiceBackend(BaseBackend):
|
|||||||
else:
|
else:
|
||||||
raise Exception("{0} is not a task_definition".format(task_definition_name))
|
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)
|
cluster = Cluster(cluster_name, self.region_name)
|
||||||
self.clusters[cluster_name] = cluster
|
self.clusters[cluster_name] = cluster
|
||||||
|
if tags:
|
||||||
|
self.tagger.tag_resource(cluster.arn, tags)
|
||||||
return cluster
|
return cluster
|
||||||
|
|
||||||
def list_clusters(self):
|
def list_clusters(self):
|
||||||
@ -770,7 +776,10 @@ class EC2ContainerServiceBackend(BaseBackend):
|
|||||||
"""
|
"""
|
||||||
return [cluster.arn for cluster in self.clusters.values()]
|
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 = []
|
list_clusters = []
|
||||||
failures = []
|
failures = []
|
||||||
if list_clusters_name is None:
|
if list_clusters_name is None:
|
||||||
@ -785,6 +794,14 @@ class EC2ContainerServiceBackend(BaseBackend):
|
|||||||
failures.append(
|
failures.append(
|
||||||
ClusterFailure("MISSING", cluster_name, self.region_name)
|
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
|
return list_clusters, failures
|
||||||
|
|
||||||
def delete_cluster(self, cluster_str):
|
def delete_cluster(self, cluster_str):
|
||||||
|
@ -27,9 +27,10 @@ class EC2ContainerServiceResponse(BaseResponse):
|
|||||||
|
|
||||||
def create_cluster(self):
|
def create_cluster(self):
|
||||||
cluster_name = self._get_param("clusterName")
|
cluster_name = self._get_param("clusterName")
|
||||||
|
tags = self._get_param("tags")
|
||||||
if cluster_name is None:
|
if cluster_name is None:
|
||||||
cluster_name = "default"
|
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})
|
return json.dumps({"cluster": cluster.response_object})
|
||||||
|
|
||||||
def list_clusters(self):
|
def list_clusters(self):
|
||||||
@ -42,8 +43,9 @@ class EC2ContainerServiceResponse(BaseResponse):
|
|||||||
)
|
)
|
||||||
|
|
||||||
def describe_clusters(self):
|
def describe_clusters(self):
|
||||||
list_clusters_name = self._get_param("clusters")
|
names = self._get_param("clusters")
|
||||||
clusters, failures = self.ecs_backend.describe_clusters(list_clusters_name)
|
include = self._get_param("include")
|
||||||
|
clusters, failures = self.ecs_backend.describe_clusters(names, include)
|
||||||
return json.dumps(
|
return json.dumps(
|
||||||
{
|
{
|
||||||
"clusters": clusters,
|
"clusters": clusters,
|
||||||
|
@ -55,6 +55,37 @@ def test_list_clusters():
|
|||||||
|
|
||||||
@mock_ecs
|
@mock_ecs
|
||||||
def test_describe_clusters():
|
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")
|
client = boto3.client("ecs", region_name="us-east-1")
|
||||||
response = client.describe_clusters(clusters=["some-cluster"])
|
response = client.describe_clusters(clusters=["some-cluster"])
|
||||||
response["failures"].should.contain(
|
response["failures"].should.contain(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user