| 1 |
#!/usr/bin/python |
| 2 |
# |
| 3 |
# Scaleway Load-balancer management module |
| 4 |
# |
| 5 |
# Copyright (C) 2018 Online SAS. |
| 6 |
# https://www.scaleway.com |
| 7 |
# |
| 8 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 9 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 10 |
|
| 11 |
from __future__ import annotations |
| 12 |
|
| 13 |
DOCUMENTATION = r""" |
| 14 |
module: scaleway_lb |
| 15 |
short_description: Scaleway load-balancer management module |
| 16 |
author: Remy Leone (@remyleone) |
| 17 |
description: |
| 18 |
- This module manages load-balancers on Scaleway. |
| 19 |
extends_documentation_fragment: |
| 20 |
- community.general._scaleway |
| 21 |
- community.general._attributes |
| 22 |
- community.general._scaleway.actiongroup_scaleway |
| 23 |
|
| 24 |
attributes: |
| 25 |
check_mode: |
| 26 |
support: full |
| 27 |
diff_mode: |
| 28 |
support: none |
| 29 |
action_group: |
| 30 |
version_added: 11.3.0 |
| 31 |
|
| 32 |
options: |
| 33 |
|
| 34 |
name: |
| 35 |
type: str |
| 36 |
description: |
| 37 |
- Name of the load-balancer. |
| 38 |
required: true |
| 39 |
|
| 40 |
description: |
| 41 |
type: str |
| 42 |
description: |
| 43 |
- Description of the load-balancer. |
| 44 |
required: true |
| 45 |
|
| 46 |
organization_id: |
| 47 |
type: str |
| 48 |
description: |
| 49 |
- Organization identifier. |
| 50 |
required: true |
| 51 |
|
| 52 |
state: |
| 53 |
type: str |
| 54 |
description: |
| 55 |
- Indicate desired state of the instance. |
| 56 |
default: present |
| 57 |
choices: |
| 58 |
- present |
| 59 |
- absent |
| 60 |
|
| 61 |
region: |
| 62 |
type: str |
| 63 |
description: |
| 64 |
- Scaleway zone. |
| 65 |
required: true |
| 66 |
choices: |
| 67 |
- nl-ams |
| 68 |
- fr-par |
| 69 |
- pl-waw |
| 70 |
|
| 71 |
tags: |
| 72 |
type: list |
| 73 |
elements: str |
| 74 |
default: [] |
| 75 |
description: |
| 76 |
- List of tags to apply to the load-balancer. |
| 77 |
wait: |
| 78 |
description: |
| 79 |
- Wait for the load-balancer to reach its desired state before returning. |
| 80 |
type: bool |
| 81 |
default: false |
| 82 |
|
| 83 |
wait_timeout: |
| 84 |
type: int |
| 85 |
description: |
| 86 |
- Time to wait for the load-balancer to reach the expected state. |
| 87 |
default: 300 |
| 88 |
|
| 89 |
wait_sleep_time: |
| 90 |
type: int |
| 91 |
description: |
| 92 |
- Time to wait before every attempt to check the state of the load-balancer. |
| 93 |
default: 3 |
| 94 |
""" |
| 95 |
|
| 96 |
EXAMPLES = r""" |
| 97 |
- name: Create a load-balancer |
| 98 |
community.general.scaleway_lb: |
| 99 |
name: foobar |
| 100 |
state: present |
| 101 |
organization_id: 951df375-e094-4d26-97c1-ba548eeb9c42 |
| 102 |
region: fr-par |
| 103 |
tags: |
| 104 |
- hello |
| 105 |
|
| 106 |
- name: Delete a load-balancer |
| 107 |
community.general.scaleway_lb: |
| 108 |
name: foobar |
| 109 |
state: absent |
| 110 |
organization_id: 951df375-e094-4d26-97c1-ba548eeb9c42 |
| 111 |
region: fr-par |
| 112 |
""" |
| 113 |
|
| 114 |
RETURN = r""" |
| 115 |
scaleway_lb: |
| 116 |
description: The load-balancer object. |
| 117 |
returned: success |
| 118 |
type: dict |
| 119 |
sample: |
| 120 |
{ |
| 121 |
"backend_count": 0, |
| 122 |
"frontend_count": 0, |
| 123 |
"description": "Description of my load-balancer", |
| 124 |
"id": "00000000-0000-0000-0000-000000000000", |
| 125 |
"instances": [ |
| 126 |
{ |
| 127 |
"id": "00000000-0000-0000-0000-000000000000", |
| 128 |
"ip_address": "10.0.0.1", |
| 129 |
"region": "fr-par", |
| 130 |
"status": "ready" |
| 131 |
}, |
| 132 |
{ |
| 133 |
"id": "00000000-0000-0000-0000-000000000000", |
| 134 |
"ip_address": "10.0.0.2", |
| 135 |
"region": "fr-par", |
| 136 |
"status": "ready" |
| 137 |
} |
| 138 |
], |
| 139 |
"ip": [ |
| 140 |
{ |
| 141 |
"id": "00000000-0000-0000-0000-000000000000", |
| 142 |
"ip_address": "192.168.0.1", |
| 143 |
"lb_id": "00000000-0000-0000-0000-000000000000", |
| 144 |
"region": "fr-par", |
| 145 |
"organization_id": "00000000-0000-0000-0000-000000000000", |
| 146 |
"reverse": "" |
| 147 |
} |
| 148 |
], |
| 149 |
"name": "lb_ansible_test", |
| 150 |
"organization_id": "00000000-0000-0000-0000-000000000000", |
| 151 |
"region": "fr-par", |
| 152 |
"status": "ready", |
| 153 |
"tags": [ |
| 154 |
"first_tag", |
| 155 |
"second_tag" |
| 156 |
] |
| 157 |
} |
| 158 |
""" |
| 159 |
|
| 160 |
import datetime |
| 161 |
import time |
| 162 |
|
| 163 |
from ansible.module_utils.basic import AnsibleModule |
| 164 |
|
| 165 |
from ansible_collections.community.general.plugins.module_utils._datetime import now |
| 166 |
from ansible_collections.community.general.plugins.module_utils._scaleway import ( |
| 167 |
SCALEWAY_ENDPOINT, |
| 168 |
SCALEWAY_REGIONS, |
| 169 |
Scaleway, |
| 170 |
scaleway_argument_spec, |
| 171 |
) |
| 172 |
|
| 173 |
STABLE_STATES = ("ready", "absent") |
| 174 |
|
| 175 |
MUTABLE_ATTRIBUTES = ("name", "description") |
| 176 |
|
| 177 |
|
| 178 |
def payload_from_wished_lb(wished_lb): |
| 179 |
return { |
| 180 |
"organization_id": wished_lb["organization_id"], |
| 181 |
"name": wished_lb["name"], |
| 182 |
"tags": wished_lb["tags"], |
| 183 |
"description": wished_lb["description"], |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
def fetch_state(api, lb): |
| 188 |
api.module.debug(f"fetch_state of load-balancer: {lb['id']}") |
| 189 |
response = api.get(path=f"{api.api_path}/{lb['id']}") |
| 190 |
|
| 191 |
if response.status_code == 404: |
| 192 |
return "absent" |
| 193 |
|
| 194 |
if not response.ok: |
| 195 |
msg = f"Error during state fetching: ({response.status_code}) {response.json}" |
| 196 |
api.module.fail_json(msg=msg) |
| 197 |
|
| 198 |
try: |
| 199 |
api.module.debug(f"Load-balancer {lb['id']} in state: {response.json['status']}") |
| 200 |
return response.json["status"] |
| 201 |
except KeyError: |
| 202 |
api.module.fail_json(msg=f"Could not fetch state in {response.json}") |
| 203 |
|
| 204 |
|
| 205 |
def wait_to_complete_state_transition(api, lb, force_wait=False): |
| 206 |
wait = api.module.params["wait"] |
| 207 |
if not (wait or force_wait): |
| 208 |
return |
| 209 |
wait_timeout = api.module.params["wait_timeout"] |
| 210 |
wait_sleep_time = api.module.params["wait_sleep_time"] |
| 211 |
|
| 212 |
start = now() |
| 213 |
end = start + datetime.timedelta(seconds=wait_timeout) |
| 214 |
while now() < end: |
| 215 |
api.module.debug("We are going to wait for the load-balancer to finish its transition") |
| 216 |
state = fetch_state(api, lb) |
| 217 |
if state in STABLE_STATES: |
| 218 |
api.module.debug("It seems that the load-balancer is not in transition anymore.") |
| 219 |
api.module.debug(f"load-balancer in state: {fetch_state(api, lb)}") |
| 220 |
break |
| 221 |
time.sleep(wait_sleep_time) |
| 222 |
else: |
| 223 |
api.module.fail_json(msg="Server takes too long to finish its transition") |
| 224 |
|
| 225 |
|
| 226 |
def lb_attributes_should_be_changed(target_lb, wished_lb): |
| 227 |
diff = {attr: wished_lb[attr] for attr in MUTABLE_ATTRIBUTES if target_lb[attr] != wished_lb[attr]} |
| 228 |
|
| 229 |
if diff: |
| 230 |
return {attr: wished_lb[attr] for attr in MUTABLE_ATTRIBUTES} |
| 231 |
|
| 232 |
return {} |
| 233 |
|
| 234 |
|
| 235 |
def present_strategy(api, wished_lb): |
| 236 |
changed = False |
| 237 |
|
| 238 |
response = api.get(path=api.api_path) |
| 239 |
if not response.ok: |
| 240 |
api.module.fail_json(msg=f"Error getting load-balancers [{response.status_code}: {response.json['message']}]") |
| 241 |
|
| 242 |
lbs_list = response.json["lbs"] |
| 243 |
lb_lookup = {lb["name"]: lb for lb in lbs_list} |
| 244 |
|
| 245 |
if wished_lb["name"] not in lb_lookup.keys(): |
| 246 |
changed = True |
| 247 |
if api.module.check_mode: |
| 248 |
return changed, {"status": "A load-balancer would be created."} |
| 249 |
|
| 250 |
# Create Load-balancer |
| 251 |
api.warn(payload_from_wished_lb(wished_lb)) |
| 252 |
creation_response = api.post(path=api.api_path, data=payload_from_wished_lb(wished_lb)) |
| 253 |
|
| 254 |
if not creation_response.ok: |
| 255 |
msg = f"Error during lb creation: {creation_response.info['msg']}: '{creation_response.json['message']}' ({creation_response.json})" |
| 256 |
api.module.fail_json(msg=msg) |
| 257 |
|
| 258 |
wait_to_complete_state_transition(api=api, lb=creation_response.json) |
| 259 |
response = api.get(path=f"{api.api_path}/{creation_response.json['id']}") |
| 260 |
return changed, response.json |
| 261 |
|
| 262 |
target_lb = lb_lookup[wished_lb["name"]] |
| 263 |
patch_payload = lb_attributes_should_be_changed(target_lb=target_lb, wished_lb=wished_lb) |
| 264 |
|
| 265 |
if not patch_payload: |
| 266 |
return changed, target_lb |
| 267 |
|
| 268 |
changed = True |
| 269 |
if api.module.check_mode: |
| 270 |
return changed, {"status": "Load-balancer attributes would be changed."} |
| 271 |
|
| 272 |
lb_patch_response = api.put(path=f"{api.api_path}/{target_lb['id']}", data=patch_payload) |
| 273 |
|
| 274 |
if not lb_patch_response.ok: |
| 275 |
api.module.fail_json( |
| 276 |
msg=f"Error during load-balancer attributes update: [{lb_patch_response.status_code}: {lb_patch_response.json['message']}]" |
| 277 |
) |
| 278 |
|
| 279 |
wait_to_complete_state_transition(api=api, lb=target_lb) |
| 280 |
return changed, lb_patch_response.json |
| 281 |
|
| 282 |
|
| 283 |
def absent_strategy(api, wished_lb): |
| 284 |
response = api.get(path=api.api_path) |
| 285 |
changed = False |
| 286 |
|
| 287 |
status_code = response.status_code |
| 288 |
lbs_json = response.json |
| 289 |
lbs_list = lbs_json["lbs"] |
| 290 |
|
| 291 |
if not response.ok: |
| 292 |
api.module.fail_json(msg=f"Error getting load-balancers [{status_code}: {response.json['message']}]") |
| 293 |
|
| 294 |
lb_lookup = {lb["name"]: lb for lb in lbs_list} |
| 295 |
if wished_lb["name"] not in lb_lookup.keys(): |
| 296 |
return changed, {} |
| 297 |
|
| 298 |
target_lb = lb_lookup[wished_lb["name"]] |
| 299 |
changed = True |
| 300 |
if api.module.check_mode: |
| 301 |
return changed, {"status": "Load-balancer would be destroyed"} |
| 302 |
|
| 303 |
wait_to_complete_state_transition(api=api, lb=target_lb, force_wait=True) |
| 304 |
response = api.delete(path=f"{api.api_path}/{target_lb['id']}") |
| 305 |
if not response.ok: |
| 306 |
api.module.fail_json(msg=f"Error deleting load-balancer [{response.status_code}: {response.json}]") |
| 307 |
|
| 308 |
wait_to_complete_state_transition(api=api, lb=target_lb) |
| 309 |
return changed, response.json |
| 310 |
|
| 311 |
|
| 312 |
state_strategy = {"present": present_strategy, "absent": absent_strategy} |
| 313 |
|
| 314 |
|
| 315 |
def core(module): |
| 316 |
region = module.params["region"] |
| 317 |
wished_load_balancer = { |
| 318 |
"state": module.params["state"], |
| 319 |
"name": module.params["name"], |
| 320 |
"description": module.params["description"], |
| 321 |
"tags": module.params["tags"], |
| 322 |
"organization_id": module.params["organization_id"], |
| 323 |
} |
| 324 |
module.params["api_url"] = SCALEWAY_ENDPOINT |
| 325 |
api = Scaleway(module=module) |
| 326 |
api.api_path = f"lb/v1/regions/{region}/lbs" |
| 327 |
|
| 328 |
changed, summary = state_strategy[wished_load_balancer["state"]](api=api, wished_lb=wished_load_balancer) |
| 329 |
module.exit_json(changed=changed, scaleway_lb=summary) |
| 330 |
|
| 331 |
|
| 332 |
def main(): |
| 333 |
argument_spec = scaleway_argument_spec() |
| 334 |
argument_spec.update( |
| 335 |
dict( |
| 336 |
name=dict(required=True), |
| 337 |
description=dict(required=True), |
| 338 |
region=dict(required=True, choices=SCALEWAY_REGIONS), |
| 339 |
state=dict(choices=list(state_strategy.keys()), default="present"), |
| 340 |
tags=dict(type="list", elements="str", default=[]), |
| 341 |
organization_id=dict(required=True), |
| 342 |
wait=dict(type="bool", default=False), |
| 343 |
wait_timeout=dict(type="int", default=300), |
| 344 |
wait_sleep_time=dict(type="int", default=3), |
| 345 |
) |
| 346 |
) |
| 347 |
module = AnsibleModule( |
| 348 |
argument_spec=argument_spec, |
| 349 |
supports_check_mode=True, |
| 350 |
) |
| 351 |
|
| 352 |
core(module) |
| 353 |
|
| 354 |
|
| 355 |
if __name__ == "__main__": |
| 356 |
main() |