Skip to content

Commit 3eebb84

Browse files
committed
Replace code for old API with hard-coded 'storagebox ID unknown'.
1 parent fe60d91 commit 3eebb84

20 files changed

+294
-3115
lines changed

changelogs/fragments/173-storagebox.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@ deprecated_features:
22
- "storagebox* modules - the ``hetzner_user`` and ``hetzner_pass`` options for these modules are deprecated; support will be removed in community.hrobot 3.0.0. Use ``hetzner_token`` instead (https://github.com/ansible-collections/community.hrobot/pull/173)."
33
- "storagebox* modules - the ``hetzner_token`` option for these modules will be required from community.hrobot 3.0.0 on (https://github.com/ansible-collections/community.hrobot/pull/173)."
44
- "storagebox* modules - membership in the ``community.hrobot.robot`` action group (module defaults group) is deprecated; the modules will be removed from the group in community.hrobot 3.0.0. Use ``community.hrobot.api`` instead (https://github.com/ansible-collections/community.hrobot/pull/173)."
5+
minor_changes:
6+
- "storagebox* modules - the code for the old API (that has been removed by Hetzner) has been replaced by hard-coding the result of the API, namely that no storagebox of this ID exists (https://github.com/ansible-collections/community.hrobot/pull/173)."

plugins/modules/storagebox.py

Lines changed: 59 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -122,13 +122,10 @@
122122

123123
from ansible.module_utils.basic import AnsibleModule
124124
from ansible.module_utils.common.text.converters import to_native
125-
from ansible.module_utils.six.moves.urllib.parse import urlencode
126125

127126
from ansible_collections.community.hrobot.plugins.module_utils.robot import (
128-
BASE_URL,
129127
ROBOT_DEFAULT_ARGUMENT_SPEC,
130128
_ROBOT_DEFAULT_ARGUMENT_SPEC_COMPAT_DEPRECATED,
131-
fetch_url_json,
132129
)
133130

134131
from ansible_collections.community.hrobot.plugins.module_utils.api import (
@@ -141,15 +138,6 @@
141138
)
142139

143140

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-
153141
UPDATE_PARAMETERS = {
154142
'name': ('name', ['name'], 'name'),
155143
}
@@ -166,11 +154,6 @@
166154
PARAMETERS.update(ACTION_PARAMETERS)
167155

168156

169-
def extract_legacy(result):
170-
sb = result['storagebox']
171-
return {key: sb.get(key) for key, dummy in PARAMETERS_LEGACY.values()}
172-
173-
174157
def extract(result):
175158
sb = result['storage_box']
176159

@@ -217,101 +200,68 @@ def main():
217200
version="3.0.0",
218201
)
219202
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+
)
258244
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(
287256
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,
293262
)
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))
315265

316266
result = dict(after)
317267
result['changed'] = bool(changes)

plugins/modules/storagebox_info.py

Lines changed: 10 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -517,13 +517,10 @@
517517
"""
518518

519519
from ansible.module_utils.basic import AnsibleModule
520-
from ansible.module_utils.six.moves.urllib.parse import urlencode
521520

522521
from ansible_collections.community.hrobot.plugins.module_utils.robot import (
523-
BASE_URL,
524522
ROBOT_DEFAULT_ARGUMENT_SPEC,
525523
_ROBOT_DEFAULT_ARGUMENT_SPEC_COMPAT_DEPRECATED,
526-
fetch_url_json,
527524
)
528525

529526
from ansible_collections.community.hrobot.plugins.module_utils.api import (
@@ -589,49 +586,18 @@ def main():
589586
version="3.0.0",
590587
)
591588
if module.params["hetzner_user"] is not None:
592-
# DEPRECATED: old API
593-
if storagebox_id is not None:
594-
storagebox_ids = [storagebox_id]
595-
else:
596-
url = "{0}/storagebox".format(BASE_URL)
597-
data = None
598-
headers = None
599-
if linked_server_number is not None:
600-
data = urlencode({
601-
"linked_server": linked_server_number,
602-
})
603-
headers = {
604-
"Content-type": "application/x-www-form-urlencoded",
605-
}
606-
result, error = fetch_url_json(module, url, accept_errors=['STORAGEBOX_NOT_FOUND'], data=data)
607-
storagebox_ids = []
608-
if not error:
609-
# When filtering by linked_server, the result should be a dictionary
610-
if isinstance(result, dict):
611-
result = [result]
612-
for entry in result:
613-
if full_info:
614-
storagebox_ids.append(entry['storagebox']['id'])
615-
else:
616-
storageboxes.append(entry['storagebox'])
617-
618-
for storagebox_id in storagebox_ids:
619-
url = "{0}/storagebox/{1}".format(BASE_URL, storagebox_id)
620-
result, error = fetch_url_json(module, url, accept_errors=['STORAGEBOX_NOT_FOUND'])
621-
if not error:
622-
storageboxes.append(result['storagebox'])
589+
module.warn("The old storagebox API has been disabled by Hetzner. The supporting code has been removed.")
590+
module.exit_json(changed=False, storageboxes=[])
623591

592+
if storagebox_id is not None:
593+
url = "{0}/v1/storage_boxes/{1}".format(API_BASE_URL, storagebox_id)
594+
result, dummy, error = api_fetch_url_json(module, url, accept_errors=["not_found"])
595+
if error is None:
596+
storageboxes = [result["storage_box"]]
624597
else:
625-
# NEW API!
626-
if storagebox_id is not None:
627-
url = "{0}/v1/storage_boxes/{1}".format(API_BASE_URL, storagebox_id)
628-
result, dummy, error = api_fetch_url_json(module, url, accept_errors=["not_found"])
629-
if error is None:
630-
storageboxes = [result["storage_box"]]
631-
else:
632-
url = "{0}/v1/storage_boxes".format(API_BASE_URL)
633-
storageboxes, dummy = api_fetch_url_json_list(module, url, data_key="storage_boxes")
634-
storageboxes = [add_hrobot_compat_shim(storagebox) for storagebox in storageboxes]
598+
url = "{0}/v1/storage_boxes".format(API_BASE_URL)
599+
storageboxes, dummy = api_fetch_url_json_list(module, url, data_key="storage_boxes")
600+
storageboxes = [add_hrobot_compat_shim(storagebox) for storagebox in storageboxes]
635601

636602
module.exit_json(
637603
changed=False,

plugins/modules/storagebox_set_password.py

Lines changed: 24 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,10 @@
8181
"""
8282

8383
from ansible.module_utils.basic import AnsibleModule
84-
from ansible.module_utils.six.moves.urllib.parse import urlencode
8584

8685
from ansible_collections.community.hrobot.plugins.module_utils.robot import (
87-
BASE_URL,
8886
ROBOT_DEFAULT_ARGUMENT_SPEC,
8987
_ROBOT_DEFAULT_ARGUMENT_SPEC_COMPAT_DEPRECATED,
90-
fetch_url_json,
9188
)
9289

9390
from ansible_collections.community.hrobot.plugins.module_utils.api import (
@@ -124,51 +121,30 @@ def main():
124121
version="3.0.0",
125122
)
126123
if module.params["hetzner_user"] is not None:
127-
# DEPRECATED: old API
128-
url = "{0}/storagebox/{1}/password".format(BASE_URL, id)
129-
accepted_errors = ["STORAGEBOX_NOT_FOUND", "STORAGEBOX_INVALID_PASSWORD"]
130-
131-
if password:
132-
headers = {"Content-type": "application/x-www-form-urlencoded"}
133-
result, error = fetch_url_json(
134-
module, url, method="POST", accept_errors=accepted_errors, data=urlencode({"password": password}), headers=headers)
135-
else:
136-
result, error = fetch_url_json(
137-
module, url, method="POST", accept_errors=accepted_errors)
138-
139-
if error == 'STORAGEBOX_NOT_FOUND':
140-
module.fail_json(
141-
msg='Storage Box with ID {0} not found'.format(id))
142-
143-
if error == 'STORAGEBOX_INVALID_PASSWORD':
144-
module.fail_json(
145-
msg="The chosen password has been considered insecure or does not comply with Hetzner's password guideline")
146-
147-
module.exit_json(changed=True, password=result["password"])
148-
149-
else:
150-
# NEW API!
151-
action_url = "{0}/v1/storage_boxes/{1}/actions/reset_password".format(API_BASE_URL, id)
152-
action = {
153-
"password": password,
154-
}
155-
try:
156-
dummy, error = api_apply_action(
157-
module,
158-
action_url,
159-
action,
160-
lambda action_id: "{0}/v1/storage_boxes/actions/{1}".format(API_BASE_URL, action_id),
161-
check_done_delay=1,
162-
check_done_timeout=60,
163-
accept_errors=["not_found"],
164-
)
165-
except ApplyActionError as exc:
166-
module.fail_json(msg='Error while resetting password: {0}'.format(exc))
167-
168-
if error == "not_found":
169-
module.fail_json(msg='Storage Box with ID {0} not found'.format(id))
170-
171-
module.exit_json(changed=True, password=password)
124+
module.warn("The old storagebox API has been disabled by Hetzner. The supporting code has been removed.")
125+
module.fail_json(msg='Storage Box with ID {0} not found'.format(id))
126+
127+
action_url = "{0}/v1/storage_boxes/{1}/actions/reset_password".format(API_BASE_URL, id)
128+
action = {
129+
"password": password,
130+
}
131+
try:
132+
dummy, error = api_apply_action(
133+
module,
134+
action_url,
135+
action,
136+
lambda action_id: "{0}/v1/storage_boxes/actions/{1}".format(API_BASE_URL, action_id),
137+
check_done_delay=1,
138+
check_done_timeout=60,
139+
accept_errors=["not_found"],
140+
)
141+
except ApplyActionError as exc:
142+
module.fail_json(msg='Error while resetting password: {0}'.format(exc))
143+
144+
if error == "not_found":
145+
module.fail_json(msg='Storage Box with ID {0} not found'.format(id))
146+
147+
module.exit_json(changed=True, password=password)
172148

173149

174150
if __name__ == '__main__': # pragma: no cover

0 commit comments

Comments
 (0)