make instanceTenancy configurable for VPCs (#819)
* make instanceTenancy configurable for VPCs * fix issue with setting tenenancy
This commit is contained in:
		
							parent
							
								
									8fc1ad03bd
								
							
						
					
					
						commit
						1045dca7b2
					
				| @ -1812,12 +1812,13 @@ class EBSBackend(object): | |||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| class VPC(TaggedEC2Resource): | 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.ec2_backend = ec2_backend | ||||||
|         self.id = vpc_id |         self.id = vpc_id | ||||||
|         self.cidr_block = cidr_block |         self.cidr_block = cidr_block | ||||||
|         self.dhcp_options = None |         self.dhcp_options = None | ||||||
|         self.state = 'available' |         self.state = 'available' | ||||||
|  |         self.instance_tenancy = instance_tenancy | ||||||
|         self.is_default = 'true' if is_default else 'false' |         self.is_default = 'true' if is_default else 'false' | ||||||
|         self.enable_dns_support = 'true' |         self.enable_dns_support = 'true' | ||||||
|         # This attribute is set to 'true' only for default VPCs |         # This attribute is set to 'true' only for default VPCs | ||||||
| @ -1831,6 +1832,7 @@ class VPC(TaggedEC2Resource): | |||||||
|         ec2_backend = ec2_backends[region_name] |         ec2_backend = ec2_backends[region_name] | ||||||
|         vpc = ec2_backend.create_vpc( |         vpc = ec2_backend.create_vpc( | ||||||
|             cidr_block=properties['CidrBlock'], |             cidr_block=properties['CidrBlock'], | ||||||
|  |             instance_tenancy=properties.get('InstanceTenancy', 'default') | ||||||
|         ) |         ) | ||||||
|         return vpc |         return vpc | ||||||
| 
 | 
 | ||||||
| @ -1843,6 +1845,8 @@ class VPC(TaggedEC2Resource): | |||||||
|             return self.id |             return self.id | ||||||
|         elif filter_name in ('cidr', 'cidr-block', 'cidrBlock'): |         elif filter_name in ('cidr', 'cidr-block', 'cidrBlock'): | ||||||
|             return self.cidr_block |             return self.cidr_block | ||||||
|  |         elif filter_name in ('instance_tenancy', 'InstanceTenancy'): | ||||||
|  |             return self.instance_tenancy | ||||||
|         elif filter_name in ('is-default', 'isDefault'): |         elif filter_name in ('is-default', 'isDefault'): | ||||||
|             return self.is_default |             return self.is_default | ||||||
|         elif filter_name == 'state': |         elif filter_name == 'state': | ||||||
| @ -1866,9 +1870,9 @@ class VPCBackend(object): | |||||||
|         self.vpcs = {} |         self.vpcs = {} | ||||||
|         super(VPCBackend, self).__init__() |         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_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 |         self.vpcs[vpc_id] = vpc | ||||||
| 
 | 
 | ||||||
|         # AWS creates a default main route table and security group. |         # AWS creates a default main route table and security group. | ||||||
|  | |||||||
| @ -7,7 +7,8 @@ from moto.ec2.utils import filters_from_querystring, vpc_ids_from_querystring | |||||||
| class VPCs(BaseResponse): | class VPCs(BaseResponse): | ||||||
|     def create_vpc(self): |     def create_vpc(self): | ||||||
|         cidr_block = self.querystring.get('CidrBlock')[0] |         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) |         template = self.response_template(CREATE_VPC_RESPONSE) | ||||||
|         return template.render(vpc=vpc) |         return template.render(vpc=vpc) | ||||||
| 
 | 
 | ||||||
| @ -51,7 +52,7 @@ CREATE_VPC_RESPONSE = """ | |||||||
|       <state>pending</state> |       <state>pending</state> | ||||||
|       <cidrBlock>{{ vpc.cidr_block }}</cidrBlock> |       <cidrBlock>{{ vpc.cidr_block }}</cidrBlock> | ||||||
|       <dhcpOptionsId>{% if vpc.dhcp_options %}{{ vpc.dhcp_options.id }}{% else %}dopt-1a2b3c4d2{% endif %}</dhcpOptionsId> |       <dhcpOptionsId>{% if vpc.dhcp_options %}{{ vpc.dhcp_options.id }}{% else %}dopt-1a2b3c4d2{% endif %}</dhcpOptionsId> | ||||||
|       <instanceTenancy>default</instanceTenancy> |       <instanceTenancy>{{ vpc.instance_tenancy }}</instanceTenancy> | ||||||
|       <tagSet> |       <tagSet> | ||||||
|         {% for tag in vpc.get_tags() %} |         {% for tag in vpc.get_tags() %} | ||||||
|           <item> |           <item> | ||||||
| @ -75,7 +76,7 @@ DESCRIBE_VPCS_RESPONSE = """ | |||||||
|         <state>{{ vpc.state }}</state> |         <state>{{ vpc.state }}</state> | ||||||
|         <cidrBlock>{{ vpc.cidr_block }}</cidrBlock> |         <cidrBlock>{{ vpc.cidr_block }}</cidrBlock> | ||||||
|         <dhcpOptionsId>{% if vpc.dhcp_options %}{{ vpc.dhcp_options.id }}{% else %}dopt-7a8b9c2d{% endif %}</dhcpOptionsId> |         <dhcpOptionsId>{% if vpc.dhcp_options %}{{ vpc.dhcp_options.id }}{% else %}dopt-7a8b9c2d{% endif %}</dhcpOptionsId> | ||||||
|         <instanceTenancy>default</instanceTenancy> |         <instanceTenancy>{{ vpc.instance_tenancy }}</instanceTenancy> | ||||||
|         <isDefault>{{ vpc.is_default }}</isDefault> |         <isDefault>{{ vpc.is_default }}</isDefault> | ||||||
|         <tagSet> |         <tagSet> | ||||||
|           {% for tag in vpc.get_tags() %} |           {% for tag in vpc.get_tags() %} | ||||||
|  | |||||||
| @ -246,6 +246,7 @@ def test_default_vpc(): | |||||||
|     # Create the default VPC |     # Create the default VPC | ||||||
|     default_vpc = list(ec2.vpcs.all())[0] |     default_vpc = list(ec2.vpcs.all())[0] | ||||||
|     default_vpc.cidr_block.should.equal('172.31.0.0/16') |     default_vpc.cidr_block.should.equal('172.31.0.0/16') | ||||||
|  |     default_vpc.instance_tenancy.should.equal('default') | ||||||
|     default_vpc.reload() |     default_vpc.reload() | ||||||
|     default_vpc.is_default.should.be.ok |     default_vpc.is_default.should.be.ok | ||||||
| 
 | 
 | ||||||
| @ -271,6 +272,9 @@ def test_non_default_vpc(): | |||||||
|     vpc.reload() |     vpc.reload() | ||||||
|     vpc.is_default.shouldnt.be.ok |     vpc.is_default.shouldnt.be.ok | ||||||
| 
 | 
 | ||||||
|  |     # Test default instance_tenancy | ||||||
|  |     vpc.instance_tenancy.should.equal('default') | ||||||
|  | 
 | ||||||
|     # Test default values for VPC attributes |     # Test default values for VPC attributes | ||||||
|     response = vpc.describe_attribute(Attribute='enableDnsSupport') |     response = vpc.describe_attribute(Attribute='enableDnsSupport') | ||||||
|     attr = response.get('EnableDnsSupport') |     attr = response.get('EnableDnsSupport') | ||||||
| @ -280,6 +284,19 @@ def test_non_default_vpc(): | |||||||
|     attr = response.get('EnableDnsHostnames') |     attr = response.get('EnableDnsHostnames') | ||||||
|     attr.get('Value').shouldnt.be.ok |     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 | @mock_ec2 | ||||||
| def test_vpc_modify_enable_dns_support(): | def test_vpc_modify_enable_dns_support(): | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user