Skip to content

Commit

Permalink
Modifies the ec2 accounts API to include subnets element as a list of
Browse files Browse the repository at this point in the history
objects with `subnet` and `region` parameters
  • Loading branch information
apozsuse committed May 14, 2024
1 parent 0c9a202 commit 3d4d077
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 7 deletions.
2 changes: 1 addition & 1 deletion mash/services/api/extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
}

api = Api(
version='13.11.0',
version='14.0.0',
contact='[email protected]',
title='MASH API',
description='MASH provides a set of endpoints for Image Release '
Expand Down
21 changes: 18 additions & 3 deletions mash/services/api/v1/schema/accounts/ec2.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,21 @@
'enum': ['aws', 'aws-cn', 'aws-us-gov']
}

ec2_subnets = {
'type': 'array',
'items': {
'type': 'object',
'properties': {
'region': string_with_example('us-east-44'),
'subnet': string_with_example('T-12345678')
},
'required': ['region', 'subnet'],
'additionalProperties': False
},
'minItems': 1,
'example': [{'region': 'us-east-44', 'subnet': 'subnet-12345678'}]
}

ec2_account = {
'type': 'object',
'properties': {
Expand All @@ -48,7 +63,7 @@
'group': string_with_example('group1'),
'partition': partition,
'region': string_with_example('us-east-1'),
'subnet': string_with_example('subnet-12345678')
'subnets': ec2_subnets
},
'additionalProperties': False,
'required': [
Expand Down Expand Up @@ -79,15 +94,15 @@
'additional_regions': additional_regions,
'group': string_with_example('group1'),
'region': string_with_example('us-east-1'),
'subnet': string_with_example('subnet-12345678'),
'subnets': ec2_subnets,
'credentials': ec2_credentials
},
'additionalProperties': False,
'anyOf': [
{'required': ['additional_regions']},
{'required': ['group']},
{'required': ['region']},
{'required': ['subnet']},
{'required': ['subnets']},
{'required': ['credentials']}
]
}
17 changes: 15 additions & 2 deletions mash/services/api/v1/utils/jobs/ec2.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,9 @@ def add_target_ec2_account(
"""
job_doc_data = cloud_accounts.get(account['name'], {})
region_name = job_doc_data.get('region') or account.get('region')
subnet = job_doc_data.get('subnet') or account.get('subnet')

subnet = job_doc_data.get('subnet') or get_subnet_for_region(
account.get('subnets'), region_name
)
if skip_replication:
regions = [region_name]
if account.get('additional_regions'): # In case an additional region is used
Expand Down Expand Up @@ -202,3 +203,15 @@ def validate_ec2_job(job_doc):
job_doc['target_account_info'] = accounts

return job_doc


def get_subnet_for_region(subnets, region_name):
"""
Provides the subnet configured in some account for a specific region
"""
subnet_name = ''
for subnet in subnets:
if subnet['region'] == region_name:
subnet_name = subnet['subnet']
break
return subnet_name
8 changes: 7 additions & 1 deletion test/unit/services/api/v1/routes/jobs/ec2_job_routes_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,13 @@ def test_api_add_job_ec2(
account = {
'region': 'ap-northeast-1',
'name': 'test-aws-gov',
'partition': 'aws'
'partition': 'aws',
'subnets': [
{
'subnet': 'subnet-1111111',
'region': 'ap-northeast-1'
}
]
}
mock_get_account.return_value = account
mock_get_accounts.return_value = [account]
Expand Down
6 changes: 6 additions & 0 deletions test/unit/services/api/v1/utils/jobs/ec2_job_utils_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ def test_add_target_ec2_account(mock_get_regions):
'name': 'us-east-100',
'helper_image': 'ami-987'
}
],
'subnets': [
{
'subnet': 'subnet-111111',
'region': 'us-east-100'
}
]
}

Expand Down

0 comments on commit 3d4d077

Please sign in to comment.