#2877 - Ensure NetworkInterfaces are assigned to the default Subnet
This commit is contained in:
parent
7419f527d4
commit
5c7e0b56af
@ -775,7 +775,14 @@ class Instance(TaggedEC2Resource, BotoInstance):
|
|||||||
if "SubnetId" in nic:
|
if "SubnetId" in nic:
|
||||||
subnet = self.ec2_backend.get_subnet(nic["SubnetId"])
|
subnet = self.ec2_backend.get_subnet(nic["SubnetId"])
|
||||||
else:
|
else:
|
||||||
subnet = None
|
# Get default Subnet
|
||||||
|
subnet = [
|
||||||
|
subnet
|
||||||
|
for subnet in self.ec2_backend.get_all_subnets(
|
||||||
|
filters={"availabilityZone": self._placement.zone}
|
||||||
|
)
|
||||||
|
if subnet.default_for_az
|
||||||
|
][0]
|
||||||
|
|
||||||
group_id = nic.get("SecurityGroupId")
|
group_id = nic.get("SecurityGroupId")
|
||||||
group_ids = [group_id] if group_id else []
|
group_ids = [group_id] if group_id else []
|
||||||
|
@ -71,7 +71,7 @@ def test_instance_launch_and_terminate():
|
|||||||
instance.id.should.equal(instance.id)
|
instance.id.should.equal(instance.id)
|
||||||
instance.state.should.equal("running")
|
instance.state.should.equal("running")
|
||||||
instance.launch_time.should.equal("2014-01-01T05:00:00.000Z")
|
instance.launch_time.should.equal("2014-01-01T05:00:00.000Z")
|
||||||
instance.vpc_id.should.equal(None)
|
instance.vpc_id.shouldnt.equal(None)
|
||||||
instance.placement.should.equal("us-east-1a")
|
instance.placement.should.equal("us-east-1a")
|
||||||
|
|
||||||
root_device_name = instance.root_device_name
|
root_device_name = instance.root_device_name
|
||||||
|
@ -599,3 +599,50 @@ def validate_subnet_details_after_creating_eni(
|
|||||||
for eni in enis_created:
|
for eni in enis_created:
|
||||||
client.delete_network_interface(NetworkInterfaceId=eni["NetworkInterfaceId"])
|
client.delete_network_interface(NetworkInterfaceId=eni["NetworkInterfaceId"])
|
||||||
client.delete_subnet(SubnetId=subnet["SubnetId"])
|
client.delete_subnet(SubnetId=subnet["SubnetId"])
|
||||||
|
|
||||||
|
|
||||||
|
@mock_ec2
|
||||||
|
def test_run_instances_should_attach_to_default_subnet():
|
||||||
|
ec2 = boto3.resource("ec2", region_name="us-west-1")
|
||||||
|
client = boto3.client("ec2", region_name="us-west-1")
|
||||||
|
ec2.create_security_group(GroupName="sg01", Description="Test security group sg01")
|
||||||
|
# run_instances
|
||||||
|
instances = client.run_instances(
|
||||||
|
MinCount=1,
|
||||||
|
MaxCount=1,
|
||||||
|
SecurityGroups=["sg01"],
|
||||||
|
TagSpecifications=[
|
||||||
|
{
|
||||||
|
"ResourceType": "instance",
|
||||||
|
"Tags": [{"Key": "Name", "Value": "test-01"},],
|
||||||
|
}
|
||||||
|
],
|
||||||
|
)
|
||||||
|
default_subnet_id = client.describe_subnets()["Subnets"][0]["SubnetId"]
|
||||||
|
instances["Instances"][0]["NetworkInterfaces"][0]["SubnetId"].should.equal(
|
||||||
|
default_subnet_id
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@mock_ec2
|
||||||
|
def test_describe_subnets_where_network_interface_has_no_subnets_attached():
|
||||||
|
# https://github.com/spulec/moto/issues/2877
|
||||||
|
# create security groups
|
||||||
|
ec2 = boto3.resource("ec2", region_name="us-west-1")
|
||||||
|
client = boto3.client("ec2", region_name="us-west-1")
|
||||||
|
ec2.create_security_group(GroupName="sg01", Description="Test security group sg01")
|
||||||
|
# run_instances
|
||||||
|
client.run_instances(
|
||||||
|
MinCount=1,
|
||||||
|
MaxCount=1,
|
||||||
|
SecurityGroups=["sg01"],
|
||||||
|
TagSpecifications=[
|
||||||
|
{
|
||||||
|
"ResourceType": "instance",
|
||||||
|
"Tags": [{"Key": "Name", "Value": "test-01"},],
|
||||||
|
}
|
||||||
|
],
|
||||||
|
)
|
||||||
|
# describe_subnets
|
||||||
|
subnets = client.describe_subnets()["Subnets"]
|
||||||
|
subnets[0]["AvailableIpAddressCount"].should.equal(4090)
|
||||||
|
Loading…
Reference in New Issue
Block a user