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