Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 #
3 # Scaleway user data 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_user_data
15 short_description: Scaleway user_data management module
16 author: Remy Leone (@remyleone)
17 description:
18 - This module manages user_data on compute instances on Scaleway.
19 - It can be used to configure cloud-init for instance.
20 extends_documentation_fragment:
21 - community.general._scaleway
22 - community.general._attributes
23 - community.general._scaleway.actiongroup_scaleway
24
25 attributes:
26 check_mode:
27 support: full
28 diff_mode:
29 support: none
30 action_group:
31 version_added: 11.3.0
32
33 options:
34
35 server_id:
36 type: str
37 description:
38 - Scaleway Compute instance ID of the server.
39 required: true
40
41 user_data:
42 type: dict
43 description:
44 - User defined data. Typically used with C(cloud-init).
45 - Pass your C(cloud-init) script here as a string.
46
47 region:
48 type: str
49 description:
50 - Scaleway compute zone.
51 required: true
52 choices:
53 - ams1
54 - EMEA-NL-EVS
55 - ams2
56 - ams3
57 - par1
58 - EMEA-FR-PAR1
59 - par2
60 - EMEA-FR-PAR2
61 - par3
62 - waw1
63 - EMEA-PL-WAW1
64 - waw2
65 - waw3
66 """
67
68 EXAMPLES = r"""
69 - name: Update the cloud-init
70 community.general.scaleway_user_data:
71 server_id: '5a33b4ab-57dd-4eb6-8b0a-d95eb63492ce'
72 region: ams1
73 user_data:
74 cloud-init: 'final_message: "Hello World!"'
75 """
76
77 RETURN = r"""
78 """
79
80 from ansible.module_utils.basic import AnsibleModule
81
82 from ansible_collections.community.general.plugins.module_utils._scaleway import (
83 SCALEWAY_LOCATION,
84 Scaleway,
85 scaleway_argument_spec,
86 )
87
88
89 def patch_user_data(compute_api, server_id, key, value):
90 compute_api.module.debug("Starting patching user_data attributes")
91
92 path = f"servers/{server_id}/user_data/{key}"
93 response = compute_api.patch(path=path, data=value, headers={"Content-Type": "text/plain"})
94 if not response.ok:
95 msg = f"Error during user_data patching: {response.status_code} {response.body}"
96 compute_api.module.fail_json(msg=msg)
97
98 return response
99
100
101 def delete_user_data(compute_api, server_id, key):
102 compute_api.module.debug(f"Starting deleting user_data attributes: {key}")
103
104 response = compute_api.delete(path=f"servers/{server_id}/user_data/{key}")
105
106 if not response.ok:
107 msg = f"Error during user_data deleting: ({response.status_code}) {response.body}"
108 compute_api.module.fail_json(msg=msg)
109
110 return response
111
112
113 def get_user_data(compute_api, server_id, key):
114 compute_api.module.debug("Starting patching user_data attributes")
115
116 path = f"servers/{server_id}/user_data/{key}"
117 response = compute_api.get(path=path)
118 if not response.ok:
119 msg = f"Error during user_data patching: {response.status_code} {response.body}"
120 compute_api.module.fail_json(msg=msg)
121
122 return response.json
123
124
125 def core(module):
126 region = module.params["region"]
127 server_id = module.params["server_id"]
128 user_data = module.params["user_data"]
129 changed = False
130
131 module.params["api_url"] = SCALEWAY_LOCATION[region]["api_endpoint"]
132 compute_api = Scaleway(module=module)
133
134 user_data_list = compute_api.get(path=f"servers/{server_id}/user_data")
135 if not user_data_list.ok:
136 msg = f"Error during user_data fetching: {user_data_list.status_code} {user_data_list.body}"
137 compute_api.module.fail_json(msg=msg)
138
139 present_user_data_keys = user_data_list.json["user_data"]
140 present_user_data = {
141 key: get_user_data(compute_api=compute_api, server_id=server_id, key=key) for key in present_user_data_keys
142 }
143
144 if present_user_data == user_data:
145 module.exit_json(changed=changed, msg=user_data_list.json)
146
147 # First we remove keys that are not defined in the wished user_data
148 for key in present_user_data:
149 if key not in user_data:
150 changed = True
151 if compute_api.module.check_mode:
152 module.exit_json(changed=changed, msg={"status": f"User-data of {server_id} would be patched."})
153
154 delete_user_data(compute_api=compute_api, server_id=server_id, key=key)
155
156 # Then we patch keys that are different
157 for key, value in user_data.items():
158 if key not in present_user_data or value != present_user_data[key]:
159 changed = True
160 if compute_api.module.check_mode:
161 module.exit_json(changed=changed, msg={"status": f"User-data of {server_id} would be patched."})
162
163 patch_user_data(compute_api=compute_api, server_id=server_id, key=key, value=value)
164
165 module.exit_json(changed=changed, msg=user_data)
166
167
168 def main():
169 argument_spec = scaleway_argument_spec()
170 argument_spec.update(
171 dict(
172 region=dict(required=True, choices=list(SCALEWAY_LOCATION.keys())),
173 user_data=dict(type="dict"),
174 server_id=dict(required=True),
175 )
176 )
177 module = AnsibleModule(
178 argument_spec=argument_spec,
179 supports_check_mode=True,
180 )
181
182 core(module)
183
184
185 if __name__ == "__main__":
186 main()