| 1 |
#!/usr/bin/python |
| 2 |
# Copyright (c) 2017, Milan Ilic <[email protected]> |
| 3 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 4 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 5 |
|
| 6 |
# Make coding more python3-ish |
| 7 |
from __future__ import annotations |
| 8 |
|
| 9 |
DOCUMENTATION = r""" |
| 10 |
module: one_service |
| 11 |
short_description: Deploy and manage OpenNebula services |
| 12 |
description: |
| 13 |
- Manage OpenNebula services. |
| 14 |
extends_documentation_fragment: |
| 15 |
- community.general._attributes |
| 16 |
attributes: |
| 17 |
check_mode: |
| 18 |
support: full |
| 19 |
diff_mode: |
| 20 |
support: none |
| 21 |
options: |
| 22 |
api_url: |
| 23 |
description: |
| 24 |
- URL of the OpenNebula OneFlow API server. |
| 25 |
- It is recommended to use HTTPS so that the username/password are not transferred over the network unencrypted. |
| 26 |
- If not set then the value of the E(ONEFLOW_URL) environment variable is used. |
| 27 |
type: str |
| 28 |
api_username: |
| 29 |
description: |
| 30 |
- Name of the user to login into the OpenNebula OneFlow API server. If not set then the value of the E(ONEFLOW_USERNAME) |
| 31 |
environment variable is used. |
| 32 |
type: str |
| 33 |
api_password: |
| 34 |
description: |
| 35 |
- Password of the user to login into OpenNebula OneFlow API server. If not set then the value of the E(ONEFLOW_PASSWORD) |
| 36 |
environment variable is used. |
| 37 |
type: str |
| 38 |
template_name: |
| 39 |
description: |
| 40 |
- Name of service template to use to create a new instance of a service. |
| 41 |
type: str |
| 42 |
template_id: |
| 43 |
description: |
| 44 |
- ID of a service template to use to create a new instance of a service. |
| 45 |
type: int |
| 46 |
service_id: |
| 47 |
description: |
| 48 |
- ID of a service instance that you would like to manage. |
| 49 |
type: int |
| 50 |
service_name: |
| 51 |
description: |
| 52 |
- Name of a service instance that you would like to manage. |
| 53 |
type: str |
| 54 |
unique: |
| 55 |
description: |
| 56 |
- Setting O(unique=true) ensures that there is only one service instance running with a name set with O(service_name) |
| 57 |
when instantiating a service from a template specified with O(template_id) or O(template_name). Check examples below. |
| 58 |
type: bool |
| 59 |
default: false |
| 60 |
state: |
| 61 |
description: |
| 62 |
- V(present) - instantiate a service from a template specified with O(template_id) or O(template_name). |
| 63 |
- V(absent) - terminate an instance of a service specified with O(template_id) or O(template_name). |
| 64 |
choices: ["present", "absent"] |
| 65 |
default: present |
| 66 |
type: str |
| 67 |
mode: |
| 68 |
description: |
| 69 |
- Set permission mode of a service instance in octet format, for example V(0600) to give owner C(use) and C(manage) |
| 70 |
and nothing to group and others. |
| 71 |
type: str |
| 72 |
owner_id: |
| 73 |
description: |
| 74 |
- ID of the user which is set as the owner of the service. |
| 75 |
type: int |
| 76 |
group_id: |
| 77 |
description: |
| 78 |
- ID of the group which is set as the group of the service. |
| 79 |
type: int |
| 80 |
wait: |
| 81 |
description: |
| 82 |
- Wait for the instance to reach RUNNING state after DEPLOYING or COOLDOWN state after SCALING. |
| 83 |
type: bool |
| 84 |
default: false |
| 85 |
wait_timeout: |
| 86 |
description: |
| 87 |
- How long before wait gives up, in seconds. |
| 88 |
default: 300 |
| 89 |
type: int |
| 90 |
custom_attrs: |
| 91 |
description: |
| 92 |
- Dictionary of key/value custom attributes which is used when instantiating a new service. |
| 93 |
default: {} |
| 94 |
type: dict |
| 95 |
role: |
| 96 |
description: |
| 97 |
- Name of the role whose cardinality should be changed. |
| 98 |
type: str |
| 99 |
cardinality: |
| 100 |
description: |
| 101 |
- Number of VMs for the specified role. |
| 102 |
type: int |
| 103 |
force: |
| 104 |
description: |
| 105 |
- Force the new cardinality even if it is outside the limits. |
| 106 |
type: bool |
| 107 |
default: false |
| 108 |
author: |
| 109 |
- "Milan Ilic (@ilicmilan)" |
| 110 |
""" |
| 111 |
|
| 112 |
EXAMPLES = r""" |
| 113 |
- name: Instantiate a new service |
| 114 |
community.general.one_service: |
| 115 |
template_id: 90 |
| 116 |
register: result |
| 117 |
|
| 118 |
- name: Print service properties |
| 119 |
ansible.builtin.debug: |
| 120 |
msg: result |
| 121 |
|
| 122 |
- name: Instantiate a new service with specified service_name, service group and mode |
| 123 |
community.general.one_service: |
| 124 |
template_name: 'app1_template' |
| 125 |
service_name: 'app1' |
| 126 |
group_id: 1 |
| 127 |
mode: '660' |
| 128 |
|
| 129 |
- name: Instantiate a new service with template_id and pass custom_attrs dict |
| 130 |
community.general.one_service: |
| 131 |
template_id: 90 |
| 132 |
custom_attrs: |
| 133 |
public_network_id: 21 |
| 134 |
private_network_id: 26 |
| 135 |
|
| 136 |
- name: Instantiate a new service 'foo' if the service doesn't already exist, otherwise do nothing |
| 137 |
community.general.one_service: |
| 138 |
template_id: 53 |
| 139 |
service_name: 'foo' |
| 140 |
unique: true |
| 141 |
|
| 142 |
- name: Delete a service by ID |
| 143 |
community.general.one_service: |
| 144 |
service_id: 153 |
| 145 |
state: absent |
| 146 |
|
| 147 |
- name: Get service info |
| 148 |
community.general.one_service: |
| 149 |
service_id: 153 |
| 150 |
register: service_info |
| 151 |
|
| 152 |
- name: Change service owner, group and mode |
| 153 |
community.general.one_service: |
| 154 |
service_name: 'app2' |
| 155 |
owner_id: 34 |
| 156 |
group_id: 113 |
| 157 |
mode: '600' |
| 158 |
|
| 159 |
- name: Instantiate service and wait for it to become RUNNING |
| 160 |
community.general.one_service: |
| 161 |
template_id: 43 |
| 162 |
service_name: 'foo1' |
| 163 |
|
| 164 |
- name: Wait service to become RUNNING |
| 165 |
community.general.one_service: |
| 166 |
service_id: 112 |
| 167 |
wait: true |
| 168 |
|
| 169 |
- name: Change role cardinality |
| 170 |
community.general.one_service: |
| 171 |
service_id: 153 |
| 172 |
role: bar |
| 173 |
cardinality: 5 |
| 174 |
|
| 175 |
- name: Change role cardinality and wait for it to be applied |
| 176 |
community.general.one_service: |
| 177 |
service_id: 112 |
| 178 |
role: foo |
| 179 |
cardinality: 7 |
| 180 |
wait: true |
| 181 |
""" |
| 182 |
|
| 183 |
RETURN = r""" |
| 184 |
service_id: |
| 185 |
description: Service ID. |
| 186 |
type: int |
| 187 |
returned: success |
| 188 |
sample: 153 |
| 189 |
service_name: |
| 190 |
description: Service name. |
| 191 |
type: str |
| 192 |
returned: success |
| 193 |
sample: app1 |
| 194 |
group_id: |
| 195 |
description: Service's group ID. |
| 196 |
type: int |
| 197 |
returned: success |
| 198 |
sample: 1 |
| 199 |
group_name: |
| 200 |
description: Service's group name. |
| 201 |
type: str |
| 202 |
returned: success |
| 203 |
sample: one-users |
| 204 |
owner_id: |
| 205 |
description: Service's owner ID. |
| 206 |
type: int |
| 207 |
returned: success |
| 208 |
sample: 143 |
| 209 |
owner_name: |
| 210 |
description: Service's owner name. |
| 211 |
type: str |
| 212 |
returned: success |
| 213 |
sample: ansible-test |
| 214 |
state: |
| 215 |
description: State of service instance. |
| 216 |
type: str |
| 217 |
returned: success |
| 218 |
sample: RUNNING |
| 219 |
mode: |
| 220 |
description: Service's mode. |
| 221 |
type: int |
| 222 |
returned: success |
| 223 |
sample: 660 |
| 224 |
roles: |
| 225 |
description: List of dictionaries of roles, each role is described by name, cardinality, state and nodes IDs. |
| 226 |
type: list |
| 227 |
returned: success |
| 228 |
sample: |
| 229 |
- {"cardinality": 1, "name": "foo", "state": "RUNNING", "ids": [123, 456]} |
| 230 |
- {"cardinality": 2, "name": "bar", "state": "RUNNING", "ids": [452, 567, 746]} |
| 231 |
""" |
| 232 |
|
| 233 |
import os |
| 234 |
import time |
| 235 |
from dataclasses import dataclass |
| 236 |
|
| 237 |
from ansible.module_utils.basic import AnsibleModule |
| 238 |
from ansible.module_utils.urls import open_url |
| 239 |
|
| 240 |
|
| 241 |
@dataclass |
| 242 |
class AuthParams: |
| 243 |
url: str |
| 244 |
user: str |
| 245 |
password: str |
| 246 |
|
| 247 |
|
| 248 |
STATES = ( |
| 249 |
"PENDING", |
| 250 |
"DEPLOYING", |
| 251 |
"RUNNING", |
| 252 |
"UNDEPLOYING", |
| 253 |
"WARNING", |
| 254 |
"DONE", |
| 255 |
"FAILED_UNDEPLOYING", |
| 256 |
"FAILED_DEPLOYING", |
| 257 |
"SCALING", |
| 258 |
"FAILED_SCALING", |
| 259 |
"COOLDOWN", |
| 260 |
) |
| 261 |
|
| 262 |
|
| 263 |
def get_all_templates(module, auth): |
| 264 |
try: |
| 265 |
all_templates = open_url( |
| 266 |
url=f"{auth.url}/service_template", |
| 267 |
method="GET", |
| 268 |
force_basic_auth=True, |
| 269 |
url_username=auth.user, |
| 270 |
url_password=auth.password, |
| 271 |
) |
| 272 |
except Exception as e: |
| 273 |
module.fail_json(msg=str(e)) |
| 274 |
|
| 275 |
return module.from_json(all_templates.read()) |
| 276 |
|
| 277 |
|
| 278 |
def get_template(module, auth, pred): |
| 279 |
all_templates_dict = get_all_templates(module, auth) |
| 280 |
|
| 281 |
found = 0 |
| 282 |
found_template = None |
| 283 |
template_name = "" |
| 284 |
|
| 285 |
if "DOCUMENT_POOL" in all_templates_dict and "DOCUMENT" in all_templates_dict["DOCUMENT_POOL"]: |
| 286 |
for template in all_templates_dict["DOCUMENT_POOL"]["DOCUMENT"]: |
| 287 |
if pred(template): |
| 288 |
found = found + 1 |
| 289 |
found_template = template |
| 290 |
template_name = template["NAME"] |
| 291 |
|
| 292 |
if found <= 0: |
| 293 |
return None |
| 294 |
elif found > 1: |
| 295 |
module.fail_json(msg=f"There is no template with unique name: {template_name}") |
| 296 |
else: |
| 297 |
return found_template |
| 298 |
|
| 299 |
|
| 300 |
def get_all_services(module, auth): |
| 301 |
try: |
| 302 |
response = open_url( |
| 303 |
f"{auth.url}/service", |
| 304 |
method="GET", |
| 305 |
force_basic_auth=True, |
| 306 |
url_username=auth.user, |
| 307 |
url_password=auth.password, |
| 308 |
) |
| 309 |
except Exception as e: |
| 310 |
module.fail_json(msg=str(e)) |
| 311 |
|
| 312 |
return module.from_json(response.read()) |
| 313 |
|
| 314 |
|
| 315 |
def get_service(module, auth, pred): |
| 316 |
all_services_dict = get_all_services(module, auth) |
| 317 |
|
| 318 |
found = 0 |
| 319 |
found_service = None |
| 320 |
service_name = "" |
| 321 |
|
| 322 |
if "DOCUMENT_POOL" in all_services_dict and "DOCUMENT" in all_services_dict["DOCUMENT_POOL"]: |
| 323 |
for service in all_services_dict["DOCUMENT_POOL"]["DOCUMENT"]: |
| 324 |
if pred(service): |
| 325 |
found = found + 1 |
| 326 |
found_service = service |
| 327 |
service_name = service["NAME"] |
| 328 |
|
| 329 |
# fail if there are more services with same name |
| 330 |
if found > 1: |
| 331 |
module.fail_json( |
| 332 |
msg=f"There are multiple services with a name: '{service_name}'. You have to use a unique service name or use 'service_id' instead." |
| 333 |
) |
| 334 |
elif found <= 0: |
| 335 |
return None |
| 336 |
else: |
| 337 |
return found_service |
| 338 |
|
| 339 |
|
| 340 |
def get_service_by_id(module, auth, service_id): |
| 341 |
return get_service(module, auth, lambda service: int(service["ID"]) == int(service_id)) if service_id else None |
| 342 |
|
| 343 |
|
| 344 |
def get_service_by_name(module, auth, service_name): |
| 345 |
return get_service(module, auth, lambda service: service["NAME"] == service_name) |
| 346 |
|
| 347 |
|
| 348 |
def get_service_info(module, auth, service): |
| 349 |
result = { |
| 350 |
"service_id": int(service["ID"]), |
| 351 |
"service_name": service["NAME"], |
| 352 |
"group_id": int(service["GID"]), |
| 353 |
"group_name": service["GNAME"], |
| 354 |
"owner_id": int(service["UID"]), |
| 355 |
"owner_name": service["UNAME"], |
| 356 |
"state": STATES[service["TEMPLATE"]["BODY"]["state"]], |
| 357 |
} |
| 358 |
|
| 359 |
roles_status = service["TEMPLATE"]["BODY"]["roles"] |
| 360 |
roles = [] |
| 361 |
for role in roles_status: |
| 362 |
nodes_ids = [] |
| 363 |
if "nodes" in role: |
| 364 |
for node in role["nodes"]: |
| 365 |
nodes_ids.append(node["deploy_id"]) |
| 366 |
roles.append( |
| 367 |
{ |
| 368 |
"name": role["name"], |
| 369 |
"cardinality": role["cardinality"], |
| 370 |
"state": STATES[int(role["state"])], |
| 371 |
"ids": nodes_ids, |
| 372 |
} |
| 373 |
) |
| 374 |
|
| 375 |
result["roles"] = roles |
| 376 |
result["mode"] = int(parse_service_permissions(service)) |
| 377 |
|
| 378 |
return result |
| 379 |
|
| 380 |
|
| 381 |
def create_service(module, auth, template_id, service_name, custom_attrs, unique, wait, wait_timeout): |
| 382 |
# make sure that the values in custom_attrs dict are strings |
| 383 |
custom_attrs_with_str = {k: str(v) for k, v in custom_attrs.items()} |
| 384 |
|
| 385 |
data = { |
| 386 |
"action": { |
| 387 |
"perform": "instantiate", |
| 388 |
"params": {"merge_template": {"custom_attrs_values": custom_attrs_with_str, "name": service_name}}, |
| 389 |
} |
| 390 |
} |
| 391 |
|
| 392 |
try: |
| 393 |
response = open_url( |
| 394 |
f"{auth.url}/service_template/{template_id!s}/action", |
| 395 |
method="POST", |
| 396 |
data=module.jsonify(data), |
| 397 |
force_basic_auth=True, |
| 398 |
url_username=auth.user, |
| 399 |
url_password=auth.password, |
| 400 |
) |
| 401 |
except Exception as e: |
| 402 |
module.fail_json(msg=str(e)) |
| 403 |
|
| 404 |
service_result = module.from_json(response.read())["DOCUMENT"] |
| 405 |
|
| 406 |
return service_result |
| 407 |
|
| 408 |
|
| 409 |
def wait_for_service_to_become_ready(module, auth, service_id, wait_timeout): |
| 410 |
start_time = time.time() |
| 411 |
|
| 412 |
while (time.time() - start_time) < wait_timeout: |
| 413 |
try: |
| 414 |
status_result = open_url( |
| 415 |
f"{auth.url}/service/{service_id!s}", |
| 416 |
method="GET", |
| 417 |
force_basic_auth=True, |
| 418 |
url_username=auth.user, |
| 419 |
url_password=auth.password, |
| 420 |
) |
| 421 |
except Exception as e: |
| 422 |
module.fail_json(msg=f"Request for service status has failed. Error message: {e!s}") |
| 423 |
|
| 424 |
status_result = module.from_json(status_result.read()) |
| 425 |
service_state = status_result["DOCUMENT"]["TEMPLATE"]["BODY"]["state"] |
| 426 |
|
| 427 |
if service_state in [STATES.index("RUNNING"), STATES.index("COOLDOWN")]: |
| 428 |
return status_result["DOCUMENT"] |
| 429 |
elif service_state not in [STATES.index("PENDING"), STATES.index("DEPLOYING"), STATES.index("SCALING")]: |
| 430 |
log_message = "" |
| 431 |
for log_info in status_result["DOCUMENT"]["TEMPLATE"]["BODY"]["log"]: |
| 432 |
if log_info["severity"] == "E": |
| 433 |
log_message = log_message + log_info["message"] |
| 434 |
break |
| 435 |
|
| 436 |
module.fail_json( |
| 437 |
msg=f"Deploying is unsuccessful. Service state: {STATES[service_state]}. Error message: {log_message}" |
| 438 |
) |
| 439 |
|
| 440 |
time.sleep(1) |
| 441 |
|
| 442 |
module.fail_json(msg="Wait timeout has expired") |
| 443 |
|
| 444 |
|
| 445 |
def change_service_permissions(module, auth, service_id, permissions): |
| 446 |
data = {"action": {"perform": "chmod", "params": {"octet": permissions}}} |
| 447 |
|
| 448 |
try: |
| 449 |
open_url( |
| 450 |
f"{auth.url}/service/{service_id!s}/action", |
| 451 |
method="POST", |
| 452 |
force_basic_auth=True, |
| 453 |
url_username=auth.user, |
| 454 |
url_password=auth.password, |
| 455 |
data=module.jsonify(data), |
| 456 |
) |
| 457 |
except Exception as e: |
| 458 |
module.fail_json(msg=str(e)) |
| 459 |
|
| 460 |
|
| 461 |
def change_service_owner(module, auth, service_id, owner_id): |
| 462 |
data = {"action": {"perform": "chown", "params": {"owner_id": owner_id}}} |
| 463 |
|
| 464 |
try: |
| 465 |
open_url( |
| 466 |
f"{auth.url}/service/{service_id!s}/action", |
| 467 |
method="POST", |
| 468 |
force_basic_auth=True, |
| 469 |
url_username=auth.user, |
| 470 |
url_password=auth.password, |
| 471 |
data=module.jsonify(data), |
| 472 |
) |
| 473 |
except Exception as e: |
| 474 |
module.fail_json(msg=str(e)) |
| 475 |
|
| 476 |
|
| 477 |
def change_service_group(module, auth, service_id, group_id): |
| 478 |
data = {"action": {"perform": "chgrp", "params": {"group_id": group_id}}} |
| 479 |
|
| 480 |
try: |
| 481 |
open_url( |
| 482 |
f"{auth.url}/service/{service_id!s}/action", |
| 483 |
method="POST", |
| 484 |
force_basic_auth=True, |
| 485 |
url_username=auth.user, |
| 486 |
url_password=auth.password, |
| 487 |
data=module.jsonify(data), |
| 488 |
) |
| 489 |
except Exception as e: |
| 490 |
module.fail_json(msg=str(e)) |
| 491 |
|
| 492 |
|
| 493 |
def change_role_cardinality(module, auth, service_id, role, cardinality, force): |
| 494 |
data = {"cardinality": cardinality, "force": force} |
| 495 |
|
| 496 |
try: |
| 497 |
status_result = open_url( |
| 498 |
f"{auth.url}/service/{service_id!s}/role/{role}", |
| 499 |
method="PUT", |
| 500 |
force_basic_auth=True, |
| 501 |
url_username=auth.user, |
| 502 |
url_password=auth.password, |
| 503 |
data=module.jsonify(data), |
| 504 |
) |
| 505 |
except Exception as e: |
| 506 |
module.fail_json(msg=str(e)) |
| 507 |
|
| 508 |
if status_result.getcode() != 204: |
| 509 |
module.fail_json(msg=f"Failed to change cardinality for role: {role}. Return code: {status_result.getcode()!s}") |
| 510 |
|
| 511 |
|
| 512 |
def check_change_service_owner(module, service, owner_id): |
| 513 |
old_owner_id = int(service["UID"]) |
| 514 |
|
| 515 |
return old_owner_id != owner_id |
| 516 |
|
| 517 |
|
| 518 |
def check_change_service_group(module, service, group_id): |
| 519 |
old_group_id = int(service["GID"]) |
| 520 |
|
| 521 |
return old_group_id != group_id |
| 522 |
|
| 523 |
|
| 524 |
def parse_service_permissions(service): |
| 525 |
perm_dict = service["PERMISSIONS"] |
| 526 |
""" |
| 527 |
This is the structure of the 'PERMISSIONS' dictionary: |
| 528 |
|
| 529 |
"PERMISSIONS": { |
| 530 |
"OWNER_U": "1", |
| 531 |
"OWNER_M": "1", |
| 532 |
"OWNER_A": "0", |
| 533 |
"GROUP_U": "0", |
| 534 |
"GROUP_M": "0", |
| 535 |
"GROUP_A": "0", |
| 536 |
"OTHER_U": "0", |
| 537 |
"OTHER_M": "0", |
| 538 |
"OTHER_A": "0" |
| 539 |
} |
| 540 |
""" |
| 541 |
|
| 542 |
owner_octal = int(perm_dict["OWNER_U"]) * 4 + int(perm_dict["OWNER_M"]) * 2 + int(perm_dict["OWNER_A"]) |
| 543 |
group_octal = int(perm_dict["GROUP_U"]) * 4 + int(perm_dict["GROUP_M"]) * 2 + int(perm_dict["GROUP_A"]) |
| 544 |
other_octal = int(perm_dict["OTHER_U"]) * 4 + int(perm_dict["OTHER_M"]) * 2 + int(perm_dict["OTHER_A"]) |
| 545 |
|
| 546 |
permissions = str(owner_octal) + str(group_octal) + str(other_octal) |
| 547 |
|
| 548 |
return permissions |
| 549 |
|
| 550 |
|
| 551 |
def check_change_service_permissions(module, service, permissions): |
| 552 |
old_permissions = parse_service_permissions(service) |
| 553 |
|
| 554 |
return old_permissions != permissions |
| 555 |
|
| 556 |
|
| 557 |
def check_change_role_cardinality(module, service, role_name, cardinality): |
| 558 |
roles_list = service["TEMPLATE"]["BODY"]["roles"] |
| 559 |
|
| 560 |
for role in roles_list: |
| 561 |
if role["name"] == role_name: |
| 562 |
return int(role["cardinality"]) != cardinality |
| 563 |
|
| 564 |
module.fail_json(msg=f"There is no role with name: {role_name}") |
| 565 |
|
| 566 |
|
| 567 |
def create_service_and_operation( |
| 568 |
module, auth, template_id, service_name, owner_id, group_id, permissions, custom_attrs, unique, wait, wait_timeout |
| 569 |
): |
| 570 |
if not service_name: |
| 571 |
service_name = "" |
| 572 |
changed = False |
| 573 |
service = None |
| 574 |
|
| 575 |
if unique: |
| 576 |
service = get_service_by_name(module, auth, service_name) |
| 577 |
|
| 578 |
if not service or service["TEMPLATE"]["BODY"]["state"] == "DONE": |
| 579 |
if not module.check_mode: |
| 580 |
service = create_service(module, auth, template_id, service_name, custom_attrs, unique, wait, wait_timeout) |
| 581 |
changed = True |
| 582 |
|
| 583 |
# if check_mode=true and there would be changes, service doesn't exist and we can not get it |
| 584 |
if module.check_mode and changed: |
| 585 |
return {"changed": True} |
| 586 |
|
| 587 |
result = service_operation( |
| 588 |
module, |
| 589 |
auth, |
| 590 |
owner_id=owner_id, |
| 591 |
group_id=group_id, |
| 592 |
wait=wait, |
| 593 |
wait_timeout=wait_timeout, |
| 594 |
permissions=permissions, |
| 595 |
service=service, |
| 596 |
) |
| 597 |
|
| 598 |
if result["changed"]: |
| 599 |
changed = True |
| 600 |
|
| 601 |
result["changed"] = changed |
| 602 |
|
| 603 |
return result |
| 604 |
|
| 605 |
|
| 606 |
def service_operation( |
| 607 |
module, |
| 608 |
auth, |
| 609 |
service_id=None, |
| 610 |
owner_id=None, |
| 611 |
group_id=None, |
| 612 |
permissions=None, |
| 613 |
role=None, |
| 614 |
cardinality=None, |
| 615 |
force=None, |
| 616 |
wait=False, |
| 617 |
wait_timeout=None, |
| 618 |
service=None, |
| 619 |
): |
| 620 |
changed = False |
| 621 |
|
| 622 |
if not service: |
| 623 |
service = get_service_by_id(module, auth, service_id) |
| 624 |
else: |
| 625 |
service_id = service["ID"] |
| 626 |
|
| 627 |
if not service: |
| 628 |
module.fail_json(msg=f"There is no service with id: {service_id!s}") |
| 629 |
|
| 630 |
if owner_id: |
| 631 |
if check_change_service_owner(module, service, owner_id): |
| 632 |
if not module.check_mode: |
| 633 |
change_service_owner(module, auth, service_id, owner_id) |
| 634 |
changed = True |
| 635 |
if group_id: |
| 636 |
if check_change_service_group(module, service, group_id): |
| 637 |
if not module.check_mode: |
| 638 |
change_service_group(module, auth, service_id, group_id) |
| 639 |
changed = True |
| 640 |
if permissions: |
| 641 |
if check_change_service_permissions(module, service, permissions): |
| 642 |
if not module.check_mode: |
| 643 |
change_service_permissions(module, auth, service_id, permissions) |
| 644 |
changed = True |
| 645 |
|
| 646 |
if role: |
| 647 |
if check_change_role_cardinality(module, service, role, cardinality): |
| 648 |
if not module.check_mode: |
| 649 |
change_role_cardinality(module, auth, service_id, role, cardinality, force) |
| 650 |
changed = True |
| 651 |
|
| 652 |
if wait and not module.check_mode: |
| 653 |
service = wait_for_service_to_become_ready(module, auth, service_id, wait_timeout) |
| 654 |
|
| 655 |
# if something has changed, fetch service info again |
| 656 |
if changed: |
| 657 |
service = get_service_by_id(module, auth, service_id) |
| 658 |
|
| 659 |
service_info = get_service_info(module, auth, service) |
| 660 |
service_info["changed"] = changed |
| 661 |
|
| 662 |
return service_info |
| 663 |
|
| 664 |
|
| 665 |
def delete_service(module, auth, service_id): |
| 666 |
service = get_service_by_id(module, auth, service_id) |
| 667 |
if not service: |
| 668 |
return {"changed": False} |
| 669 |
|
| 670 |
service_info = get_service_info(module, auth, service) |
| 671 |
|
| 672 |
service_info["changed"] = True |
| 673 |
|
| 674 |
if module.check_mode: |
| 675 |
return service_info |
| 676 |
|
| 677 |
try: |
| 678 |
open_url( |
| 679 |
f"{auth.url}/service/{service_id!s}", |
| 680 |
method="DELETE", |
| 681 |
force_basic_auth=True, |
| 682 |
url_username=auth.user, |
| 683 |
url_password=auth.password, |
| 684 |
) |
| 685 |
except Exception as e: |
| 686 |
module.fail_json(msg=f"Service deletion has failed. Error message: {e}") |
| 687 |
|
| 688 |
return service_info |
| 689 |
|
| 690 |
|
| 691 |
def get_template_by_name(module, auth, template_name): |
| 692 |
return get_template(module, auth, lambda template: template["NAME"] == template_name) |
| 693 |
|
| 694 |
|
| 695 |
def get_template_by_id(module, auth, template_id): |
| 696 |
return get_template(module, auth, lambda template: int(template["ID"]) == int(template_id)) if template_id else None |
| 697 |
|
| 698 |
|
| 699 |
def get_template_id(module, auth, requested_id, requested_name): |
| 700 |
template = ( |
| 701 |
get_template_by_id(module, auth, requested_id) |
| 702 |
if requested_id |
| 703 |
else get_template_by_name(module, auth, requested_name) |
| 704 |
) |
| 705 |
|
| 706 |
if template: |
| 707 |
return template["ID"] |
| 708 |
|
| 709 |
return None |
| 710 |
|
| 711 |
|
| 712 |
def get_service_id_by_name(module, auth, service_name): |
| 713 |
service = get_service_by_name(module, auth, service_name) |
| 714 |
|
| 715 |
if service: |
| 716 |
return service["ID"] |
| 717 |
|
| 718 |
return None |
| 719 |
|
| 720 |
|
| 721 |
def get_connection_info(module): |
| 722 |
url = module.params.get("api_url") |
| 723 |
username = module.params.get("api_username") |
| 724 |
password = module.params.get("api_password") |
| 725 |
|
| 726 |
if not url: |
| 727 |
url = os.environ.get("ONEFLOW_URL") |
| 728 |
|
| 729 |
if not username: |
| 730 |
username = os.environ.get("ONEFLOW_USERNAME") |
| 731 |
|
| 732 |
if not password: |
| 733 |
password = os.environ.get("ONEFLOW_PASSWORD") |
| 734 |
|
| 735 |
if not (url and username and password): |
| 736 |
module.fail_json( |
| 737 |
msg="One or more connection parameters (api_url, api_username, api_password) were not specified" |
| 738 |
) |
| 739 |
return AuthParams(url=url, user=username, password=password) |
| 740 |
|
| 741 |
|
| 742 |
def main(): |
| 743 |
fields = { |
| 744 |
"api_url": {"required": False, "type": "str"}, |
| 745 |
"api_username": {"required": False, "type": "str"}, |
| 746 |
"api_password": {"required": False, "type": "str", "no_log": True}, |
| 747 |
"service_name": {"required": False, "type": "str"}, |
| 748 |
"service_id": {"required": False, "type": "int"}, |
| 749 |
"template_name": {"required": False, "type": "str"}, |
| 750 |
"template_id": {"required": False, "type": "int"}, |
| 751 |
"state": {"default": "present", "choices": ["present", "absent"], "type": "str"}, |
| 752 |
"mode": {"required": False, "type": "str"}, |
| 753 |
"owner_id": {"required": False, "type": "int"}, |
| 754 |
"group_id": {"required": False, "type": "int"}, |
| 755 |
"unique": {"default": False, "type": "bool"}, |
| 756 |
"wait": {"default": False, "type": "bool"}, |
| 757 |
"wait_timeout": {"default": 300, "type": "int"}, |
| 758 |
"custom_attrs": {"default": {}, "type": "dict"}, |
| 759 |
"role": {"required": False, "type": "str"}, |
| 760 |
"cardinality": {"required": False, "type": "int"}, |
| 761 |
"force": {"default": False, "type": "bool"}, |
| 762 |
} |
| 763 |
|
| 764 |
module = AnsibleModule( |
| 765 |
argument_spec=fields, |
| 766 |
mutually_exclusive=[ |
| 767 |
["template_id", "template_name", "service_id"], |
| 768 |
["service_id", "service_name"], |
| 769 |
["template_id", "template_name", "role"], |
| 770 |
["template_id", "template_name", "cardinality"], |
| 771 |
["service_id", "custom_attrs"], |
| 772 |
], |
| 773 |
required_together=[["role", "cardinality"]], |
| 774 |
supports_check_mode=True, |
| 775 |
) |
| 776 |
|
| 777 |
auth = get_connection_info(module) |
| 778 |
params = module.params |
| 779 |
service_name = params.get("service_name") |
| 780 |
service_id = params.get("service_id") |
| 781 |
|
| 782 |
requested_template_id = params.get("template_id") |
| 783 |
requested_template_name = params.get("template_name") |
| 784 |
state = params.get("state") |
| 785 |
permissions = params.get("mode") |
| 786 |
owner_id = params.get("owner_id") |
| 787 |
group_id = params.get("group_id") |
| 788 |
unique = params.get("unique") |
| 789 |
wait = params.get("wait") |
| 790 |
wait_timeout = params.get("wait_timeout") |
| 791 |
custom_attrs = params.get("custom_attrs") |
| 792 |
role = params.get("role") |
| 793 |
cardinality = params.get("cardinality") |
| 794 |
force = params.get("force") |
| 795 |
|
| 796 |
template_id = None |
| 797 |
|
| 798 |
if requested_template_id or requested_template_name: |
| 799 |
template_id = get_template_id(module, auth, requested_template_id, requested_template_name) |
| 800 |
if not template_id: |
| 801 |
if requested_template_id: |
| 802 |
module.fail_json(msg=f"There is no template with template_id: {requested_template_id!s}") |
| 803 |
elif requested_template_name: |
| 804 |
module.fail_json(msg=f"There is no template with name: {requested_template_name}") |
| 805 |
|
| 806 |
if unique and not service_name: |
| 807 |
module.fail_json(msg="You cannot use unique without passing service_name!") |
| 808 |
|
| 809 |
if template_id and state == "absent": |
| 810 |
module.fail_json(msg="State absent is not valid for template") |
| 811 |
|
| 812 |
if template_id and state == "present": # Instantiate a service |
| 813 |
result = create_service_and_operation( |
| 814 |
module, |
| 815 |
auth, |
| 816 |
template_id, |
| 817 |
service_name, |
| 818 |
owner_id, |
| 819 |
group_id, |
| 820 |
permissions, |
| 821 |
custom_attrs, |
| 822 |
unique, |
| 823 |
wait, |
| 824 |
wait_timeout, |
| 825 |
) |
| 826 |
else: |
| 827 |
if not (service_id or service_name): |
| 828 |
module.fail_json(msg="To manage the service at least the service id or service name should be specified!") |
| 829 |
if custom_attrs: |
| 830 |
module.fail_json(msg="You can only set custom_attrs when instantiate service!") |
| 831 |
|
| 832 |
if not service_id: |
| 833 |
service_id = get_service_id_by_name(module, auth, service_name) |
| 834 |
# The task should be failed when we want to manage a non-existent service identified by its name |
| 835 |
if not service_id and state == "present": |
| 836 |
module.fail_json(msg=f"There is no service with name: {service_name}") |
| 837 |
|
| 838 |
if state == "absent": |
| 839 |
result = delete_service(module, auth, service_id) |
| 840 |
else: |
| 841 |
result = service_operation( |
| 842 |
module, auth, service_id, owner_id, group_id, permissions, role, cardinality, force, wait, wait_timeout |
| 843 |
) |
| 844 |
|
| 845 |
module.exit_json(**result) |
| 846 |
|
| 847 |
|
| 848 |
if __name__ == "__main__": |
| 849 |
main() |