Debian Codegraph @

ansible_collections/community/hrobot/plugins/modules/storagebox_info.py

raw
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_info
14 short_description: Query information on one or more storage boxes
15 version_added: 2.1.0
16 author:
17 - Felix Fontein (@felixfontein)
18 description:
19 - Query information on one or more 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 - Limit result list to storage boxes with this ID.
37 type: int
38 linked_server_number:
39 description:
40 - Limit result list to storage boxes linked to the server with this number.
41 - Ignored when O(storagebox_id) has been specified, or when O(hetzner_token) has been specified.
42 type: int
43 full_info:
44 description:
45 - Whether to provide full information for every storage box.
46 - Setting this to V(true) requires one REST call per storage box, which is slow and reduces your rate limit. Use with care.
47 - When O(storagebox_id) is specified, this option is always treated as having value V(true).
48 - B(Note) that this option has no effect if O(hetzner_token) is specified.
49 type: bool
50 default: false
51 """
52
53 EXAMPLES = r"""
54 ---
55 - name: Query a list of all storage boxes
56 community.hrobot.storagebox_info:
57 hetzner_user: foo
58 hetzner_password: bar
59 register: result
60
61 - name: Query a specific storage box
62 community.hrobot.storagebox_info:
63 hetzner_user: foo
64 hetzner_password: bar
65 storagebox_id: 23
66 register: result
67
68 - name: Output data on specific storage box
69 ansible.builtin.debug:
70 msg: "Storage box name: {{ result.storageboxes[0].name }}"
71 """
72
73 RETURN = r"""
74 storageboxes:
75 description:
76 - List of storage boxes matching the provided options.
77 returned: success
78 type: list
79 elements: dict
80 contains:
81 id:
82 description:
83 - The storage box's ID.
84 type: int
85 sample: 123456
86 returned: success
87 login:
88 description:
89 - The storage box's login name.
90 - Note that this is copied from RV(storageboxes[].username) in case O(hetzner_token) is specified.
91 - B(This return value is deprecated and will be removed from community.hrobot 3.0.0.)
92 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).
93 type: str
94 sample: u12345
95 returned: success
96 username:
97 description:
98 - The storage box's login name.
99 type: str
100 sample: u12345
101 returned: success if O(hetzner_token) is specified.
102 version_added: 2.5.0
103 name:
104 description:
105 - The storage box's name.
106 type: str
107 sample: Backup Server 1
108 returned: success and O(hetzner_user) is specified
109 product:
110 description:
111 - The product name.
112 type: str
113 sample: BX60
114 returned: success and O(hetzner_user) is specified
115 cancelled:
116 description:
117 - Whether the storage box has been cancelled.
118 - The cancellation can still be un-done until RV(storageboxes[].paid_until) has been exceeded.
119 type: bool
120 sample: false
121 returned: success and O(hetzner_user) is specified
122 locked:
123 description:
124 - Whether the IP is locked.
125 type: bool
126 sample: false
127 returned: success and O(hetzner_user) is specified
128 location:
129 description:
130 - The storage box's location.
131 - This is a string if O(hetzner_user) is specified, and a dictionary if O(hetzner_token) is specified.
132 type: raw
133 sample: FSN1
134 returned: success
135 # location:
136 # description:
137 # - The storage box's location.
138 # type: dict
139 # returned: when O(hetzner_token) is specified
140 # contains:
141 # id:
142 # description:
143 # - The location's ID.
144 # type: int
145 # sample: 42
146 # version_added: 2.5.0
147 # name:
148 # description:
149 # - The location's name (unique identifier).
150 # type: str
151 # sample: "fsn1"
152 # version_added: 2.5.0
153 # description:
154 # description:
155 # - Human readable description of the location.
156 # type: str
157 # sample: "Falkenstein DC Park 1"
158 # version_added: 2.5.0
159 # country:
160 # description:
161 # - Country code (ISO 3166-1 alpha-2).
162 # type: str
163 # sample: "DE"
164 # version_added: 2.5.0
165 # city:
166 # description:
167 # - Closest city to the location.
168 # type: str
169 # sample: "Falkenstein"
170 # version_added: 2.5.0
171 # latitude:
172 # description:
173 # - Latitude of the closest city to the location.
174 # type: float
175 # sample: 50.47612
176 # version_added: 2.5.0
177 # longitude:
178 # description:
179 # - Longitude of the closest city to the location.
180 # type: float
181 # sample: 12.370071
182 # version_added: 2.5.0
183 # network_zone:
184 # description:
185 # - Name of the network zone the location is part of.
186 # type: str
187 # sample: "eu-central"
188 # version_added: 2.5.0
189 linked_server:
190 description:
191 - The ID (server number) of the connected server, if available. Is V(null) otherwise.
192 type: int
193 sample: 123456
194 returned: success and O(hetzner_user) is specified
195 paid_until:
196 description:
197 - The date until which the storage box has been paid for.
198 type: str
199 sample: "2015-10-23"
200 returned: success and O(hetzner_user) is specified
201 disk_quota:
202 description:
203 - Total amount of MB available.
204 - Note that this is copied from RV(storageboxes[].storage_box_type.size) in case O(hetzner_token) is specified.
205 - B(This return value is deprecated and will be removed from community.hrobot 3.0.0.)
206 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).
207 type: int
208 sample: 10240000
209 returned: when O(full_info=true), or O(hetzner_token) is specified
210 disk_usage:
211 description:
212 - The amount of MB in use.
213 - Note that this is copied from RV(storageboxes[].stats.size) in case O(hetzner_token) is specified.
214 - B(This return value is deprecated and will be removed from community.hrobot 3.0.0.)
215 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).
216 type: int
217 sample: 900
218 returned: when O(full_info=true), or O(hetzner_token) is specified
219 disk_usage_data:
220 description:
221 - The amount of MB used by files.
222 - Note that this is copied from RV(storageboxes[].stats.size_data) in case O(hetzner_token) is specified.
223 - B(This return value is deprecated and will be removed from community.hrobot 3.0.0.)
224 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).
225 type: int
226 sample: 500
227 returned: when O(full_info=true), or O(hetzner_token) is specified
228 disk_usage_snapshots:
229 description:
230 - The amount of MB used by snapshots.
231 - Note that this is copied from RV(storageboxes[].stats.size_snapshots) in case O(hetzner_token) is specified.
232 - B(This return value is deprecated and will be removed from community.hrobot 3.0.0.)
233 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).
234 type: int
235 sample: 400
236 returned: when O(full_info=true), or O(hetzner_token) is specified
237 webdav:
238 description:
239 - Whether WebDAV is active.
240 - Note that this is copied from RV(storageboxes[].access_settings.webdav_enabled) in case O(hetzner_token) is specified.
241 - B(This return value is deprecated and will be removed from community.hrobot 3.0.0.)
242 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).
243 type: bool
244 sample: true
245 returned: when O(full_info=true), or O(hetzner_token) is specified
246 samba:
247 description:
248 - Whether SAMBA is active.
249 - Note that this is copied from RV(storageboxes[].access_settings.samba_enabled) in case O(hetzner_token) is specified.
250 - B(This return value is deprecated and will be removed from community.hrobot 3.0.0.)
251 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).
252 type: bool
253 sample: true
254 returned: when O(full_info=true), or O(hetzner_token) is specified
255 ssh:
256 description:
257 - Whether SSH is active.
258 - Note that this is copied from RV(storageboxes[].access_settings.ssh_enabled) in case O(hetzner_token) is specified.
259 - B(This return value is deprecated and will be removed from community.hrobot 3.0.0.)
260 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).
261 type: bool
262 sample: true
263 returned: when O(full_info=true), or O(hetzner_token) is specified
264 external_reachability:
265 description:
266 - Whether the storage box is reachable externally.
267 - Note that this is copied from RV(storageboxes[].access_settings.reachable_externally) in case O(hetzner_token) is specified.
268 - B(This return value is deprecated and will be removed from community.hrobot 3.0.0.)
269 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).
270 type: bool
271 sample: true
272 returned: when O(full_info=true), or O(hetzner_token) is specified
273 zfs:
274 description:
275 - Shows whether the ZFS directory is visible.
276 - Note that this is copied from RV(storageboxes[].access_settings.zfs_enabled) in case O(hetzner_token) is specified.
277 - B(This return value is deprecated and will be removed from community.hrobot 3.0.0.)
278 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).
279 type: bool
280 sample: false
281 returned: when O(full_info=true), or O(hetzner_token) is specified
282 server:
283 description:
284 - The storage box's hostname.
285 type: str
286 sample: u12345.your-storagebox.de
287 returned: when O(full_info=true); or O(hetzner_token) is specified and RV(storageboxes[].status) is not V(initializing)
288 host_system:
289 description:
290 - Identifier of the storage box's host.
291 type: str
292 sample: FSN1-BX355
293 returned: when O(full_info=true) and O(hetzner_user) is specified
294 system:
295 description:
296 - Identifier of the storage box's host.
297 type: str
298 sample: FSN1-BX355
299 returned: when O(hetzner_token) is specified and RV(storageboxes[].status) is not V(initializing)
300 version_added: 2.5.0
301 status:
302 description:
303 - Status of the storage box.
304 type: str
305 sample: active
306 choices:
307 - active
308 - initializing
309 - locked
310 returned: when O(hetzner_token) is specified
311 version_added: 2.5.0
312 created:
313 description:
314 - Creation timestamp in ISO 8601 format.
315 type: str
316 sample: "2016-01-30T23:55:00+00:00"
317 returned: when O(hetzner_token) is specified
318 version_added: 2.5.0
319 storage_box_type:
320 description:
321 - Information on the storage box's type.
322 type: dict
323 returned: when O(hetzner_token) is specified
324 version_added: 2.5.0
325 contains:
326 name:
327 description:
328 - Identifier of the storage box's type.
329 type: str
330 sample: "bx11"
331 description:
332 description:
333 - Description of the storage box's type.
334 type: str
335 sample: "BX11"
336 snapshot_limit:
337 description:
338 - Maximum number of allowed manual snapshots.
339 type: int
340 sample: 10
341 automatic_snapshot_limit:
342 description:
343 - Maximum number of snapshots created automatically by a snapshot plan.
344 type: int
345 sample: 10
346 subaccounts_limit:
347 description:
348 - Maximum number of allowed subaccounts.
349 type: int
350 sample: 200
351 size:
352 description:
353 - Available storage in bytes.
354 type: int
355 sample: 1073741824
356 prices:
357 description:
358 - Price per location.
359 type: list
360 elements: dict
361 contains:
362 location:
363 description:
364 - Name of the location.
365 type: str
366 sample: "fsn1"
367 price_hourly:
368 description:
369 - The hourly rate.
370 type: dict
371 contains:
372 net:
373 description:
374 - Hourly price (without VAT).
375 type: str
376 sample: "1.0000"
377 gross:
378 description:
379 - Hourly price (with VAT).
380 type: str
381 sample: "1.1900"
382 price_monthly:
383 description:
384 - The monthly rate.
385 type: dict
386 contains:
387 net:
388 description:
389 - Monthly price (without VAT).
390 type: str
391 sample: "1.0000"
392 gross:
393 description:
394 - Monthly price (with VAT).
395 type: str
396 sample: "1.1900"
397 setup_fee:
398 description:
399 - The setup fee.
400 type: dict
401 contains:
402 net:
403 description:
404 - Setup fee (without VAT).
405 type: str
406 sample: "1.0000"
407 gross:
408 description:
409 - Setup fee (with VAT).
410 type: str
411 sample: "1.1900"
412 deprecation:
413 description:
414 - Set to V(none) (JSON V(null)) if the storage box's type is not deprecated.
415 - If set to a dictionary, this storage box's type is deprecated.
416 type: dict
417 contains:
418 unavailable_after:
419 description:
420 - ISO 8601 timestamp when the resource will be removed.
421 type: str
422 sample: "2023-09-01T00:00:00+00:00"
423 announced:
424 description:
425 - ISO 8601 timestamp when the deprecation was announced.
426 type: str
427 sample: "2023-06-01T00:00:00+00:00"
428 access_settings:
429 description:
430 - Access settings for the storage box.
431 type: dict
432 returned: when O(hetzner_token) is specified
433 version_added: 2.5.0
434 contains:
435 reachable_externally:
436 description:
437 - Whether the storage box is accessible from outside Hetzner's network.
438 type: bool
439 sample: false
440 samba_enabled:
441 description:
442 - Whether SAMBA is enabled.
443 type: bool
444 sample: false
445 ssh_enabled:
446 description:
447 - Whether SSH is enabled.
448 type: bool
449 sample: false
450 webdav_enabled:
451 description:
452 - Whether WebDAV is enabled.
453 type: bool
454 sample: false
455 zfs_enabled:
456 description:
457 - Whether ZFS is enabled.
458 type: bool
459 sample: false
460 stats:
461 description:
462 - Information on disk usage.
463 type: dict
464 returned: when O(hetzner_token) is specified and RV(storageboxes[].status) is not V(initializing)
465 version_added: 2.5.0
466 contains:
467 size:
468 description:
469 - Current disk usage in bytes.
470 type: int
471 sample: 0
472 size_data:
473 description:
474 - Current disk usage for data in bytes.
475 type: int
476 sample: 0
477 size_snapshots:
478 description:
479 - Current disk usage for snapshots in bytes.
480 type: int
481 sample: 0
482 labels:
483 description:
484 - User-defined labels for the storage box.
485 type: dict
486 returned: when O(hetzner_token) is specified
487 version_added: 2.5.0
488 protection:
489 description:
490 - Protection configuration for the storage box.
491 type: dict
492 returned: when O(hetzner_token) is specified
493 version_added: 2.5.0
494 contains:
495 delete:
496 description:
497 - Whether deletion of the storage box is disabled.
498 type: bool
499 sample: false
500 snapshot_plan:
501 description:
502 - The snapshot plan for the storage box.
503 - Will be V(none) (JSON V(null)) if no plan is active.
504 type: dict
505 returned: when O(hetzner_token) is specified and RV(storageboxes[].status) is not V(initializing)
506 version_added: 2.5.0
507 contains:
508 max_snapshots:
509 description:
510 - Maximum number of automatic snapshots to be kept.
511 type: int
512 sample: 10
513 minute:
514 description:
515 - Minute the snapshot plan is executed (in UTC).
516 type: int
517 sample: null
518 hour:
519 description:
520 - Hour the snapshot plan is executed (in UTC).
521 type: int
522 sample: null
523 day_of_week:
524 description:
525 - Day of the week the snapshot plan is executed.
526 - Will be V(none) (JSON V(null)) if this restriction is not set (that is, create a snapshot every day unless the day of month is specified).
527 type: int
528 sample: null
529 day_of_month:
530 description:
531 - Day of the month the snapshot plan is executed.
532 - Will be V(none) (JSON V(null)) if this restriction is not set (that is, create a snapshot every day unless the day of week is specified).
533 type: int
534 sample: null
535 """
536
537 from ansible.module_utils.basic import AnsibleModule
538
539 from ansible_collections.community.hrobot.plugins.module_utils.robot import (
540 BASE_URL,
541 ROBOT_DEFAULT_ARGUMENT_SPEC,
542 _ROBOT_DEFAULT_ARGUMENT_SPEC_COMPAT_DEPRECATED,
543 fetch_url_json,
544 )
545
546 from ansible_collections.community.hrobot.plugins.module_utils.api import (
547 API_BASE_URL,
548 API_DEFAULT_ARGUMENT_SPEC,
549 _API_DEFAULT_ARGUMENT_SPEC_COMPAT,
550 api_fetch_url_json,
551 api_fetch_url_json_list,
552 )
553
554 from ansible_collections.community.hrobot.plugins.module_utils._tagging import (
555 deprecate_value,
556 )
557
558 try:
559 from urllib.parse import urlencode
560 except ImportError:
561 # Python 2.x fallback:
562 from urllib import urlencode
563
564
565 _CONVERT = {
566 "login": ["username"],
567 "disk_quota": ["storage_box_type", "size"],
568 "disk_usage": ["stats", "size"],
569 "disk_usage_data": ["stats", "size_data"],
570 "disk_usage_snapshots": ["stats", "size_snapshots"],
571 "webdav": ["access_settings", "webdav_enabled"],
572 "samba": ["access_settings", "samba_enabled"],
573 "ssh": ["access_settings", "ssh_enabled"],
574 "external_reachability": ["access_settings", "reachable_externally"],
575 "zfs": ["access_settings", "zfs_enabled"],
576 }
577
578
579 def add_hrobot_compat_shim(storagebox):
580 result = dict(storagebox)
581 for dest, source in _CONVERT.items():
582 value = storagebox
583 for src in source:
584 value = value[src]
585 result[dest] = deprecate_value(
586 value,
587 "The return value `{0}` is deprecated; use `{1}` instead.".format(dest, ".".join(source)),
588 version="3.0.0",
589 )
590 return result
591
592
593 def main():
594 argument_spec = dict(
595 storagebox_id=dict(type='int'),
596 linked_server_number=dict(type='int'),
597 full_info=dict(type='bool', default=False),
598 )
599 argument_spec.update(ROBOT_DEFAULT_ARGUMENT_SPEC)
600 argument_spec.update(_ROBOT_DEFAULT_ARGUMENT_SPEC_COMPAT_DEPRECATED)
601 argument_spec.update(API_DEFAULT_ARGUMENT_SPEC)
602 argument_spec.update(_API_DEFAULT_ARGUMENT_SPEC_COMPAT)
603 module = AnsibleModule(
604 argument_spec=argument_spec,
605 supports_check_mode=True,
606 required_together=[("hetzner_user", "hetzner_password")],
607 required_one_of=[("hetzner_user", "hetzner_token")],
608 mutually_exclusive=[("hetzner_user", "hetzner_token")],
609 )
610
611 storagebox_id = module.params['storagebox_id']
612 linked_server_number = module.params['linked_server_number']
613 full_info = module.params['full_info']
614
615 storageboxes = []
616 if module.params["hetzner_user"] is not None:
617 module.deprecate(
618 "The hetzner_token parameter will be required from community.hrobot 3.0.0 on.",
619 collection_name="community.hrobot",
620 version="3.0.0",
621 )
622 # DEPRECATED: old API
623 if storagebox_id is not None:
624 storagebox_ids = [storagebox_id]
625 else:
626 url = "{0}/storagebox".format(BASE_URL)
627 data = None
628 headers = None
629 if linked_server_number is not None:
630 data = urlencode({
631 "linked_server": linked_server_number,
632 })
633 headers = {
634 "Content-type": "application/x-www-form-urlencoded",
635 }
636 result, error = fetch_url_json(module, url, accept_errors=['STORAGEBOX_NOT_FOUND'], data=data)
637 storagebox_ids = []
638 if not error:
639 # When filtering by linked_server, the result should be a dictionary
640 if isinstance(result, dict):
641 result = [result]
642 for entry in result:
643 if full_info:
644 storagebox_ids.append(entry['storagebox']['id'])
645 else:
646 storageboxes.append(entry['storagebox'])
647
648 for storagebox_id in storagebox_ids:
649 url = "{0}/storagebox/{1}".format(BASE_URL, storagebox_id)
650 result, error = fetch_url_json(module, url, accept_errors=['STORAGEBOX_NOT_FOUND'])
651 if not error:
652 storageboxes.append(result['storagebox'])
653
654 else:
655 # NEW API!
656 if storagebox_id is not None:
657 url = "{0}/v1/storage_boxes/{1}".format(API_BASE_URL, storagebox_id)
658 result, dummy, error = api_fetch_url_json(module, url, accept_errors=["not_found"])
659 if error is None:
660 storageboxes = [result["storage_box"]]
661 else:
662 url = "{0}/v1/storage_boxes".format(API_BASE_URL)
663 storageboxes, dummy = api_fetch_url_json_list(module, url, data_key="storage_boxes")
664 storageboxes = [add_hrobot_compat_shim(storagebox) for storagebox in storageboxes]
665
666 module.exit_json(
667 changed=False,
668 storageboxes=storageboxes,
669 )
670
671
672 if __name__ == '__main__': # pragma: no cover
673 main() # pragma: no cover