Debian Codegraph @

ansible_collections/hetzner/hcloud/plugins/modules/network.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: network
12
13 short_description: Create and manage cloud Networks on the Hetzner Cloud.
14
15
16 description:
17 - Create, update and manage cloud Networks on the Hetzner Cloud.
18 - You need at least hcloud-python 1.3.0.
19
20 author:
21 - Lukas Kaemmerling (@lkaemmerling)
22
23 options:
24 id:
25 description:
26 - The ID of the Hetzner Cloud Networks to manage.
27 - Only required if no Network I(name) is given.
28 type: int
29 name:
30 description:
31 - The Name of the Hetzner Cloud Network to manage.
32 - Only required if no Network I(id) is given or a Network does not exist.
33 type: str
34 ip_range:
35 description:
36 - IP range of the Network.
37 - Required if Network does not exist.
38 type: str
39 expose_routes_to_vswitch:
40 description:
41 - Indicates if the routes from this network should be exposed to the vSwitch connection.
42 - The exposing only takes effect if a vSwitch connection is active.
43 type: bool
44 labels:
45 description:
46 - User-defined labels (key-value pairs).
47 type: dict
48 delete_protection:
49 description:
50 - Protect the Network for deletion.
51 type: bool
52 state:
53 description:
54 - State of the Network.
55 default: present
56 choices: [ absent, present ]
57 type: str
58
59 extends_documentation_fragment:
60 - hetzner.hcloud.hcloud
61 """
62
63 EXAMPLES = """
64 - name: Create a basic network
65 hetzner.hcloud.network:
66 name: my-network
67 ip_range: 10.0.0.0/8
68 state: present
69
70 - name: Ensure the Network is absent (remove if needed)
71 hetzner.hcloud.network:
72 name: my-network
73 state: absent
74 """
75
76 RETURN = """
77 hcloud_network:
78 description: The Network
79 returned: always
80 type: dict
81 contains:
82 id:
83 description: ID of the Network
84 type: int
85 returned: always
86 sample: 12345
87 name:
88 description: Name of the Network
89 type: str
90 returned: always
91 sample: my-volume
92 ip_range:
93 description: IP range of the Network
94 type: str
95 returned: always
96 sample: 10.0.0.0/8
97 expose_routes_to_vswitch:
98 description: Indicates if the routes from this network should be exposed to the vSwitch connection.
99 type: bool
100 returned: always
101 sample: false
102 delete_protection:
103 description: True if Network is protected for deletion
104 type: bool
105 returned: always
106 sample: false
107 version_added: "0.1.0"
108 labels:
109 description: User-defined labels (key-value pairs)
110 type: dict
111 returned: always
112 sample:
113 key: value
114 mylabel: 123
115 """
116
117 from ansible.module_utils.basic import AnsibleModule
118
119 from ..module_utils._base import AnsibleHCloud
120 from ..module_utils._vendor.hcloud import HCloudException
121 from ..module_utils._vendor.hcloud.networks import BoundNetwork
122
123
124 class AnsibleHCloudNetwork(AnsibleHCloud):
125 represent = "hcloud_network"
126
127 hcloud_network: BoundNetwork | None = None
128
129 def _prepare_result(self):
130 return {
131 "id": self.hcloud_network.id,
132 "name": self.hcloud_network.name,
133 "ip_range": self.hcloud_network.ip_range,
134 "expose_routes_to_vswitch": self.hcloud_network.expose_routes_to_vswitch,
135 "delete_protection": self.hcloud_network.protection["delete"],
136 "labels": self.hcloud_network.labels,
137 }
138
139 def _get_network(self):
140 try:
141 if self.module.params.get("id") is not None:
142 self.hcloud_network = self.client.networks.get_by_id(self.module.params.get("id"))
143 else:
144 self.hcloud_network = self.client.networks.get_by_name(self.module.params.get("name"))
145 except HCloudException as exception:
146 self.fail_json_hcloud(exception)
147
148 def _create_network(self):
149 self.module.fail_on_missing_params(required_params=["name", "ip_range"])
150 params = {
151 "name": self.module.params.get("name"),
152 "ip_range": self.module.params.get("ip_range"),
153 "labels": self.module.params.get("labels"),
154 }
155
156 expose_routes_to_vswitch = self.module.params.get("expose_routes_to_vswitch")
157 if expose_routes_to_vswitch is not None:
158 params["expose_routes_to_vswitch"] = expose_routes_to_vswitch
159
160 try:
161 if not self.module.check_mode:
162 self.client.networks.create(**params)
163
164 delete_protection = self.module.params.get("delete_protection")
165 if delete_protection is not None:
166 self._get_network()
167 action = self.hcloud_network.change_protection(delete=delete_protection)
168 action.wait_until_finished()
169 except HCloudException as exception:
170 self.fail_json_hcloud(exception)
171 self._mark_as_changed()
172 self._get_network()
173
174 def _update_network(self):
175 try:
176 name = self.module.params.get("name")
177 if name is not None and self.hcloud_network.name != name:
178 self.module.fail_on_missing_params(required_params=["id"])
179 if not self.module.check_mode:
180 self.hcloud_network.update(name=name)
181 self._mark_as_changed()
182
183 labels = self.module.params.get("labels")
184 if labels is not None and labels != self.hcloud_network.labels:
185 if not self.module.check_mode:
186 self.hcloud_network.update(labels=labels)
187 self._mark_as_changed()
188
189 ip_range = self.module.params.get("ip_range")
190 if ip_range is not None and ip_range != self.hcloud_network.ip_range:
191 if not self.module.check_mode:
192 action = self.hcloud_network.change_ip_range(ip_range=ip_range)
193 action.wait_until_finished()
194 self._mark_as_changed()
195
196 expose_routes_to_vswitch = self.module.params.get("expose_routes_to_vswitch")
197 if (
198 expose_routes_to_vswitch is not None
199 and expose_routes_to_vswitch != self.hcloud_network.expose_routes_to_vswitch
200 ):
201 if not self.module.check_mode:
202 self.hcloud_network.update(expose_routes_to_vswitch=expose_routes_to_vswitch)
203 self._mark_as_changed()
204
205 delete_protection = self.module.params.get("delete_protection")
206 if delete_protection is not None and delete_protection != self.hcloud_network.protection["delete"]:
207 if not self.module.check_mode:
208 action = self.hcloud_network.change_protection(delete=delete_protection)
209 action.wait_until_finished()
210 self._mark_as_changed()
211 except HCloudException as exception:
212 self.fail_json_hcloud(exception)
213 self._get_network()
214
215 def present_network(self):
216 self._get_network()
217 if self.hcloud_network is None:
218 self._create_network()
219 else:
220 self._update_network()
221
222 def delete_network(self):
223 try:
224 self._get_network()
225 if self.hcloud_network is not None:
226 if not self.module.check_mode:
227 self.client.networks.delete(self.hcloud_network)
228 self._mark_as_changed()
229 except HCloudException as exception:
230 self.fail_json_hcloud(exception)
231 self.hcloud_network = None
232
233 @classmethod
234 def define_module(cls):
235 return AnsibleModule(
236 argument_spec=dict(
237 id={"type": "int"},
238 name={"type": "str"},
239 ip_range={"type": "str"},
240 expose_routes_to_vswitch={"type": "bool"},
241 labels={"type": "dict"},
242 delete_protection={"type": "bool"},
243 state={
244 "choices": ["absent", "present"],
245 "default": "present",
246 },
247 **super().base_module_arguments(),
248 ),
249 required_one_of=[["id", "name"]],
250 supports_check_mode=True,
251 )
252
253
254 def main():
255 module = AnsibleHCloudNetwork.define_module()
256
257 hcloud = AnsibleHCloudNetwork(module)
258 state = module.params["state"]
259 if state == "absent":
260 hcloud.delete_network()
261 elif state == "present":
262 hcloud.present_network()
263
264 module.exit_json(**hcloud.get_result())
265
266
267 if __name__ == "__main__":
268 main()