|
122 | 122 |
|
123 | 123 | from ansible.module_utils.basic import AnsibleModule |
124 | 124 | from ansible.module_utils.common.text.converters import to_native |
125 | | -from ansible.module_utils.six.moves.urllib.parse import urlencode |
126 | 125 |
|
127 | 126 | from ansible_collections.community.hrobot.plugins.module_utils.robot import ( |
128 | | - BASE_URL, |
129 | 127 | ROBOT_DEFAULT_ARGUMENT_SPEC, |
130 | 128 | _ROBOT_DEFAULT_ARGUMENT_SPEC_COMPAT_DEPRECATED, |
131 | | - fetch_url_json, |
132 | 129 | ) |
133 | 130 |
|
134 | 131 | from ansible_collections.community.hrobot.plugins.module_utils.api import ( |
|
141 | 138 | ) |
142 | 139 |
|
143 | 140 |
|
144 | | -PARAMETERS_LEGACY = { |
145 | | - 'name': ('name', 'storagebox_name'), |
146 | | - 'webdav': ('webdav', 'webdav'), |
147 | | - 'samba': ('samba', 'samba'), |
148 | | - 'ssh': ('ssh', 'ssh'), |
149 | | - 'external_reachability': ('external_reachability', 'external_reachability'), |
150 | | - 'zfs': ('zfs', 'zfs'), |
151 | | -} |
152 | | - |
153 | 141 | UPDATE_PARAMETERS = { |
154 | 142 | 'name': ('name', ['name'], 'name'), |
155 | 143 | } |
|
166 | 154 | PARAMETERS.update(ACTION_PARAMETERS) |
167 | 155 |
|
168 | 156 |
|
169 | | -def extract_legacy(result): |
170 | | - sb = result['storagebox'] |
171 | | - return {key: sb.get(key) for key, dummy in PARAMETERS_LEGACY.values()} |
172 | | - |
173 | | - |
174 | 157 | def extract(result): |
175 | 158 | sb = result['storage_box'] |
176 | 159 |
|
@@ -217,101 +200,68 @@ def main(): |
217 | 200 | version="3.0.0", |
218 | 201 | ) |
219 | 202 | if module.params["hetzner_user"] is not None: |
220 | | - # DEPRECATED: old API |
221 | | - url = "{0}/storagebox/{1}".format(BASE_URL, storagebox_id) |
222 | | - result, error = fetch_url_json(module, url, accept_errors=['STORAGEBOX_NOT_FOUND']) |
223 | | - if error: |
224 | | - module.fail_json(msg='Storagebox with ID {0} does not exist'.format(storagebox_id)) |
225 | | - |
226 | | - before = extract_legacy(result) |
227 | | - after = dict(before) |
228 | | - |
229 | | - for option_name, (data_name, change_name) in PARAMETERS_LEGACY.items(): |
230 | | - value = module.params[option_name] |
231 | | - if value is not None: |
232 | | - if before[data_name] != value: |
233 | | - after[data_name] = value |
234 | | - if isinstance(value, bool): |
235 | | - changes[change_name] = str(value).lower() |
236 | | - else: |
237 | | - changes[change_name] = value |
238 | | - |
239 | | - if changes and not module.check_mode: |
240 | | - headers = {"Content-type": "application/x-www-form-urlencoded"} |
241 | | - result, error = fetch_url_json( |
242 | | - module, |
243 | | - url, |
244 | | - data=urlencode(changes), |
245 | | - headers=headers, |
246 | | - method='POST', |
247 | | - accept_errors=['INVALID_INPUT'], |
248 | | - ) |
249 | | - if error: |
250 | | - invalid = result['error'].get('invalid') or [] |
251 | | - module.fail_json(msg='The values to update were invalid ({0})'.format(', '.join(invalid))) |
252 | | - after = extract_legacy(result) |
253 | | - |
254 | | - else: |
255 | | - # NEW API! |
256 | | - url = "{0}/v1/storage_boxes/{1}".format(API_BASE_URL, storagebox_id) |
257 | | - result, dummy, error = api_fetch_url_json(module, url, accept_errors=['not_found']) |
| 203 | + module.warn("The old storagebox API has been disabled by Hetzner. The supporting code has been removed.") |
| 204 | + module.fail_json(msg='Storagebox with ID {0} does not exist'.format(storagebox_id)) |
| 205 | + |
| 206 | + url = "{0}/v1/storage_boxes/{1}".format(API_BASE_URL, storagebox_id) |
| 207 | + result, dummy, error = api_fetch_url_json(module, url, accept_errors=['not_found']) |
| 208 | + if error: |
| 209 | + module.fail_json(msg='Storagebox with ID {0} does not exist'.format(storagebox_id)) |
| 210 | + |
| 211 | + before = extract(result) |
| 212 | + after = dict(before) |
| 213 | + |
| 214 | + update = {} |
| 215 | + for option_name, (data_name, dummy, change_name) in UPDATE_PARAMETERS.items(): |
| 216 | + value = module.params[option_name] |
| 217 | + if value is not None: |
| 218 | + if before[data_name] != value: |
| 219 | + after[data_name] = value |
| 220 | + changes[change_name] = value |
| 221 | + update[change_name] = value |
| 222 | + |
| 223 | + action = {} |
| 224 | + update_after_update = {} |
| 225 | + for option_name, (data_name, dummy, change_name) in ACTION_PARAMETERS.items(): |
| 226 | + value = module.params[option_name] |
| 227 | + if value is not None: |
| 228 | + if before[data_name] != value: |
| 229 | + after[data_name] = value |
| 230 | + update_after_update[data_name] = value |
| 231 | + changes[change_name] = value |
| 232 | + action[change_name] = value |
| 233 | + |
| 234 | + if update and not module.check_mode: |
| 235 | + headers = {"Content-type": "application/json"} |
| 236 | + result, dummy, error = api_fetch_url_json( |
| 237 | + module, |
| 238 | + url, |
| 239 | + data=module.jsonify(update), |
| 240 | + headers=headers, |
| 241 | + method='PUT', |
| 242 | + accept_errors=['invalid_input'], |
| 243 | + ) |
258 | 244 | if error: |
259 | | - module.fail_json(msg='Storagebox with ID {0} does not exist'.format(storagebox_id)) |
260 | | - |
261 | | - before = extract(result) |
262 | | - after = dict(before) |
263 | | - |
264 | | - update = {} |
265 | | - for option_name, (data_name, dummy, change_name) in UPDATE_PARAMETERS.items(): |
266 | | - value = module.params[option_name] |
267 | | - if value is not None: |
268 | | - if before[data_name] != value: |
269 | | - after[data_name] = value |
270 | | - changes[change_name] = value |
271 | | - update[change_name] = value |
272 | | - |
273 | | - action = {} |
274 | | - update_after_update = {} |
275 | | - for option_name, (data_name, dummy, change_name) in ACTION_PARAMETERS.items(): |
276 | | - value = module.params[option_name] |
277 | | - if value is not None: |
278 | | - if before[data_name] != value: |
279 | | - after[data_name] = value |
280 | | - update_after_update[data_name] = value |
281 | | - changes[change_name] = value |
282 | | - action[change_name] = value |
283 | | - |
284 | | - if update and not module.check_mode: |
285 | | - headers = {"Content-type": "application/json"} |
286 | | - result, dummy, error = api_fetch_url_json( |
| 245 | + details = result['error'].get('details') or {} |
| 246 | + fields = details.get("fields") or [] |
| 247 | + details_str = ", ".join(['{0}: {1}'.format(to_native(field["name"]), to_native(field["message"])) for field in fields]) |
| 248 | + module.fail_json(msg='The values to update were invalid ({0})'.format(details_str or "no details")) |
| 249 | + after = extract(result) |
| 250 | + |
| 251 | + if action and not module.check_mode: |
| 252 | + after.update(update_after_update) |
| 253 | + action_url = "{0}/actions/update_access_settings".format(url) |
| 254 | + try: |
| 255 | + api_apply_action( |
287 | 256 | module, |
288 | | - url, |
289 | | - data=module.jsonify(update), |
290 | | - headers=headers, |
291 | | - method='PUT', |
292 | | - accept_errors=['invalid_input'], |
| 257 | + action_url, |
| 258 | + action, |
| 259 | + lambda action_id: "{0}/v1/storage_boxes/actions/{1}".format(API_BASE_URL, action_id), |
| 260 | + check_done_delay=1, |
| 261 | + check_done_timeout=60, |
293 | 262 | ) |
294 | | - if error: |
295 | | - details = result['error'].get('details') or {} |
296 | | - fields = details.get("fields") or [] |
297 | | - details_str = ", ".join(['{0}: {1}'.format(to_native(field["name"]), to_native(field["message"])) for field in fields]) |
298 | | - module.fail_json(msg='The values to update were invalid ({0})'.format(details_str or "no details")) |
299 | | - after = extract(result) |
300 | | - |
301 | | - if action and not module.check_mode: |
302 | | - after.update(update_after_update) |
303 | | - action_url = "{0}/actions/update_access_settings".format(url) |
304 | | - try: |
305 | | - api_apply_action( |
306 | | - module, |
307 | | - action_url, |
308 | | - action, |
309 | | - lambda action_id: "{0}/v1/storage_boxes/actions/{1}".format(API_BASE_URL, action_id), |
310 | | - check_done_delay=1, |
311 | | - check_done_timeout=60, |
312 | | - ) |
313 | | - except ApplyActionError as exc: |
314 | | - module.fail_json(msg='Error while updating access settings: {0}'.format(exc)) |
| 263 | + except ApplyActionError as exc: |
| 264 | + module.fail_json(msg='Error while updating access settings: {0}'.format(exc)) |
315 | 265 |
|
316 | 266 | result = dict(after) |
317 | 267 | result['changed'] = bool(changes) |
|
0 commit comments