| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright (c) 2025 Felix Fontein <[email protected]> |
| 5 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 7 |
|
| 8 |
from __future__ import absolute_import, division, print_function |
| 9 |
__metaclass__ = type |
| 10 |
|
| 11 |
|
| 12 |
DOCUMENTATION = r""" |
| 13 |
module: storagebox_snapshot_plan_info |
| 14 |
short_description: Query the snapshot plans for a storage box |
| 15 |
version_added: 2.1.0 |
| 16 |
author: |
| 17 |
- Felix Fontein (@felixfontein) |
| 18 |
description: |
| 19 |
- Query the snapshot plans for a storage box. |
| 20 |
extends_documentation_fragment: |
| 21 |
- community.hrobot.api._robot_compat_shim_deprecation # must come before api and robot |
| 22 |
- community.hrobot.api |
| 23 |
- community.hrobot.robot |
| 24 |
- community.hrobot.attributes |
| 25 |
- community.hrobot.attributes._actiongroup_robot_and_api_deprecation # must come before the other two! |
| 26 |
- community.hrobot.attributes.actiongroup_api |
| 27 |
- community.hrobot.attributes.actiongroup_robot |
| 28 |
- community.hrobot.attributes.idempotent_not_modify_state |
| 29 |
- community.hrobot.attributes.info_module |
| 30 |
|
| 31 |
options: |
| 32 |
hetzner_token: |
| 33 |
version_added: 2.5.0 |
| 34 |
storagebox_id: |
| 35 |
description: |
| 36 |
- The ID of the storage box to query. |
| 37 |
type: int |
| 38 |
required: true |
| 39 |
""" |
| 40 |
|
| 41 |
EXAMPLES = r""" |
| 42 |
--- |
| 43 |
- name: Query the snapshot plans |
| 44 |
community.hrobot.storagebox_snapshot_plan_info: |
| 45 |
hetzner_user: foo |
| 46 |
hetzner_password: bar |
| 47 |
id: 123 |
| 48 |
register: result |
| 49 |
|
| 50 |
- name: Output data |
| 51 |
ansible.builtin.debug: |
| 52 |
msg: "Status of the first snapshot plan: {{ result.plans[0].status }}" |
| 53 |
""" |
| 54 |
|
| 55 |
RETURN = r""" |
| 56 |
plans: |
| 57 |
description: |
| 58 |
- The storage box's snapshot plan configurations. |
| 59 |
- All date and time parameters are in UTC. |
| 60 |
returned: success |
| 61 |
type: list |
| 62 |
elements: dict |
| 63 |
contains: |
| 64 |
status: |
| 65 |
description: |
| 66 |
- The status of the snapshot plan. |
| 67 |
type: str |
| 68 |
sample: enabled |
| 69 |
returned: success |
| 70 |
choices: |
| 71 |
- enabled |
| 72 |
- disabled |
| 73 |
minute: |
| 74 |
description: |
| 75 |
- The minute of execution of the plan. |
| 76 |
type: int |
| 77 |
sample: 5 |
| 78 |
returned: success |
| 79 |
hour: |
| 80 |
description: |
| 81 |
- The hour of execution of the plan. |
| 82 |
type: int |
| 83 |
sample: 12 |
| 84 |
returned: success |
| 85 |
day_of_week: |
| 86 |
description: |
| 87 |
- The day of the week of execution of the plan. V(1) is Monday, V(7) is Sunday. |
| 88 |
- If set to V(null), the plan is run every day of a week, unless there are other restrictions. |
| 89 |
type: int |
| 90 |
sample: 2 |
| 91 |
returned: success |
| 92 |
day_of_month: |
| 93 |
description: |
| 94 |
- The day of month of execution of the plan. V(1) is the 1st day of the month. |
| 95 |
- If set to V(null), the plan is run every day of a month, unless there are other restrictions. |
| 96 |
type: int |
| 97 |
sample: null |
| 98 |
returned: success |
| 99 |
month: |
| 100 |
description: |
| 101 |
- The month of execution of the plan. V(1) is January, V(12) is December. |
| 102 |
- If set to V(null), the plan is run every month. |
| 103 |
- Always V(null) if O(hetzner_token) is provided. |
| 104 |
- B(This return value is deprecated and will be removed from community.hrobot 3.0.0.) |
| 105 |
If you are using ansible-core 2.19 or newer, you will see a deprecation message when using this return value when using O(hetzner_token). |
| 106 |
type: int |
| 107 |
sample: null |
| 108 |
returned: success |
| 109 |
max_snapshots: |
| 110 |
description: |
| 111 |
- The maximum number of automatic snapshots of this plan. |
| 112 |
type: int |
| 113 |
sample: 2 |
| 114 |
returned: success |
| 115 |
""" |
| 116 |
|
| 117 |
from ansible.module_utils.basic import AnsibleModule |
| 118 |
|
| 119 |
from ansible_collections.community.hrobot.plugins.module_utils.robot import ( |
| 120 |
BASE_URL, |
| 121 |
ROBOT_DEFAULT_ARGUMENT_SPEC, |
| 122 |
_ROBOT_DEFAULT_ARGUMENT_SPEC_COMPAT_DEPRECATED, |
| 123 |
fetch_url_json, |
| 124 |
) |
| 125 |
|
| 126 |
from ansible_collections.community.hrobot.plugins.module_utils.api import ( |
| 127 |
API_BASE_URL, |
| 128 |
API_DEFAULT_ARGUMENT_SPEC, |
| 129 |
_API_DEFAULT_ARGUMENT_SPEC_COMPAT, |
| 130 |
api_fetch_url_json, |
| 131 |
) |
| 132 |
|
| 133 |
from ansible_collections.community.hrobot.plugins.module_utils._tagging import ( |
| 134 |
deprecate_value, |
| 135 |
) |
| 136 |
|
| 137 |
|
| 138 |
def extract_legacy(result): |
| 139 |
sb = result['snapshotplan'] |
| 140 |
return { |
| 141 |
'status': sb['status'], |
| 142 |
'minute': sb['minute'], |
| 143 |
'hour': sb['hour'], |
| 144 |
'day_of_week': sb['day_of_week'], |
| 145 |
'day_of_month': sb['day_of_month'], |
| 146 |
'month': sb['month'], |
| 147 |
'max_snapshots': sb['max_snapshots'], |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
def extract(result): |
| 152 |
sb = result['storage_box'] |
| 153 |
sp = sb.get('snapshot_plan') |
| 154 |
if not sp: |
| 155 |
return { |
| 156 |
'status': 'disabled', |
| 157 |
'minute': None, |
| 158 |
'hour': None, |
| 159 |
'day_of_week': None, |
| 160 |
'day_of_month': None, |
| 161 |
'month': deprecate_value(None, "The return value `month` is deprecated; it is always null.", version="3.0.0"), |
| 162 |
'max_snapshots': None, |
| 163 |
} |
| 164 |
|
| 165 |
return { |
| 166 |
'status': 'enabled', |
| 167 |
'minute': sp['minute'], |
| 168 |
'hour': sp['hour'], |
| 169 |
'day_of_week': sp['day_of_week'], |
| 170 |
'day_of_month': sp['day_of_month'], |
| 171 |
'month': deprecate_value(None, "The return value `month` is deprecated; it is always null.", version="3.0.0"), |
| 172 |
'max_snapshots': sp['max_snapshots'], |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
def main(): |
| 177 |
argument_spec = dict( |
| 178 |
storagebox_id=dict(type='int', required=True), |
| 179 |
) |
| 180 |
argument_spec.update(ROBOT_DEFAULT_ARGUMENT_SPEC) |
| 181 |
argument_spec.update(_ROBOT_DEFAULT_ARGUMENT_SPEC_COMPAT_DEPRECATED) |
| 182 |
argument_spec.update(API_DEFAULT_ARGUMENT_SPEC) |
| 183 |
argument_spec.update(_API_DEFAULT_ARGUMENT_SPEC_COMPAT) |
| 184 |
module = AnsibleModule( |
| 185 |
argument_spec=argument_spec, |
| 186 |
supports_check_mode=True, |
| 187 |
) |
| 188 |
|
| 189 |
storagebox_id = module.params['storagebox_id'] |
| 190 |
|
| 191 |
if module.params["hetzner_user"] is not None: |
| 192 |
module.deprecate( |
| 193 |
"The hetzner_token parameter will be required from community.hrobot 3.0.0 on.", |
| 194 |
collection_name="community.hrobot", |
| 195 |
version="3.0.0", |
| 196 |
) |
| 197 |
# DEPRECATED: old API |
| 198 |
url = "{0}/storagebox/{1}/snapshotplan".format(BASE_URL, storagebox_id) |
| 199 |
result, error = fetch_url_json(module, url, accept_errors=['STORAGEBOX_NOT_FOUND']) |
| 200 |
if error: |
| 201 |
module.fail_json(msg='Storagebox with ID {0} does not exist'.format(storagebox_id)) |
| 202 |
|
| 203 |
# The documentation (https://robot.hetzner.com/doc/webservice/en.html#get-storagebox-storagebox-id-snapshotplan) |
| 204 |
# claims that the result is a list, but actually it is a dictionary. Convert it to a list of dicts if that's the case. |
| 205 |
if isinstance(result, dict): |
| 206 |
result = [result] |
| 207 |
|
| 208 |
plans = [extract_legacy(plan) for plan in result] |
| 209 |
|
| 210 |
else: |
| 211 |
# NEW API! |
| 212 |
url = "{0}/v1/storage_boxes/{1}".format(API_BASE_URL, storagebox_id) |
| 213 |
result, dummy, error = api_fetch_url_json(module, url, accept_errors=["not_found"]) |
| 214 |
if error: |
| 215 |
module.fail_json(msg='Storagebox with ID {0} does not exist'.format(storagebox_id)) |
| 216 |
|
| 217 |
plans = [extract(result)] |
| 218 |
|
| 219 |
module.exit_json( |
| 220 |
changed=False, |
| 221 |
plans=plans, |
| 222 |
) |
| 223 |
|
| 224 |
|
| 225 |
if __name__ == '__main__': # pragma: no cover |
| 226 |
main() # pragma: no cover |