Debian Codegraph @

ansible_collections/hetzner/hcloud/plugins/modules/location_info.py

raw
1 #!/usr/bin/python
2
3 # Copyright: (c) 2019, Hetzner Cloud GmbH <[email protected]>
4 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
5
6
7 from __future__ import annotations
8
9 DOCUMENTATION = """
10 ---
11 module: location_info
12
13 short_description: Gather infos about your Hetzner Cloud locations.
14
15
16 description:
17 - Gather infos about your Hetzner Cloud locations.
18
19 author:
20 - Lukas Kaemmerling (@LKaemmerling)
21
22 options:
23 id:
24 description:
25 - The ID of the location you want to get.
26 - The module will fail if the provided ID is invalid.
27 type: int
28 name:
29 description:
30 - The name of the location you want to get.
31 type: str
32 extends_documentation_fragment:
33 - hetzner.hcloud.hcloud
34
35 """
36
37 EXAMPLES = """
38 - name: Gather hcloud location infos
39 hetzner.hcloud.location_info:
40 register: output
41
42 - name: Print the gathered infos
43 debug:
44 var: output
45 """
46
47 RETURN = """
48 hcloud_location_info:
49 description: The location infos as list
50 returned: always
51 type: list
52 contains:
53 id:
54 description: Numeric identifier of the location
55 returned: always
56 type: int
57 sample: 1937415
58 name:
59 description: Name of the location
60 returned: always
61 type: str
62 sample: fsn1
63 description:
64 description: Detail description of the location
65 returned: always
66 type: str
67 sample: Falkenstein DC Park 1
68 country:
69 description: Country code of the location
70 returned: always
71 type: str
72 sample: DE
73 city:
74 description: City of the location
75 returned: always
76 type: str
77 sample: Falkenstein
78 """
79
80 from ansible.module_utils.basic import AnsibleModule
81
82 from ..module_utils._base import AnsibleHCloud
83 from ..module_utils._vendor.hcloud import HCloudException
84 from ..module_utils._vendor.hcloud.locations import BoundLocation
85
86
87 class AnsibleHCloudLocationInfo(AnsibleHCloud):
88 represent = "hcloud_location_info"
89
90 hcloud_location_info: list[BoundLocation] | None = None
91
92 def _prepare_result(self):
93 tmp = []
94
95 for location in self.hcloud_location_info:
96 if location is None:
97 continue
98
99 tmp.append(
100 {
101 "id": location.id,
102 "name": location.name,
103 "description": location.description,
104 "city": location.city,
105 "country": location.country,
106 }
107 )
108 return tmp
109
110 def get_locations(self):
111 try:
112 if self.module.params.get("id") is not None:
113 self.hcloud_location_info = [self.client.locations.get_by_id(self.module.params.get("id"))]
114 elif self.module.params.get("name") is not None:
115 self.hcloud_location_info = [self.client.locations.get_by_name(self.module.params.get("name"))]
116 else:
117 self.hcloud_location_info = self.client.locations.get_all()
118
119 except HCloudException as exception:
120 self.fail_json_hcloud(exception)
121
122 @classmethod
123 def define_module(cls):
124 return AnsibleModule(
125 argument_spec=dict(
126 id={"type": "int"},
127 name={"type": "str"},
128 **super().base_module_arguments(),
129 ),
130 supports_check_mode=True,
131 )
132
133
134 def main():
135 module = AnsibleHCloudLocationInfo.define_module()
136 hcloud = AnsibleHCloudLocationInfo(module)
137
138 hcloud.get_locations()
139 result = hcloud.get_result()
140
141 ansible_info = {"hcloud_location_info": result["hcloud_location_info"]}
142 module.exit_json(**ansible_info)
143
144
145 if __name__ == "__main__":
146 main()