| 1 |
#!/usr/bin/python |
| 2 |
# |
| 3 |
# Scaleway Serverless function management module |
| 4 |
# |
| 5 |
# Copyright (c) 2022, Guillaume MARTINEZ <[email protected]> |
| 6 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 7 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 8 |
|
| 9 |
from __future__ import annotations |
| 10 |
|
| 11 |
DOCUMENTATION = r""" |
| 12 |
module: scaleway_function |
| 13 |
short_description: Scaleway Function management |
| 14 |
version_added: 6.0.0 |
| 15 |
author: Guillaume MARTINEZ (@Lunik) |
| 16 |
description: |
| 17 |
- This module manages function on Scaleway account. |
| 18 |
extends_documentation_fragment: |
| 19 |
- community.general._scaleway |
| 20 |
- community.general._scaleway_waitable_resource |
| 21 |
- community.general._attributes |
| 22 |
- community.general._scaleway.actiongroup_scaleway |
| 23 |
requirements: |
| 24 |
- passlib[argon2] >= 1.7.4 |
| 25 |
|
| 26 |
attributes: |
| 27 |
check_mode: |
| 28 |
support: full |
| 29 |
diff_mode: |
| 30 |
support: none |
| 31 |
action_group: |
| 32 |
version_added: 11.3.0 |
| 33 |
|
| 34 |
options: |
| 35 |
state: |
| 36 |
type: str |
| 37 |
description: |
| 38 |
- Indicate desired state of the function. |
| 39 |
default: present |
| 40 |
choices: |
| 41 |
- present |
| 42 |
- absent |
| 43 |
|
| 44 |
namespace_id: |
| 45 |
type: str |
| 46 |
description: |
| 47 |
- Function namespace identifier. |
| 48 |
required: true |
| 49 |
|
| 50 |
region: |
| 51 |
type: str |
| 52 |
description: |
| 53 |
- Scaleway region to use (for example V(fr-par)). |
| 54 |
required: true |
| 55 |
choices: |
| 56 |
- fr-par |
| 57 |
- nl-ams |
| 58 |
- pl-waw |
| 59 |
|
| 60 |
name: |
| 61 |
type: str |
| 62 |
description: |
| 63 |
- Name of the function. |
| 64 |
required: true |
| 65 |
|
| 66 |
description: |
| 67 |
description: |
| 68 |
- Description of the function. |
| 69 |
type: str |
| 70 |
default: '' |
| 71 |
|
| 72 |
min_scale: |
| 73 |
description: |
| 74 |
- Minimum number of replicas for the function. |
| 75 |
type: int |
| 76 |
|
| 77 |
max_scale: |
| 78 |
description: |
| 79 |
- Maximum number of replicas for the function. |
| 80 |
type: int |
| 81 |
|
| 82 |
environment_variables: |
| 83 |
description: |
| 84 |
- Environment variables of the function. |
| 85 |
- Injected in function at runtime. |
| 86 |
type: dict |
| 87 |
default: {} |
| 88 |
|
| 89 |
secret_environment_variables: |
| 90 |
description: |
| 91 |
- Secret environment variables of the function. |
| 92 |
- Updating those values does not output a RV(ignore:changed) state in Ansible. |
| 93 |
- Injected in function at runtime. |
| 94 |
type: dict |
| 95 |
default: {} |
| 96 |
|
| 97 |
runtime: |
| 98 |
description: |
| 99 |
- Runtime of the function. |
| 100 |
- See U(https://www.scaleway.com/en/docs/compute/functions/reference-content/functions-lifecycle/) for all available |
| 101 |
runtimes. |
| 102 |
type: str |
| 103 |
required: true |
| 104 |
|
| 105 |
memory_limit: |
| 106 |
description: |
| 107 |
- Resources define performance characteristics of your function. |
| 108 |
- They are allocated to your function at runtime. |
| 109 |
type: int |
| 110 |
|
| 111 |
function_timeout: |
| 112 |
description: |
| 113 |
- The length of time your handler can spend processing a request before being stopped. |
| 114 |
type: str |
| 115 |
|
| 116 |
handler: |
| 117 |
description: |
| 118 |
- The C(module-name.export) value in your function. |
| 119 |
type: str |
| 120 |
|
| 121 |
privacy: |
| 122 |
description: |
| 123 |
- Privacy policies define whether a function can be executed anonymously. |
| 124 |
- Choose V(public) to enable anonymous execution, or V(private) to protect your function with an authentication mechanism |
| 125 |
provided by the Scaleway API. |
| 126 |
type: str |
| 127 |
default: public |
| 128 |
choices: |
| 129 |
- public |
| 130 |
- private |
| 131 |
|
| 132 |
redeploy: |
| 133 |
description: |
| 134 |
- Redeploy the function if update is required. |
| 135 |
type: bool |
| 136 |
default: false |
| 137 |
""" |
| 138 |
|
| 139 |
EXAMPLES = r""" |
| 140 |
- name: Create a function |
| 141 |
community.general.scaleway_function: |
| 142 |
namespace_id: '{{ scw_function_namespace }}' |
| 143 |
region: fr-par |
| 144 |
state: present |
| 145 |
name: my-awesome-function |
| 146 |
runtime: python3 |
| 147 |
environment_variables: |
| 148 |
MY_VAR: my_value |
| 149 |
secret_environment_variables: |
| 150 |
MY_SECRET_VAR: my_secret_value |
| 151 |
register: function_creation_task |
| 152 |
|
| 153 |
- name: Make sure function is deleted |
| 154 |
community.general.scaleway_function: |
| 155 |
namespace_id: '{{ scw_function_namespace }}' |
| 156 |
region: fr-par |
| 157 |
state: absent |
| 158 |
name: my-awesome-function |
| 159 |
""" |
| 160 |
|
| 161 |
RETURN = r""" |
| 162 |
function: |
| 163 |
description: The function information. |
| 164 |
returned: when O(state=present) |
| 165 |
type: dict |
| 166 |
sample: |
| 167 |
cpu_limit: 140 |
| 168 |
description: Function used for testing scaleway_function ansible module |
| 169 |
domain_name: fnansibletestfxamabuc-fn-ansible-test.functions.fnc.fr-par.scw.cloud |
| 170 |
environment_variables: |
| 171 |
MY_VAR: my_value |
| 172 |
error_message: null |
| 173 |
handler: handler.handle |
| 174 |
http_option: "" |
| 175 |
id: ceb64dc4-4464-4196-8e20-ecef705475d3 |
| 176 |
max_scale: 5 |
| 177 |
memory_limit: 256 |
| 178 |
min_scale: 0 |
| 179 |
name: fn-ansible-test |
| 180 |
namespace_id: 82737d8d-0ebb-4d89-b0ad-625876eca50d |
| 181 |
privacy: public |
| 182 |
region: fr-par |
| 183 |
runtime: python310 |
| 184 |
runtime_message: "" |
| 185 |
secret_environment_variables: |
| 186 |
- key: MY_SECRET_VAR |
| 187 |
value: $argon2id$v=19$m=65536,t=1,p=2$tb6UwSPWx/rH5Vyxt9Ujfw$5ZlvaIjWwNDPxD9Rdght3NarJz4IETKjpvAU3mMSmFg |
| 188 |
status: created |
| 189 |
timeout: 300s |
| 190 |
""" |
| 191 |
|
| 192 |
from copy import deepcopy |
| 193 |
|
| 194 |
from ansible.module_utils.basic import AnsibleModule |
| 195 |
|
| 196 |
from ansible_collections.community.general.plugins.module_utils._scaleway import ( |
| 197 |
SCALEWAY_REGIONS, |
| 198 |
Scaleway, |
| 199 |
SecretVariables, |
| 200 |
resource_attributes_should_be_changed, |
| 201 |
scaleway_argument_spec, |
| 202 |
scaleway_waitable_resource_argument_spec, |
| 203 |
) |
| 204 |
|
| 205 |
STABLE_STATES = ("ready", "created", "absent") |
| 206 |
|
| 207 |
VERIFIABLE_MUTABLE_ATTRIBUTES = ( |
| 208 |
"description", |
| 209 |
"min_scale", |
| 210 |
"max_scale", |
| 211 |
"environment_variables", |
| 212 |
"runtime", |
| 213 |
"memory_limit", |
| 214 |
"timeout", |
| 215 |
"handler", |
| 216 |
"privacy", |
| 217 |
"secret_environment_variables", |
| 218 |
) |
| 219 |
|
| 220 |
MUTABLE_ATTRIBUTES = VERIFIABLE_MUTABLE_ATTRIBUTES + ("redeploy",) |
| 221 |
|
| 222 |
|
| 223 |
def payload_from_wished_fn(wished_fn): |
| 224 |
payload = { |
| 225 |
"namespace_id": wished_fn["namespace_id"], |
| 226 |
"name": wished_fn["name"], |
| 227 |
"description": wished_fn["description"], |
| 228 |
"min_scale": wished_fn["min_scale"], |
| 229 |
"max_scale": wished_fn["max_scale"], |
| 230 |
"runtime": wished_fn["runtime"], |
| 231 |
"memory_limit": wished_fn["memory_limit"], |
| 232 |
"timeout": wished_fn["timeout"], |
| 233 |
"handler": wished_fn["handler"], |
| 234 |
"privacy": wished_fn["privacy"], |
| 235 |
"redeploy": wished_fn["redeploy"], |
| 236 |
"environment_variables": wished_fn["environment_variables"], |
| 237 |
"secret_environment_variables": SecretVariables.dict_to_list(wished_fn["secret_environment_variables"]), |
| 238 |
} |
| 239 |
|
| 240 |
return payload |
| 241 |
|
| 242 |
|
| 243 |
def absent_strategy(api, wished_fn): |
| 244 |
changed = False |
| 245 |
|
| 246 |
fn_list = api.fetch_all_resources("functions") |
| 247 |
fn_lookup = {fn["name"]: fn for fn in fn_list} |
| 248 |
|
| 249 |
if wished_fn["name"] not in fn_lookup: |
| 250 |
return changed, {} |
| 251 |
|
| 252 |
target_fn = fn_lookup[wished_fn["name"]] |
| 253 |
changed = True |
| 254 |
if api.module.check_mode: |
| 255 |
return changed, {"status": "Function would be destroyed"} |
| 256 |
|
| 257 |
api.wait_to_complete_state_transition(resource=target_fn, stable_states=STABLE_STATES, force_wait=True) |
| 258 |
response = api.delete(path=f"{api.api_path}/{target_fn['id']}") |
| 259 |
if not response.ok: |
| 260 |
api.module.fail_json(msg=f"Error deleting function [{response.status_code}: {response.json}]") |
| 261 |
|
| 262 |
api.wait_to_complete_state_transition(resource=target_fn, stable_states=STABLE_STATES) |
| 263 |
return changed, response.json |
| 264 |
|
| 265 |
|
| 266 |
def present_strategy(api, wished_fn): |
| 267 |
changed = False |
| 268 |
|
| 269 |
fn_list = api.fetch_all_resources("functions") |
| 270 |
fn_lookup = {fn["name"]: fn for fn in fn_list} |
| 271 |
|
| 272 |
payload_fn = payload_from_wished_fn(wished_fn) |
| 273 |
|
| 274 |
if wished_fn["name"] not in fn_lookup: |
| 275 |
changed = True |
| 276 |
if api.module.check_mode: |
| 277 |
return changed, {"status": "A function would be created."} |
| 278 |
|
| 279 |
# Creation doesn't support `redeploy` parameter |
| 280 |
del payload_fn["redeploy"] |
| 281 |
|
| 282 |
# Create function |
| 283 |
api.warn(payload_fn) |
| 284 |
creation_response = api.post(path=api.api_path, data=payload_fn) |
| 285 |
|
| 286 |
if not creation_response.ok: |
| 287 |
msg = f"Error during function creation: {creation_response.info['msg']}: '{creation_response.json['message']}' ({creation_response.json})" |
| 288 |
api.module.fail_json(msg=msg) |
| 289 |
|
| 290 |
api.wait_to_complete_state_transition(resource=creation_response.json, stable_states=STABLE_STATES) |
| 291 |
response = api.get(path=f"{api.api_path}/{creation_response.json['id']}") |
| 292 |
return changed, response.json |
| 293 |
|
| 294 |
target_fn = fn_lookup[wished_fn["name"]] |
| 295 |
decoded_target_fn = deepcopy(target_fn) |
| 296 |
decoded_target_fn["secret_environment_variables"] = SecretVariables.decode( |
| 297 |
decoded_target_fn["secret_environment_variables"], payload_fn["secret_environment_variables"] |
| 298 |
) |
| 299 |
|
| 300 |
patch_payload = resource_attributes_should_be_changed( |
| 301 |
target=decoded_target_fn, |
| 302 |
wished=payload_fn, |
| 303 |
verifiable_mutable_attributes=VERIFIABLE_MUTABLE_ATTRIBUTES, |
| 304 |
mutable_attributes=MUTABLE_ATTRIBUTES, |
| 305 |
) |
| 306 |
|
| 307 |
if not patch_payload: |
| 308 |
return changed, target_fn |
| 309 |
|
| 310 |
changed = True |
| 311 |
if api.module.check_mode: |
| 312 |
return changed, {"status": "Function attributes would be changed."} |
| 313 |
|
| 314 |
fn_patch_response = api.patch(path=f"{api.api_path}/{target_fn['id']}", data=patch_payload) |
| 315 |
|
| 316 |
if not fn_patch_response.ok: |
| 317 |
api.module.fail_json( |
| 318 |
msg=f"Error during function attributes update: [{fn_patch_response.status_code}: {fn_patch_response.json['message']}]" |
| 319 |
) |
| 320 |
|
| 321 |
api.wait_to_complete_state_transition(resource=target_fn, stable_states=STABLE_STATES) |
| 322 |
response = api.get(path=f"{api.api_path}/{target_fn['id']}") |
| 323 |
return changed, response.json |
| 324 |
|
| 325 |
|
| 326 |
state_strategy = {"present": present_strategy, "absent": absent_strategy} |
| 327 |
|
| 328 |
|
| 329 |
def core(module): |
| 330 |
SecretVariables.ensure_scaleway_secret_package(module) |
| 331 |
|
| 332 |
region = module.params["region"] |
| 333 |
wished_function = { |
| 334 |
"state": module.params["state"], |
| 335 |
"namespace_id": module.params["namespace_id"], |
| 336 |
"name": module.params["name"], |
| 337 |
"description": module.params["description"], |
| 338 |
"min_scale": module.params["min_scale"], |
| 339 |
"max_scale": module.params["max_scale"], |
| 340 |
"runtime": module.params["runtime"], |
| 341 |
"memory_limit": module.params["memory_limit"], |
| 342 |
"timeout": module.params["function_timeout"], |
| 343 |
"handler": module.params["handler"], |
| 344 |
"privacy": module.params["privacy"], |
| 345 |
"redeploy": module.params["redeploy"], |
| 346 |
"environment_variables": module.params["environment_variables"], |
| 347 |
"secret_environment_variables": module.params["secret_environment_variables"], |
| 348 |
} |
| 349 |
|
| 350 |
api = Scaleway(module=module) |
| 351 |
api.api_path = f"functions/v1beta1/regions/{region}/functions" |
| 352 |
|
| 353 |
changed, summary = state_strategy[wished_function["state"]](api=api, wished_fn=wished_function) |
| 354 |
|
| 355 |
module.exit_json(changed=changed, function=summary) |
| 356 |
|
| 357 |
|
| 358 |
def main(): |
| 359 |
argument_spec = scaleway_argument_spec() |
| 360 |
argument_spec.update(scaleway_waitable_resource_argument_spec()) |
| 361 |
argument_spec.update( |
| 362 |
dict( |
| 363 |
state=dict(type="str", default="present", choices=["absent", "present"]), |
| 364 |
namespace_id=dict(type="str", required=True), |
| 365 |
region=dict(type="str", required=True, choices=SCALEWAY_REGIONS), |
| 366 |
name=dict(type="str", required=True), |
| 367 |
description=dict(type="str", default=""), |
| 368 |
min_scale=dict(type="int"), |
| 369 |
max_scale=dict(type="int"), |
| 370 |
runtime=dict(type="str", required=True), |
| 371 |
memory_limit=dict(type="int"), |
| 372 |
function_timeout=dict(type="str"), |
| 373 |
handler=dict(type="str"), |
| 374 |
privacy=dict(type="str", default="public", choices=["public", "private"]), |
| 375 |
redeploy=dict(type="bool", default=False), |
| 376 |
environment_variables=dict(type="dict", default={}), |
| 377 |
secret_environment_variables=dict(type="dict", default={}, no_log=True), |
| 378 |
) |
| 379 |
) |
| 380 |
module = AnsibleModule( |
| 381 |
argument_spec=argument_spec, |
| 382 |
supports_check_mode=True, |
| 383 |
) |
| 384 |
|
| 385 |
core(module) |
| 386 |
|
| 387 |
|
| 388 |
if __name__ == "__main__": |
| 389 |
main() |