Debian Codegraph @

ansible_collections/community/general/plugins/modules/scaleway_container_registry.py

raw
1 #!/usr/bin/python
2 #
3 # Scaleway Container registry 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_registry
13 short_description: Scaleway Container registry management module
14 version_added: 5.8.0
15 author: Guillaume MARTINEZ (@Lunik)
16 description:
17 - This module manages container registries 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
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 state:
34 type: str
35 description:
36 - Indicate desired state of the container registry.
37 default: present
38 choices:
39 - present
40 - absent
41
42 project_id:
43 type: str
44 description:
45 - Project identifier.
46 required: true
47
48 region:
49 type: str
50 description:
51 - Scaleway region to use (for example V(fr-par)).
52 required: true
53 choices:
54 - fr-par
55 - nl-ams
56 - pl-waw
57
58 name:
59 type: str
60 description:
61 - Name of the container registry.
62 required: true
63
64 description:
65 description:
66 - Description of the container registry.
67 type: str
68 default: ''
69
70 privacy_policy:
71 type: str
72 description:
73 - Default visibility policy.
74 - Everyone can pull images from a V(public) registry.
75 choices:
76 - public
77 - private
78 default: private
79 """
80
81 EXAMPLES = r"""
82 - name: Create a container registry
83 community.general.scaleway_container_registry:
84 project_id: '{{ scw_project }}'
85 state: present
86 region: fr-par
87 name: my-awesome-container-registry
88 register: container_registry_creation_task
89
90 - name: Make sure container registry is deleted
91 community.general.scaleway_container_registry:
92 project_id: '{{ scw_project }}'
93 state: absent
94 region: fr-par
95 name: my-awesome-container-registry
96 """
97
98 RETURN = r"""
99 container_registry:
100 description: The container registry information.
101 returned: when O(state=present)
102 type: dict
103 sample:
104 created_at: "2022-10-14T09:51:07.949716Z"
105 description: Managed by Ansible
106 endpoint: rg.fr-par.scw.cloud/my-awesome-registry
107 id: 0d7d5270-7864-49c2-920b-9fd6731f3589
108 image_count: 0
109 is_public: false
110 name: my-awesome-registry
111 organization_id: 10697b59-5c34-4d24-8d15-9ff2d3b89f58
112 project_id: 3da4f0b2-06be-4773-8ec4-5dfa435381be
113 region: fr-par
114 size: 0
115 status: ready
116 status_message: ""
117 updated_at: "2022-10-14T09:51:07.949716Z"
118 """
119
120 from ansible.module_utils.basic import AnsibleModule
121
122 from ansible_collections.community.general.plugins.module_utils._scaleway import (
123 SCALEWAY_REGIONS,
124 Scaleway,
125 resource_attributes_should_be_changed,
126 scaleway_argument_spec,
127 scaleway_waitable_resource_argument_spec,
128 )
129
130 STABLE_STATES = ("ready", "absent")
131
132 MUTABLE_ATTRIBUTES = ("description", "is_public")
133
134
135 def payload_from_wished_cr(wished_cr):
136 payload = {
137 "project_id": wished_cr["project_id"],
138 "name": wished_cr["name"],
139 "description": wished_cr["description"],
140 "is_public": wished_cr["privacy_policy"] == "public",
141 }
142
143 return payload
144
145
146 def absent_strategy(api, wished_cr):
147 changed = False
148
149 cr_list = api.fetch_all_resources("namespaces")
150 cr_lookup = {cr["name"]: cr for cr in cr_list}
151
152 if wished_cr["name"] not in cr_lookup:
153 return changed, {}
154
155 target_cr = cr_lookup[wished_cr["name"]]
156 changed = True
157 if api.module.check_mode:
158 return changed, {"status": "Container registry would be destroyed"}
159
160 api.wait_to_complete_state_transition(resource=target_cr, stable_states=STABLE_STATES, force_wait=True)
161 response = api.delete(path=f"{api.api_path}/{target_cr['id']}")
162 if not response.ok:
163 api.module.fail_json(msg=f"Error deleting container registry [{response.status_code}: {response.json}]")
164
165 api.wait_to_complete_state_transition(resource=target_cr, stable_states=STABLE_STATES)
166 return changed, response.json
167
168
169 def present_strategy(api, wished_cr):
170 changed = False
171
172 cr_list = api.fetch_all_resources("namespaces")
173 cr_lookup = {cr["name"]: cr for cr in cr_list}
174
175 payload_cr = payload_from_wished_cr(wished_cr)
176
177 if wished_cr["name"] not in cr_lookup:
178 changed = True
179 if api.module.check_mode:
180 return changed, {"status": "A container registry would be created."}
181
182 # Create container registry
183 api.warn(payload_cr)
184 creation_response = api.post(path=api.api_path, data=payload_cr)
185
186 if not creation_response.ok:
187 msg = f"Error during container registry creation: {creation_response.info['msg']}: '{creation_response.json['message']}' ({creation_response.json})"
188 api.module.fail_json(msg=msg)
189
190 api.wait_to_complete_state_transition(resource=creation_response.json, stable_states=STABLE_STATES)
191 response = api.get(path=f"{api.api_path}/{creation_response.json['id']}")
192 return changed, response.json
193
194 target_cr = cr_lookup[wished_cr["name"]]
195 patch_payload = resource_attributes_should_be_changed(
196 target=target_cr,
197 wished=payload_cr,
198 verifiable_mutable_attributes=MUTABLE_ATTRIBUTES,
199 mutable_attributes=MUTABLE_ATTRIBUTES,
200 )
201
202 if not patch_payload:
203 return changed, target_cr
204
205 changed = True
206 if api.module.check_mode:
207 return changed, {"status": "Container registry attributes would be changed."}
208
209 cr_patch_response = api.patch(path=f"{api.api_path}/{target_cr['id']}", data=patch_payload)
210
211 if not cr_patch_response.ok:
212 api.module.fail_json(
213 msg=f"Error during container registry attributes update: [{cr_patch_response.status_code}: {cr_patch_response.json['message']}]"
214 )
215
216 api.wait_to_complete_state_transition(resource=target_cr, stable_states=STABLE_STATES)
217 response = api.get(path=f"{api.api_path}/{target_cr['id']}")
218 return changed, response.json
219
220
221 state_strategy = {"present": present_strategy, "absent": absent_strategy}
222
223
224 def core(module):
225 region = module.params["region"]
226 wished_container_registry = {
227 "state": module.params["state"],
228 "project_id": module.params["project_id"],
229 "name": module.params["name"],
230 "description": module.params["description"],
231 "privacy_policy": module.params["privacy_policy"],
232 }
233
234 api = Scaleway(module=module)
235 api.api_path = f"registry/v1/regions/{region}/namespaces"
236
237 changed, summary = state_strategy[wished_container_registry["state"]](api=api, wished_cr=wished_container_registry)
238
239 module.exit_json(changed=changed, container_registry=summary)
240
241
242 def main():
243 argument_spec = scaleway_argument_spec()
244 argument_spec.update(scaleway_waitable_resource_argument_spec())
245 argument_spec.update(
246 dict(
247 state=dict(type="str", default="present", choices=["absent", "present"]),
248 project_id=dict(type="str", required=True),
249 region=dict(type="str", required=True, choices=SCALEWAY_REGIONS),
250 name=dict(type="str", required=True),
251 description=dict(type="str", default=""),
252 privacy_policy=dict(type="str", default="private", choices=["public", "private"]),
253 )
254 )
255 module = AnsibleModule(
256 argument_spec=argument_spec,
257 supports_check_mode=True,
258 )
259
260 core(module)
261
262
263 if __name__ == "__main__":
264 main()