diff --git a/moto/ec2/models.py b/moto/ec2/models.py index 25befabc0..00e0f4960 100755 --- a/moto/ec2/models.py +++ b/moto/ec2/models.py @@ -1812,12 +1812,13 @@ class EBSBackend(object): class VPC(TaggedEC2Resource): - def __init__(self, ec2_backend, vpc_id, cidr_block, is_default): + def __init__(self, ec2_backend, vpc_id, cidr_block, is_default, instance_tenancy='default'): self.ec2_backend = ec2_backend self.id = vpc_id self.cidr_block = cidr_block self.dhcp_options = None self.state = 'available' + self.instance_tenancy = instance_tenancy self.is_default = 'true' if is_default else 'false' self.enable_dns_support = 'true' # This attribute is set to 'true' only for default VPCs @@ -1831,6 +1832,7 @@ class VPC(TaggedEC2Resource): ec2_backend = ec2_backends[region_name] vpc = ec2_backend.create_vpc( cidr_block=properties['CidrBlock'], + instance_tenancy=properties.get('InstanceTenancy', 'default') ) return vpc @@ -1843,6 +1845,8 @@ class VPC(TaggedEC2Resource): return self.id elif filter_name in ('cidr', 'cidr-block', 'cidrBlock'): return self.cidr_block + elif filter_name in ('instance_tenancy', 'InstanceTenancy'): + return self.instance_tenancy elif filter_name in ('is-default', 'isDefault'): return self.is_default elif filter_name == 'state': @@ -1866,9 +1870,9 @@ class VPCBackend(object): self.vpcs = {} super(VPCBackend, self).__init__() - def create_vpc(self, cidr_block): + def create_vpc(self, cidr_block, instance_tenancy='default'): vpc_id = random_vpc_id() - vpc = VPC(self, vpc_id, cidr_block, len(self.vpcs) == 0) + vpc = VPC(self, vpc_id, cidr_block, len(self.vpcs) == 0, instance_tenancy) self.vpcs[vpc_id] = vpc # AWS creates a default main route table and security group. diff --git a/moto/ec2/responses/vpcs.py b/moto/ec2/responses/vpcs.py index 58c5e80dd..3d2a99894 100644 --- a/moto/ec2/responses/vpcs.py +++ b/moto/ec2/responses/vpcs.py @@ -7,7 +7,8 @@ from moto.ec2.utils import filters_from_querystring, vpc_ids_from_querystring class VPCs(BaseResponse): def create_vpc(self): cidr_block = self.querystring.get('CidrBlock')[0] - vpc = self.ec2_backend.create_vpc(cidr_block) + instance_tenancy = self.querystring.get('InstanceTenancy', ['default'])[0] + vpc = self.ec2_backend.create_vpc(cidr_block, instance_tenancy) template = self.response_template(CREATE_VPC_RESPONSE) return template.render(vpc=vpc) @@ -51,7 +52,7 @@ CREATE_VPC_RESPONSE = """ pending {{ vpc.cidr_block }} {% if vpc.dhcp_options %}{{ vpc.dhcp_options.id }}{% else %}dopt-1a2b3c4d2{% endif %} - default + {{ vpc.instance_tenancy }} {% for tag in vpc.get_tags() %} @@ -75,7 +76,7 @@ DESCRIBE_VPCS_RESPONSE = """ {{ vpc.state }} {{ vpc.cidr_block }} {% if vpc.dhcp_options %}{{ vpc.dhcp_options.id }}{% else %}dopt-7a8b9c2d{% endif %} - default + {{ vpc.instance_tenancy }} {{ vpc.is_default }} {% for tag in vpc.get_tags() %} diff --git a/tests/test_ec2/test_vpcs.py b/tests/test_ec2/test_vpcs.py index def2700e3..513238001 100644 --- a/tests/test_ec2/test_vpcs.py +++ b/tests/test_ec2/test_vpcs.py @@ -246,6 +246,7 @@ def test_default_vpc(): # Create the default VPC default_vpc = list(ec2.vpcs.all())[0] default_vpc.cidr_block.should.equal('172.31.0.0/16') + default_vpc.instance_tenancy.should.equal('default') default_vpc.reload() default_vpc.is_default.should.be.ok @@ -271,6 +272,9 @@ def test_non_default_vpc(): vpc.reload() vpc.is_default.shouldnt.be.ok + # Test default instance_tenancy + vpc.instance_tenancy.should.equal('default') + # Test default values for VPC attributes response = vpc.describe_attribute(Attribute='enableDnsSupport') attr = response.get('EnableDnsSupport') @@ -280,6 +284,19 @@ def test_non_default_vpc(): attr = response.get('EnableDnsHostnames') attr.get('Value').shouldnt.be.ok +@mock_ec2 +def test_vpc_dedicated_tenancy(): + ec2 = boto3.resource('ec2', region_name='us-west-1') + + # Create the default VPC + ec2.create_vpc(CidrBlock='172.31.0.0/16') + + # Create the non default VPC + vpc = ec2.create_vpc(CidrBlock='10.0.0.0/16', InstanceTenancy='dedicated') + vpc.reload() + vpc.is_default.shouldnt.be.ok + + vpc.instance_tenancy.should.equal('dedicated') @mock_ec2 def test_vpc_modify_enable_dns_support():