| 1 |
#!/usr/bin/python |
| 2 |
# |
| 3 |
# Scaleway Serverless container namespace 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_container_namespace |
| 13 |
short_description: Scaleway Container namespace management |
| 14 |
version_added: 6.0.0 |
| 15 |
author: Guillaume MARTINEZ (@Lunik) |
| 16 |
description: |
| 17 |
- This module manages container namespaces 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 container namespace. |
| 39 |
default: present |
| 40 |
choices: |
| 41 |
- present |
| 42 |
- absent |
| 43 |
|
| 44 |
project_id: |
| 45 |
type: str |
| 46 |
description: |
| 47 |
- Project 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 container namespace. |
| 64 |
required: true |
| 65 |
|
| 66 |
description: |
| 67 |
description: |
| 68 |
- Description of the container namespace. |
| 69 |
type: str |
| 70 |
default: '' |
| 71 |
|
| 72 |
environment_variables: |
| 73 |
description: |
| 74 |
- Environment variables of the container namespace. |
| 75 |
- Injected in containers at runtime. |
| 76 |
type: dict |
| 77 |
default: {} |
| 78 |
|
| 79 |
secret_environment_variables: |
| 80 |
description: |
| 81 |
- Secret environment variables of the container namespace. |
| 82 |
- Updating those values does not output a RV(ignore:changed) state in Ansible. |
| 83 |
- Injected in containers at runtime. |
| 84 |
type: dict |
| 85 |
default: {} |
| 86 |
""" |
| 87 |
|
| 88 |
EXAMPLES = r""" |
| 89 |
- name: Create a container namespace |
| 90 |
community.general.scaleway_container_namespace: |
| 91 |
project_id: '{{ scw_project }}' |
| 92 |
state: present |
| 93 |
region: fr-par |
| 94 |
name: my-awesome-container-namespace |
| 95 |
environment_variables: |
| 96 |
MY_VAR: my_value |
| 97 |
secret_environment_variables: |
| 98 |
MY_SECRET_VAR: my_secret_value |
| 99 |
register: container_namespace_creation_task |
| 100 |
|
| 101 |
- name: Make sure container namespace is deleted |
| 102 |
community.general.scaleway_container_namespace: |
| 103 |
project_id: '{{ scw_project }}' |
| 104 |
state: absent |
| 105 |
region: fr-par |
| 106 |
name: my-awesome-container-namespace |
| 107 |
""" |
| 108 |
|
| 109 |
RETURN = r""" |
| 110 |
container_namespace: |
| 111 |
description: The container namespace information. |
| 112 |
returned: when O(state=present) |
| 113 |
type: dict |
| 114 |
sample: |
| 115 |
description: "" |
| 116 |
environment_variables: |
| 117 |
MY_VAR: my_value |
| 118 |
error_message: null |
| 119 |
id: 531a1fd7-98d2-4a74-ad77-d398324304b8 |
| 120 |
name: my-awesome-container-namespace |
| 121 |
organization_id: e04e3bdc-015c-4514-afde-9389e9be24b0 |
| 122 |
project_id: d44cea58-dcb7-4c95-bff1-1105acb60a98 |
| 123 |
region: fr-par |
| 124 |
registry_endpoint: "" |
| 125 |
registry_namespace_id: "" |
| 126 |
secret_environment_variables: |
| 127 |
- key: MY_SECRET_VAR |
| 128 |
value: $argon2id$v=19$m=65536,t=1,p=2$tb6UwSPWx/rH5Vyxt9Ujfw$5ZlvaIjWwNDPxD9Rdght3NarJz4IETKjpvAU3mMSmFg |
| 129 |
status: pending |
| 130 |
""" |
| 131 |
|
| 132 |
from copy import deepcopy |
| 133 |
|
| 134 |
from ansible.module_utils.basic import AnsibleModule |
| 135 |
|
| 136 |
from ansible_collections.community.general.plugins.module_utils._scaleway import ( |
| 137 |
SCALEWAY_REGIONS, |
| 138 |
Scaleway, |
| 139 |
SecretVariables, |
| 140 |
resource_attributes_should_be_changed, |
| 141 |
scaleway_argument_spec, |
| 142 |
scaleway_waitable_resource_argument_spec, |
| 143 |
) |
| 144 |
|
| 145 |
STABLE_STATES = ("ready", "absent") |
| 146 |
|
| 147 |
MUTABLE_ATTRIBUTES = ("description", "environment_variables", "secret_environment_variables") |
| 148 |
|
| 149 |
|
| 150 |
def payload_from_wished_cn(wished_cn): |
| 151 |
payload = { |
| 152 |
"project_id": wished_cn["project_id"], |
| 153 |
"name": wished_cn["name"], |
| 154 |
"description": wished_cn["description"], |
| 155 |
"environment_variables": wished_cn["environment_variables"], |
| 156 |
"secret_environment_variables": SecretVariables.dict_to_list(wished_cn["secret_environment_variables"]), |
| 157 |
} |
| 158 |
|
| 159 |
return payload |
| 160 |
|
| 161 |
|
| 162 |
def absent_strategy(api, wished_cn): |
| 163 |
changed = False |
| 164 |
|
| 165 |
cn_list = api.fetch_all_resources("namespaces") |
| 166 |
cn_lookup = {cn["name"]: cn for cn in cn_list} |
| 167 |
|
| 168 |
if wished_cn["name"] not in cn_lookup: |
| 169 |
return changed, {} |
| 170 |
|
| 171 |
target_cn = cn_lookup[wished_cn["name"]] |
| 172 |
changed = True |
| 173 |
if api.module.check_mode: |
| 174 |
return changed, {"status": "Container namespace would be destroyed"} |
| 175 |
|
| 176 |
api.wait_to_complete_state_transition(resource=target_cn, stable_states=STABLE_STATES, force_wait=True) |
| 177 |
response = api.delete(path=f"{api.api_path}/{target_cn['id']}") |
| 178 |
if not response.ok: |
| 179 |
api.module.fail_json(msg=f"Error deleting container namespace [{response.status_code}: {response.json}]") |
| 180 |
|
| 181 |
api.wait_to_complete_state_transition(resource=target_cn, stable_states=STABLE_STATES) |
| 182 |
return changed, response.json |
| 183 |
|
| 184 |
|
| 185 |
def present_strategy(api, wished_cn): |
| 186 |
changed = False |
| 187 |
|
| 188 |
cn_list = api.fetch_all_resources("namespaces") |
| 189 |
cn_lookup = {cn["name"]: cn for cn in cn_list} |
| 190 |
|
| 191 |
payload_cn = payload_from_wished_cn(wished_cn) |
| 192 |
|
| 193 |
if wished_cn["name"] not in cn_lookup: |
| 194 |
changed = True |
| 195 |
if api.module.check_mode: |
| 196 |
return changed, {"status": "A container namespace would be created."} |
| 197 |
|
| 198 |
# Create container namespace |
| 199 |
api.warn(payload_cn) |
| 200 |
creation_response = api.post(path=api.api_path, data=payload_cn) |
| 201 |
|
| 202 |
if not creation_response.ok: |
| 203 |
msg = ( |
| 204 |
f"Error during container namespace creation: {creation_response.info['msg']}: " |
| 205 |
f"'{creation_response.json['message']}' ({creation_response.json})" |
| 206 |
) |
| 207 |
api.module.fail_json(msg=msg) |
| 208 |
|
| 209 |
api.wait_to_complete_state_transition(resource=creation_response.json, stable_states=STABLE_STATES) |
| 210 |
response = api.get(path=f"{api.api_path}/{creation_response.json['id']}") |
| 211 |
return changed, response.json |
| 212 |
|
| 213 |
target_cn = cn_lookup[wished_cn["name"]] |
| 214 |
decoded_target_cn = deepcopy(target_cn) |
| 215 |
decoded_target_cn["secret_environment_variables"] = SecretVariables.decode( |
| 216 |
decoded_target_cn["secret_environment_variables"], payload_cn["secret_environment_variables"] |
| 217 |
) |
| 218 |
patch_payload = resource_attributes_should_be_changed( |
| 219 |
target=decoded_target_cn, |
| 220 |
wished=payload_cn, |
| 221 |
verifiable_mutable_attributes=MUTABLE_ATTRIBUTES, |
| 222 |
mutable_attributes=MUTABLE_ATTRIBUTES, |
| 223 |
) |
| 224 |
|
| 225 |
if not patch_payload: |
| 226 |
return changed, target_cn |
| 227 |
|
| 228 |
changed = True |
| 229 |
if api.module.check_mode: |
| 230 |
return changed, {"status": "Container namespace attributes would be changed."} |
| 231 |
|
| 232 |
cn_patch_response = api.patch(path=f"{api.api_path}/{target_cn['id']}", data=patch_payload) |
| 233 |
|
| 234 |
if not cn_patch_response.ok: |
| 235 |
api.module.fail_json( |
| 236 |
msg=f"Error during container namespace attributes update: [{cn_patch_response.status_code}: {cn_patch_response.json['message']}]" |
| 237 |
) |
| 238 |
|
| 239 |
api.wait_to_complete_state_transition(resource=target_cn, stable_states=STABLE_STATES) |
| 240 |
response = api.get(path=f"{api.api_path}/{target_cn['id']}") |
| 241 |
return changed, cn_patch_response.json |
| 242 |
|
| 243 |
|
| 244 |
state_strategy = {"present": present_strategy, "absent": absent_strategy} |
| 245 |
|
| 246 |
|
| 247 |
def core(module): |
| 248 |
SecretVariables.ensure_scaleway_secret_package(module) |
| 249 |
|
| 250 |
region = module.params["region"] |
| 251 |
wished_container_namespace = { |
| 252 |
"state": module.params["state"], |
| 253 |
"project_id": module.params["project_id"], |
| 254 |
"name": module.params["name"], |
| 255 |
"description": module.params["description"], |
| 256 |
"environment_variables": module.params["environment_variables"], |
| 257 |
"secret_environment_variables": module.params["secret_environment_variables"], |
| 258 |
} |
| 259 |
|
| 260 |
api = Scaleway(module=module) |
| 261 |
api.api_path = f"containers/v1beta1/regions/{region}/namespaces" |
| 262 |
|
| 263 |
changed, summary = state_strategy[wished_container_namespace["state"]]( |
| 264 |
api=api, wished_cn=wished_container_namespace |
| 265 |
) |
| 266 |
|
| 267 |
module.exit_json(changed=changed, container_namespace=summary) |
| 268 |
|
| 269 |
|
| 270 |
def main(): |
| 271 |
argument_spec = scaleway_argument_spec() |
| 272 |
argument_spec.update(scaleway_waitable_resource_argument_spec()) |
| 273 |
argument_spec.update( |
| 274 |
dict( |
| 275 |
state=dict(type="str", default="present", choices=["absent", "present"]), |
| 276 |
project_id=dict(type="str", required=True), |
| 277 |
region=dict(type="str", required=True, choices=SCALEWAY_REGIONS), |
| 278 |
name=dict(type="str", required=True), |
| 279 |
description=dict(type="str", default=""), |
| 280 |
environment_variables=dict(type="dict", default={}), |
| 281 |
secret_environment_variables=dict(type="dict", default={}, no_log=True), |
| 282 |
) |
| 283 |
) |
| 284 |
module = AnsibleModule( |
| 285 |
argument_spec=argument_spec, |
| 286 |
supports_check_mode=True, |
| 287 |
) |
| 288 |
|
| 289 |
core(module) |
| 290 |
|
| 291 |
|
| 292 |
if __name__ == "__main__": |
| 293 |
main() |