Use region to create and describe ECS resources
This commit is contained in:
parent
60183b29c1
commit
4fe66f521d
@ -44,15 +44,17 @@ class BaseObject(BaseModel):
|
|||||||
|
|
||||||
class Cluster(BaseObject):
|
class Cluster(BaseObject):
|
||||||
|
|
||||||
def __init__(self, cluster_name):
|
def __init__(self, cluster_name, region_name):
|
||||||
self.active_services_count = 0
|
self.active_services_count = 0
|
||||||
self.arn = 'arn:aws:ecs:us-east-1:012345678910:cluster/{0}'.format(
|
self.arn = 'arn:aws:ecs:{0}:012345678910:cluster/{1}'.format(
|
||||||
|
region_name,
|
||||||
cluster_name)
|
cluster_name)
|
||||||
self.name = cluster_name
|
self.name = cluster_name
|
||||||
self.pending_tasks_count = 0
|
self.pending_tasks_count = 0
|
||||||
self.registered_container_instances_count = 0
|
self.registered_container_instances_count = 0
|
||||||
self.running_tasks_count = 0
|
self.running_tasks_count = 0
|
||||||
self.status = 'ACTIVE'
|
self.status = 'ACTIVE'
|
||||||
|
self.region_name = region_name
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def physical_resource_id(self):
|
def physical_resource_id(self):
|
||||||
@ -108,11 +110,11 @@ class Cluster(BaseObject):
|
|||||||
|
|
||||||
class TaskDefinition(BaseObject):
|
class TaskDefinition(BaseObject):
|
||||||
|
|
||||||
def __init__(self, family, revision, container_definitions, volumes=None, tags=None):
|
def __init__(self, family, revision, container_definitions, region_name, volumes=None, tags=None):
|
||||||
self.family = family
|
self.family = family
|
||||||
self.revision = revision
|
self.revision = revision
|
||||||
self.arn = 'arn:aws:ecs:us-east-1:012345678910:task-definition/{0}:{1}'.format(
|
self.arn = 'arn:aws:ecs:{0}:012345678910:task-definition/{1}:{2}'.format(
|
||||||
family, revision)
|
region_name, family, revision)
|
||||||
self.container_definitions = container_definitions
|
self.container_definitions = container_definitions
|
||||||
self.tags = tags if tags is not None else []
|
self.tags = tags if tags is not None else []
|
||||||
if volumes is None:
|
if volumes is None:
|
||||||
@ -172,7 +174,8 @@ class Task(BaseObject):
|
|||||||
def __init__(self, cluster, task_definition, container_instance_arn,
|
def __init__(self, cluster, task_definition, container_instance_arn,
|
||||||
resource_requirements, overrides={}, started_by=''):
|
resource_requirements, overrides={}, started_by=''):
|
||||||
self.cluster_arn = cluster.arn
|
self.cluster_arn = cluster.arn
|
||||||
self.task_arn = 'arn:aws:ecs:us-east-1:012345678910:task/{0}'.format(
|
self.task_arn = 'arn:aws:ecs:{0}:012345678910:task/{1}'.format(
|
||||||
|
cluster.region_name,
|
||||||
str(uuid.uuid4()))
|
str(uuid.uuid4()))
|
||||||
self.container_instance_arn = container_instance_arn
|
self.container_instance_arn = container_instance_arn
|
||||||
self.last_status = 'RUNNING'
|
self.last_status = 'RUNNING'
|
||||||
@ -194,7 +197,8 @@ class Service(BaseObject):
|
|||||||
|
|
||||||
def __init__(self, cluster, service_name, task_definition, desired_count, load_balancers=None, scheduling_strategy=None):
|
def __init__(self, cluster, service_name, task_definition, desired_count, load_balancers=None, scheduling_strategy=None):
|
||||||
self.cluster_arn = cluster.arn
|
self.cluster_arn = cluster.arn
|
||||||
self.arn = 'arn:aws:ecs:us-east-1:012345678910:service/{0}'.format(
|
self.arn = 'arn:aws:ecs:{0}:012345678910:service/{1}'.format(
|
||||||
|
cluster.region_name,
|
||||||
service_name)
|
service_name)
|
||||||
self.name = service_name
|
self.name = service_name
|
||||||
self.status = 'ACTIVE'
|
self.status = 'ACTIVE'
|
||||||
@ -273,7 +277,7 @@ class Service(BaseObject):
|
|||||||
|
|
||||||
ecs_backend = ecs_backends[region_name]
|
ecs_backend = ecs_backends[region_name]
|
||||||
service_name = original_resource.name
|
service_name = original_resource.name
|
||||||
if original_resource.cluster_arn != Cluster(cluster_name).arn:
|
if original_resource.cluster_arn != Cluster(cluster_name, region_name).arn:
|
||||||
# TODO: LoadBalancers
|
# TODO: LoadBalancers
|
||||||
# TODO: Role
|
# TODO: Role
|
||||||
ecs_backend.delete_service(cluster_name, service_name)
|
ecs_backend.delete_service(cluster_name, service_name)
|
||||||
@ -320,7 +324,8 @@ class ContainerInstance(BaseObject):
|
|||||||
'name': 'PORTS_UDP',
|
'name': 'PORTS_UDP',
|
||||||
'stringSetValue': [],
|
'stringSetValue': [],
|
||||||
'type': 'STRINGSET'}]
|
'type': 'STRINGSET'}]
|
||||||
self.container_instance_arn = "arn:aws:ecs:us-east-1:012345678910:container-instance/{0}".format(
|
self.container_instance_arn = "arn:aws:ecs:{0}:012345678910:container-instance/{1}".format(
|
||||||
|
region_name,
|
||||||
str(uuid.uuid4()))
|
str(uuid.uuid4()))
|
||||||
self.pending_tasks_count = 0
|
self.pending_tasks_count = 0
|
||||||
self.remaining_resources = [
|
self.remaining_resources = [
|
||||||
@ -378,9 +383,10 @@ class ContainerInstance(BaseObject):
|
|||||||
|
|
||||||
|
|
||||||
class ClusterFailure(BaseObject):
|
class ClusterFailure(BaseObject):
|
||||||
def __init__(self, reason, cluster_name):
|
def __init__(self, reason, cluster_name, region_name):
|
||||||
self.reason = reason
|
self.reason = reason
|
||||||
self.arn = "arn:aws:ecs:us-east-1:012345678910:cluster/{0}".format(
|
self.arn = "arn:aws:ecs:{0}:012345678910:cluster/{1}".format(
|
||||||
|
region_name,
|
||||||
cluster_name)
|
cluster_name)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@ -393,9 +399,10 @@ class ClusterFailure(BaseObject):
|
|||||||
|
|
||||||
class ContainerInstanceFailure(BaseObject):
|
class ContainerInstanceFailure(BaseObject):
|
||||||
|
|
||||||
def __init__(self, reason, container_instance_id):
|
def __init__(self, reason, container_instance_id, region_name):
|
||||||
self.reason = reason
|
self.reason = reason
|
||||||
self.arn = "arn:aws:ecs:us-east-1:012345678910:container-instance/{0}".format(
|
self.arn = "arn:aws:ecs:{0}:012345678910:container-instance/{1}".format(
|
||||||
|
region_name,
|
||||||
container_instance_id)
|
container_instance_id)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@ -438,7 +445,7 @@ class EC2ContainerServiceBackend(BaseBackend):
|
|||||||
"{0} is not a task_definition".format(task_definition_name))
|
"{0} is not a task_definition".format(task_definition_name))
|
||||||
|
|
||||||
def create_cluster(self, cluster_name):
|
def create_cluster(self, cluster_name):
|
||||||
cluster = Cluster(cluster_name)
|
cluster = Cluster(cluster_name, self.region_name)
|
||||||
self.clusters[cluster_name] = cluster
|
self.clusters[cluster_name] = cluster
|
||||||
return cluster
|
return cluster
|
||||||
|
|
||||||
@ -461,7 +468,7 @@ class EC2ContainerServiceBackend(BaseBackend):
|
|||||||
list_clusters.append(
|
list_clusters.append(
|
||||||
self.clusters[cluster_name].response_object)
|
self.clusters[cluster_name].response_object)
|
||||||
else:
|
else:
|
||||||
failures.append(ClusterFailure('MISSING', cluster_name))
|
failures.append(ClusterFailure('MISSING', cluster_name, self.region_name))
|
||||||
return list_clusters, failures
|
return list_clusters, failures
|
||||||
|
|
||||||
def delete_cluster(self, cluster_str):
|
def delete_cluster(self, cluster_str):
|
||||||
@ -479,7 +486,7 @@ class EC2ContainerServiceBackend(BaseBackend):
|
|||||||
self.task_definitions[family] = {}
|
self.task_definitions[family] = {}
|
||||||
revision = 1
|
revision = 1
|
||||||
task_definition = TaskDefinition(
|
task_definition = TaskDefinition(
|
||||||
family, revision, container_definitions, volumes, tags)
|
family, revision, container_definitions, self.region_name, volumes, tags)
|
||||||
self.task_definitions[family][revision] = task_definition
|
self.task_definitions[family][revision] = task_definition
|
||||||
|
|
||||||
return task_definition
|
return task_definition
|
||||||
@ -792,7 +799,7 @@ class EC2ContainerServiceBackend(BaseBackend):
|
|||||||
container_instance_objects.append(container_instance)
|
container_instance_objects.append(container_instance)
|
||||||
else:
|
else:
|
||||||
failures.append(ContainerInstanceFailure(
|
failures.append(ContainerInstanceFailure(
|
||||||
'MISSING', container_instance_id))
|
'MISSING', container_instance_id, self.region_name))
|
||||||
|
|
||||||
return container_instance_objects, failures
|
return container_instance_objects, failures
|
||||||
|
|
||||||
@ -814,7 +821,7 @@ class EC2ContainerServiceBackend(BaseBackend):
|
|||||||
container_instance.status = status
|
container_instance.status = status
|
||||||
container_instance_objects.append(container_instance)
|
container_instance_objects.append(container_instance)
|
||||||
else:
|
else:
|
||||||
failures.append(ContainerInstanceFailure('MISSING', container_instance_id))
|
failures.append(ContainerInstanceFailure('MISSING', container_instance_id, self.region_name))
|
||||||
|
|
||||||
return container_instance_objects, failures
|
return container_instance_objects, failures
|
||||||
|
|
||||||
|
@ -34,7 +34,7 @@ def test_create_cluster():
|
|||||||
|
|
||||||
@mock_ecs
|
@mock_ecs
|
||||||
def test_list_clusters():
|
def test_list_clusters():
|
||||||
client = boto3.client('ecs', region_name='us-east-1')
|
client = boto3.client('ecs', region_name='us-east-2')
|
||||||
_ = client.create_cluster(
|
_ = client.create_cluster(
|
||||||
clusterName='test_cluster0'
|
clusterName='test_cluster0'
|
||||||
)
|
)
|
||||||
@ -43,9 +43,9 @@ def test_list_clusters():
|
|||||||
)
|
)
|
||||||
response = client.list_clusters()
|
response = client.list_clusters()
|
||||||
response['clusterArns'].should.contain(
|
response['clusterArns'].should.contain(
|
||||||
'arn:aws:ecs:us-east-1:012345678910:cluster/test_cluster0')
|
'arn:aws:ecs:us-east-2:012345678910:cluster/test_cluster0')
|
||||||
response['clusterArns'].should.contain(
|
response['clusterArns'].should.contain(
|
||||||
'arn:aws:ecs:us-east-1:012345678910:cluster/test_cluster1')
|
'arn:aws:ecs:us-east-2:012345678910:cluster/test_cluster1')
|
||||||
|
|
||||||
|
|
||||||
@mock_ecs
|
@mock_ecs
|
||||||
|
Loading…
Reference in New Issue
Block a user