From e120a6ce39e4bf607412329f8400b6e593e94955 Mon Sep 17 00:00:00 2001 From: hmmmsausages Date: Tue, 28 Apr 2020 15:13:43 +0100 Subject: [PATCH] Add --tag parameter to filter for EIPs with a specific tag --- README.md | 13 +++++--- aws_ec2_assign_elastic_ip/__init__.py | 31 +++++++++++++++++++ .../command_line_options.py | 9 ++++++ 3 files changed, 49 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index c9e42a2..affd93d 100644 --- a/README.md +++ b/README.md @@ -32,9 +32,11 @@ credential config options](http://boto.readthedocs.org/en/latest/boto_config_tut and AWS instance profiles. usage: aws-ec2-assign-elastic-ip [-h] [--version] [--region REGION] - [--access-key ACCESS_KEY] - [--secret-key SECRET_KEY] [--dry-run] - [--valid-ips VALID_IPS] + [--access-key ACCESS_KEY] + [--secret-key SECRET_KEY] [--dry-run] + [--valid-ips VALID_IPS] + [--invalid-ips INVALID_IPS] + [--tag [KEY=VALUE]] Assign EC2 Elastic IP to the current instance @@ -63,7 +65,10 @@ and AWS instance profiles. - 58.0.0.0/8 - 123.213.0.0/16,58.0.0.0/8,195.234.023.0 - 195.234.234.23,195.234.234.24 - + --tag [KEY=VALUE] A case-sensitive tag (key=value pair) that valid Elastic IPs should have + Valid examples: + - Name=my-eip + - Group=prod The `--valid-ips` and `--invalid-ips` options require the public IPs in a comma separated sequence. E.g. `56.123.56.123,56.123.56.124,56.123.56.125`. diff --git a/aws_ec2_assign_elastic_ip/__init__.py b/aws_ec2_assign_elastic_ip/__init__.py index f93bb36..0136947 100644 --- a/aws_ec2_assign_elastic_ip/__init__.py +++ b/aws_ec2_assign_elastic_ip/__init__.py @@ -132,6 +132,14 @@ def _get_unassociated_address(): address['PublicIp'], address['NetworkInterfaceId'])) continue + # Check if the address has the correct tag + if not _has_correct_tag(address): + logger.debug( + '{0} is unassociated, but does not have correct tag'.format( + address['PublicIp'])) + continue + + # Check if the address is in the valid IP's list if _is_valid(address['PublicIp']): logger.debug('{0} is unassociated and OK for us to take'.format( @@ -161,6 +169,29 @@ def _has_associated_address(instance_id): return True return False + +def _has_correct_tag(address): + """ Check if the EIP has given tag + + :type address: str + :param address: IP address to check + :returns: bool -- True if IP address has tag + """ + if not args.tag: + return True + + (tag_key, tag_value) = args.tag.split('=') + + try: + for address_tag in address['Tags']: + if address_tag['Key'] == tag_key and address_tag['Value'] == tag_value: + return True + except KeyError as error: + logger.debug('IP: {0} has no tags assigned'.format(address['PublicIp'])) + return False + + return False + def _is_ip_in_range(address, ips): """ Check if the IP is in a given range. diff --git a/aws_ec2_assign_elastic_ip/command_line_options.py b/aws_ec2_assign_elastic_ip/command_line_options.py index a5a16d5..19854d1 100644 --- a/aws_ec2_assign_elastic_ip/command_line_options.py +++ b/aws_ec2_assign_elastic_ip/command_line_options.py @@ -56,6 +56,15 @@ '- 58.0.0.0/8\n' '- 123.213.0.0/16,58.0.0.0/8,195.234.023.0\n' '- 195.234.234.23,195.234.234.24\n')) +PARSER.add_argument( + '--tag', + metavar="KEY=VALUE", + nargs='?', + help=( + 'A case-sensitive tag (key=value pair) that valid Elastic IPs should have\n' + 'Valid examples:\n' + '- Name=my-eip\n' + '- Group=prod\n')) ARGS = PARSER.parse_args() if ARGS.version: