Added JobDefinition to cloudformation

This commit is contained in:
Terry Cain 2017-10-20 19:10:31 +01:00
parent 9805a279c7
commit 629503398c
No known key found for this signature in database
GPG Key ID: 14D90844E4E9B9F3
3 changed files with 116 additions and 16 deletions

View File

@ -19,7 +19,7 @@ from moto.ecs import ecs_backends
from moto.logs import logs_backends
from .exceptions import InvalidParameterValueException, InternalFailure, ClientException
from .utils import make_arn_for_compute_env, make_arn_for_job_queue, make_arn_for_task_def
from .utils import make_arn_for_compute_env, make_arn_for_job_queue, make_arn_for_task_def, lowercase_first_key
from moto.ec2.exceptions import InvalidSubnetIdError
from moto.ec2.models import INSTANCE_TYPES as EC2_INSTANCE_TYPES
from moto.iam.exceptions import IAMNotFoundException
@ -64,18 +64,11 @@ class ComputeEnvironment(BaseModel):
backend = batch_backends[region_name]
properties = cloudformation_json['Properties']
# Need to deal with difference case from cloudformation compute_resources, e.g. instanceRole vs InstanceRole
# Hacky fix to normalise keys
new_comp_res = {}
for key, value in properties['ComputeResources'].items():
new_key = key[0].lower() + key[1:]
new_comp_res[new_key] = value
env = backend.create_compute_environment(
resource_name,
properties['Type'],
properties.get('State', 'ENABLED'),
new_comp_res,
lowercase_first_key(properties['ComputeResources']),
properties['ServiceRole']
)
arn = env[1]
@ -132,13 +125,7 @@ class JobQueue(BaseModel):
# Need to deal with difference case from cloudformation compute_resources, e.g. instanceRole vs InstanceRole
# Hacky fix to normalise keys, is making me think I want to start spamming cAsEiNsEnSiTiVe dictionaries
compute_envs = []
for compute_env in properties['ComputeEnvironmentOrder']:
tmp_compute_env_order = {}
for key, value in compute_env.items():
new_key = key[0].lower() + key[1:]
tmp_compute_env_order[new_key] = value
compute_envs.append(tmp_compute_env_order)
compute_envs = [lowercase_first_key(dict_item) for dict_item in properties['ComputeEnvironmentOrder']]
queue = backend.create_job_queue(
queue_name=resource_name,
@ -228,6 +215,27 @@ class JobDefinition(BaseModel):
return result
@property
def physical_resource_id(self):
return self.arn
@classmethod
def create_from_cloudformation_json(cls, resource_name, cloudformation_json, region_name):
backend = batch_backends[region_name]
properties = cloudformation_json['Properties']
res = backend.register_job_definition(
def_name=resource_name,
parameters=lowercase_first_key(properties.get('Parameters', {})),
_type='container',
retry_strategy=lowercase_first_key(properties['RetryStrategy']),
container_properties=lowercase_first_key(properties['ContainerProperties'])
)
arn = res[1]
return backend.get_job_definition_by_arn(arn)
class Job(threading.Thread, BaseModel):
def __init__(self, name, job_def, job_queue, log_backend):

View File

@ -11,3 +11,12 @@ def make_arn_for_job_queue(account_id, name, region_name):
def make_arn_for_task_def(account_id, name, revision, region_name):
return "arn:aws:batch:{0}:{1}:job-definition/{2}:{3}".format(region_name, account_id, name, revision)
def lowercase_first_key(some_dict):
new_dict = {}
for key, value in some_dict.items():
new_key = key[0].lower() + key[1:]
new_dict[new_key] = value
return new_dict

View File

@ -162,3 +162,86 @@ def test_create_job_queue_cf():
job_queue_resource['PhysicalResourceId'].startswith('arn:aws:batch:')
job_queue_resource['PhysicalResourceId'].should.contain('test_stack')
job_queue_resource['PhysicalResourceId'].should.contain('job-queue/')
@mock_cloudformation()
@mock_ec2
@mock_ecs
@mock_iam
@mock_batch
def test_create_job_def_cf():
ec2_client, iam_client, ecs_client, logs_client, batch_client = _get_clients()
vpc_id, subnet_id, sg_id, iam_arn = _setup(ec2_client, iam_client)
create_environment_template = {
'Resources': {
"ComputeEnvironment": {
"Type": "AWS::Batch::ComputeEnvironment",
"Properties": {
"Type": "MANAGED",
"ComputeResources": {
"Type": "EC2",
"MinvCpus": 0,
"DesiredvCpus": 0,
"MaxvCpus": 64,
"InstanceTypes": [
"optimal"
],
"Subnets": [subnet_id],
"SecurityGroupIds": [sg_id],
"InstanceRole": iam_arn
},
"ServiceRole": iam_arn
}
},
"JobQueue": {
"Type": "AWS::Batch::JobQueue",
"Properties": {
"Priority": 1,
"ComputeEnvironmentOrder": [
{
"Order": 1,
"ComputeEnvironment": {"Ref": "ComputeEnvironment"}
}
]
}
},
"JobDefinition": {
"Type": "AWS::Batch::JobDefinition",
"Properties": {
"Type": "container",
"ContainerProperties": {
"Image": {
"Fn::Join": ["", ["137112412989.dkr.ecr.", {"Ref": "AWS::Region"}, ".amazonaws.com/amazonlinux:latest"]]
},
"Vcpus": 2,
"Memory": 2000,
"Command": ["echo", "Hello world"]
},
"RetryStrategy": {
"Attempts": 1
}
}
},
}
}
cf_json = json.dumps(create_environment_template)
cf_conn = boto3.client('cloudformation', DEFAULT_REGION)
stack_id = cf_conn.create_stack(
StackName='test_stack',
TemplateBody=cf_json,
)['StackId']
stack_resources = cf_conn.list_stack_resources(StackName=stack_id)
len(stack_resources['StackResourceSummaries']).should.equal(3)
job_def_resource = list(filter(lambda item: item['ResourceType'] == 'AWS::Batch::JobDefinition', stack_resources['StackResourceSummaries']))[0]
job_def_resource['ResourceStatus'].should.equal('CREATE_COMPLETE')
# Spot checks on the ARN
job_def_resource['PhysicalResourceId'].startswith('arn:aws:batch:')
job_def_resource['PhysicalResourceId'].should.contain('test_stack-JobDef')
job_def_resource['PhysicalResourceId'].should.contain('job-definition/')