moto/tests/test_ec2/test_elastic_ip_addresses.py
Waldemar Hummer f4f8527955
Merge LocalStack changes into upstream moto (#4082)
* fix OPTIONS requests on non-existing API GW integrations

* add cloudformation models for API Gateway deployments

* bump version

* add backdoor to return CloudWatch metrics

* Updating implementation coverage

* Updating implementation coverage

* add cloudformation models for API Gateway deployments

* Updating implementation coverage

* Updating implementation coverage

* Implemented get-caller-identity returning real data depending on the access key used.

* bump version

* minor fixes

* fix Number data_type for SQS message attribute

* fix handling of encoding errors

* bump version

* make CF stack queryable before starting to initialize its resources

* bump version

* fix integration_method for API GW method integrations

* fix undefined status in CF FakeStack

* Fix apigateway issues with terraform v0.12.21
* resource_methods -> add handle for "DELETE" method
* integrations -> fix issue that "httpMethod" wasn't included in body request (this value was set as the value from refer method resource)

* bump version

* Fix setting http method for API gateway integrations (#6)

* bump version

* remove duplicate methods

* add storage class to S3 Key when completing multipart upload (#7)

* fix SQS performance issues; bump version

* add pagination to SecretsManager list-secrets (#9)

* fix default parameter groups in RDS

* fix adding S3 metadata headers with names containing dots (#13)

* Updating implementation coverage

* Updating implementation coverage

* add cloudformation models for API Gateway deployments

* Updating implementation coverage

* Updating implementation coverage

* Implemented get-caller-identity returning real data depending on the access key used.

* make CF stack queryable before starting to initialize its resources

* bump version

* remove duplicate methods

* fix adding S3 metadata headers with names containing dots (#13)

* Update amis.json to support EKS AMI mocks (#15)

* fix PascalCase for boolean value in ListMultipartUploads response (#17); fix _get_multi_param to parse nested list/dict query params

* determine non-zero container exit code in Batch API

* support filtering by dimensions in CW get_metric_statistics

* fix storing attributes for ELBv2 Route entities; API GW refactorings for TF tests

* add missing fields for API GW resources

* fix error messages for Route53 (TF-compat)

* various fixes for IAM resources (tf-compat)

* minor fixes for API GW models (tf-compat)

* minor fixes for API GW responses (tf-compat)

* add s3 exception for bucket notification filter rule validation

* change the way RESTErrors generate the response body and content-type header

* fix lint errors and disable "black" syntax enforcement

* remove return type hint in RESTError.get_body

* add RESTError XML template for IAM exceptions

* add support for API GW minimumCompressionSize

* fix casing getting PrivateDnsEnabled API GW attribute

* minor fixes for error responses

* fix escaping special chars for IAM role descriptions (tf-compat)

* minor fixes and tagging support for API GW and ELB v2 (tf-compat)

* Merge branch 'master' into localstack

* add "AlarmRule" attribute to enable support for composite CloudWatch metrics

* fix recursive parsing of complex/nested query params

* bump version

* add API to delete S3 website configurations (#18)

* use dict copy to allow parallelism and avoid concurrent modification exceptions in S3

* fix precondition check for etags in S3 (#19)

* minor fix for user filtering in Cognito

* fix API Gateway error response; avoid returning empty response templates (tf-compat)

* support tags and tracingEnabled attribute for API GW stages

* fix boolean value in S3 encryption response (#20)

* fix connection arn structure

* fix api destination arn structure

* black format

* release 2.0.3.37

* fix s3 exception tests

see botocore/parsers.py:1002 where RequestId is removed from parsed

* remove python 2 from build action

* add test failure annotations in build action

* fix events test arn comparisons

* fix s3 encryption response test

* return default value "0" if EC2 availableIpAddressCount is empty

* fix extracting SecurityGroupIds for EC2 VPC endpoints

* support deleting/updating API Gateway DomainNames

* fix(events): Return empty string instead of null when no pattern is specified in EventPattern (tf-compat) (#22)

* fix logic and revert CF changes to get tests running again (#21)

* add support for EC2 customer gateway API (#25)

* add support for EC2 Transit Gateway APIs (#24)

* feat(logs): add `kmsKeyId` into `LogGroup` entity (#23)

* minor change in ELBv2 logic to fix tests

* feat(events): add APIs to describe and delete CloudWatch Events connections (#26)

* add support for EC2 transit gateway route tables (#27)

* pass transit gateway route table ID in Describe API, minor refactoring (#29)

* add support for EC2 Transit Gateway Routes (#28)

* fix region on ACM certificate import (#31)

* add support for EC2 transit gateway attachments (#30)

* add support for EC2 Transit Gateway VPN attachments (#32)

* fix account ID for logs API

* add support for DeleteOrganization API

* feat(events): store raw filter representation for CloudWatch events patterns (tf-compat) (#36)

* feat(events): add support to describe/update/delete CloudWatch API destinations (#35)

* add Cognito UpdateIdentityPool, CW Logs PutResourcePolicy

* feat(events): add support for tags in EventBus API (#38)

* fix parameter validation for Batch compute environments (tf-compat)

* revert merge conflicts in IMPLEMENTATION_COVERAGE.md

* format code using black

* restore original README; re-enable and fix CloudFormation tests

* restore tests and old logic for CF stack parameters from SSM

* parameterize RequestId/RequestID in response messages and revert related test changes

* undo LocalStack-specific adaptations

* minor fix

* Update CodeCov config to reflect removal of Py2

* undo change related to CW metric filtering; add additional test for CW metric statistics with dimensions

* Terraform - Extend whitelist of running tests

Co-authored-by: acsbendi <acsbendi28@gmail.com>
Co-authored-by: Phan Duong <duongpv@outlook.com>
Co-authored-by: Thomas Rausch <thomas@thrau.at>
Co-authored-by: Macwan Nevil <macnev2013@gmail.com>
Co-authored-by: Dominik Schubert <dominik.schubert91@gmail.com>
Co-authored-by: Gonzalo Saad <saad.gonzalo.ale@gmail.com>
Co-authored-by: Mohit Alonja <monty16597@users.noreply.github.com>
Co-authored-by: Miguel Gagliardo <migag9@gmail.com>
Co-authored-by: Bert Blommers <info@bertblommers.nl>
2021-07-26 15:21:17 +01:00

597 lines
20 KiB
Python

from __future__ import unicode_literals
import pytest
import boto
import boto3
from boto.exception import EC2ResponseError
import sure # noqa
from moto import mock_ec2, mock_ec2_deprecated
from tests import EXAMPLE_AMI_ID
import logging
@mock_ec2_deprecated
def test_eip_allocate_classic():
"""Allocate/release Classic EIP"""
conn = boto.connect_ec2("the_key", "the_secret")
with pytest.raises(EC2ResponseError) as ex:
standard = conn.allocate_address(dry_run=True)
ex.value.error_code.should.equal("DryRunOperation")
ex.value.status.should.equal(400)
ex.value.message.should.equal(
"An error occurred (DryRunOperation) when calling the AllocateAddress operation: Request would have succeeded, but DryRun flag is set"
)
standard = conn.allocate_address()
standard.should.be.a(boto.ec2.address.Address)
standard.public_ip.should.be.a(str)
standard.instance_id.should.be.none
standard.domain.should.be.equal("standard")
with pytest.raises(EC2ResponseError) as ex:
standard.release(dry_run=True)
ex.value.error_code.should.equal("DryRunOperation")
ex.value.status.should.equal(400)
ex.value.message.should.equal(
"An error occurred (DryRunOperation) when calling the ReleaseAddress operation: Request would have succeeded, but DryRun flag is set"
)
standard.release()
standard.should_not.be.within(conn.get_all_addresses())
@mock_ec2_deprecated
def test_eip_allocate_vpc():
"""Allocate/release VPC EIP"""
conn = boto.connect_ec2("the_key", "the_secret")
with pytest.raises(EC2ResponseError) as ex:
vpc = conn.allocate_address(domain="vpc", dry_run=True)
ex.value.error_code.should.equal("DryRunOperation")
ex.value.status.should.equal(400)
ex.value.message.should.equal(
"An error occurred (DryRunOperation) when calling the AllocateAddress operation: Request would have succeeded, but DryRun flag is set"
)
vpc = conn.allocate_address(domain="vpc")
vpc.should.be.a(boto.ec2.address.Address)
vpc.domain.should.be.equal("vpc")
logging.debug("vpc alloc_id:".format(vpc.allocation_id))
vpc.release()
@mock_ec2
def test_specific_eip_allocate_vpc():
"""Allocate VPC EIP with specific address"""
service = boto3.resource("ec2", region_name="us-west-1")
client = boto3.client("ec2", region_name="us-west-1")
vpc = client.allocate_address(Domain="vpc", Address="127.38.43.222")
vpc["Domain"].should.be.equal("vpc")
vpc["PublicIp"].should.be.equal("127.38.43.222")
logging.debug("vpc alloc_id:".format(vpc["AllocationId"]))
@mock_ec2_deprecated
def test_eip_allocate_invalid_domain():
"""Allocate EIP invalid domain"""
conn = boto.connect_ec2("the_key", "the_secret")
with pytest.raises(EC2ResponseError) as cm:
conn.allocate_address(domain="bogus")
cm.value.code.should.equal("InvalidParameterValue")
cm.value.status.should.equal(400)
cm.value.request_id.should_not.be.none
@mock_ec2_deprecated
def test_eip_associate_classic():
"""Associate/Disassociate EIP to classic instance"""
conn = boto.connect_ec2("the_key", "the_secret")
reservation = conn.run_instances(EXAMPLE_AMI_ID)
instance = reservation.instances[0]
eip = conn.allocate_address()
eip.instance_id.should.be.none
with pytest.raises(EC2ResponseError) as cm:
conn.associate_address(public_ip=eip.public_ip)
cm.value.code.should.equal("MissingParameter")
cm.value.status.should.equal(400)
cm.value.request_id.should_not.be.none
with pytest.raises(EC2ResponseError) as ex:
conn.associate_address(
instance_id=instance.id, public_ip=eip.public_ip, dry_run=True
)
ex.value.error_code.should.equal("DryRunOperation")
ex.value.status.should.equal(400)
ex.value.message.should.equal(
"An error occurred (DryRunOperation) when calling the AssociateAddress operation: Request would have succeeded, but DryRun flag is set"
)
conn.associate_address(instance_id=instance.id, public_ip=eip.public_ip)
# no .update() on address ):
eip = conn.get_all_addresses(addresses=[eip.public_ip])[0]
eip.instance_id.should.be.equal(instance.id)
with pytest.raises(EC2ResponseError) as ex:
conn.disassociate_address(public_ip=eip.public_ip, dry_run=True)
ex.value.error_code.should.equal("DryRunOperation")
ex.value.status.should.equal(400)
ex.value.message.should.equal(
"An error occurred (DryRunOperation) when calling the DisAssociateAddress operation: Request would have succeeded, but DryRun flag is set"
)
conn.disassociate_address(public_ip=eip.public_ip)
# no .update() on address ):
eip = conn.get_all_addresses(addresses=[eip.public_ip])[0]
eip.instance_id.should.be.equal("")
eip.release()
eip.should_not.be.within(conn.get_all_addresses())
eip = None
instance.terminate()
@mock_ec2_deprecated
def test_eip_associate_vpc():
"""Associate/Disassociate EIP to VPC instance"""
conn = boto.connect_ec2("the_key", "the_secret")
reservation = conn.run_instances(EXAMPLE_AMI_ID)
instance = reservation.instances[0]
eip = conn.allocate_address(domain="vpc")
eip.instance_id.should.be.none
with pytest.raises(EC2ResponseError) as cm:
conn.associate_address(allocation_id=eip.allocation_id)
cm.value.code.should.equal("MissingParameter")
cm.value.status.should.equal(400)
cm.value.request_id.should_not.be.none
conn.associate_address(instance_id=instance.id, allocation_id=eip.allocation_id)
# no .update() on address ):
eip = conn.get_all_addresses(addresses=[eip.public_ip])[0]
eip.instance_id.should.be.equal(instance.id)
conn.disassociate_address(association_id=eip.association_id)
# no .update() on address ):
eip = conn.get_all_addresses(addresses=[eip.public_ip])[0]
eip.instance_id.should.be.equal("")
eip.association_id.should.be.none
with pytest.raises(EC2ResponseError) as ex:
eip.release(dry_run=True)
ex.value.error_code.should.equal("DryRunOperation")
ex.value.status.should.equal(400)
ex.value.message.should.equal(
"An error occurred (DryRunOperation) when calling the ReleaseAddress operation: Request would have succeeded, but DryRun flag is set"
)
eip.release()
eip = None
instance.terminate()
@mock_ec2
def test_eip_boto3_vpc_association():
"""Associate EIP to VPC instance in a new subnet with boto3"""
service = boto3.resource("ec2", region_name="us-west-1")
client = boto3.client("ec2", region_name="us-west-1")
vpc_res = client.create_vpc(CidrBlock="10.0.0.0/24")
subnet_res = client.create_subnet(
VpcId=vpc_res["Vpc"]["VpcId"], CidrBlock="10.0.0.0/24"
)
instance = service.create_instances(
**{
"InstanceType": "t2.micro",
"ImageId": EXAMPLE_AMI_ID,
"MinCount": 1,
"MaxCount": 1,
"SubnetId": subnet_res["Subnet"]["SubnetId"],
}
)[0]
allocation_id = client.allocate_address(Domain="vpc")["AllocationId"]
address = service.VpcAddress(allocation_id)
address.load()
address.association_id.should.be.none
address.instance_id.should.be.empty
address.network_interface_id.should.be.empty
client.associate_address(
InstanceId=instance.id, AllocationId=allocation_id, AllowReassociation=False
)
instance.load()
address.reload()
address.association_id.should_not.be.none
instance.public_ip_address.should_not.be.none
instance.public_dns_name.should_not.be.none
address.network_interface_id.should.equal(
instance.network_interfaces_attribute[0].get("NetworkInterfaceId")
)
address.public_ip.should.equal(instance.public_ip_address)
address.instance_id.should.equal(instance.id)
client.disassociate_address(AssociationId=address.association_id)
instance.reload()
address.reload()
instance.public_ip_address.should.be.none
address.network_interface_id.should.be.empty
address.association_id.should.be.none
address.instance_id.should.be.empty
@mock_ec2_deprecated
def test_eip_associate_network_interface():
"""Associate/Disassociate EIP to NIC"""
conn = boto.connect_vpc("the_key", "the_secret")
vpc = conn.create_vpc("10.0.0.0/16")
subnet = conn.create_subnet(vpc.id, "10.0.0.0/18")
eni = conn.create_network_interface(subnet.id)
eip = conn.allocate_address(domain="vpc")
eip.network_interface_id.should.be.none
with pytest.raises(EC2ResponseError) as cm:
conn.associate_address(network_interface_id=eni.id)
cm.value.code.should.equal("MissingParameter")
cm.value.status.should.equal(400)
cm.value.request_id.should_not.be.none
conn.associate_address(network_interface_id=eni.id, allocation_id=eip.allocation_id)
# no .update() on address ):
eip = conn.get_all_addresses(addresses=[eip.public_ip])[0]
eip.network_interface_id.should.be.equal(eni.id)
conn.disassociate_address(association_id=eip.association_id)
# no .update() on address ):
eip = conn.get_all_addresses(addresses=[eip.public_ip])[0]
eip.network_interface_id.should.be.equal("")
eip.association_id.should.be.none
eip.release()
eip = None
@mock_ec2_deprecated
def test_eip_reassociate():
"""reassociate EIP"""
conn = boto.connect_ec2("the_key", "the_secret")
reservation = conn.run_instances(EXAMPLE_AMI_ID, min_count=2)
instance1, instance2 = reservation.instances
eip = conn.allocate_address()
conn.associate_address(instance_id=instance1.id, public_ip=eip.public_ip)
# Same ID is idempotent
conn.associate_address(instance_id=instance1.id, public_ip=eip.public_ip)
# Different ID detects resource association
with pytest.raises(EC2ResponseError) as cm:
conn.associate_address(
instance_id=instance2.id, public_ip=eip.public_ip, allow_reassociation=False
)
cm.value.code.should.equal("Resource.AlreadyAssociated")
cm.value.status.should.equal(400)
cm.value.request_id.should_not.be.none
conn.associate_address.when.called_with(
instance_id=instance2.id, public_ip=eip.public_ip, allow_reassociation=True
).should_not.throw(EC2ResponseError)
eip.release()
instance1.terminate()
instance2.terminate()
@mock_ec2_deprecated
def test_eip_reassociate_nic():
"""reassociate EIP"""
conn = boto.connect_vpc("the_key", "the_secret")
vpc = conn.create_vpc("10.0.0.0/16")
subnet = conn.create_subnet(vpc.id, "10.0.0.0/18")
eni1 = conn.create_network_interface(subnet.id)
eni2 = conn.create_network_interface(subnet.id)
eip = conn.allocate_address()
conn.associate_address(network_interface_id=eni1.id, public_ip=eip.public_ip)
# Same ID is idempotent
conn.associate_address(network_interface_id=eni1.id, public_ip=eip.public_ip)
# Different ID detects resource association
with pytest.raises(EC2ResponseError) as cm:
conn.associate_address(network_interface_id=eni2.id, public_ip=eip.public_ip)
cm.value.code.should.equal("Resource.AlreadyAssociated")
cm.value.status.should.equal(400)
cm.value.request_id.should_not.be.none
conn.associate_address.when.called_with(
network_interface_id=eni2.id, public_ip=eip.public_ip, allow_reassociation=True
).should_not.throw(EC2ResponseError)
eip.release()
eip = None
@mock_ec2_deprecated
def test_eip_associate_invalid_args():
"""Associate EIP, invalid args"""
conn = boto.connect_ec2("the_key", "the_secret")
reservation = conn.run_instances(EXAMPLE_AMI_ID)
instance = reservation.instances[0]
eip = conn.allocate_address()
with pytest.raises(EC2ResponseError) as cm:
conn.associate_address(instance_id=instance.id)
cm.value.code.should.equal("MissingParameter")
cm.value.status.should.equal(400)
cm.value.request_id.should_not.be.none
instance.terminate()
@mock_ec2_deprecated
def test_eip_disassociate_bogus_association():
"""Disassociate bogus EIP"""
conn = boto.connect_ec2("the_key", "the_secret")
with pytest.raises(EC2ResponseError) as cm:
conn.disassociate_address(association_id="bogus")
cm.value.code.should.equal("InvalidAssociationID.NotFound")
cm.value.status.should.equal(400)
cm.value.request_id.should_not.be.none
@mock_ec2_deprecated
def test_eip_release_bogus_eip():
"""Release bogus EIP"""
conn = boto.connect_ec2("the_key", "the_secret")
with pytest.raises(EC2ResponseError) as cm:
conn.release_address(allocation_id="bogus")
cm.value.code.should.equal("InvalidAllocationID.NotFound")
cm.value.status.should.equal(400)
cm.value.request_id.should_not.be.none
@mock_ec2_deprecated
def test_eip_disassociate_arg_error():
"""Invalid arguments disassociate address"""
conn = boto.connect_ec2("the_key", "the_secret")
with pytest.raises(EC2ResponseError) as cm:
conn.disassociate_address()
cm.value.code.should.equal("MissingParameter")
cm.value.status.should.equal(400)
cm.value.request_id.should_not.be.none
@mock_ec2_deprecated
def test_eip_release_arg_error():
"""Invalid arguments release address"""
conn = boto.connect_ec2("the_key", "the_secret")
with pytest.raises(EC2ResponseError) as cm:
conn.release_address()
cm.value.code.should.equal("MissingParameter")
cm.value.status.should.equal(400)
cm.value.request_id.should_not.be.none
@mock_ec2_deprecated
def test_eip_describe():
"""Listing of allocated Elastic IP Addresses."""
conn = boto.connect_ec2("the_key", "the_secret")
eips = []
number_of_classic_ips = 2
number_of_vpc_ips = 2
# allocate some IPs
for _ in range(number_of_classic_ips):
eips.append(conn.allocate_address())
for _ in range(number_of_vpc_ips):
eips.append(conn.allocate_address(domain="vpc"))
len(eips).should.be.equal(number_of_classic_ips + number_of_vpc_ips)
# Can we find each one individually?
for eip in eips:
if eip.allocation_id:
lookup_addresses = conn.get_all_addresses(
allocation_ids=[eip.allocation_id]
)
else:
lookup_addresses = conn.get_all_addresses(addresses=[eip.public_ip])
len(lookup_addresses).should.be.equal(1)
lookup_addresses[0].public_ip.should.be.equal(eip.public_ip)
# Can we find first two when we search for them?
lookup_addresses = conn.get_all_addresses(
addresses=[eips[0].public_ip, eips[1].public_ip]
)
len(lookup_addresses).should.be.equal(2)
lookup_addresses[0].public_ip.should.be.equal(eips[0].public_ip)
lookup_addresses[1].public_ip.should.be.equal(eips[1].public_ip)
# Release all IPs
for eip in eips:
eip.release()
len(conn.get_all_addresses()).should.be.equal(0)
@mock_ec2_deprecated
def test_eip_describe_none():
"""Error when search for bogus IP"""
conn = boto.connect_ec2("the_key", "the_secret")
with pytest.raises(EC2ResponseError) as cm:
conn.get_all_addresses(addresses=["256.256.256.256"])
cm.value.code.should.equal("InvalidAddress.NotFound")
cm.value.status.should.equal(400)
cm.value.request_id.should_not.be.none
@mock_ec2
def test_eip_filters():
service = boto3.resource("ec2", region_name="us-west-1")
client = boto3.client("ec2", region_name="us-west-1")
vpc_res = client.create_vpc(CidrBlock="10.0.0.0/24")
subnet_res = client.create_subnet(
VpcId=vpc_res["Vpc"]["VpcId"], CidrBlock="10.0.0.0/24"
)
def create_inst_with_eip():
instance = service.create_instances(
**{
"InstanceType": "t2.micro",
"ImageId": EXAMPLE_AMI_ID,
"MinCount": 1,
"MaxCount": 1,
"SubnetId": subnet_res["Subnet"]["SubnetId"],
}
)[0]
allocation_id = client.allocate_address(Domain="vpc")["AllocationId"]
_ = client.associate_address(
InstanceId=instance.id, AllocationId=allocation_id, AllowReassociation=False
)
instance.load()
address = service.VpcAddress(allocation_id)
address.load()
return instance, address
inst1, eip1 = create_inst_with_eip()
inst2, eip2 = create_inst_with_eip()
inst3, eip3 = create_inst_with_eip()
# Param search by AllocationId
addresses = list(service.vpc_addresses.filter(AllocationIds=[eip2.allocation_id]))
len(addresses).should.be.equal(1)
addresses[0].public_ip.should.equal(eip2.public_ip)
inst2.public_ip_address.should.equal(addresses[0].public_ip)
# Param search by PublicIp
addresses = list(service.vpc_addresses.filter(PublicIps=[eip3.public_ip]))
len(addresses).should.be.equal(1)
addresses[0].public_ip.should.equal(eip3.public_ip)
inst3.public_ip_address.should.equal(addresses[0].public_ip)
# Param search by Filter
def check_vpc_filter_valid(filter_name, filter_values):
addresses = list(
service.vpc_addresses.filter(
Filters=[{"Name": filter_name, "Values": filter_values}]
)
)
len(addresses).should.equal(2)
ips = [addr.public_ip for addr in addresses]
set(ips).should.equal(set([eip1.public_ip, eip2.public_ip]))
ips.should.contain(inst1.public_ip_address)
def check_vpc_filter_invalid(filter_name):
addresses = list(
service.vpc_addresses.filter(
Filters=[{"Name": filter_name, "Values": ["dummy1", "dummy2"]}]
)
)
len(addresses).should.equal(0)
def check_vpc_filter(filter_name, filter_values):
check_vpc_filter_valid(filter_name, filter_values)
check_vpc_filter_invalid(filter_name)
check_vpc_filter("allocation-id", [eip1.allocation_id, eip2.allocation_id])
check_vpc_filter("association-id", [eip1.association_id, eip2.association_id])
check_vpc_filter("instance-id", [inst1.id, inst2.id])
check_vpc_filter(
"network-interface-id",
[
inst1.network_interfaces_attribute[0].get("NetworkInterfaceId"),
inst2.network_interfaces_attribute[0].get("NetworkInterfaceId"),
],
)
check_vpc_filter(
"private-ip-address",
[
inst1.network_interfaces_attribute[0].get("PrivateIpAddress"),
inst2.network_interfaces_attribute[0].get("PrivateIpAddress"),
],
)
check_vpc_filter("public-ip", [inst1.public_ip_address, inst2.public_ip_address])
# all the ips are in a VPC
addresses = list(
service.vpc_addresses.filter(Filters=[{"Name": "domain", "Values": ["vpc"]}])
)
len(addresses).should.equal(3)
@mock_ec2
def test_eip_tags():
service = boto3.resource("ec2", region_name="us-west-1")
client = boto3.client("ec2", region_name="us-west-1")
# Allocate one address without tags
client.allocate_address(Domain="vpc")
# Allocate one address and add tags
alloc_tags = client.allocate_address(Domain="vpc")
with_tags = client.create_tags(
Resources=[alloc_tags["AllocationId"]],
Tags=[{"Key": "ManagedBy", "Value": "MyCode"}],
)
addresses_with_tags = client.describe_addresses(
Filters=[
{"Name": "domain", "Values": ["vpc"]},
{"Name": "tag:ManagedBy", "Values": ["MyCode"]},
]
)
len(addresses_with_tags["Addresses"]).should.equal(1)
addresses_with_tags = list(
service.vpc_addresses.filter(
Filters=[
{"Name": "domain", "Values": ["vpc"]},
{"Name": "tag:ManagedBy", "Values": ["MyCode"]},
]
)
)
len(addresses_with_tags).should.equal(1)
addresses_with_tags = list(
service.vpc_addresses.filter(
Filters=[
{"Name": "domain", "Values": ["vpc"]},
{"Name": "tag:ManagedBy", "Values": ["SomethingOther"]},
]
)
)
len(addresses_with_tags).should.equal(0)
addresses = list(
service.vpc_addresses.filter(Filters=[{"Name": "domain", "Values": ["vpc"]}])
)
# Expected total is 2, one with and one without tags
len(addresses).should.equal(2)
@mock_ec2
def test_describe_addresses_tags():
client = boto3.client("ec2", region_name="us-west-1")
alloc_tags = client.allocate_address(Domain="vpc")
client.create_tags(
Resources=[alloc_tags["AllocationId"]],
Tags=[{"Key": "ManagedBy", "Value": "MyCode"}],
)
addresses_with_tags = client.describe_addresses()
assert addresses_with_tags.get("Addresses")[0].get("Tags") == [
{"Key": "ManagedBy", "Value": "MyCode"}
]