| 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_security_group |
| 15 |
description: |
| 16 |
- VPC security group management. |
| 17 |
short_description: Creates a resource of VPC/SecurityGroup in Huawei Cloud |
| 18 |
notes: |
| 19 |
- If O(id) option is provided, it takes precedence over O(name), O(enterprise_project_id), and O(vpc_id) for security group |
| 20 |
selection. |
| 21 |
- O(name), O(enterprise_project_id) and O(vpc_id) are used for security group selection. If more than one security group |
| 22 |
with this options exists, execution is aborted. |
| 23 |
- No parameter support updating. If one of option is changed, the module creates a new resource. |
| 24 |
version_added: '0.2.0' |
| 25 |
author: Huawei Inc. (@huaweicloud) |
| 26 |
requirements: |
| 27 |
- keystoneauth1 >= 3.6.0 |
| 28 |
attributes: |
| 29 |
check_mode: |
| 30 |
support: full |
| 31 |
diff_mode: |
| 32 |
support: none |
| 33 |
options: |
| 34 |
state: |
| 35 |
description: |
| 36 |
- Whether the given object should exist in Huawei Cloud. |
| 37 |
type: str |
| 38 |
choices: ['present', 'absent'] |
| 39 |
default: 'present' |
| 40 |
name: |
| 41 |
description: |
| 42 |
- Specifies the security group name. The value is a string of 1 to 64 characters that can contain letters, digits, underscores |
| 43 |
(V(_)), hyphens (V(-)), and periods (V(.)). |
| 44 |
type: str |
| 45 |
required: true |
| 46 |
enterprise_project_id: |
| 47 |
description: |
| 48 |
- Specifies the enterprise project ID. When creating a security group, associate the enterprise project ID with the |
| 49 |
security group.s. |
| 50 |
type: str |
| 51 |
vpc_id: |
| 52 |
description: |
| 53 |
- Specifies the resource ID of the VPC to which the security group belongs. |
| 54 |
type: str |
| 55 |
extends_documentation_fragment: |
| 56 |
- community.general._hwc |
| 57 |
- community.general._attributes |
| 58 |
""" |
| 59 |
|
| 60 |
EXAMPLES = r""" |
| 61 |
# create a security group |
| 62 |
- name: Create a security group |
| 63 |
community.general.hwc_vpc_security_group: |
| 64 |
name: "ansible_network_security_group_test" |
| 65 |
""" |
| 66 |
|
| 67 |
RETURN = r""" |
| 68 |
name: |
| 69 |
description: |
| 70 |
- Specifies the security group name. The value is a string of 1 to 64 characters that can contain letters, digits, underscores |
| 71 |
(V(_)), hyphens (V(-)), and periods (V(.)). |
| 72 |
type: str |
| 73 |
returned: success |
| 74 |
enterprise_project_id: |
| 75 |
description: |
| 76 |
- Specifies the enterprise project ID. When creating a security group, associate the enterprise project ID with the security |
| 77 |
group. |
| 78 |
type: str |
| 79 |
returned: success |
| 80 |
vpc_id: |
| 81 |
description: |
| 82 |
- Specifies the resource ID of the VPC to which the security group belongs. |
| 83 |
type: str |
| 84 |
returned: success |
| 85 |
rules: |
| 86 |
description: |
| 87 |
- Specifies the security group rule, which ensures that resources in the security group can communicate with one another. |
| 88 |
type: list |
| 89 |
elements: dict |
| 90 |
returned: success |
| 91 |
contains: |
| 92 |
description: |
| 93 |
description: |
| 94 |
- Provides supplementary information about the security group rule. |
| 95 |
type: str |
| 96 |
returned: success |
| 97 |
direction: |
| 98 |
description: |
| 99 |
- Specifies the direction of access control. The value can be egress or ingress. |
| 100 |
type: str |
| 101 |
returned: success |
| 102 |
ethertype: |
| 103 |
description: |
| 104 |
- Specifies the IP protocol version. The value can be IPv4 or IPv6. |
| 105 |
type: str |
| 106 |
returned: success |
| 107 |
id: |
| 108 |
description: |
| 109 |
- Specifies the security group rule ID. |
| 110 |
type: str |
| 111 |
returned: success |
| 112 |
port_range_max: |
| 113 |
description: |
| 114 |
- Specifies the end port number. The value ranges from 1 to 65535. If the protocol is not icmp, the value cannot be |
| 115 |
smaller than the port_range_min value. An empty value indicates all ports. |
| 116 |
type: int |
| 117 |
returned: success |
| 118 |
port_range_min: |
| 119 |
description: |
| 120 |
- Specifies the start port number. The value ranges from 1 to 65535. The value cannot be greater than the port_range_max |
| 121 |
value. An empty value indicates all ports. |
| 122 |
type: int |
| 123 |
returned: success |
| 124 |
protocol: |
| 125 |
description: |
| 126 |
- Specifies the protocol type. The value can be icmp, tcp, udp, or others. If the parameter is left blank, the security |
| 127 |
group supports all protocols. |
| 128 |
type: str |
| 129 |
returned: success |
| 130 |
remote_address_group_id: |
| 131 |
description: |
| 132 |
- Specifies the ID of remote IP address group. |
| 133 |
type: str |
| 134 |
returned: success |
| 135 |
remote_group_id: |
| 136 |
description: |
| 137 |
- Specifies the ID of the peer security group. |
| 138 |
type: str |
| 139 |
returned: success |
| 140 |
remote_ip_prefix: |
| 141 |
description: |
| 142 |
- Specifies the remote IP address. If the access control direction is set to egress, the parameter specifies the source |
| 143 |
IP address. If the access control direction is set to ingress, the parameter specifies the destination IP address. |
| 144 |
type: str |
| 145 |
returned: success |
| 146 |
""" |
| 147 |
|
| 148 |
from ansible_collections.community.general.plugins.module_utils._hwc_utils import ( |
| 149 |
Config, |
| 150 |
HwcClientException, |
| 151 |
HwcModule, |
| 152 |
are_different_dicts, |
| 153 |
build_path, |
| 154 |
get_region, |
| 155 |
is_empty_value, |
| 156 |
navigate_value, |
| 157 |
) |
| 158 |
|
| 159 |
|
| 160 |
def build_module(): |
| 161 |
return HwcModule( |
| 162 |
argument_spec=dict( |
| 163 |
state=dict(default="present", choices=["present", "absent"], type="str"), |
| 164 |
name=dict(type="str", required=True), |
| 165 |
enterprise_project_id=dict(type="str"), |
| 166 |
vpc_id=dict(type="str"), |
| 167 |
), |
| 168 |
supports_check_mode=True, |
| 169 |
) |
| 170 |
|
| 171 |
|
| 172 |
def main(): |
| 173 |
"""Main function""" |
| 174 |
|
| 175 |
module = build_module() |
| 176 |
config = Config(module, "vpc") |
| 177 |
|
| 178 |
try: |
| 179 |
resource = None |
| 180 |
if module.params.get("id"): |
| 181 |
resource = read_resource(config) |
| 182 |
if module.params["state"] == "present": |
| 183 |
check_resource_option(resource, module) |
| 184 |
else: |
| 185 |
v = search_resource(config) |
| 186 |
if len(v) > 1: |
| 187 |
raise Exception(f"Found more than one resource({', '.join([navigate_value(i, ['id']) for i in v])})") |
| 188 |
|
| 189 |
if len(v) == 1: |
| 190 |
resource = update_properties(module, {"read": v[0]}, None) |
| 191 |
module.params["id"] = navigate_value(resource, ["id"]) |
| 192 |
|
| 193 |
result = {} |
| 194 |
changed = False |
| 195 |
if module.params["state"] == "present": |
| 196 |
if resource is None: |
| 197 |
if not module.check_mode: |
| 198 |
resource = create(config) |
| 199 |
changed = True |
| 200 |
|
| 201 |
result = resource |
| 202 |
else: |
| 203 |
if resource: |
| 204 |
if not module.check_mode: |
| 205 |
delete(config) |
| 206 |
changed = True |
| 207 |
|
| 208 |
except Exception as ex: |
| 209 |
module.fail_json(msg=str(ex)) |
| 210 |
|
| 211 |
else: |
| 212 |
result["changed"] = changed |
| 213 |
module.exit_json(**result) |
| 214 |
|
| 215 |
|
| 216 |
def user_input_parameters(module): |
| 217 |
return { |
| 218 |
"enterprise_project_id": module.params.get("enterprise_project_id"), |
| 219 |
"name": module.params.get("name"), |
| 220 |
"vpc_id": module.params.get("vpc_id"), |
| 221 |
"id": module.params.get("id"), |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
def check_resource_option(resource, module): |
| 226 |
opts = user_input_parameters(module) |
| 227 |
|
| 228 |
resource = { |
| 229 |
"enterprise_project_id": resource.get("enterprise_project_id"), |
| 230 |
"name": resource.get("name"), |
| 231 |
"vpc_id": resource.get("vpc_id"), |
| 232 |
"id": resource.get("id"), |
| 233 |
} |
| 234 |
|
| 235 |
if are_different_dicts(resource, opts): |
| 236 |
raise Exception( |
| 237 |
f"Cannot change option from ({resource}) to ({opts}) for an existing security group({module.params['id']})." |
| 238 |
) |
| 239 |
|
| 240 |
|
| 241 |
def create(config): |
| 242 |
module = config.module |
| 243 |
client = config.client(get_region(module), "vpc", "project") |
| 244 |
opts = user_input_parameters(module) |
| 245 |
|
| 246 |
params = build_create_parameters(opts) |
| 247 |
r = send_create_request(module, params, client) |
| 248 |
module.params["id"] = navigate_value(r, ["security_group", "id"]) |
| 249 |
|
| 250 |
result = update_properties(module, {"read": fill_read_resp_body(r)}, None) |
| 251 |
return result |
| 252 |
|
| 253 |
|
| 254 |
def delete(config): |
| 255 |
module = config.module |
| 256 |
client = config.client(get_region(module), "vpc", "project") |
| 257 |
|
| 258 |
send_delete_request(module, None, client) |
| 259 |
|
| 260 |
|
| 261 |
def read_resource(config, exclude_output=False): |
| 262 |
module = config.module |
| 263 |
client = config.client(get_region(module), "vpc", "project") |
| 264 |
|
| 265 |
res = {} |
| 266 |
|
| 267 |
r = send_read_request(module, client) |
| 268 |
res["read"] = fill_read_resp_body(r) |
| 269 |
|
| 270 |
return update_properties(module, res, None, exclude_output) |
| 271 |
|
| 272 |
|
| 273 |
def _build_query_link(opts): |
| 274 |
query_params = [] |
| 275 |
|
| 276 |
v = navigate_value(opts, ["enterprise_project_id"]) |
| 277 |
if v: |
| 278 |
query_params.append(f"enterprise_project_id={v}") |
| 279 |
|
| 280 |
v = navigate_value(opts, ["vpc_id"]) |
| 281 |
if v: |
| 282 |
query_params.append(f"vpc_id={v}") |
| 283 |
|
| 284 |
query_link = "?marker={marker}&limit=10" |
| 285 |
if query_params: |
| 286 |
query_link += f"&{'&'.join(query_params)}" |
| 287 |
|
| 288 |
return query_link |
| 289 |
|
| 290 |
|
| 291 |
def search_resource(config): |
| 292 |
module = config.module |
| 293 |
client = config.client(get_region(module), "vpc", "project") |
| 294 |
opts = user_input_parameters(module) |
| 295 |
identity_obj = _build_identity_object(opts) |
| 296 |
query_link = _build_query_link(opts) |
| 297 |
link = f"security-groups{query_link}" |
| 298 |
|
| 299 |
result = [] |
| 300 |
p = {"marker": ""} |
| 301 |
while True: |
| 302 |
url = link.format(**p) |
| 303 |
r = send_list_request(module, client, url) |
| 304 |
if not r: |
| 305 |
break |
| 306 |
|
| 307 |
for item in r: |
| 308 |
item = fill_list_resp_body(item) |
| 309 |
if not are_different_dicts(identity_obj, item): |
| 310 |
result.append(item) |
| 311 |
|
| 312 |
if len(result) > 1: |
| 313 |
break |
| 314 |
|
| 315 |
p["marker"] = r[-1].get("id") |
| 316 |
|
| 317 |
return result |
| 318 |
|
| 319 |
|
| 320 |
def build_create_parameters(opts): |
| 321 |
params = dict() |
| 322 |
|
| 323 |
v = navigate_value(opts, ["enterprise_project_id"], None) |
| 324 |
if not is_empty_value(v): |
| 325 |
params["enterprise_project_id"] = v |
| 326 |
|
| 327 |
v = navigate_value(opts, ["name"], None) |
| 328 |
if not is_empty_value(v): |
| 329 |
params["name"] = v |
| 330 |
|
| 331 |
v = navigate_value(opts, ["vpc_id"], None) |
| 332 |
if not is_empty_value(v): |
| 333 |
params["vpc_id"] = v |
| 334 |
|
| 335 |
if not params: |
| 336 |
return params |
| 337 |
|
| 338 |
params = {"security_group": params} |
| 339 |
|
| 340 |
return params |
| 341 |
|
| 342 |
|
| 343 |
def send_create_request(module, params, client): |
| 344 |
url = "security-groups" |
| 345 |
try: |
| 346 |
r = client.post(url, params) |
| 347 |
except HwcClientException as ex: |
| 348 |
msg = f"module(hwc_vpc_security_group): error running api(create), error: {ex}" |
| 349 |
module.fail_json(msg=msg) |
| 350 |
|
| 351 |
return r |
| 352 |
|
| 353 |
|
| 354 |
def send_delete_request(module, params, client): |
| 355 |
url = build_path(module, "security-groups/{id}") |
| 356 |
|
| 357 |
try: |
| 358 |
r = client.delete(url, params) |
| 359 |
except HwcClientException as ex: |
| 360 |
msg = f"module(hwc_vpc_security_group): error running api(delete), error: {ex}" |
| 361 |
module.fail_json(msg=msg) |
| 362 |
|
| 363 |
return r |
| 364 |
|
| 365 |
|
| 366 |
def send_read_request(module, client): |
| 367 |
url = build_path(module, "security-groups/{id}") |
| 368 |
|
| 369 |
r = None |
| 370 |
try: |
| 371 |
r = client.get(url) |
| 372 |
except HwcClientException as ex: |
| 373 |
msg = f"module(hwc_vpc_security_group): error running api(read), error: {ex}" |
| 374 |
module.fail_json(msg=msg) |
| 375 |
|
| 376 |
return navigate_value(r, ["security_group"], None) |
| 377 |
|
| 378 |
|
| 379 |
def fill_read_resp_body(body): |
| 380 |
result = dict() |
| 381 |
|
| 382 |
result["enterprise_project_id"] = body.get("enterprise_project_id") |
| 383 |
|
| 384 |
result["id"] = body.get("id") |
| 385 |
|
| 386 |
result["name"] = body.get("name") |
| 387 |
|
| 388 |
v = fill_read_resp_security_group_rules(body.get("security_group_rules")) |
| 389 |
result["security_group_rules"] = v |
| 390 |
|
| 391 |
result["vpc_id"] = body.get("vpc_id") |
| 392 |
|
| 393 |
return result |
| 394 |
|
| 395 |
|
| 396 |
def fill_read_resp_security_group_rules(value): |
| 397 |
if not value: |
| 398 |
return None |
| 399 |
|
| 400 |
result = [] |
| 401 |
for item in value: |
| 402 |
val = dict() |
| 403 |
|
| 404 |
val["description"] = item.get("description") |
| 405 |
|
| 406 |
val["direction"] = item.get("direction") |
| 407 |
|
| 408 |
val["ethertype"] = item.get("ethertype") |
| 409 |
|
| 410 |
val["id"] = item.get("id") |
| 411 |
|
| 412 |
val["port_range_max"] = item.get("port_range_max") |
| 413 |
|
| 414 |
val["port_range_min"] = item.get("port_range_min") |
| 415 |
|
| 416 |
val["protocol"] = item.get("protocol") |
| 417 |
|
| 418 |
val["remote_address_group_id"] = item.get("remote_address_group_id") |
| 419 |
|
| 420 |
val["remote_group_id"] = item.get("remote_group_id") |
| 421 |
|
| 422 |
val["remote_ip_prefix"] = item.get("remote_ip_prefix") |
| 423 |
|
| 424 |
val["security_group_id"] = item.get("security_group_id") |
| 425 |
|
| 426 |
result.append(val) |
| 427 |
|
| 428 |
return result |
| 429 |
|
| 430 |
|
| 431 |
def update_properties(module, response, array_index, exclude_output=False): |
| 432 |
r = user_input_parameters(module) |
| 433 |
|
| 434 |
v = navigate_value(response, ["read", "enterprise_project_id"], array_index) |
| 435 |
r["enterprise_project_id"] = v |
| 436 |
|
| 437 |
v = navigate_value(response, ["read", "name"], array_index) |
| 438 |
r["name"] = v |
| 439 |
|
| 440 |
if not exclude_output: |
| 441 |
v = r.get("rules") |
| 442 |
v = flatten_rules(response, array_index, v, exclude_output) |
| 443 |
r["rules"] = v |
| 444 |
|
| 445 |
v = navigate_value(response, ["read", "vpc_id"], array_index) |
| 446 |
r["vpc_id"] = v |
| 447 |
|
| 448 |
v = navigate_value(response, ["read", "id"], array_index) |
| 449 |
r["id"] = v |
| 450 |
|
| 451 |
return r |
| 452 |
|
| 453 |
|
| 454 |
def flatten_rules(d, array_index, current_value, exclude_output): |
| 455 |
n = 0 |
| 456 |
result = current_value |
| 457 |
has_init_value = True |
| 458 |
if result: |
| 459 |
n = len(result) |
| 460 |
else: |
| 461 |
has_init_value = False |
| 462 |
result = [] |
| 463 |
v = navigate_value(d, ["read", "security_group_rules"], array_index) |
| 464 |
if not v: |
| 465 |
return current_value |
| 466 |
n = len(v) |
| 467 |
|
| 468 |
new_array_index = dict() |
| 469 |
if array_index: |
| 470 |
new_array_index.update(array_index) |
| 471 |
|
| 472 |
for i in range(n): |
| 473 |
new_array_index["read.security_group_rules"] = i |
| 474 |
|
| 475 |
val = dict() |
| 476 |
if len(result) >= (i + 1) and result[i]: |
| 477 |
val = result[i] |
| 478 |
|
| 479 |
if not exclude_output: |
| 480 |
v = navigate_value(d, ["read", "security_group_rules", "description"], new_array_index) |
| 481 |
val["description"] = v |
| 482 |
|
| 483 |
if not exclude_output: |
| 484 |
v = navigate_value(d, ["read", "security_group_rules", "direction"], new_array_index) |
| 485 |
val["direction"] = v |
| 486 |
|
| 487 |
if not exclude_output: |
| 488 |
v = navigate_value(d, ["read", "security_group_rules", "ethertype"], new_array_index) |
| 489 |
val["ethertype"] = v |
| 490 |
|
| 491 |
if not exclude_output: |
| 492 |
v = navigate_value(d, ["read", "security_group_rules", "id"], new_array_index) |
| 493 |
val["id"] = v |
| 494 |
|
| 495 |
if not exclude_output: |
| 496 |
v = navigate_value(d, ["read", "security_group_rules", "port_range_max"], new_array_index) |
| 497 |
val["port_range_max"] = v |
| 498 |
|
| 499 |
if not exclude_output: |
| 500 |
v = navigate_value(d, ["read", "security_group_rules", "port_range_min"], new_array_index) |
| 501 |
val["port_range_min"] = v |
| 502 |
|
| 503 |
if not exclude_output: |
| 504 |
v = navigate_value(d, ["read", "security_group_rules", "protocol"], new_array_index) |
| 505 |
val["protocol"] = v |
| 506 |
|
| 507 |
if not exclude_output: |
| 508 |
v = navigate_value(d, ["read", "security_group_rules", "remote_address_group_id"], new_array_index) |
| 509 |
val["remote_address_group_id"] = v |
| 510 |
|
| 511 |
if not exclude_output: |
| 512 |
v = navigate_value(d, ["read", "security_group_rules", "remote_group_id"], new_array_index) |
| 513 |
val["remote_group_id"] = v |
| 514 |
|
| 515 |
if not exclude_output: |
| 516 |
v = navigate_value(d, ["read", "security_group_rules", "remote_ip_prefix"], new_array_index) |
| 517 |
val["remote_ip_prefix"] = v |
| 518 |
|
| 519 |
if len(result) >= (i + 1): |
| 520 |
result[i] = val |
| 521 |
else: |
| 522 |
for v in val.values(): |
| 523 |
if v is not None: |
| 524 |
result.append(val) |
| 525 |
break |
| 526 |
|
| 527 |
return result if (has_init_value or result) else current_value |
| 528 |
|
| 529 |
|
| 530 |
def send_list_request(module, client, url): |
| 531 |
r = None |
| 532 |
try: |
| 533 |
r = client.get(url) |
| 534 |
except HwcClientException as ex: |
| 535 |
msg = f"module(hwc_vpc_security_group): error running api(list), error: {ex}" |
| 536 |
module.fail_json(msg=msg) |
| 537 |
|
| 538 |
return navigate_value(r, ["security_groups"], None) |
| 539 |
|
| 540 |
|
| 541 |
def _build_identity_object(all_opts): |
| 542 |
result = dict() |
| 543 |
|
| 544 |
v = navigate_value(all_opts, ["enterprise_project_id"], None) |
| 545 |
result["enterprise_project_id"] = v |
| 546 |
|
| 547 |
result["id"] = None |
| 548 |
|
| 549 |
v = navigate_value(all_opts, ["name"], None) |
| 550 |
result["name"] = v |
| 551 |
|
| 552 |
result["security_group_rules"] = None |
| 553 |
|
| 554 |
v = navigate_value(all_opts, ["vpc_id"], None) |
| 555 |
result["vpc_id"] = v |
| 556 |
|
| 557 |
return result |
| 558 |
|
| 559 |
|
| 560 |
def fill_list_resp_body(body): |
| 561 |
result = dict() |
| 562 |
|
| 563 |
result["enterprise_project_id"] = body.get("enterprise_project_id") |
| 564 |
|
| 565 |
result["id"] = body.get("id") |
| 566 |
|
| 567 |
result["name"] = body.get("name") |
| 568 |
|
| 569 |
v = fill_list_resp_security_group_rules(body.get("security_group_rules")) |
| 570 |
result["security_group_rules"] = v |
| 571 |
|
| 572 |
result["vpc_id"] = body.get("vpc_id") |
| 573 |
|
| 574 |
return result |
| 575 |
|
| 576 |
|
| 577 |
def fill_list_resp_security_group_rules(value): |
| 578 |
if not value: |
| 579 |
return None |
| 580 |
|
| 581 |
result = [] |
| 582 |
for item in value: |
| 583 |
val = dict() |
| 584 |
|
| 585 |
val["description"] = item.get("description") |
| 586 |
|
| 587 |
val["direction"] = item.get("direction") |
| 588 |
|
| 589 |
val["ethertype"] = item.get("ethertype") |
| 590 |
|
| 591 |
val["id"] = item.get("id") |
| 592 |
|
| 593 |
val["port_range_max"] = item.get("port_range_max") |
| 594 |
|
| 595 |
val["port_range_min"] = item.get("port_range_min") |
| 596 |
|
| 597 |
val["protocol"] = item.get("protocol") |
| 598 |
|
| 599 |
val["remote_address_group_id"] = item.get("remote_address_group_id") |
| 600 |
|
| 601 |
val["remote_group_id"] = item.get("remote_group_id") |
| 602 |
|
| 603 |
val["remote_ip_prefix"] = item.get("remote_ip_prefix") |
| 604 |
|
| 605 |
val["security_group_id"] = item.get("security_group_id") |
| 606 |
|
| 607 |
result.append(val) |
| 608 |
|
| 609 |
return result |
| 610 |
|
| 611 |
|
| 612 |
if __name__ == "__main__": |
| 613 |
main() |