| 1 |
#!/usr/bin/python |
| 2 |
# |
| 3 |
# Copyright (C) 2019 Huawei |
| 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 |
############################################################################### |
| 10 |
# Documentation |
| 11 |
############################################################################### |
| 12 |
|
| 13 |
DOCUMENTATION = r""" |
| 14 |
module: hwc_smn_topic |
| 15 |
description: |
| 16 |
- Represents a SMN notification topic resource. |
| 17 |
short_description: Creates a resource of SMNTopic in Huawei Cloud |
| 18 |
author: Huawei Inc. (@huaweicloud) |
| 19 |
requirements: |
| 20 |
- requests >= 2.18.4 |
| 21 |
- keystoneauth1 >= 3.6.0 |
| 22 |
attributes: |
| 23 |
check_mode: |
| 24 |
support: full |
| 25 |
diff_mode: |
| 26 |
support: none |
| 27 |
options: |
| 28 |
state: |
| 29 |
description: |
| 30 |
- Whether the given object should exist in Huawei Cloud. |
| 31 |
type: str |
| 32 |
choices: ['present', 'absent'] |
| 33 |
default: 'present' |
| 34 |
display_name: |
| 35 |
description: |
| 36 |
- Topic display name, which is presented as the name of the email sender in an email message. The topic display name |
| 37 |
contains a maximum of 192 bytes. |
| 38 |
type: str |
| 39 |
name: |
| 40 |
description: |
| 41 |
- Name of the topic to be created. The topic name is a string of 1 to 256 characters. It must contain upper- or lower-case |
| 42 |
letters, digits, hyphens (V(-)), and underscores (V(_)), and must start with a letter or digit. |
| 43 |
type: str |
| 44 |
required: true |
| 45 |
extends_documentation_fragment: |
| 46 |
- community.general._hwc |
| 47 |
- community.general._attributes |
| 48 |
""" |
| 49 |
|
| 50 |
EXAMPLES = r""" |
| 51 |
- name: Create a smn topic |
| 52 |
community.general.hwc_smn_topic: |
| 53 |
identity_endpoint: "{{ identity_endpoint }}" |
| 54 |
user_name: "{{ user_name }}" |
| 55 |
password: "{{ password }}" |
| 56 |
domain_name: "{{ domain_name }}" |
| 57 |
project_name: "{{ project_name }}" |
| 58 |
region: "{{ region }}" |
| 59 |
name: "ansible_smn_topic_test" |
| 60 |
state: present |
| 61 |
""" |
| 62 |
|
| 63 |
RETURN = r""" |
| 64 |
create_time: |
| 65 |
description: |
| 66 |
- Time when the topic was created. |
| 67 |
returned: success |
| 68 |
type: str |
| 69 |
display_name: |
| 70 |
description: |
| 71 |
- Topic display name, which is presented as the name of the email sender in an email message. The topic display name contains |
| 72 |
a maximum of 192 bytes. |
| 73 |
returned: success |
| 74 |
type: str |
| 75 |
name: |
| 76 |
description: |
| 77 |
- Name of the topic to be created. The topic name is a string of 1 to 256 characters. It must contain upper- or lower-case |
| 78 |
letters, digits, hyphens (V(-)), and underscores (V(_)), and must start with a letter or digit. |
| 79 |
returned: success |
| 80 |
type: str |
| 81 |
push_policy: |
| 82 |
description: |
| 83 |
- Message pushing policy. V(0) indicates that the message sending fails and the message is cached in the queue. V(1) indicates |
| 84 |
that the failed message is discarded. |
| 85 |
returned: success |
| 86 |
type: int |
| 87 |
topic_urn: |
| 88 |
description: |
| 89 |
- Resource identifier of a topic, which is unique. |
| 90 |
returned: success |
| 91 |
type: str |
| 92 |
update_time: |
| 93 |
description: |
| 94 |
- Time when the topic was updated. |
| 95 |
returned: success |
| 96 |
type: str |
| 97 |
""" |
| 98 |
|
| 99 |
############################################################################### |
| 100 |
# Imports |
| 101 |
############################################################################### |
| 102 |
|
| 103 |
import re |
| 104 |
|
| 105 |
from ansible_collections.community.general.plugins.module_utils._hwc_utils import ( |
| 106 |
Config, |
| 107 |
HwcClientException, |
| 108 |
HwcModule, |
| 109 |
are_different_dicts, |
| 110 |
build_path, |
| 111 |
get_region, |
| 112 |
is_empty_value, |
| 113 |
navigate_value, |
| 114 |
) |
| 115 |
|
| 116 |
############################################################################### |
| 117 |
# Main |
| 118 |
############################################################################### |
| 119 |
|
| 120 |
|
| 121 |
def main(): |
| 122 |
"""Main function""" |
| 123 |
|
| 124 |
module = HwcModule( |
| 125 |
argument_spec=dict( |
| 126 |
state=dict(default="present", choices=["present", "absent"], type="str"), |
| 127 |
display_name=dict(type="str"), |
| 128 |
name=dict(required=True, type="str"), |
| 129 |
), |
| 130 |
supports_check_mode=True, |
| 131 |
) |
| 132 |
|
| 133 |
config = Config(module, "smn") |
| 134 |
|
| 135 |
state = module.params["state"] |
| 136 |
|
| 137 |
if not module.params.get("id"): |
| 138 |
module.params["id"] = get_resource_id(config) |
| 139 |
|
| 140 |
fetch = None |
| 141 |
link = self_link(module) |
| 142 |
# the link will include Nones if required format parameters are missed |
| 143 |
if not re.search("/None/|/None$", link): |
| 144 |
client = config.client(get_region(module), "smn", "project") |
| 145 |
fetch = fetch_resource(module, client, link) |
| 146 |
changed = False |
| 147 |
|
| 148 |
if fetch: |
| 149 |
if state == "present": |
| 150 |
expect = _get_resource_editable_properties(module) |
| 151 |
current_state = response_to_hash(module, fetch) |
| 152 |
current = {"display_name": current_state["display_name"]} |
| 153 |
if are_different_dicts(expect, current): |
| 154 |
if not module.check_mode: |
| 155 |
fetch = update(config) |
| 156 |
fetch = response_to_hash(module, fetch) |
| 157 |
changed = True |
| 158 |
else: |
| 159 |
fetch = current_state |
| 160 |
else: |
| 161 |
if not module.check_mode: |
| 162 |
delete(config) |
| 163 |
fetch = {} |
| 164 |
changed = True |
| 165 |
else: |
| 166 |
if state == "present": |
| 167 |
if not module.check_mode: |
| 168 |
fetch = create(config) |
| 169 |
fetch = response_to_hash(module, fetch) |
| 170 |
changed = True |
| 171 |
else: |
| 172 |
fetch = {} |
| 173 |
|
| 174 |
fetch.update({"changed": changed}) |
| 175 |
|
| 176 |
module.exit_json(**fetch) |
| 177 |
|
| 178 |
|
| 179 |
def create(config): |
| 180 |
module = config.module |
| 181 |
client = config.client(get_region(module), "smn", "project") |
| 182 |
|
| 183 |
link = "notifications/topics" |
| 184 |
r = None |
| 185 |
try: |
| 186 |
r = client.post(link, create_resource_opts(module)) |
| 187 |
except HwcClientException as ex: |
| 188 |
msg = f"module(hwc_smn_topic): error creating resource, error: {ex}" |
| 189 |
module.fail_json(msg=msg) |
| 190 |
|
| 191 |
return get_resource(config, r) |
| 192 |
|
| 193 |
|
| 194 |
def update(config): |
| 195 |
module = config.module |
| 196 |
client = config.client(get_region(module), "smn", "project") |
| 197 |
|
| 198 |
link = self_link(module) |
| 199 |
try: |
| 200 |
client.put(link, update_resource_opts(module)) |
| 201 |
except HwcClientException as ex: |
| 202 |
msg = f"module(hwc_smn_topic): error updating resource, error: {ex}" |
| 203 |
module.fail_json(msg=msg) |
| 204 |
|
| 205 |
return fetch_resource(module, client, link) |
| 206 |
|
| 207 |
|
| 208 |
def delete(config): |
| 209 |
module = config.module |
| 210 |
client = config.client(get_region(module), "smn", "project") |
| 211 |
|
| 212 |
link = self_link(module) |
| 213 |
try: |
| 214 |
client.delete(link) |
| 215 |
except HwcClientException as ex: |
| 216 |
msg = f"module(hwc_smn_topic): error deleting resource, error: {ex}" |
| 217 |
module.fail_json(msg=msg) |
| 218 |
|
| 219 |
|
| 220 |
def fetch_resource(module, client, link): |
| 221 |
try: |
| 222 |
return client.get(link) |
| 223 |
except HwcClientException as ex: |
| 224 |
msg = f"module(hwc_smn_topic): error fetching resource, error: {ex}" |
| 225 |
module.fail_json(msg=msg) |
| 226 |
|
| 227 |
|
| 228 |
def get_resource(config, result): |
| 229 |
module = config.module |
| 230 |
client = config.client(get_region(module), "smn", "project") |
| 231 |
|
| 232 |
v = "" |
| 233 |
try: |
| 234 |
v = navigate_value(result, ["topic_urn"]) |
| 235 |
except Exception as ex: |
| 236 |
module.fail_json(msg=str(ex)) |
| 237 |
|
| 238 |
d = {"topic_urn": v} |
| 239 |
url = build_path(module, "notifications/topics/{topic_urn}", d) |
| 240 |
|
| 241 |
return fetch_resource(module, client, url) |
| 242 |
|
| 243 |
|
| 244 |
def get_resource_id(config): |
| 245 |
module = config.module |
| 246 |
client = config.client(get_region(module), "smn", "project") |
| 247 |
|
| 248 |
link = "notifications/topics" |
| 249 |
query_link = "?offset={offset}&limit=10" |
| 250 |
link += query_link |
| 251 |
|
| 252 |
p = {"offset": 0} |
| 253 |
v = module.params.get("name") |
| 254 |
ids = set() |
| 255 |
while True: |
| 256 |
r = None |
| 257 |
try: |
| 258 |
r = client.get(link.format(**p)) |
| 259 |
except Exception: |
| 260 |
pass |
| 261 |
if r is None: |
| 262 |
break |
| 263 |
r = r.get("topics", []) |
| 264 |
if r == []: |
| 265 |
break |
| 266 |
for i in r: |
| 267 |
if i.get("name") == v: |
| 268 |
ids.add(i.get("topic_urn")) |
| 269 |
if len(ids) >= 2: |
| 270 |
module.fail_json(msg="Multiple resources are found") |
| 271 |
|
| 272 |
p["offset"] += 1 |
| 273 |
|
| 274 |
return ids.pop() if ids else None |
| 275 |
|
| 276 |
|
| 277 |
def self_link(module): |
| 278 |
return build_path(module, "notifications/topics/{id}") |
| 279 |
|
| 280 |
|
| 281 |
def create_resource_opts(module): |
| 282 |
params = dict() |
| 283 |
|
| 284 |
v = module.params.get("display_name") |
| 285 |
if not is_empty_value(v): |
| 286 |
params["display_name"] = v |
| 287 |
|
| 288 |
v = module.params.get("name") |
| 289 |
if not is_empty_value(v): |
| 290 |
params["name"] = v |
| 291 |
|
| 292 |
return params |
| 293 |
|
| 294 |
|
| 295 |
def update_resource_opts(module): |
| 296 |
params = dict() |
| 297 |
|
| 298 |
v = module.params.get("display_name") |
| 299 |
if not is_empty_value(v): |
| 300 |
params["display_name"] = v |
| 301 |
|
| 302 |
return params |
| 303 |
|
| 304 |
|
| 305 |
def _get_resource_editable_properties(module): |
| 306 |
return { |
| 307 |
"display_name": module.params.get("display_name"), |
| 308 |
} |
| 309 |
|
| 310 |
|
| 311 |
def response_to_hash(module, response): |
| 312 |
"""Remove unnecessary properties from the response. |
| 313 |
This is for doing comparisons with Ansible's current parameters. |
| 314 |
""" |
| 315 |
return { |
| 316 |
"create_time": response.get("create_time"), |
| 317 |
"display_name": response.get("display_name"), |
| 318 |
"name": response.get("name"), |
| 319 |
"push_policy": _push_policy_convert_from_response(response.get("push_policy")), |
| 320 |
"topic_urn": response.get("topic_urn"), |
| 321 |
"update_time": response.get("update_time"), |
| 322 |
} |
| 323 |
|
| 324 |
|
| 325 |
def _push_policy_convert_from_response(value): |
| 326 |
return { |
| 327 |
0: "the message sending fails and is cached in the queue", |
| 328 |
1: "the failed message is discarded", |
| 329 |
}.get(int(value)) |
| 330 |
|
| 331 |
|
| 332 |
if __name__ == "__main__": |
| 333 |
main() |