|
| 1 | +# Copyright 2017-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"). You |
| 4 | +# may not use this file except in compliance with the License. A copy of |
| 5 | +# the License is located at |
| 6 | +# |
| 7 | +# http://aws.amazon.com/apache2.0/ |
| 8 | +# |
| 9 | +# or in the "license" file accompanying this file. This file is |
| 10 | +# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF |
| 11 | +# ANY KIND, either express or implied. See the License for the specific |
| 12 | +# language governing permissions and limitations under the License. |
| 13 | + |
| 14 | +def _get_subnet_id_by_name(ec2_client, name): |
| 15 | + desc = ec2_client.describe_subnets(Filters=[ |
| 16 | + {'Name': 'tag-value', 'Values': [name]} |
| 17 | + ]) |
| 18 | + if len(desc['Subnets']) == 0: |
| 19 | + return None |
| 20 | + else: |
| 21 | + return desc['Subnets'][0]['SubnetId'] |
| 22 | + |
| 23 | + |
| 24 | +def _get_security_id_by_name(ec2_client, name): |
| 25 | + desc = ec2_client.describe_security_groups(Filters=[ |
| 26 | + {'Name': 'tag-value', 'Values': [name]} |
| 27 | + ]) |
| 28 | + if len(desc['SecurityGroups']) == 0: |
| 29 | + return None |
| 30 | + else: |
| 31 | + return desc['SecurityGroups'][0]['GroupId'] |
| 32 | + |
| 33 | + |
| 34 | +def _vpc_exists(ec2_client, name): |
| 35 | + desc = ec2_client.describe_vpcs(Filters=[ |
| 36 | + {'Name': 'tag-value', 'Values': [name]} |
| 37 | + ]) |
| 38 | + return len(desc['Vpcs']) > 0 |
| 39 | + |
| 40 | + |
| 41 | +def _get_route_table_id(ec2_client, vpc_id): |
| 42 | + desc = ec2_client.describe_route_tables(Filters=[ |
| 43 | + {'Name': 'vpc-id', 'Values': [vpc_id]} |
| 44 | + ]) |
| 45 | + return desc['RouteTables'][0]['RouteTableId'] |
| 46 | + |
| 47 | + |
| 48 | +def create_vpc_with_name(ec2_client, name): |
| 49 | + vpc_id = ec2_client.create_vpc(CidrBlock='10.0.0.0/16')['Vpc']['VpcId'] |
| 50 | + |
| 51 | + subnet_id = ec2_client.create_subnet(CidrBlock='10.0.0.0/24', VpcId=vpc_id)['Subnet']['SubnetId'] |
| 52 | + |
| 53 | + s3_service = [s for s in ec2_client.describe_vpc_endpoint_services()['ServiceNames'] if s.endswith('s3')][0] |
| 54 | + ec2_client.create_vpc_endpoint(VpcId=vpc_id, ServiceName=s3_service, |
| 55 | + RouteTableIds=[_get_route_table_id(ec2_client, vpc_id)]) |
| 56 | + |
| 57 | + security_group_id = ec2_client.create_security_group(GroupName='TrainingJobTestGroup', Description='Testing', |
| 58 | + VpcId=vpc_id)['GroupId'] |
| 59 | + |
| 60 | + ec2_client.create_tags(Resources=[vpc_id, subnet_id, security_group_id], Tags=[{'Key': 'Name', 'Value': name}]) |
| 61 | + |
| 62 | + return subnet_id, security_group_id |
| 63 | + |
| 64 | + |
| 65 | +def get_or_create_subnet_and_security_group(ec2_client, name): |
| 66 | + if _vpc_exists(ec2_client, name): |
| 67 | + return _get_subnet_id_by_name(ec2_client, name), _get_security_id_by_name(ec2_client, name) |
| 68 | + else: |
| 69 | + return create_vpc_with_name(ec2_client, name) |
0 commit comments