diff --git a/aws/ec2/helpers.py b/aws/ec2/helpers.py index a924c77..25074e2 100644 --- a/aws/ec2/helpers.py +++ b/aws/ec2/helpers.py @@ -269,6 +269,11 @@ def ec2_security_group_test_id(ec2_security_group): return "{0[GroupId]} {0[GroupName]}".format(ec2_security_group) +def ec2_address_id(ec2_address): + """Format an Elastic IP address.""" + return ec2_address["PublicIp"] + + def is_ebs_volume_encrypted(ebs): """ Checks the EBS volume 'Encrypted' value. diff --git a/aws/ec2/resources.py b/aws/ec2/resources.py index ba00c3a..83e9ab6 100644 --- a/aws/ec2/resources.py +++ b/aws/ec2/resources.py @@ -102,6 +102,16 @@ def ec2_vpcs(): ) +def ec2_addresses(): + "https://botocore.readthedocs.io/en/latest/reference/services/ec2.html#EC2.Client.describe_addresses" + return ( + botocore_client.get("ec2", "describe_addresses", [], {}) + .extract_key("Addresses") + .flatten() + .values() + ) + + def ec2_security_groups_with_in_use_flag(): """Returns security groups with an additional "InUse" key, which is True if it is associated with at least one resource. diff --git a/aws/ec2/test_ec2_all_eips_bound.py b/aws/ec2/test_ec2_all_eips_bound.py new file mode 100644 index 0000000..603b0ad --- /dev/null +++ b/aws/ec2/test_ec2_all_eips_bound.py @@ -0,0 +1,11 @@ +import pytest + +from aws.ec2.helpers import ec2_address_id +from aws.ec2.resources import ec2_addresses + + +@pytest.mark.ec2 +@pytest.mark.parametrize("ec2_address", ec2_addresses(), ids=ec2_address_id) +def test_ec2_all_eips_bound(ec2_address): + """Checks whether all EIPs are bound to instances.""" + assert ec2_address.get("InstanceId"), "No associated instance."