-
Notifications
You must be signed in to change notification settings - Fork 342
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2d8aea3
commit 3ba2ba5
Showing
1 changed file
with
21 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,10 @@ | ||
# (c) 2016 James Turner <[email protected]> | ||
# (c) 2017 Ansible Project | ||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) | ||
from __future__ import absolute_import, division, print_function | ||
|
||
from __future__ import (absolute_import, division, print_function) | ||
__metaclass__ = type | ||
|
||
DOCUMENTATION = """ | ||
DOCUMENTATION = ''' | ||
lookup: aws_service_ip_ranges | ||
author: | ||
- James Turner <[email protected]> | ||
|
@@ -22,7 +21,7 @@ | |
description: 'The AWS region to narrow the ranges to. Examples: us-east-1, eu-west-2, ap-southeast-1' | ||
ipv6_prefix: | ||
description: 'Return only ipv6 addresses. Option: ipv6_prefix=True' | ||
""" | ||
''' | ||
|
||
EXAMPLES = """ | ||
vars: | ||
|
@@ -47,9 +46,12 @@ | |
import json | ||
|
||
from ansible.errors import AnsibleError | ||
from ansible.module_utils.six.moves.urllib.error import HTTPError | ||
from ansible.module_utils.six.moves.urllib.error import URLError | ||
from ansible.module_utils._text import to_native | ||
from ansible.module_utils.six.moves.urllib.error import HTTPError, URLError | ||
from ansible.module_utils.urls import ConnectionError, SSLValidationError, open_url | ||
from ansible.module_utils.urls import ConnectionError | ||
from ansible.module_utils.urls import open_url | ||
from ansible.module_utils.urls import SSLValidationError | ||
from ansible.plugins.lookup import LookupBase | ||
|
||
|
||
|
@@ -61,36 +63,28 @@ def run(self, terms, variables, **kwargs): | |
else: | ||
prefixes_label = "ipv6_prefixes" | ||
ip_prefix_label = "ipv6_prefix" | ||
|
||
try: | ||
resp = open_url("https://ip-ranges.amazonaws.com/ip-ranges.json") | ||
resp = open_url('https://ip-ranges.amazonaws.com/ip-ranges.json') | ||
amazon_response = json.load(resp)[prefixes_label] | ||
except getattr(json.decoder, "JSONDecodeError", ValueError) as e: | ||
except getattr(json.decoder, 'JSONDecodeError', ValueError) as e: | ||
# on Python 3+, json.decoder.JSONDecodeError is raised for bad | ||
# JSON. On 2.x it's a ValueError | ||
raise AnsibleError("Could not decode AWS IP ranges: %s" % to_native(e)) | ||
except HTTPError as e: | ||
raise AnsibleError( | ||
"Received HTTP error while pulling IP ranges: %s" % to_native(e) | ||
) | ||
raise AnsibleError("Received HTTP error while pulling IP ranges: %s" % to_native(e)) | ||
except SSLValidationError as e: | ||
raise AnsibleError( | ||
"Error validating the server's certificate for: %s" % to_native(e) | ||
) | ||
raise AnsibleError("Error validating the server's certificate for: %s" % to_native(e)) | ||
except URLError as e: | ||
raise AnsibleError("Failed look up IP range service: %s" % to_native(e)) | ||
except ConnectionError as e: | ||
raise AnsibleError( | ||
"Error connecting to IP range service: %s" % to_native(e) | ||
) | ||
raise AnsibleError("Error connecting to IP range service: %s" % to_native(e)) | ||
|
||
if 'region' in kwargs: | ||
region = kwargs['region'] | ||
amazon_response = (item for item in amazon_response if item['region'] == region) | ||
if 'service' in kwargs: | ||
service = str.upper(kwargs['service']) | ||
amazon_response = (item for item in amazon_response if item['service'] == service) | ||
|
||
if "region" in kwargs: | ||
region = kwargs["region"] | ||
amazon_response = ( | ||
item for item in amazon_response if item["region"] == region | ||
) | ||
if "service" in kwargs: | ||
service = str.upper(kwargs["service"]) | ||
amazon_response = ( | ||
item for item in amazon_response if item["service"] == service | ||
) | ||
return [item[ip_prefix_label] for item in amazon_response] |