ENI: DescribeNetworkInterfaces attachment.instance-id filter (#3549)

This commit is contained in:
Preston Doster 2021-11-23 16:59:35 -06:00 committed by GitHub
parent 4920793498
commit d64fd52b57
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 0 deletions

View File

@ -504,6 +504,8 @@ class NetworkInterface(TaggedEC2Resource, CloudFormationModel):
return self.subnet.availability_zone
elif filter_name == "description":
return self.description
elif filter_name == "attachment.instance-id":
return self.instance.id if self.instance else None
else:
return super().get_filter_value(filter_name, "DescribeNetworkInterfaces")

View File

@ -10,6 +10,7 @@ import sure # noqa # pylint: disable=unused-import
from moto import mock_ec2, mock_ec2_deprecated, settings
from moto.ec2.utils import random_private_ip
from tests import EXAMPLE_AMI_ID
from tests.helpers import requires_boto_gte
from uuid import uuid4
@ -638,6 +639,48 @@ def test_elastic_network_interfaces_get_by_description():
enis.should.have.length_of(0)
@mock_ec2
def test_elastic_network_interfaces_get_by_attachment_instance_id():
ec2_resource = boto3.resource("ec2", region_name="us-west-2")
ec2_client = boto3.client("ec2", region_name="us-west-2")
vpc = ec2_resource.create_vpc(CidrBlock="10.0.0.0/16")
subnet = ec2_resource.create_subnet(
VpcId=vpc.id, CidrBlock="10.0.0.0/24", AvailabilityZone="us-west-2a"
)
security_group1 = ec2_resource.create_security_group(
GroupName=str(uuid4()), Description="desc"
)
create_instances_result = ec2_resource.create_instances(
ImageId=EXAMPLE_AMI_ID, MinCount=1, MaxCount=1
)
instance = create_instances_result[0]
# we should have one ENI attached to our ec2 instance by default
filters = [{"Name": "attachment.instance-id", "Values": [instance.id]}]
enis = ec2_client.describe_network_interfaces(Filters=filters)
enis.get("NetworkInterfaces").should.have.length_of(1)
# attach another ENI to our existing instance, total should be 2
eni1 = ec2_resource.create_network_interface(
SubnetId=subnet.id, Groups=[security_group1.id]
)
ec2_client.attach_network_interface(
NetworkInterfaceId=eni1.id, InstanceId=instance.id, DeviceIndex=1
)
filters = [{"Name": "attachment.instance-id", "Values": [instance.id]}]
enis = ec2_client.describe_network_interfaces(Filters=filters)
enis.get("NetworkInterfaces").should.have.length_of(2)
# we shouldn't find any ENIs that are attached to this fake instance ID
filters = [{"Name": "attachment.instance-id", "Values": ["this-doesnt-match-lol"]}]
enis = ec2_client.describe_network_interfaces(Filters=filters)
enis.get("NetworkInterfaces").should.have.length_of(0)
@mock_ec2
def test_elastic_network_interfaces_describe_network_interfaces_with_filter():
ec2 = boto3.resource("ec2", region_name="us-west-2")