| 1 |
#!/usr/bin/python |
| 2 |
# |
| 3 |
# Copyright (c) 2018, Simon Weald <[email protected]> |
| 4 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 6 |
|
| 7 |
from __future__ import annotations |
| 8 |
|
| 9 |
DOCUMENTATION = r""" |
| 10 |
module: memset_memstore_info |
| 11 |
author: "Simon Weald (@glitchcrab)" |
| 12 |
short_description: Retrieve Memstore product usage information |
| 13 |
notes: |
| 14 |
- An API key generated using the Memset customer control panel is needed with the following minimum scope - C(memstore.usage). |
| 15 |
description: |
| 16 |
- Retrieve Memstore product usage information. |
| 17 |
extends_documentation_fragment: |
| 18 |
- community.general._attributes |
| 19 |
- community.general._attributes.info_module |
| 20 |
attributes: |
| 21 |
check_mode: |
| 22 |
version_added: 3.3.0 |
| 23 |
# This was backported to 2.5.4 and 1.3.11 as well, since this was a bugfix |
| 24 |
options: |
| 25 |
api_key: |
| 26 |
required: true |
| 27 |
type: str |
| 28 |
description: |
| 29 |
- The API key obtained from the Memset control panel. |
| 30 |
name: |
| 31 |
required: true |
| 32 |
type: str |
| 33 |
description: |
| 34 |
- The Memstore product name (that is, V(mstestyaa1)). |
| 35 |
""" |
| 36 |
|
| 37 |
EXAMPLES = r""" |
| 38 |
- name: Get usage for mstestyaa1 |
| 39 |
community.general.memset_memstore_info: |
| 40 |
name: mstestyaa1 |
| 41 |
api_key: 5eb86c9896ab03919abcf03857163741 |
| 42 |
delegate_to: localhost |
| 43 |
""" |
| 44 |
|
| 45 |
RETURN = r""" |
| 46 |
memset_api: |
| 47 |
description: Info from the Memset API. |
| 48 |
returned: always |
| 49 |
type: complex |
| 50 |
contains: |
| 51 |
cdn_bandwidth: |
| 52 |
description: Dictionary of CDN bandwidth facts. |
| 53 |
returned: always |
| 54 |
type: complex |
| 55 |
contains: |
| 56 |
bytes_out: |
| 57 |
description: Outbound CDN bandwidth for the last 24 hours in bytes. |
| 58 |
returned: always |
| 59 |
type: int |
| 60 |
sample: 1000 |
| 61 |
requests: |
| 62 |
description: Number of requests in the last 24 hours. |
| 63 |
returned: always |
| 64 |
type: int |
| 65 |
sample: 10 |
| 66 |
bytes_in: |
| 67 |
description: Inbound CDN bandwidth for the last 24 hours in bytes. |
| 68 |
returned: always |
| 69 |
type: int |
| 70 |
sample: 1000 |
| 71 |
containers: |
| 72 |
description: Number of containers. |
| 73 |
returned: always |
| 74 |
type: int |
| 75 |
sample: 10 |
| 76 |
bytes: |
| 77 |
description: Space used in bytes. |
| 78 |
returned: always |
| 79 |
type: int |
| 80 |
sample: 3860997965 |
| 81 |
objs: |
| 82 |
description: Number of objects. |
| 83 |
returned: always |
| 84 |
type: int |
| 85 |
sample: 1000 |
| 86 |
bandwidth: |
| 87 |
description: Dictionary of CDN bandwidth facts. |
| 88 |
returned: always |
| 89 |
type: complex |
| 90 |
contains: |
| 91 |
bytes_out: |
| 92 |
description: Outbound bandwidth for the last 24 hours in bytes. |
| 93 |
returned: always |
| 94 |
type: int |
| 95 |
sample: 1000 |
| 96 |
requests: |
| 97 |
description: Number of requests in the last 24 hours. |
| 98 |
returned: always |
| 99 |
type: int |
| 100 |
sample: 10 |
| 101 |
bytes_in: |
| 102 |
description: Inbound bandwidth for the last 24 hours in bytes. |
| 103 |
returned: always |
| 104 |
type: int |
| 105 |
sample: 1000 |
| 106 |
""" |
| 107 |
|
| 108 |
from ansible.module_utils.basic import AnsibleModule |
| 109 |
|
| 110 |
from ansible_collections.community.general.plugins.module_utils._memset import memset_api_call |
| 111 |
|
| 112 |
|
| 113 |
def get_facts(args=None): |
| 114 |
""" |
| 115 |
Performs a simple API call and returns a JSON blob. |
| 116 |
""" |
| 117 |
retvals, payload = dict(), dict() |
| 118 |
has_changed, has_failed = False, False |
| 119 |
|
| 120 |
payload["name"] = args["name"] |
| 121 |
|
| 122 |
api_method = "memstore.usage" |
| 123 |
has_failed, msg, response = memset_api_call(api_key=args["api_key"], api_method=api_method, payload=payload) |
| 124 |
|
| 125 |
if has_failed: |
| 126 |
# this is the first time the API is called; incorrect credentials will |
| 127 |
# manifest themselves at this point so we need to ensure the user is |
| 128 |
# informed of the reason. |
| 129 |
retvals["failed"] = has_failed |
| 130 |
retvals["msg"] = msg |
| 131 |
if response.status_code is not None: |
| 132 |
retvals["stderr"] = f"API returned an error: {response.status_code}" |
| 133 |
else: |
| 134 |
retvals["stderr"] = f"{response.stderr}" |
| 135 |
return retvals |
| 136 |
|
| 137 |
# we don't want to return the same thing twice |
| 138 |
memset_api = response.json() |
| 139 |
|
| 140 |
retvals["changed"] = has_changed |
| 141 |
retvals["failed"] = has_failed |
| 142 |
retvals["msg"] = None |
| 143 |
retvals["memset_api"] = memset_api |
| 144 |
|
| 145 |
return retvals |
| 146 |
|
| 147 |
|
| 148 |
def main(): |
| 149 |
global module |
| 150 |
module = AnsibleModule( |
| 151 |
argument_spec=dict(api_key=dict(required=True, type="str", no_log=True), name=dict(required=True, type="str")), |
| 152 |
supports_check_mode=True, |
| 153 |
) |
| 154 |
|
| 155 |
# populate the dict with the user-provided vars. |
| 156 |
args = dict(module.params) |
| 157 |
|
| 158 |
retvals = get_facts(args) |
| 159 |
|
| 160 |
if retvals["failed"]: |
| 161 |
module.fail_json(**retvals) |
| 162 |
else: |
| 163 |
module.exit_json(**retvals) |
| 164 |
|
| 165 |
|
| 166 |
if __name__ == "__main__": |
| 167 |
main() |