def test_elastic_network_interfaces_get_by_availability_zone():
    ec2 = boto3.resource("ec2", region_name="us-west-2")
    ec2_client = boto3.client("ec2", region_name="us-west-2")

    vpc = ec2.create_vpc(CidrBlock="10.0.0.0/16")
    subnet1 = ec2.create_subnet(VpcId=vpc.id,
                                CidrBlock="10.0.0.0/24",
                                AvailabilityZone="us-west-2a")

    subnet2 = ec2.create_subnet(VpcId=vpc.id,
                                CidrBlock="10.0.1.0/24",
                                AvailabilityZone="us-west-2b")

    eni1 = ec2.create_network_interface(SubnetId=subnet1.id,
                                        PrivateIpAddress="10.0.0.15")

    eni2 = ec2.create_network_interface(SubnetId=subnet2.id,
                                        PrivateIpAddress="10.0.1.15")

    # The status of the new interface should be 'available'
    waiter = ec2_client.get_waiter("network_interface_available")
    waiter.wait(NetworkInterfaceIds=[eni1.id, eni2.id])

    filters = [{"Name": "availability-zone", "Values": ["us-west-2a"]}]
    enis = list(ec2.network_interfaces.filter(Filters=filters))
    [eni.id for eni in enis].should.contain(eni1.id)
    [eni.id for eni in enis].shouldnt.contain(eni2.id)

    filters = [{"Name": "availability-zone", "Values": ["us-west-2c"]}]
    enis = list(ec2.network_interfaces.filter(Filters=filters))
    [eni.id for eni in enis].shouldnt.contain(eni1.id)
    [eni.id for eni in enis].shouldnt.contain(eni2.id)
예제 #2
0
def test_elastic_network_interfaces_get_by_availability_zone():
    ec2 = boto3.resource('ec2', region_name='us-west-2')
    ec2_client = boto3.client('ec2', region_name='us-west-2')

    vpc = ec2.create_vpc(CidrBlock='10.0.0.0/16')
    subnet1 = ec2.create_subnet(
        VpcId=vpc.id, CidrBlock='10.0.0.0/24', AvailabilityZone='us-west-2a')

    subnet2 = ec2.create_subnet(
        VpcId=vpc.id, CidrBlock='10.0.1.0/24', AvailabilityZone='us-west-2b')

    eni1 = ec2.create_network_interface(
        SubnetId=subnet1.id, PrivateIpAddress='10.0.0.15')

    eni2 = ec2.create_network_interface(
        SubnetId=subnet2.id, PrivateIpAddress='10.0.1.15')

    # The status of the new interface should be 'available'
    waiter = ec2_client.get_waiter('network_interface_available')
    waiter.wait(NetworkInterfaceIds=[eni1.id, eni2.id])

    filters = [{'Name': 'availability-zone', 'Values': ['us-west-2a']}]
    enis = list(ec2.network_interfaces.filter(Filters=filters))
    enis.should.have.length_of(1)

    filters = [{'Name': 'availability-zone', 'Values': ['us-west-2c']}]
    enis = list(ec2.network_interfaces.filter(Filters=filters))
    enis.should.have.length_of(0)
def test_elastic_network_interfaces_boto3():
    ec2 = boto3.resource("ec2", region_name="us-east-1")
    client = boto3.client("ec2", "us-east-1")

    vpc = ec2.create_vpc(CidrBlock="10.0.0.0/16")
    subnet = ec2.create_subnet(VpcId=vpc.id, CidrBlock="10.0.0.0/18")

    with pytest.raises(ClientError) as ex:
        ec2.create_network_interface(SubnetId=subnet.id, DryRun=True)
    ex.value.response["ResponseMetadata"]["HTTPStatusCode"].should.equal(412)
    ex.value.response["Error"]["Code"].should.equal("DryRunOperation")
    ex.value.response["Error"]["Message"].should.equal(
        "An error occurred (DryRunOperation) when calling the CreateNetworkInterface operation: Request would have succeeded, but DryRun flag is set"
    )

    eni_id = ec2.create_network_interface(SubnetId=subnet.id).id

    my_enis = client.describe_network_interfaces(NetworkInterfaceIds=[eni_id])[
        "NetworkInterfaces"
    ]
    my_enis.should.have.length_of(1)
    eni = my_enis[0]
    eni["Groups"].should.have.length_of(1)
    eni["PrivateIpAddresses"].should.have.length_of(1)
    eni["PrivateIpAddresses"][0]["PrivateIpAddress"].startswith("10.").should.be.true

    with pytest.raises(ClientError) as ex:
        client.delete_network_interface(NetworkInterfaceId=eni_id, DryRun=True)
    ex.value.response["ResponseMetadata"]["HTTPStatusCode"].should.equal(412)
    ex.value.response["Error"]["Code"].should.equal("DryRunOperation")
    ex.value.response["Error"]["Message"].should.equal(
        "An error occurred (DryRunOperation) when calling the DeleteNetworkInterface operation: Request would have succeeded, but DryRun flag is set"
    )

    client.delete_network_interface(NetworkInterfaceId=eni_id)

    all_enis = client.describe_network_interfaces()["NetworkInterfaces"]
    [eni["NetworkInterfaceId"] for eni in all_enis].shouldnt.contain(eni_id)

    with pytest.raises(ClientError) as ex:
        client.describe_network_interfaces(NetworkInterfaceIds=[eni_id])
    ex.value.response["ResponseMetadata"]["HTTPStatusCode"].should.equal(400)
    ex.value.response["ResponseMetadata"].should.have.key("RequestId")
    ex.value.response["Error"]["Code"].should.equal(
        "InvalidNetworkInterfaceID.NotFound"
    )

    with pytest.raises(ClientError) as ex:
        client.delete_network_interface(NetworkInterfaceId=eni_id)
    ex.value.response["ResponseMetadata"]["HTTPStatusCode"].should.equal(400)
    ex.value.response["ResponseMetadata"].should.have.key("RequestId")
    ex.value.response["Error"]["Code"].should.equal(
        "InvalidNetworkInterfaceID.NotFound"
    )
def test_elastic_network_interfaces_get_by_tag_name():
    ec2 = boto3.resource("ec2", region_name="us-west-2")
    ec2_client = boto3.client("ec2", region_name="us-west-2")

    vpc = ec2.create_vpc(CidrBlock="10.0.0.0/16")
    subnet = ec2.create_subnet(VpcId=vpc.id,
                               CidrBlock="10.0.0.0/24",
                               AvailabilityZone="us-west-2a")

    eni1 = ec2.create_network_interface(SubnetId=subnet.id,
                                        PrivateIpAddress="10.0.10.5")

    with pytest.raises(ClientError) as ex:
        eni1.create_tags(Tags=[{"Key": "Name", "Value": "eni1"}], DryRun=True)
    ex.value.response["Error"]["Code"].should.equal("DryRunOperation")
    ex.value.response["ResponseMetadata"]["HTTPStatusCode"].should.equal(412)
    ex.value.response["Error"]["Message"].should.equal(
        "An error occurred (DryRunOperation) when calling the CreateTags operation: Request would have succeeded, but DryRun flag is set"
    )

    tag_value = str(uuid4())
    eni1.create_tags(Tags=[{"Key": "Name", "Value": tag_value}])

    # The status of the new interface should be 'available'
    waiter = ec2_client.get_waiter("network_interface_available")
    waiter.wait(NetworkInterfaceIds=[eni1.id])

    filters = [{"Name": "tag:Name", "Values": [tag_value]}]
    enis = list(ec2.network_interfaces.filter(Filters=filters))
    enis.should.have.length_of(1)

    filters = [{"Name": "tag:Name", "Values": ["wrong-name"]}]
    enis = list(ec2.network_interfaces.filter(Filters=filters))
    enis.should.have.length_of(0)
예제 #5
0
def test_elastic_network_interfaces_get_by_tag_name():
    ec2 = boto3.resource('ec2', region_name='us-west-2')
    ec2_client = boto3.client('ec2', region_name='us-west-2')

    vpc = ec2.create_vpc(CidrBlock='10.0.0.0/16')
    subnet = ec2.create_subnet(
        VpcId=vpc.id, CidrBlock='10.0.0.0/24', AvailabilityZone='us-west-2a')

    eni1 = ec2.create_network_interface(
        SubnetId=subnet.id, PrivateIpAddress='10.0.10.5')

    with assert_raises(ClientError) as ex:
        eni1.create_tags(Tags=[{'Key': 'Name', 'Value': 'eni1'}], DryRun=True)
    ex.exception.response['Error']['Code'].should.equal('DryRunOperation')
    ex.exception.response['ResponseMetadata'][
        'HTTPStatusCode'].should.equal(400)
    ex.exception.response['Error']['Message'].should.equal(
        'An error occurred (DryRunOperation) when calling the CreateTags operation: Request would have succeeded, but DryRun flag is set')

    eni1.create_tags(Tags=[{'Key': 'Name', 'Value': 'eni1'}])

    # The status of the new interface should be 'available'
    waiter = ec2_client.get_waiter('network_interface_available')
    waiter.wait(NetworkInterfaceIds=[eni1.id])

    filters = [{'Name': 'tag:Name', 'Values': ['eni1']}]
    enis = list(ec2.network_interfaces.filter(Filters=filters))
    enis.should.have.length_of(1)

    filters = [{'Name': 'tag:Name', 'Values': ['wrong-name']}]
    enis = list(ec2.network_interfaces.filter(Filters=filters))
    enis.should.have.length_of(0)
def test_elastic_network_interfaces_get_by_description():
    ec2 = boto3.resource("ec2", region_name="us-west-2")
    ec2_client = boto3.client("ec2", region_name="us-west-2")

    vpc = ec2.create_vpc(CidrBlock="10.0.0.0/16")
    subnet = ec2.create_subnet(VpcId=vpc.id,
                               CidrBlock="10.0.0.0/24",
                               AvailabilityZone="us-west-2a")

    desc = str(uuid4())
    eni1 = ec2.create_network_interface(SubnetId=subnet.id,
                                        PrivateIpAddress="10.0.10.5",
                                        Description=desc)

    # The status of the new interface should be 'available'
    waiter = ec2_client.get_waiter("network_interface_available")
    waiter.wait(NetworkInterfaceIds=[eni1.id])

    filters = [{"Name": "description", "Values": [eni1.description]}]
    enis = list(ec2.network_interfaces.filter(Filters=filters))
    enis.should.have.length_of(1)

    filters = [{"Name": "description", "Values": ["bad description"]}]
    enis = list(ec2.network_interfaces.filter(Filters=filters))
    enis.should.have.length_of(0)
def test_assign_ipv6_addresses__by_address():
    ec2 = boto3.resource("ec2", region_name="us-east-1")
    client = boto3.client("ec2", "us-east-1")

    vpc = ec2.create_vpc(CidrBlock="10.0.0.0/16")
    subnet = ec2.create_subnet(VpcId=vpc.id, CidrBlock="10.0.0.0/18")

    ipv6_orig = random_private_ip("2001:db8::/101", ipv6=True)
    ipv6_2 = random_private_ip("2001:db8::/101", ipv6=True)
    ipv6_3 = random_private_ip("2001:db8::/101", ipv6=True)
    eni = ec2.create_network_interface(
        SubnetId=subnet.id, Ipv6Addresses=[{"Ipv6Address": ipv6_orig}]
    )
    resp = client.describe_network_interfaces(NetworkInterfaceIds=[eni.id])
    my_eni = resp["NetworkInterfaces"][0]
    my_eni.should.have.key("Ipv6Addresses").equals([{"Ipv6Address": ipv6_orig}])

    client.assign_ipv6_addresses(
        NetworkInterfaceId=eni.id, Ipv6Addresses=[ipv6_2, ipv6_3]
    )

    resp = client.describe_network_interfaces(NetworkInterfaceIds=[eni.id])
    my_eni = resp["NetworkInterfaces"][0]
    my_eni.should.have.key("Ipv6Addresses").length_of(3)
    my_eni.should.have.key("Ipv6Addresses").should.contain({"Ipv6Address": ipv6_orig})
    my_eni.should.have.key("Ipv6Addresses").should.contain({"Ipv6Address": ipv6_2})
    my_eni.should.have.key("Ipv6Addresses").should.contain({"Ipv6Address": ipv6_3})
def test_unassign_private_ip_addresses():
    ec2 = boto3.resource("ec2", region_name="us-east-1")
    client = boto3.client("ec2", "us-east-1")

    vpc = ec2.create_vpc(CidrBlock="10.0.0.0/16")
    subnet = ec2.create_subnet(VpcId=vpc.id, CidrBlock="10.0.0.0/18")

    private_ip = "54.0.0.1"
    eni = ec2.create_network_interface(SubnetId=subnet.id, PrivateIpAddress=private_ip)

    client.assign_private_ip_addresses(
        NetworkInterfaceId=eni.id, SecondaryPrivateIpAddressCount=2
    )
    resp = client.describe_network_interfaces(NetworkInterfaceIds=[eni.id])
    my_eni = resp["NetworkInterfaces"][0]
    ips_before = [addr["PrivateIpAddress"] for addr in my_eni["PrivateIpAddresses"]]

    # Remove IP
    resp = client.unassign_private_ip_addresses(
        NetworkInterfaceId=eni.id, PrivateIpAddresses=[ips_before[1]]
    )

    # Verify it's gone
    resp = client.describe_network_interfaces(NetworkInterfaceIds=[eni.id])
    my_eni = resp["NetworkInterfaces"][0]
    my_eni.should.have.key("PrivateIpAddresses").should.have.length_of(2)
    my_eni.should.have.key("PrivateIpAddresses").contain(
        {"Primary": True, "PrivateIpAddress": "54.0.0.1"}
    )
    my_eni.should.have.key("PrivateIpAddresses").contain(
        {"Primary": False, "PrivateIpAddress": ips_before[2]}
    )
def test_assign_private_ip_addresses__with_secondary_count():
    ec2 = boto3.resource("ec2", region_name="us-east-1")
    client = boto3.client("ec2", "us-east-1")

    vpc = ec2.create_vpc(CidrBlock="10.0.0.0/16")
    subnet = ec2.create_subnet(VpcId=vpc.id, CidrBlock="10.0.0.0/18")

    private_ip = "54.0.0.1"
    eni = ec2.create_network_interface(SubnetId=subnet.id, PrivateIpAddress=private_ip)

    client.assign_private_ip_addresses(
        NetworkInterfaceId=eni.id, SecondaryPrivateIpAddressCount=2
    )

    # Verify second ip's are added
    resp = client.describe_network_interfaces(NetworkInterfaceIds=[eni.id])
    my_eni = resp["NetworkInterfaces"][0]

    my_eni.should.have.key("PrivateIpAddress").equals("54.0.0.1")
    my_eni.should.have.key("PrivateIpAddresses").should.have.length_of(3)
    my_eni.should.have.key("PrivateIpAddresses").contain(
        {"Primary": True, "PrivateIpAddress": "54.0.0.1"}
    )

    # Not as ipv6 addresses though
    my_eni.should.have.key("Ipv6Addresses").equals([])
def test_assign_private_ip_addresses():
    ec2 = boto3.resource("ec2", region_name="us-east-1")
    client = boto3.client("ec2", "us-east-1")

    vpc = ec2.create_vpc(CidrBlock="10.0.0.0/16")
    subnet = ec2.create_subnet(VpcId=vpc.id, CidrBlock="10.0.0.0/18")

    private_ip = "54.0.0.1"
    eni = ec2.create_network_interface(SubnetId=subnet.id, PrivateIpAddress=private_ip)

    resp = client.describe_network_interfaces(NetworkInterfaceIds=[eni.id])
    my_eni = resp["NetworkInterfaces"][0]
    my_eni.should.have.key("PrivateIpAddress").equals("54.0.0.1")
    my_eni.should.have.key("PrivateIpAddresses").equals(
        [{"Primary": True, "PrivateIpAddress": "54.0.0.1"}]
    )

    # Do not pass SecondaryPrivateIpAddressCount-parameter
    client.assign_private_ip_addresses(NetworkInterfaceId=eni.id)

    # Verify nothing changes
    resp = client.describe_network_interfaces(NetworkInterfaceIds=[eni.id])
    my_eni = resp["NetworkInterfaces"][0]
    my_eni.should.have.key("PrivateIpAddress").equals("54.0.0.1")
    my_eni.should.have.key("PrivateIpAddresses").equals(
        [{"Primary": True, "PrivateIpAddress": "54.0.0.1"}]
    )
예제 #11
0
def test_elastic_network_interfaces_get_by_private_ip():
    ec2 = boto3.resource("ec2", region_name="us-west-2")
    ec2_client = boto3.client("ec2", region_name="us-west-2")
    random_ip = ".".join(map(str, (random.randint(0, 99) for _ in range(4))))

    vpc = ec2.create_vpc(CidrBlock="10.0.0.0/16")
    subnet = ec2.create_subnet(VpcId=vpc.id,
                               CidrBlock="10.0.0.0/24",
                               AvailabilityZone="us-west-2a")

    eni1 = ec2.create_network_interface(SubnetId=subnet.id,
                                        PrivateIpAddress=random_ip)

    # The status of the new interface should be 'available'
    waiter = ec2_client.get_waiter("network_interface_available")
    waiter.wait(NetworkInterfaceIds=[eni1.id])

    filters = [{"Name": "private-ip-address", "Values": [random_ip]}]
    enis = list(ec2.network_interfaces.filter(Filters=filters))
    enis.should.have.length_of(1)

    filters = [{"Name": "private-ip-address", "Values": ["10.0.10.10"]}]
    enis = list(ec2.network_interfaces.filter(Filters=filters))
    enis.should.have.length_of(0)

    filters = [{"Name": "addresses.private-ip-address", "Values": [random_ip]}]
    enis = list(ec2.network_interfaces.filter(Filters=filters))
    enis.should.have.length_of(1)

    filters = [{
        "Name": "addresses.private-ip-address",
        "Values": ["10.0.10.10"]
    }]
    enis = list(ec2.network_interfaces.filter(Filters=filters))
    enis.should.have.length_of(0)
예제 #12
0
def test_elastic_network_interfaces_with_private_ip_boto3():
    ec2 = boto3.resource("ec2", region_name="us-east-1")
    client = boto3.client("ec2", "us-east-1")

    vpc = ec2.create_vpc(CidrBlock="10.0.0.0/16")
    subnet = ec2.create_subnet(VpcId=vpc.id, CidrBlock="10.0.0.0/18")

    private_ip = "54.0.0.1"
    eni = ec2.create_network_interface(SubnetId=subnet.id,
                                       PrivateIpAddress=private_ip)

    all_enis = client.describe_network_interfaces()["NetworkInterfaces"]
    [eni["NetworkInterfaceId"] for eni in all_enis].should.contain(eni.id)

    my_enis = client.describe_network_interfaces(
        NetworkInterfaceIds=[eni.id])["NetworkInterfaces"]

    eni = my_enis[0]
    eni["Groups"].should.have.length_of(0)

    eni["PrivateIpAddresses"].should.have.length_of(1)
    eni["PrivateIpAddresses"][0]["PrivateIpAddress"].should.equal(private_ip)
예제 #13
0
def test_elastic_network_interfaces_auto_create_securitygroup():
    ec2 = boto3.resource("ec2", region_name="us-west-2")
    ec2_client = boto3.client("ec2", region_name="us-west-2")

    vpc = ec2.create_vpc(CidrBlock="10.0.0.0/16")
    subnet = ec2.create_subnet(VpcId=vpc.id,
                               CidrBlock="10.0.0.0/24",
                               AvailabilityZone="us-west-2a")

    eni1 = ec2.create_network_interface(SubnetId=subnet.id,
                                        PrivateIpAddress="10.0.10.5",
                                        Groups=["testgroup"])

    # The status of the new interface should be 'available'
    waiter = ec2_client.get_waiter("network_interface_available")
    waiter.wait(NetworkInterfaceIds=[eni1.id])

    sgs = ec2_client.describe_security_groups()["SecurityGroups"]
    found_sg = [sg for sg in sgs if sg["GroupId"] == "testgroup"]
    found_sg.should.have.length_of(1)

    found_sg[0]["GroupName"].should.equal("testgroup")
    found_sg[0]["Description"].should.equal("testgroup")
예제 #14
0
def test_elastic_network_interfaces_describe_network_interfaces_with_filter():
    ec2 = boto3.resource('ec2', region_name='us-west-2')
    ec2_client = boto3.client('ec2', region_name='us-west-2')

    vpc = ec2.create_vpc(CidrBlock='10.0.0.0/16')
    subnet = ec2.create_subnet(VpcId=vpc.id,
                               CidrBlock='10.0.0.0/24',
                               AvailabilityZone='us-west-2a')

    eni1 = ec2.create_network_interface(SubnetId=subnet.id,
                                        PrivateIpAddress='10.0.10.5',
                                        Description='test interface')

    # The status of the new interface should be 'available'
    waiter = ec2_client.get_waiter('network_interface_available')
    waiter.wait(NetworkInterfaceIds=[eni1.id])

    # Filter by network-interface-id
    response = ec2_client.describe_network_interfaces(
        Filters=[{
            'Name': 'network-interface-id',
            'Values': [eni1.id]
        }])
    response['NetworkInterfaces'].should.have.length_of(1)
    response['NetworkInterfaces'][0]['NetworkInterfaceId'].should.equal(
        eni1.id)
    response['NetworkInterfaces'][0]['PrivateIpAddress'].should.equal(
        eni1.private_ip_address)
    response['NetworkInterfaces'][0]['Description'].should.equal(
        eni1.description)

    response = ec2_client.describe_network_interfaces(
        Filters=[{
            'Name': 'network-interface-id',
            'Values': ['bad-id']
        }])
    response['NetworkInterfaces'].should.have.length_of(0)

    # Filter by private-ip-address
    response = ec2_client.describe_network_interfaces(
        Filters=[{
            'Name': 'private-ip-address',
            'Values': [eni1.private_ip_address]
        }])
    response['NetworkInterfaces'].should.have.length_of(1)
    response['NetworkInterfaces'][0]['NetworkInterfaceId'].should.equal(
        eni1.id)
    response['NetworkInterfaces'][0]['PrivateIpAddress'].should.equal(
        eni1.private_ip_address)
    response['NetworkInterfaces'][0]['Description'].should.equal(
        eni1.description)

    response = ec2_client.describe_network_interfaces(
        Filters=[{
            'Name': 'private-ip-address',
            'Values': ['11.11.11.11']
        }])
    response['NetworkInterfaces'].should.have.length_of(0)

    # Filter by sunet-id
    response = ec2_client.describe_network_interfaces(
        Filters=[{
            'Name': 'subnet-id',
            'Values': [eni1.subnet.id]
        }])
    response['NetworkInterfaces'].should.have.length_of(1)
    response['NetworkInterfaces'][0]['NetworkInterfaceId'].should.equal(
        eni1.id)
    response['NetworkInterfaces'][0]['PrivateIpAddress'].should.equal(
        eni1.private_ip_address)
    response['NetworkInterfaces'][0]['Description'].should.equal(
        eni1.description)

    response = ec2_client.describe_network_interfaces(
        Filters=[{
            'Name': 'subnet-id',
            'Values': ['sn-bad-id']
        }])
    response['NetworkInterfaces'].should.have.length_of(0)

    # Filter by description
    response = ec2_client.describe_network_interfaces(
        Filters=[{
            'Name': 'description',
            'Values': [eni1.description]
        }])
    response['NetworkInterfaces'].should.have.length_of(1)
    response['NetworkInterfaces'][0]['NetworkInterfaceId'].should.equal(
        eni1.id)
    response['NetworkInterfaces'][0]['PrivateIpAddress'].should.equal(
        eni1.private_ip_address)
    response['NetworkInterfaces'][0]['Description'].should.equal(
        eni1.description)

    response = ec2_client.describe_network_interfaces(
        Filters=[{
            'Name': 'description',
            'Values': ['bad description']
        }])
    response['NetworkInterfaces'].should.have.length_of(0)

    # Filter by multiple filters
    response = ec2_client.describe_network_interfaces(
        Filters=[{
            'Name': 'private-ip-address',
            'Values': [eni1.private_ip_address]
        }, {
            'Name': 'network-interface-id',
            'Values': [eni1.id]
        }, {
            'Name': 'subnet-id',
            'Values': [eni1.subnet.id]
        }])
    response['NetworkInterfaces'].should.have.length_of(1)
    response['NetworkInterfaces'][0]['NetworkInterfaceId'].should.equal(
        eni1.id)
    response['NetworkInterfaces'][0]['PrivateIpAddress'].should.equal(
        eni1.private_ip_address)
    response['NetworkInterfaces'][0]['Description'].should.equal(
        eni1.description)
예제 #15
0
def test_elastic_network_interfaces_describe_network_interfaces_with_filter():
    ec2 = boto3.resource("ec2", region_name="us-west-2")
    ec2_client = boto3.client("ec2", region_name="us-west-2")

    random_ip = ".".join(map(str, (random.randint(0, 99) for _ in range(4))))

    vpc = ec2.create_vpc(CidrBlock="10.0.0.0/16")
    subnet = ec2.create_subnet(VpcId=vpc.id,
                               CidrBlock="10.0.0.0/24",
                               AvailabilityZone="us-west-2a")

    sg = ec2_client.create_security_group(Description="test",
                                          GroupName=str(uuid4()))
    sg_id = sg["GroupId"]

    eni1 = ec2.create_network_interface(
        SubnetId=subnet.id,
        PrivateIpAddress=random_ip,
        Description=str(uuid4()),
        Groups=[sg_id],
    )

    # The status of the new interface should be 'available'
    waiter = ec2_client.get_waiter("network_interface_available")
    waiter.wait(NetworkInterfaceIds=[eni1.id])

    # Filter by network-interface-id
    response = ec2_client.describe_network_interfaces(
        Filters=[{
            "Name": "network-interface-id",
            "Values": [eni1.id]
        }])
    response["NetworkInterfaces"].should.have.length_of(1)
    response["NetworkInterfaces"][0]["NetworkInterfaceId"].should.equal(
        eni1.id)
    response["NetworkInterfaces"][0]["PrivateIpAddress"].should.equal(
        eni1.private_ip_address)
    response["NetworkInterfaces"][0]["Description"].should.equal(
        eni1.description)

    # Filter by network-interface-id
    response = ec2_client.describe_network_interfaces(
        Filters=[{
            "Name": "group-id",
            "Values": [sg_id]
        }])
    response["NetworkInterfaces"].should.have.length_of(1)
    response["NetworkInterfaces"][0]["NetworkInterfaceId"].should.equal(
        eni1.id)

    response = ec2_client.describe_network_interfaces(
        Filters=[{
            "Name": "network-interface-id",
            "Values": ["bad-id"]
        }])
    response["NetworkInterfaces"].should.have.length_of(0)

    # Filter by private-ip-address
    response = ec2_client.describe_network_interfaces(
        Filters=[{
            "Name": "private-ip-address",
            "Values": [eni1.private_ip_address]
        }])
    response["NetworkInterfaces"].should.have.length_of(1)
    response["NetworkInterfaces"][0]["NetworkInterfaceId"].should.equal(
        eni1.id)
    response["NetworkInterfaces"][0]["PrivateIpAddress"].should.equal(
        eni1.private_ip_address)
    response["NetworkInterfaces"][0]["Description"].should.equal(
        eni1.description)

    response = ec2_client.describe_network_interfaces(
        Filters=[{
            "Name": "private-ip-address",
            "Values": ["11.11.11.11"]
        }])
    response["NetworkInterfaces"].should.have.length_of(0)

    # Filter by sunet-id
    response = ec2_client.describe_network_interfaces(
        Filters=[{
            "Name": "subnet-id",
            "Values": [eni1.subnet.id]
        }])
    response["NetworkInterfaces"].should.have.length_of(1)
    response["NetworkInterfaces"][0]["NetworkInterfaceId"].should.equal(
        eni1.id)
    response["NetworkInterfaces"][0]["PrivateIpAddress"].should.equal(
        eni1.private_ip_address)
    response["NetworkInterfaces"][0]["Description"].should.equal(
        eni1.description)

    response = ec2_client.describe_network_interfaces(
        Filters=[{
            "Name": "subnet-id",
            "Values": ["sn-bad-id"]
        }])
    response["NetworkInterfaces"].should.have.length_of(0)

    # Filter by description
    response = ec2_client.describe_network_interfaces(
        Filters=[{
            "Name": "description",
            "Values": [eni1.description]
        }])
    response["NetworkInterfaces"].should.have.length_of(1)
    response["NetworkInterfaces"][0]["NetworkInterfaceId"].should.equal(
        eni1.id)
    response["NetworkInterfaces"][0]["PrivateIpAddress"].should.equal(
        eni1.private_ip_address)
    response["NetworkInterfaces"][0]["Description"].should.equal(
        eni1.description)

    response = ec2_client.describe_network_interfaces(
        Filters=[{
            "Name": "description",
            "Values": ["bad description"]
        }])
    response["NetworkInterfaces"].should.have.length_of(0)

    # Filter by multiple filters
    response = ec2_client.describe_network_interfaces(Filters=[
        {
            "Name": "private-ip-address",
            "Values": [eni1.private_ip_address]
        },
        {
            "Name": "network-interface-id",
            "Values": [eni1.id]
        },
        {
            "Name": "subnet-id",
            "Values": [eni1.subnet.id]
        },
    ])
    response["NetworkInterfaces"].should.have.length_of(1)
    response["NetworkInterfaces"][0]["NetworkInterfaceId"].should.equal(
        eni1.id)
    response["NetworkInterfaces"][0]["PrivateIpAddress"].should.equal(
        eni1.private_ip_address)
    response["NetworkInterfaces"][0]["Description"].should.equal(
        eni1.description)
예제 #16
0
def test_elastic_network_interfaces_filter_by_tag():
    ec2 = boto3.resource("ec2", region_name="us-west-2")
    ec2_client = boto3.client("ec2", region_name="us-west-2")

    vpc = ec2.create_vpc(CidrBlock="10.0.0.0/16")
    subnet = ec2.create_subnet(VpcId=vpc.id,
                               CidrBlock="10.0.0.0/24",
                               AvailabilityZone="us-west-2a")

    dev_env = f"dev-{str(uuid4())[0:4]}"
    prod_env = f"prod-{str(uuid4())[0:4]}"

    eni_dev = ec2.create_network_interface(
        SubnetId=subnet.id,
        PrivateIpAddress="10.0.10.5",
        Description="dev interface",
        TagSpecifications=[
            {
                "ResourceType": "network-interface",
                "Tags": [{
                    "Key": "environment",
                    "Value": dev_env
                }],
            },
        ],
    )

    eni_prod = ec2.create_network_interface(
        SubnetId=subnet.id,
        PrivateIpAddress="10.0.10.6",
        Description="prod interface",
        TagSpecifications=[
            {
                "ResourceType": "network-interface",
                "Tags": [{
                    "Key": "environment",
                    "Value": prod_env
                }],
            },
        ],
    )

    for eni in [eni_dev, eni_prod]:
        waiter = ec2_client.get_waiter("network_interface_available")
        waiter.wait(NetworkInterfaceIds=[eni.id])

    resp = ec2_client.describe_network_interfaces(
        Filters=[{
            "Name": "tag:environment",
            "Values": ["staging"]
        }])
    resp["NetworkInterfaces"].should.have.length_of(0)

    resp = ec2_client.describe_network_interfaces(Filters=[{
        "Name": "tag:environment",
        "Values": [dev_env]
    }])
    resp["NetworkInterfaces"].should.have.length_of(1)
    resp["NetworkInterfaces"][0]["Description"].should.equal("dev interface")

    resp = ec2_client.describe_network_interfaces(Filters=[{
        "Name": "tag:environment",
        "Values": [prod_env]
    }])
    resp["NetworkInterfaces"].should.have.length_of(1)
    resp["NetworkInterfaces"][0]["Description"].should.equal("prod interface")

    resp = ec2_client.describe_network_interfaces(
        Filters=[{
            "Name": "tag:environment",
            "Values": [dev_env, prod_env]
        }])
    resp["NetworkInterfaces"].should.have.length_of(2)
예제 #17
0
def test_elastic_network_interfaces_describe_network_interfaces_with_filter():
    ec2 = boto3.resource("ec2", region_name="us-west-2")
    ec2_client = boto3.client("ec2", region_name="us-west-2")

    vpc = ec2.create_vpc(CidrBlock="10.0.0.0/16")
    subnet = ec2.create_subnet(
        VpcId=vpc.id, CidrBlock="10.0.0.0/24", AvailabilityZone="us-west-2a"
    )

    eni1 = ec2.create_network_interface(
        SubnetId=subnet.id, PrivateIpAddress="10.0.10.5", Description="test interface"
    )

    # The status of the new interface should be 'available'
    waiter = ec2_client.get_waiter("network_interface_available")
    waiter.wait(NetworkInterfaceIds=[eni1.id])

    # Filter by network-interface-id
    response = ec2_client.describe_network_interfaces(
        Filters=[{"Name": "network-interface-id", "Values": [eni1.id]}]
    )
    response["NetworkInterfaces"].should.have.length_of(1)
    response["NetworkInterfaces"][0]["NetworkInterfaceId"].should.equal(eni1.id)
    response["NetworkInterfaces"][0]["PrivateIpAddress"].should.equal(
        eni1.private_ip_address
    )
    response["NetworkInterfaces"][0]["Description"].should.equal(eni1.description)

    response = ec2_client.describe_network_interfaces(
        Filters=[{"Name": "network-interface-id", "Values": ["bad-id"]}]
    )
    response["NetworkInterfaces"].should.have.length_of(0)

    # Filter by private-ip-address
    response = ec2_client.describe_network_interfaces(
        Filters=[{"Name": "private-ip-address", "Values": [eni1.private_ip_address]}]
    )
    response["NetworkInterfaces"].should.have.length_of(1)
    response["NetworkInterfaces"][0]["NetworkInterfaceId"].should.equal(eni1.id)
    response["NetworkInterfaces"][0]["PrivateIpAddress"].should.equal(
        eni1.private_ip_address
    )
    response["NetworkInterfaces"][0]["Description"].should.equal(eni1.description)

    response = ec2_client.describe_network_interfaces(
        Filters=[{"Name": "private-ip-address", "Values": ["11.11.11.11"]}]
    )
    response["NetworkInterfaces"].should.have.length_of(0)

    # Filter by sunet-id
    response = ec2_client.describe_network_interfaces(
        Filters=[{"Name": "subnet-id", "Values": [eni1.subnet.id]}]
    )
    response["NetworkInterfaces"].should.have.length_of(1)
    response["NetworkInterfaces"][0]["NetworkInterfaceId"].should.equal(eni1.id)
    response["NetworkInterfaces"][0]["PrivateIpAddress"].should.equal(
        eni1.private_ip_address
    )
    response["NetworkInterfaces"][0]["Description"].should.equal(eni1.description)

    response = ec2_client.describe_network_interfaces(
        Filters=[{"Name": "subnet-id", "Values": ["sn-bad-id"]}]
    )
    response["NetworkInterfaces"].should.have.length_of(0)

    # Filter by description
    response = ec2_client.describe_network_interfaces(
        Filters=[{"Name": "description", "Values": [eni1.description]}]
    )
    response["NetworkInterfaces"].should.have.length_of(1)
    response["NetworkInterfaces"][0]["NetworkInterfaceId"].should.equal(eni1.id)
    response["NetworkInterfaces"][0]["PrivateIpAddress"].should.equal(
        eni1.private_ip_address
    )
    response["NetworkInterfaces"][0]["Description"].should.equal(eni1.description)

    response = ec2_client.describe_network_interfaces(
        Filters=[{"Name": "description", "Values": ["bad description"]}]
    )
    response["NetworkInterfaces"].should.have.length_of(0)

    # Filter by multiple filters
    response = ec2_client.describe_network_interfaces(
        Filters=[
            {"Name": "private-ip-address", "Values": [eni1.private_ip_address]},
            {"Name": "network-interface-id", "Values": [eni1.id]},
            {"Name": "subnet-id", "Values": [eni1.subnet.id]},
        ]
    )
    response["NetworkInterfaces"].should.have.length_of(1)
    response["NetworkInterfaces"][0]["NetworkInterfaceId"].should.equal(eni1.id)
    response["NetworkInterfaces"][0]["PrivateIpAddress"].should.equal(
        eni1.private_ip_address
    )
    response["NetworkInterfaces"][0]["Description"].should.equal(eni1.description)