This commit is contained in:
parent
29829e2eaa
commit
b8932b19c9
@ -424,10 +424,26 @@ class Instance(TaggedEC2Resource, BotoInstance, CloudFormationModel):
|
||||
def dynamic_group_list(self):
|
||||
return self.security_groups
|
||||
|
||||
def _get_private_ip_from_nic(self, nic):
|
||||
private_ip = nic.get("PrivateIpAddress")
|
||||
if private_ip:
|
||||
return private_ip
|
||||
for address in nic.get("PrivateIpAddresses", []):
|
||||
if address.get("Primary") == "true":
|
||||
return address.get("PrivateIpAddress")
|
||||
|
||||
def prep_nics(
|
||||
self, nic_spec, private_ip=None, associate_public_ip=None, security_groups=None
|
||||
):
|
||||
self.nics = {}
|
||||
for nic in nic_spec:
|
||||
if int(nic.get("DeviceIndex")) == 0:
|
||||
nic_associate_public_ip = nic.get("AssociatePublicIpAddress")
|
||||
if nic_associate_public_ip is not None:
|
||||
associate_public_ip = nic_associate_public_ip == "true"
|
||||
if private_ip is None:
|
||||
private_ip = self._get_private_ip_from_nic(nic)
|
||||
break
|
||||
|
||||
if self.subnet_id:
|
||||
subnet = self.ec2_backend.get_subnet(self.subnet_id)
|
||||
|
@ -2418,6 +2418,63 @@ def test_run_instance_cannot_have_subnet_and_networkinterface_parameter():
|
||||
)
|
||||
|
||||
|
||||
@mock_ec2
|
||||
def test_run_instance_in_subnet_with_nic_private_ip():
|
||||
vpc_cidr_block = "10.26.0.0/16"
|
||||
subnet_cidr_block = "10.26.1.0/24"
|
||||
private_ip = "10.26.1.3"
|
||||
ec2 = boto3.resource("ec2", region_name="eu-west-1")
|
||||
vpc = ec2.create_vpc(CidrBlock=vpc_cidr_block)
|
||||
subnet = ec2.create_subnet(
|
||||
VpcId=vpc.id,
|
||||
CidrBlock=subnet_cidr_block,
|
||||
)
|
||||
my_interface = {
|
||||
"SubnetId": subnet.id,
|
||||
"DeviceIndex": 0,
|
||||
"PrivateIpAddress": private_ip,
|
||||
}
|
||||
[instance] = ec2.create_instances(
|
||||
ImageId=EXAMPLE_AMI_ID, NetworkInterfaces=[my_interface], MinCount=1, MaxCount=1
|
||||
)
|
||||
instance.private_ip_address.should.equal(private_ip)
|
||||
|
||||
interfaces = instance.network_interfaces_attribute
|
||||
address = interfaces[0]["PrivateIpAddresses"][0]
|
||||
address.shouldnt.have.key("Association")
|
||||
|
||||
|
||||
@mock_ec2
|
||||
def test_run_instance_in_subnet_with_nic_private_ip_and_public_association():
|
||||
vpc_cidr_block = "10.26.0.0/16"
|
||||
subnet_cidr_block = "10.26.1.0/24"
|
||||
primary_private_ip = "10.26.1.3"
|
||||
other_private_ip = "10.26.1.4"
|
||||
ec2 = boto3.resource("ec2", region_name="eu-west-1")
|
||||
vpc = ec2.create_vpc(CidrBlock=vpc_cidr_block)
|
||||
subnet = ec2.create_subnet(
|
||||
VpcId=vpc.id,
|
||||
CidrBlock=subnet_cidr_block,
|
||||
)
|
||||
my_interface = {
|
||||
"SubnetId": subnet.id,
|
||||
"DeviceIndex": 0,
|
||||
"AssociatePublicIpAddress": True,
|
||||
"PrivateIpAddresses": [
|
||||
{"Primary": True, "PrivateIpAddress": primary_private_ip},
|
||||
{"Primary": False, "PrivateIpAddress": other_private_ip},
|
||||
],
|
||||
}
|
||||
[instance] = ec2.create_instances(
|
||||
ImageId=EXAMPLE_AMI_ID, NetworkInterfaces=[my_interface], MinCount=1, MaxCount=1
|
||||
)
|
||||
instance.private_ip_address.should.equal(primary_private_ip)
|
||||
|
||||
interfaces = instance.network_interfaces_attribute
|
||||
address = interfaces[0]["PrivateIpAddresses"][0]
|
||||
address["Association"].should.have.key("IpOwnerId").equal(ACCOUNT_ID)
|
||||
|
||||
|
||||
@mock_ec2
|
||||
def test_describe_instances_dryrun():
|
||||
client = boto3.client("ec2", region_name="us-east-1")
|
||||
|
Loading…
Reference in New Issue
Block a user