Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow filtering EIPs by AWS tag (New --tag parameter) #40

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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`.
Expand Down
31 changes: 31 additions & 0 deletions aws_ec2_assign_elastic_ip/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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.

Expand Down
9 changes: 9 additions & 0 deletions aws_ec2_assign_elastic_ip/command_line_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down