Debian Codegraph @

ansible_collections/hetzner/hcloud/plugins/modules/floating_ip_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: floating_ip_info
12
13 short_description: Gather infos about the Hetzner Cloud Floating IPs.
14
15 description:
16 - Gather facts about your Hetzner Cloud Floating IPs.
17
18 author:
19 - Lukas Kaemmerling (@LKaemmerling)
20
21 options:
22 id:
23 description:
24 - The ID of the Floating IP you want to get.
25 - The module will fail if the provided ID is invalid.
26 type: int
27 name:
28 description:
29 - The name for the Floating IP you want to get.
30 type: str
31 label_selector:
32 description:
33 - The label selector for the Floating IP you want to get.
34 type: str
35 extends_documentation_fragment:
36 - hetzner.hcloud.hcloud
37
38 """
39
40 EXAMPLES = """
41 - name: Gather hcloud Floating ip infos
42 hetzner.hcloud.floating_ip_info:
43 register: output
44 - name: Print the gathered infos
45 debug:
46 var: output
47 """
48
49 RETURN = """
50 hcloud_floating_ip_info:
51 description: The Floating ip infos as list
52 returned: always
53 type: list
54 contains:
55 id:
56 description: Numeric identifier of the Floating IP
57 returned: always
58 type: int
59 sample: 1937415
60 name:
61 description: Name of the Floating IP
62 returned: Always
63 type: str
64 sample: my-floating-ip
65 version_added: "0.1.0"
66 description:
67 description: Description of the Floating IP
68 returned: always
69 type: str
70 sample: Falkenstein DC 8
71 ip:
72 description: IP address of the Floating IP
73 returned: always
74 type: str
75 sample: 131.232.99.1
76 type:
77 description: Type of the Floating IP
78 returned: always
79 type: str
80 sample: ipv4
81 server:
82 description: Name of the server where the Floating IP is assigned to.
83 returned: always
84 type: str
85 sample: my-server
86 home_location:
87 description: Location the Floating IP was created in
88 returned: always
89 type: str
90 sample: fsn1
91 delete_protection:
92 description: True if the Floating IP is protected for deletion
93 returned: always
94 type: bool
95 version_added: "0.1.0"
96 labels:
97 description: User-defined labels (key-value pairs)
98 returned: always
99 type: dict
100 """
101
102 from ansible.module_utils.basic import AnsibleModule
103
104 from ..module_utils._base import AnsibleHCloud
105 from ..module_utils._vendor.hcloud import HCloudException
106 from ..module_utils._vendor.hcloud.floating_ips import BoundFloatingIP
107
108
109 class AnsibleHCloudFloatingIPInfo(AnsibleHCloud):
110 represent = "hcloud_floating_ip_info"
111
112 hcloud_floating_ip_info: list[BoundFloatingIP] | None = None
113
114 def _prepare_result(self):
115 tmp = []
116
117 for floating_ip in self.hcloud_floating_ip_info:
118 if floating_ip is None:
119 continue
120
121 tmp.append(
122 {
123 "id": floating_ip.id,
124 "name": floating_ip.name,
125 "description": floating_ip.description,
126 "ip": floating_ip.ip,
127 "type": floating_ip.type,
128 "server": floating_ip.server.name if floating_ip.server is not None else None,
129 "home_location": floating_ip.home_location.name,
130 "labels": floating_ip.labels,
131 "delete_protection": floating_ip.protection["delete"],
132 }
133 )
134
135 return tmp
136
137 def get_floating_ips(self):
138 try:
139 if self.module.params.get("id") is not None:
140 self.hcloud_floating_ip_info = [self.client.floating_ips.get_by_id(self.module.params.get("id"))]
141 elif self.module.params.get("name") is not None:
142 self.hcloud_floating_ip_info = [self.client.floating_ips.get_by_name(self.module.params.get("name"))]
143 elif self.module.params.get("label_selector") is not None:
144 self.hcloud_floating_ip_info = self.client.floating_ips.get_all(
145 label_selector=self.module.params.get("label_selector")
146 )
147 else:
148 self.hcloud_floating_ip_info = self.client.floating_ips.get_all()
149
150 except HCloudException as exception:
151 self.fail_json_hcloud(exception)
152
153 @classmethod
154 def define_module(cls):
155 return AnsibleModule(
156 argument_spec=dict(
157 id={"type": "int"},
158 name={"type": "str"},
159 label_selector={"type": "str"},
160 **super().base_module_arguments(),
161 ),
162 supports_check_mode=True,
163 )
164
165
166 def main():
167 module = AnsibleHCloudFloatingIPInfo.define_module()
168 hcloud = AnsibleHCloudFloatingIPInfo(module)
169
170 hcloud.get_floating_ips()
171 result = hcloud.get_result()
172
173 ansible_info = {"hcloud_floating_ip_info": result["hcloud_floating_ip_info"]}
174 module.exit_json(**ansible_info)
175
176
177 if __name__ == "__main__":
178 main()