| 1 |
# This code is part of Ansible, but is an independent component. |
| 2 |
# This particular file snippet, and this file snippet only, is BSD licensed. |
| 3 |
# Modules you write using this snippet, which is embedded dynamically by Ansible |
| 4 |
# still belong to the author of the module, and may assign their own license |
| 5 |
# to the complete work. |
| 6 |
# |
| 7 |
# Copyright (c) 2018, Simon Weald <[email protected]> |
| 8 |
# |
| 9 |
# Simplified BSD License (see LICENSES/BSD-2-Clause.txt or https://opensource.org/licenses/BSD-2-Clause) |
| 10 |
# SPDX-License-Identifier: BSD-2-Clause |
| 11 |
|
| 12 |
# Note that this module util is **PRIVATE** to the collection. It can have breaking changes at any time. |
| 13 |
# Do not use this from other collections or standalone plugins/modules! |
| 14 |
|
| 15 |
from __future__ import annotations |
| 16 |
|
| 17 |
import urllib.error as urllib_error |
| 18 |
from http import HTTPStatus |
| 19 |
from urllib.parse import urlencode |
| 20 |
|
| 21 |
from ansible.module_utils.basic import json |
| 22 |
from ansible.module_utils.urls import open_url |
| 23 |
|
| 24 |
|
| 25 |
class Response: |
| 26 |
""" |
| 27 |
Create a response object to mimic that of requests. |
| 28 |
""" |
| 29 |
|
| 30 |
def __init__(self): |
| 31 |
self.content = None |
| 32 |
self.status_code = None |
| 33 |
self.stderr = None |
| 34 |
|
| 35 |
def json(self): |
| 36 |
return json.loads(self.content) |
| 37 |
|
| 38 |
|
| 39 |
def memset_api_call(api_key, api_method, payload=None): |
| 40 |
""" |
| 41 |
Generic function which returns results back to calling function. |
| 42 |
|
| 43 |
Requires an API key and an API method to assemble the API URL. |
| 44 |
Returns response text to be analysed. |
| 45 |
""" |
| 46 |
# instantiate a response object |
| 47 |
response = Response() |
| 48 |
|
| 49 |
# if we've already started preloading the payload then copy it |
| 50 |
# and use that, otherwise we need to isntantiate it. |
| 51 |
if payload is None: |
| 52 |
payload = dict() |
| 53 |
else: |
| 54 |
payload = payload.copy() |
| 55 |
|
| 56 |
# set some sane defaults |
| 57 |
has_failed = False |
| 58 |
msg = None |
| 59 |
|
| 60 |
data = urlencode(payload) |
| 61 |
headers = {"Content-Type": "application/x-www-form-urlencoded"} |
| 62 |
api_uri_base = "https://api.memset.com/v1/json/" |
| 63 |
api_uri = f"{api_uri_base}{api_method}/" |
| 64 |
|
| 65 |
try: |
| 66 |
resp = open_url(api_uri, data=data, headers=headers, method="POST", force_basic_auth=True, url_username=api_key) |
| 67 |
response.content = resp.read().decode("utf-8") |
| 68 |
response.status_code = resp.getcode() |
| 69 |
except urllib_error.HTTPError as e: |
| 70 |
try: |
| 71 |
errorcode = e.code |
| 72 |
except AttributeError: |
| 73 |
errorcode = None |
| 74 |
|
| 75 |
has_failed = True |
| 76 |
response.content = e.read().decode("utf8") |
| 77 |
response.status_code = errorcode |
| 78 |
|
| 79 |
if response.status_code is not None: |
| 80 |
msg = f"Memset API returned a {response.status_code} response ({response.json()['error_type']}, {response.json()['error']})." |
| 81 |
else: |
| 82 |
msg = f"Memset API returned an error ({response.json()['error_type']}, {response.json()['error']})." |
| 83 |
except urllib_error.URLError as e: |
| 84 |
has_failed = True |
| 85 |
msg = f"An URLError occurred ({type(e)})." |
| 86 |
response.stderr = f"{e}" |
| 87 |
|
| 88 |
if msg is None: |
| 89 |
msg = response.json() |
| 90 |
|
| 91 |
return has_failed, msg, response |
| 92 |
|
| 93 |
|
| 94 |
def check_zone_domain(data, domain): |
| 95 |
""" |
| 96 |
Returns true if domain already exists, and false if not. |
| 97 |
""" |
| 98 |
exists = False |
| 99 |
|
| 100 |
if data.status_code in (HTTPStatus.CREATED, HTTPStatus.OK): |
| 101 |
for zone_domain in data.json(): |
| 102 |
if zone_domain["domain"] == domain: |
| 103 |
exists = True |
| 104 |
|
| 105 |
return exists |
| 106 |
|
| 107 |
|
| 108 |
def check_zone(data, name): |
| 109 |
""" |
| 110 |
Returns true if zone already exists, and false if not. |
| 111 |
""" |
| 112 |
counter = 0 |
| 113 |
exists = False |
| 114 |
|
| 115 |
if data.status_code in (HTTPStatus.CREATED, HTTPStatus.OK): |
| 116 |
for zone in data.json(): |
| 117 |
if zone["nickname"] == name: |
| 118 |
counter += 1 |
| 119 |
if counter == 1: |
| 120 |
exists = True |
| 121 |
|
| 122 |
return exists, counter |
| 123 |
|
| 124 |
|
| 125 |
def get_zone_id(zone_name, current_zones): |
| 126 |
""" |
| 127 |
Returns the zone's id if it exists and is unique |
| 128 |
""" |
| 129 |
zone_exists = False |
| 130 |
zone_id, msg = None, None |
| 131 |
zone_list = [] |
| 132 |
|
| 133 |
for zone in current_zones: |
| 134 |
if zone["nickname"] == zone_name: |
| 135 |
zone_list.append(zone["id"]) |
| 136 |
|
| 137 |
counter = len(zone_list) |
| 138 |
|
| 139 |
if counter == 0: |
| 140 |
msg = "No matching zone found" |
| 141 |
elif counter == 1: |
| 142 |
zone_id = zone_list[0] |
| 143 |
zone_exists = True |
| 144 |
elif counter > 1: |
| 145 |
zone_id = None |
| 146 |
msg = "Zone ID could not be returned as duplicate zone names were detected" |
| 147 |
|
| 148 |
return zone_exists, msg, counter, zone_id |