Skip to content

Commit 57bb47d

Browse files
authored
Implement basic Hetzner API support for storagebox_subaccount[_info] (#169)
* Implement basic Hetzner API support for storagebox_subaccount_info. * Add changelog. * Add IDs to snapshot test. * Remove superfluous no_log. * Implement basic Hetzner API support for storagebox_subaccount. * Improve creation of snapshots and subaccounts by returning the created object.
1 parent b3612ba commit 57bb47d

12 files changed

+2369
-124
lines changed

antsibull-nox.toml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,7 @@ doc_fragment = "community.hrobot.attributes.actiongroup_robot"
4848
[[sessions.extra_checks.action_groups_config]]
4949
name = "api"
5050
pattern = "^storagebox.*$"
51-
exclusions = [
52-
"storagebox_subaccount_info",
53-
"storagebox_subaccount",
54-
]
51+
exclusions = []
5552
doc_fragment = "community.hrobot.attributes.actiongroup_api"
5653

5754
[sessions.build_import_check]

changelogs/fragments/166-storagebox.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@ minor_changes:
1616
the Robot API for storage boxes is removed by Hetzner
1717
(https://github.com/ansible-collections/community.hrobot/pull/166,
1818
https://github.com/ansible-collections/community.hrobot/pull/167,
19-
https://github.com/ansible-collections/community.hrobot/pull/168).
19+
https://github.com/ansible-collections/community.hrobot/pull/168,
20+
https://github.com/ansible-collections/community.hrobot/pull/169).
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
minor_changes:
2+
- "storagebox_subaccount - support the new Hetzner API (https://github.com/ansible-collections/community.hrobot/pull/168)."
3+
- "storagebox_subaccount - no longer mark ``password_mode`` as ``no_log`` (https://github.com/ansible-collections/community.hrobot/pull/168)."
4+
- "storagebox_subaccount_info - support the new Hetzner API (https://github.com/ansible-collections/community.hrobot/pull/168)."

meta/runtime.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,5 @@ action_groups:
3636
- storagebox_snapshot_info
3737
- storagebox_snapshot_plan
3838
- storagebox_snapshot_plan_info
39+
- storagebox_subaccount
40+
- storagebox_subaccount_info

plugins/module_utils/api.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,9 @@ def api_fetch_url_json_with_retries(module, url, check_done_callback, check_done
257257

258258

259259
class ApplyActionError(Exception):
260-
pass
260+
def __init__(self, msg, extracted_ids=None):
261+
super(ApplyActionError, self).__init__(msg)
262+
self.extracted_ids = extracted_ids or {}
261263

262264

263265
def api_apply_action(
@@ -280,8 +282,11 @@ def api_apply_action(
280282
accept_errors=accept_errors,
281283
)
282284
if error:
283-
return error
285+
return None, error
284286
action_id = result["action"]["id"]
287+
extracted_ids = {
288+
res["type"]: res["id"] for res in result["action"]["resources"] or [] if res.get("id") is not None and res.get("type")
289+
}
285290
if result["action"]["status"] == "running":
286291
this_action_url = action_check_url_provider(action_id)
287292

@@ -295,10 +300,10 @@ def action_done_callback(result_, info_, error_):
295300
module, this_action_url, action_done_callback, check_done_delay=1, check_done_timeout=60, skip_first=True,
296301
)
297302
except CheckDoneTimeoutException as dummy:
298-
raise ApplyActionError("Timeout")
303+
raise ApplyActionError("Timeout", extracted_ids=extracted_ids)
299304
error = result["action"].get("error")
300305
if isinstance(error, dict):
301-
raise ApplyActionError('[{0}] {1}'.format(to_native(error.get("code")), to_native(error.get("message"))))
306+
raise ApplyActionError('[{0}] {1}'.format(to_native(error.get("code")), to_native(error.get("message"))), extracted_ids=extracted_ids)
302307
elif result["action"]["status"] == "error":
303-
raise ApplyActionError('Unknown error')
304-
return None
308+
raise ApplyActionError('Unknown error', extracted_ids=extracted_ids)
309+
return extracted_ids, None

plugins/modules/storagebox_set_password.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ def main():
147147
"password": password,
148148
}
149149
try:
150-
error = api_apply_action(
150+
dummy, error = api_apply_action(
151151
module,
152152
action_url,
153153
action,

plugins/modules/storagebox_snapshot.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,8 @@
8585
RETURN = r"""
8686
snapshot:
8787
description:
88-
- The snapshot that was created if O(hetzner_user) is provided.
89-
- The snapshot that was updated if O(hetzner_token) is provided.
90-
returned: success and O(state=present); if O(hetzner_token) is provided and O(snapshot_name) not, this will not be returned
88+
- The snapshot that was created or updated.
89+
returned: success and O(state=present)
9190
type: dict
9291
contains:
9392
name:
@@ -224,7 +223,7 @@ def main():
224223
if snapshot_comment:
225224
action["description"] = snapshot_comment
226225
try:
227-
error = api_apply_action(
226+
extracted_ids, error = api_apply_action(
228227
module,
229228
action_url,
230229
action,
@@ -239,7 +238,12 @@ def main():
239238
if error == "not_found":
240239
module.fail_json(msg="Storagebox with ID {0} does not exist".format(storagebox_id))
241240

242-
module.exit_json(changed=True)
241+
new_snapshot_id = extracted_ids["storage_box_snapshot"]
242+
# Retrieve created snapshot
243+
url = "{0}/v1/storage_boxes/{1}/snapshots/{2}".format(API_BASE_URL, storagebox_id, new_snapshot_id)
244+
snapshot = api_fetch_url_json(module, url, method='GET')[0]["snapshot"]
245+
246+
module.exit_json(changed=True, snapshot=extract_legacy(snapshot))
243247

244248
# Update snapshot comment
245249
elif state == 'present' and snapshot_name:

0 commit comments

Comments
 (0)