| 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_vpc_eip |
| 15 |
description: |
| 16 |
- Elastic IP management. |
| 17 |
short_description: Creates a resource of VPC/EIP in Huawei Cloud |
| 18 |
version_added: '0.2.0' |
| 19 |
author: Huawei Inc. (@huaweicloud) |
| 20 |
requirements: |
| 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 |
timeouts: |
| 35 |
description: |
| 36 |
- The timeouts for each operations. |
| 37 |
type: dict |
| 38 |
default: {} |
| 39 |
suboptions: |
| 40 |
create: |
| 41 |
description: |
| 42 |
- The timeouts for create operation. |
| 43 |
type: str |
| 44 |
default: '5m' |
| 45 |
update: |
| 46 |
description: |
| 47 |
- The timeouts for update operation. |
| 48 |
type: str |
| 49 |
default: '5m' |
| 50 |
type: |
| 51 |
description: |
| 52 |
- Specifies the EIP type. |
| 53 |
type: str |
| 54 |
required: true |
| 55 |
dedicated_bandwidth: |
| 56 |
description: |
| 57 |
- Specifies the dedicated bandwidth object. |
| 58 |
type: dict |
| 59 |
suboptions: |
| 60 |
charge_mode: |
| 61 |
description: |
| 62 |
- Specifies whether the bandwidth is billed by traffic or by bandwidth size. The value can be bandwidth or traffic. |
| 63 |
If this parameter is left blank or is null character string, default value bandwidth is used. For IPv6 addresses, |
| 64 |
the default parameter value is bandwidth outside China and is traffic in China. |
| 65 |
type: str |
| 66 |
required: true |
| 67 |
name: |
| 68 |
description: |
| 69 |
- Specifies the bandwidth name. The value is a string of 1 to 64 characters that can contain letters, digits, underscores |
| 70 |
(V(_)), hyphens (V(-)), and periods (V(.)). |
| 71 |
type: str |
| 72 |
required: true |
| 73 |
size: |
| 74 |
description: |
| 75 |
- Specifies the bandwidth size. The value ranges from 1 Mbit/s to 2000 Mbit/s by default. (The specific range may |
| 76 |
vary depending on the configuration in each region. You can see the bandwidth range of each region on the management |
| 77 |
console.) The minimum unit for bandwidth adjustment varies depending on the bandwidth range. The details are as |
| 78 |
follows. |
| 79 |
- The minimum unit is 1 Mbit/s if the allowed bandwidth size ranges from 0 to 300 Mbit/s (with 300 Mbit/s included). |
| 80 |
- The minimum unit is 50 Mbit/s if the allowed bandwidth size ranges 300 Mbit/s to 1000 Mbit/s (with 1000 Mbit/s |
| 81 |
included). |
| 82 |
- The minimum unit is 500 Mbit/s if the allowed bandwidth size is greater than 1000 Mbit/s. |
| 83 |
type: int |
| 84 |
required: true |
| 85 |
enterprise_project_id: |
| 86 |
description: |
| 87 |
- Specifies the enterprise project ID. |
| 88 |
type: str |
| 89 |
ip_version: |
| 90 |
description: |
| 91 |
- The value can be 4 (IPv4 address) or 6 (IPv6 address). If this parameter is left blank, an IPv4 address is assigned. |
| 92 |
type: int |
| 93 |
ipv4_address: |
| 94 |
description: |
| 95 |
- Specifies the obtained IPv4 EIP. The system automatically assigns an EIP if you do not specify it. |
| 96 |
type: str |
| 97 |
port_id: |
| 98 |
description: |
| 99 |
- Specifies the port ID. This parameter is returned only when a private IP address is bound with the EIP. |
| 100 |
type: str |
| 101 |
shared_bandwidth_id: |
| 102 |
description: |
| 103 |
- Specifies the ID of shared bandwidth. |
| 104 |
type: str |
| 105 |
extends_documentation_fragment: |
| 106 |
- community.general._hwc |
| 107 |
- community.general._attributes |
| 108 |
""" |
| 109 |
|
| 110 |
EXAMPLES = r""" |
| 111 |
# create an eip and bind it to a port |
| 112 |
- name: Create vpc |
| 113 |
hwc_network_vpc: |
| 114 |
cidr: "192.168.100.0/24" |
| 115 |
name: "ansible_network_vpc_test" |
| 116 |
register: vpc |
| 117 |
- name: Create subnet |
| 118 |
hwc_vpc_subnet: |
| 119 |
gateway_ip: "192.168.100.32" |
| 120 |
name: "ansible_network_subnet_test" |
| 121 |
dhcp_enable: true |
| 122 |
vpc_id: "{{ vpc.id }}" |
| 123 |
cidr: "192.168.100.0/26" |
| 124 |
register: subnet |
| 125 |
- name: Create a port |
| 126 |
hwc_vpc_port: |
| 127 |
subnet_id: "{{ subnet.id }}" |
| 128 |
ip_address: "192.168.100.33" |
| 129 |
register: port |
| 130 |
- name: Create an eip and bind it to a port |
| 131 |
community.general.hwc_vpc_eip: |
| 132 |
type: "5_bgp" |
| 133 |
dedicated_bandwidth: |
| 134 |
charge_mode: "traffic" |
| 135 |
name: "ansible_test_dedicated_bandwidth" |
| 136 |
size: 1 |
| 137 |
port_id: "{{ port.id }}" |
| 138 |
""" |
| 139 |
|
| 140 |
RETURN = r""" |
| 141 |
type: |
| 142 |
description: |
| 143 |
- Specifies the EIP type. |
| 144 |
type: str |
| 145 |
returned: success |
| 146 |
dedicated_bandwidth: |
| 147 |
description: |
| 148 |
- Specifies the dedicated bandwidth object. |
| 149 |
type: dict |
| 150 |
returned: success |
| 151 |
contains: |
| 152 |
charge_mode: |
| 153 |
description: |
| 154 |
- Specifies whether the bandwidth is billed by traffic or by bandwidth size. The value can be bandwidth or traffic. |
| 155 |
If this parameter is left blank or is null character string, default value bandwidth is used. For IPv6 addresses, |
| 156 |
the default parameter value is bandwidth outside China and is traffic in China. |
| 157 |
type: str |
| 158 |
returned: success |
| 159 |
name: |
| 160 |
description: |
| 161 |
- Specifies the bandwidth name. The value is a string of 1 to 64 characters that can contain letters, digits, underscores |
| 162 |
(V(_)), hyphens (V(-)), and periods (V(.)). |
| 163 |
type: str |
| 164 |
returned: success |
| 165 |
size: |
| 166 |
description: |
| 167 |
- Specifies the bandwidth size. The value ranges from 1 Mbit/s to 2000 Mbit/s by default. (The specific range may |
| 168 |
vary depending on the configuration in each region. You can see the bandwidth range of each region on the management |
| 169 |
console.) The minimum unit for bandwidth adjustment varies depending on the bandwidth range. The details are as |
| 170 |
follows:. |
| 171 |
- The minimum unit is 1 Mbit/s if the allowed bandwidth size ranges from 0 to 300 Mbit/s (with 300 Mbit/s included). |
| 172 |
- The minimum unit is 50 Mbit/s if the allowed bandwidth size ranges 300 Mbit/s to 1000 Mbit/s (with 1000 Mbit/s included). |
| 173 |
- The minimum unit is 500 Mbit/s if the allowed bandwidth size is greater than 1000 Mbit/s. |
| 174 |
type: int |
| 175 |
returned: success |
| 176 |
id: |
| 177 |
description: |
| 178 |
- Specifies the ID of dedicated bandwidth. |
| 179 |
type: str |
| 180 |
returned: success |
| 181 |
enterprise_project_id: |
| 182 |
description: |
| 183 |
- Specifies the enterprise project ID. |
| 184 |
type: str |
| 185 |
returned: success |
| 186 |
ip_version: |
| 187 |
description: |
| 188 |
- The value can be 4 (IPv4 address) or 6 (IPv6 address). If this parameter is left blank, an IPv4 address is assigned. |
| 189 |
type: int |
| 190 |
returned: success |
| 191 |
ipv4_address: |
| 192 |
description: |
| 193 |
- Specifies the obtained IPv4 EIP. The system automatically assigns an EIP if you do not specify it. |
| 194 |
type: str |
| 195 |
returned: success |
| 196 |
port_id: |
| 197 |
description: |
| 198 |
- Specifies the port ID. This parameter is returned only when a private IP address is bound with the EIP. |
| 199 |
type: str |
| 200 |
returned: success |
| 201 |
shared_bandwidth_id: |
| 202 |
description: |
| 203 |
- Specifies the ID of shared bandwidth. |
| 204 |
type: str |
| 205 |
returned: success |
| 206 |
create_time: |
| 207 |
description: |
| 208 |
- Specifies the time (UTC time) when the EIP was assigned. |
| 209 |
type: str |
| 210 |
returned: success |
| 211 |
ipv6_address: |
| 212 |
description: |
| 213 |
- Specifies the obtained IPv6 EIP. |
| 214 |
type: str |
| 215 |
returned: success |
| 216 |
private_ip_address: |
| 217 |
description: |
| 218 |
- Specifies the private IP address bound with the EIP. This parameter is returned only when a private IP address is bound |
| 219 |
with the EIP. |
| 220 |
type: str |
| 221 |
returned: success |
| 222 |
""" |
| 223 |
|
| 224 |
from ansible_collections.community.general.plugins.module_utils._hwc_utils import ( |
| 225 |
Config, |
| 226 |
HwcClientException, |
| 227 |
HwcClientException404, |
| 228 |
HwcModule, |
| 229 |
are_different_dicts, |
| 230 |
build_path, |
| 231 |
get_region, |
| 232 |
is_empty_value, |
| 233 |
navigate_value, |
| 234 |
wait_to_finish, |
| 235 |
) |
| 236 |
|
| 237 |
|
| 238 |
def build_module(): |
| 239 |
return HwcModule( |
| 240 |
argument_spec=dict( |
| 241 |
state=dict(default="present", choices=["present", "absent"], type="str"), |
| 242 |
timeouts=dict( |
| 243 |
type="dict", |
| 244 |
options=dict( |
| 245 |
create=dict(default="5m", type="str"), |
| 246 |
update=dict(default="5m", type="str"), |
| 247 |
), |
| 248 |
default=dict(), |
| 249 |
), |
| 250 |
type=dict(type="str", required=True), |
| 251 |
dedicated_bandwidth=dict( |
| 252 |
type="dict", |
| 253 |
options=dict( |
| 254 |
charge_mode=dict(type="str", required=True), |
| 255 |
name=dict(type="str", required=True), |
| 256 |
size=dict(type="int", required=True), |
| 257 |
), |
| 258 |
), |
| 259 |
enterprise_project_id=dict(type="str"), |
| 260 |
ip_version=dict(type="int"), |
| 261 |
ipv4_address=dict(type="str"), |
| 262 |
port_id=dict(type="str"), |
| 263 |
shared_bandwidth_id=dict(type="str"), |
| 264 |
), |
| 265 |
supports_check_mode=True, |
| 266 |
) |
| 267 |
|
| 268 |
|
| 269 |
def main(): |
| 270 |
"""Main function""" |
| 271 |
|
| 272 |
module = build_module() |
| 273 |
config = Config(module, "vpc") |
| 274 |
|
| 275 |
try: |
| 276 |
resource = None |
| 277 |
if module.params["id"]: |
| 278 |
resource = True |
| 279 |
else: |
| 280 |
v = search_resource(config) |
| 281 |
if len(v) > 1: |
| 282 |
raise Exception(f"Found more than one resource({', '.join([navigate_value(i, ['id']) for i in v])})") |
| 283 |
|
| 284 |
if len(v) == 1: |
| 285 |
resource = v[0] |
| 286 |
module.params["id"] = navigate_value(resource, ["id"]) |
| 287 |
|
| 288 |
result = {} |
| 289 |
changed = False |
| 290 |
if module.params["state"] == "present": |
| 291 |
if resource is None: |
| 292 |
if not module.check_mode: |
| 293 |
create(config) |
| 294 |
changed = True |
| 295 |
|
| 296 |
current = read_resource(config, exclude_output=True) |
| 297 |
expect = user_input_parameters(module) |
| 298 |
if are_different_dicts(expect, current): |
| 299 |
if not module.check_mode: |
| 300 |
update(config) |
| 301 |
changed = True |
| 302 |
|
| 303 |
result = read_resource(config) |
| 304 |
result["id"] = module.params.get("id") |
| 305 |
else: |
| 306 |
if resource: |
| 307 |
if not module.check_mode: |
| 308 |
delete(config) |
| 309 |
changed = True |
| 310 |
|
| 311 |
except Exception as ex: |
| 312 |
module.fail_json(msg=str(ex)) |
| 313 |
|
| 314 |
else: |
| 315 |
result["changed"] = changed |
| 316 |
module.exit_json(**result) |
| 317 |
|
| 318 |
|
| 319 |
def user_input_parameters(module): |
| 320 |
return { |
| 321 |
"dedicated_bandwidth": module.params.get("dedicated_bandwidth"), |
| 322 |
"enterprise_project_id": module.params.get("enterprise_project_id"), |
| 323 |
"ip_version": module.params.get("ip_version"), |
| 324 |
"ipv4_address": module.params.get("ipv4_address"), |
| 325 |
"port_id": module.params.get("port_id"), |
| 326 |
"shared_bandwidth_id": module.params.get("shared_bandwidth_id"), |
| 327 |
"type": module.params.get("type"), |
| 328 |
} |
| 329 |
|
| 330 |
|
| 331 |
def create(config): |
| 332 |
module = config.module |
| 333 |
client = config.client(get_region(module), "vpc", "project") |
| 334 |
timeout = 60 * int(module.params["timeouts"]["create"].rstrip("m")) |
| 335 |
opts = user_input_parameters(module) |
| 336 |
|
| 337 |
params = build_create_parameters(opts) |
| 338 |
r = send_create_request(module, params, client) |
| 339 |
obj = async_wait_create(config, r, client, timeout) |
| 340 |
module.params["id"] = navigate_value(obj, ["publicip", "id"]) |
| 341 |
|
| 342 |
|
| 343 |
def update(config): |
| 344 |
module = config.module |
| 345 |
client = config.client(get_region(module), "vpc", "project") |
| 346 |
timeout = 60 * int(module.params["timeouts"]["update"].rstrip("m")) |
| 347 |
opts = user_input_parameters(module) |
| 348 |
|
| 349 |
params = build_update_parameters(opts) |
| 350 |
if params: |
| 351 |
r = send_update_request(module, params, client) |
| 352 |
async_wait_update(config, r, client, timeout) |
| 353 |
|
| 354 |
|
| 355 |
def delete(config): |
| 356 |
module = config.module |
| 357 |
client = config.client(get_region(module), "vpc", "project") |
| 358 |
|
| 359 |
if module.params["port_id"]: |
| 360 |
module.params["port_id"] = "" |
| 361 |
update(config) |
| 362 |
|
| 363 |
send_delete_request(module, None, client) |
| 364 |
|
| 365 |
url = build_path(module, "publicips/{id}") |
| 366 |
|
| 367 |
def _refresh_status(): |
| 368 |
try: |
| 369 |
client.get(url) |
| 370 |
except HwcClientException404: |
| 371 |
return True, "Done" |
| 372 |
|
| 373 |
except Exception: |
| 374 |
return None, "" |
| 375 |
|
| 376 |
return True, "Pending" |
| 377 |
|
| 378 |
timeout = 60 * int(module.params["timeouts"]["create"].rstrip("m")) |
| 379 |
try: |
| 380 |
wait_to_finish(["Done"], ["Pending"], _refresh_status, timeout) |
| 381 |
except Exception as ex: |
| 382 |
module.fail_json(msg=f"module(hwc_vpc_eip): error waiting for api(delete) to be done, error= {ex}") |
| 383 |
|
| 384 |
|
| 385 |
def read_resource(config, exclude_output=False): |
| 386 |
module = config.module |
| 387 |
client = config.client(get_region(module), "vpc", "project") |
| 388 |
|
| 389 |
res = {} |
| 390 |
|
| 391 |
r = send_read_request(module, client) |
| 392 |
res["read"] = fill_read_resp_body(r) |
| 393 |
|
| 394 |
return update_properties(module, res, None, exclude_output) |
| 395 |
|
| 396 |
|
| 397 |
def _build_query_link(opts): |
| 398 |
query_params = [] |
| 399 |
|
| 400 |
v = navigate_value(opts, ["ip_version"]) |
| 401 |
if v: |
| 402 |
query_params.append(f"ip_version={v}") |
| 403 |
|
| 404 |
v = navigate_value(opts, ["enterprise_project_id"]) |
| 405 |
if v: |
| 406 |
query_params.append(f"enterprise_project_id={v}") |
| 407 |
|
| 408 |
query_link = "?marker={marker}&limit=10" |
| 409 |
if query_params: |
| 410 |
query_link += f"&{'&'.join(query_params)}" |
| 411 |
|
| 412 |
return query_link |
| 413 |
|
| 414 |
|
| 415 |
def search_resource(config): |
| 416 |
module = config.module |
| 417 |
client = config.client(get_region(module), "vpc", "project") |
| 418 |
opts = user_input_parameters(module) |
| 419 |
identity_obj = _build_identity_object(opts) |
| 420 |
query_link = _build_query_link(opts) |
| 421 |
link = f"publicips{query_link}" |
| 422 |
|
| 423 |
result = [] |
| 424 |
p = {"marker": ""} |
| 425 |
while True: |
| 426 |
url = link.format(**p) |
| 427 |
r = send_list_request(module, client, url) |
| 428 |
if not r: |
| 429 |
break |
| 430 |
|
| 431 |
for item in r: |
| 432 |
item = fill_list_resp_body(item) |
| 433 |
if not are_different_dicts(identity_obj, item): |
| 434 |
result.append(item) |
| 435 |
|
| 436 |
if len(result) > 1: |
| 437 |
break |
| 438 |
|
| 439 |
p["marker"] = r[-1].get("id") |
| 440 |
|
| 441 |
return result |
| 442 |
|
| 443 |
|
| 444 |
def build_create_parameters(opts): |
| 445 |
params = dict() |
| 446 |
|
| 447 |
v = expand_create_bandwidth(opts, None) |
| 448 |
if not is_empty_value(v): |
| 449 |
params["bandwidth"] = v |
| 450 |
|
| 451 |
v = navigate_value(opts, ["enterprise_project_id"], None) |
| 452 |
if not is_empty_value(v): |
| 453 |
params["enterprise_project_id"] = v |
| 454 |
|
| 455 |
v = expand_create_publicip(opts, None) |
| 456 |
if not is_empty_value(v): |
| 457 |
params["publicip"] = v |
| 458 |
|
| 459 |
return params |
| 460 |
|
| 461 |
|
| 462 |
def expand_create_bandwidth(d, array_index): |
| 463 |
v = navigate_value(d, ["dedicated_bandwidth"], array_index) |
| 464 |
sbwid = navigate_value(d, ["shared_bandwidth_id"], array_index) |
| 465 |
if v and sbwid: |
| 466 |
raise Exception("don't input shared_bandwidth_id and dedicated_bandwidth at same time") |
| 467 |
|
| 468 |
if not (v or sbwid): |
| 469 |
raise Exception("must input shared_bandwidth_id or dedicated_bandwidth") |
| 470 |
|
| 471 |
if sbwid: |
| 472 |
return {"id": sbwid, "share_type": "WHOLE"} |
| 473 |
|
| 474 |
return {"charge_mode": v["charge_mode"], "name": v["name"], "share_type": "PER", "size": v["size"]} |
| 475 |
|
| 476 |
|
| 477 |
def expand_create_publicip(d, array_index): |
| 478 |
r = dict() |
| 479 |
|
| 480 |
v = navigate_value(d, ["ipv4_address"], array_index) |
| 481 |
if not is_empty_value(v): |
| 482 |
r["ip_address"] = v |
| 483 |
|
| 484 |
v = navigate_value(d, ["ip_version"], array_index) |
| 485 |
if not is_empty_value(v): |
| 486 |
r["ip_version"] = v |
| 487 |
|
| 488 |
v = navigate_value(d, ["type"], array_index) |
| 489 |
if not is_empty_value(v): |
| 490 |
r["type"] = v |
| 491 |
|
| 492 |
return r |
| 493 |
|
| 494 |
|
| 495 |
def send_create_request(module, params, client): |
| 496 |
url = "publicips" |
| 497 |
try: |
| 498 |
r = client.post(url, params) |
| 499 |
except HwcClientException as ex: |
| 500 |
msg = f"module(hwc_vpc_eip): error running api(create), error: {ex}" |
| 501 |
module.fail_json(msg=msg) |
| 502 |
|
| 503 |
return r |
| 504 |
|
| 505 |
|
| 506 |
def async_wait_create(config, result, client, timeout): |
| 507 |
module = config.module |
| 508 |
|
| 509 |
path_parameters = { |
| 510 |
"publicip_id": ["publicip", "id"], |
| 511 |
} |
| 512 |
data = {key: navigate_value(result, path) for key, path in path_parameters.items()} |
| 513 |
|
| 514 |
url = build_path(module, "publicips/{publicip_id}", data) |
| 515 |
|
| 516 |
def _query_status(): |
| 517 |
r = None |
| 518 |
try: |
| 519 |
r = client.get(url, timeout=timeout) |
| 520 |
except HwcClientException: |
| 521 |
return None, "" |
| 522 |
|
| 523 |
try: |
| 524 |
s = navigate_value(r, ["publicip", "status"]) |
| 525 |
return r, s |
| 526 |
except Exception: |
| 527 |
return None, "" |
| 528 |
|
| 529 |
try: |
| 530 |
return wait_to_finish(["ACTIVE", "DOWN"], None, _query_status, timeout) |
| 531 |
except Exception as ex: |
| 532 |
module.fail_json(msg=f"module(hwc_vpc_eip): error waiting for api(create) to be done, error= {ex}") |
| 533 |
|
| 534 |
|
| 535 |
def build_update_parameters(opts): |
| 536 |
params = dict() |
| 537 |
|
| 538 |
v = navigate_value(opts, ["ip_version"], None) |
| 539 |
if not is_empty_value(v): |
| 540 |
params["ip_version"] = v |
| 541 |
|
| 542 |
v = navigate_value(opts, ["port_id"], None) |
| 543 |
if v is not None: |
| 544 |
params["port_id"] = v |
| 545 |
|
| 546 |
if not params: |
| 547 |
return params |
| 548 |
|
| 549 |
params = {"publicip": params} |
| 550 |
|
| 551 |
return params |
| 552 |
|
| 553 |
|
| 554 |
def send_update_request(module, params, client): |
| 555 |
url = build_path(module, "publicips/{id}") |
| 556 |
|
| 557 |
try: |
| 558 |
r = client.put(url, params) |
| 559 |
except HwcClientException as ex: |
| 560 |
msg = f"module(hwc_vpc_eip): error running api(update), error: {ex}" |
| 561 |
module.fail_json(msg=msg) |
| 562 |
|
| 563 |
return r |
| 564 |
|
| 565 |
|
| 566 |
def async_wait_update(config, result, client, timeout): |
| 567 |
module = config.module |
| 568 |
|
| 569 |
url = build_path(module, "publicips/{id}") |
| 570 |
|
| 571 |
def _query_status(): |
| 572 |
r = None |
| 573 |
try: |
| 574 |
r = client.get(url, timeout=timeout) |
| 575 |
except HwcClientException: |
| 576 |
return None, "" |
| 577 |
|
| 578 |
try: |
| 579 |
s = navigate_value(r, ["publicip", "status"]) |
| 580 |
return r, s |
| 581 |
except Exception: |
| 582 |
return None, "" |
| 583 |
|
| 584 |
try: |
| 585 |
return wait_to_finish(["ACTIVE", "DOWN"], None, _query_status, timeout) |
| 586 |
except Exception as ex: |
| 587 |
module.fail_json(msg=f"module(hwc_vpc_eip): error waiting for api(update) to be done, error= {ex}") |
| 588 |
|
| 589 |
|
| 590 |
def send_delete_request(module, params, client): |
| 591 |
url = build_path(module, "publicips/{id}") |
| 592 |
|
| 593 |
try: |
| 594 |
r = client.delete(url, params) |
| 595 |
except HwcClientException as ex: |
| 596 |
msg = f"module(hwc_vpc_eip): error running api(delete), error: {ex}" |
| 597 |
module.fail_json(msg=msg) |
| 598 |
|
| 599 |
return r |
| 600 |
|
| 601 |
|
| 602 |
def send_read_request(module, client): |
| 603 |
url = build_path(module, "publicips/{id}") |
| 604 |
|
| 605 |
r = None |
| 606 |
try: |
| 607 |
r = client.get(url) |
| 608 |
except HwcClientException as ex: |
| 609 |
msg = f"module(hwc_vpc_eip): error running api(read), error: {ex}" |
| 610 |
module.fail_json(msg=msg) |
| 611 |
|
| 612 |
return navigate_value(r, ["publicip"], None) |
| 613 |
|
| 614 |
|
| 615 |
def fill_read_resp_body(body): |
| 616 |
result = dict() |
| 617 |
|
| 618 |
result["bandwidth_id"] = body.get("bandwidth_id") |
| 619 |
|
| 620 |
result["bandwidth_name"] = body.get("bandwidth_name") |
| 621 |
|
| 622 |
result["bandwidth_share_type"] = body.get("bandwidth_share_type") |
| 623 |
|
| 624 |
result["bandwidth_size"] = body.get("bandwidth_size") |
| 625 |
|
| 626 |
result["create_time"] = body.get("create_time") |
| 627 |
|
| 628 |
result["enterprise_project_id"] = body.get("enterprise_project_id") |
| 629 |
|
| 630 |
result["id"] = body.get("id") |
| 631 |
|
| 632 |
result["ip_version"] = body.get("ip_version") |
| 633 |
|
| 634 |
result["port_id"] = body.get("port_id") |
| 635 |
|
| 636 |
result["private_ip_address"] = body.get("private_ip_address") |
| 637 |
|
| 638 |
result["public_ip_address"] = body.get("public_ip_address") |
| 639 |
|
| 640 |
result["public_ipv6_address"] = body.get("public_ipv6_address") |
| 641 |
|
| 642 |
result["status"] = body.get("status") |
| 643 |
|
| 644 |
result["tenant_id"] = body.get("tenant_id") |
| 645 |
|
| 646 |
result["type"] = body.get("type") |
| 647 |
|
| 648 |
return result |
| 649 |
|
| 650 |
|
| 651 |
def update_properties(module, response, array_index, exclude_output=False): |
| 652 |
r = user_input_parameters(module) |
| 653 |
|
| 654 |
if not exclude_output: |
| 655 |
v = navigate_value(response, ["read", "create_time"], array_index) |
| 656 |
r["create_time"] = v |
| 657 |
|
| 658 |
v = r.get("dedicated_bandwidth") |
| 659 |
v = flatten_dedicated_bandwidth(response, array_index, v, exclude_output) |
| 660 |
r["dedicated_bandwidth"] = v |
| 661 |
|
| 662 |
v = navigate_value(response, ["read", "enterprise_project_id"], array_index) |
| 663 |
r["enterprise_project_id"] = v |
| 664 |
|
| 665 |
v = navigate_value(response, ["read", "ip_version"], array_index) |
| 666 |
r["ip_version"] = v |
| 667 |
|
| 668 |
v = navigate_value(response, ["read", "public_ip_address"], array_index) |
| 669 |
r["ipv4_address"] = v |
| 670 |
|
| 671 |
if not exclude_output: |
| 672 |
v = navigate_value(response, ["read", "public_ipv6_address"], array_index) |
| 673 |
r["ipv6_address"] = v |
| 674 |
|
| 675 |
v = navigate_value(response, ["read", "port_id"], array_index) |
| 676 |
r["port_id"] = v |
| 677 |
|
| 678 |
if not exclude_output: |
| 679 |
v = navigate_value(response, ["read", "private_ip_address"], array_index) |
| 680 |
r["private_ip_address"] = v |
| 681 |
|
| 682 |
v = r.get("shared_bandwidth_id") |
| 683 |
v = flatten_shared_bandwidth_id(response, array_index, v, exclude_output) |
| 684 |
r["shared_bandwidth_id"] = v |
| 685 |
|
| 686 |
v = navigate_value(response, ["read", "type"], array_index) |
| 687 |
r["type"] = v |
| 688 |
|
| 689 |
return r |
| 690 |
|
| 691 |
|
| 692 |
def flatten_dedicated_bandwidth(d, array_index, current_value, exclude_output): |
| 693 |
v = navigate_value(d, ["read", "bandwidth_share_type"], array_index) |
| 694 |
if not (v and v == "PER"): |
| 695 |
return current_value |
| 696 |
|
| 697 |
result = current_value |
| 698 |
if not result: |
| 699 |
result = dict() |
| 700 |
|
| 701 |
if not exclude_output: |
| 702 |
v = navigate_value(d, ["read", "bandwidth_id"], array_index) |
| 703 |
if v is not None: |
| 704 |
result["id"] = v |
| 705 |
|
| 706 |
v = navigate_value(d, ["read", "bandwidth_name"], array_index) |
| 707 |
if v is not None: |
| 708 |
result["name"] = v |
| 709 |
|
| 710 |
v = navigate_value(d, ["read", "bandwidth_size"], array_index) |
| 711 |
if v is not None: |
| 712 |
result["size"] = v |
| 713 |
|
| 714 |
return result if result else current_value |
| 715 |
|
| 716 |
|
| 717 |
def flatten_shared_bandwidth_id(d, array_index, current_value, exclude_output): |
| 718 |
v = navigate_value(d, ["read", "bandwidth_id"], array_index) |
| 719 |
|
| 720 |
v1 = navigate_value(d, ["read", "bandwidth_share_type"], array_index) |
| 721 |
|
| 722 |
return v if (v1 and v1 == "WHOLE") else current_value |
| 723 |
|
| 724 |
|
| 725 |
def send_list_request(module, client, url): |
| 726 |
r = None |
| 727 |
try: |
| 728 |
r = client.get(url) |
| 729 |
except HwcClientException as ex: |
| 730 |
msg = f"module(hwc_vpc_eip): error running api(list), error: {ex}" |
| 731 |
module.fail_json(msg=msg) |
| 732 |
|
| 733 |
return navigate_value(r, ["publicips"], None) |
| 734 |
|
| 735 |
|
| 736 |
def _build_identity_object(all_opts): |
| 737 |
result = dict() |
| 738 |
|
| 739 |
v = expand_list_bandwidth_id(all_opts, None) |
| 740 |
result["bandwidth_id"] = v |
| 741 |
|
| 742 |
v = navigate_value(all_opts, ["dedicated_bandwidth", "name"], None) |
| 743 |
result["bandwidth_name"] = v |
| 744 |
|
| 745 |
result["bandwidth_share_type"] = None |
| 746 |
|
| 747 |
v = navigate_value(all_opts, ["dedicated_bandwidth", "size"], None) |
| 748 |
result["bandwidth_size"] = v |
| 749 |
|
| 750 |
result["create_time"] = None |
| 751 |
|
| 752 |
v = navigate_value(all_opts, ["enterprise_project_id"], None) |
| 753 |
result["enterprise_project_id"] = v |
| 754 |
|
| 755 |
result["id"] = None |
| 756 |
|
| 757 |
v = navigate_value(all_opts, ["ip_version"], None) |
| 758 |
result["ip_version"] = v |
| 759 |
|
| 760 |
v = navigate_value(all_opts, ["port_id"], None) |
| 761 |
result["port_id"] = v |
| 762 |
|
| 763 |
result["private_ip_address"] = None |
| 764 |
|
| 765 |
v = navigate_value(all_opts, ["ipv4_address"], None) |
| 766 |
result["public_ip_address"] = v |
| 767 |
|
| 768 |
result["public_ipv6_address"] = None |
| 769 |
|
| 770 |
result["status"] = None |
| 771 |
|
| 772 |
result["tenant_id"] = None |
| 773 |
|
| 774 |
v = navigate_value(all_opts, ["type"], None) |
| 775 |
result["type"] = v |
| 776 |
|
| 777 |
return result |
| 778 |
|
| 779 |
|
| 780 |
def expand_list_bandwidth_id(d, array_index): |
| 781 |
v = navigate_value(d, ["dedicated_bandwidth"], array_index) |
| 782 |
sbwid = navigate_value(d, ["shared_bandwidth_id"], array_index) |
| 783 |
if v and sbwid: |
| 784 |
raise Exception("don't input shared_bandwidth_id and dedicated_bandwidth at same time") |
| 785 |
|
| 786 |
return sbwid |
| 787 |
|
| 788 |
|
| 789 |
def fill_list_resp_body(body): |
| 790 |
result = dict() |
| 791 |
|
| 792 |
result["bandwidth_id"] = body.get("bandwidth_id") |
| 793 |
|
| 794 |
result["bandwidth_name"] = body.get("bandwidth_name") |
| 795 |
|
| 796 |
result["bandwidth_share_type"] = body.get("bandwidth_share_type") |
| 797 |
|
| 798 |
result["bandwidth_size"] = body.get("bandwidth_size") |
| 799 |
|
| 800 |
result["create_time"] = body.get("create_time") |
| 801 |
|
| 802 |
result["enterprise_project_id"] = body.get("enterprise_project_id") |
| 803 |
|
| 804 |
result["id"] = body.get("id") |
| 805 |
|
| 806 |
result["ip_version"] = body.get("ip_version") |
| 807 |
|
| 808 |
result["port_id"] = body.get("port_id") |
| 809 |
|
| 810 |
result["private_ip_address"] = body.get("private_ip_address") |
| 811 |
|
| 812 |
result["public_ip_address"] = body.get("public_ip_address") |
| 813 |
|
| 814 |
result["public_ipv6_address"] = body.get("public_ipv6_address") |
| 815 |
|
| 816 |
result["status"] = body.get("status") |
| 817 |
|
| 818 |
result["tenant_id"] = body.get("tenant_id") |
| 819 |
|
| 820 |
result["type"] = body.get("type") |
| 821 |
|
| 822 |
return result |
| 823 |
|
| 824 |
|
| 825 |
if __name__ == "__main__": |
| 826 |
main() |