Debian Codegraph @

ansible_collections/hetzner/hcloud/plugins/modules/subnetwork.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: subnetwork
12
13 short_description: Manage cloud subnetworks on the Hetzner Cloud.
14
15
16 description:
17 - Create, update and delete cloud subnetworks on the Hetzner Cloud.
18
19 author:
20 - Lukas Kaemmerling (@lkaemmerling)
21
22 options:
23 network:
24 description:
25 - The name or ID of the Hetzner Cloud Networks.
26 type: str
27 required: true
28 ip_range:
29 description:
30 - IP range of the subnetwork.
31 type: str
32 required: true
33 type:
34 description:
35 - Type of subnetwork.
36 type: str
37 choices: [ server, cloud, vswitch ]
38 required: true
39 network_zone:
40 description:
41 - Name of network zone.
42 type: str
43 required: true
44 vswitch_id:
45 description:
46 - ID of the vSwitch you want to couple with your Network.
47 - Required if type == vswitch
48 type: int
49 state:
50 description:
51 - State of the subnetwork.
52 default: present
53 choices: [ absent, present ]
54 type: str
55
56 extends_documentation_fragment:
57 - hetzner.hcloud.hcloud
58 """
59
60 EXAMPLES = """
61 - name: Create a basic subnetwork
62 hetzner.hcloud.subnetwork:
63 network: my-network
64 ip_range: 10.0.0.0/16
65 network_zone: eu-central
66 type: cloud
67 state: present
68
69 - name: Create a basic subnetwork
70 hetzner.hcloud.subnetwork:
71 network: my-vswitch-network
72 ip_range: 10.0.0.0/24
73 network_zone: eu-central
74 type: vswitch
75 vswitch_id: 123
76 state: present
77
78 - name: Ensure the subnetwork is absent (remove if needed)
79 hetzner.hcloud.subnetwork:
80 network: my-network
81 ip_range: 10.0.0.0/8
82 network_zone: eu-central
83 type: cloud
84 state: absent
85 """
86
87 RETURN = """
88 hcloud_subnetwork:
89 description: One Subnet of a Network
90 returned: always
91 type: dict
92 contains:
93 network:
94 description: Name of the Network
95 type: str
96 returned: always
97 sample: my-network
98 ip_range:
99 description: IP range of the Network
100 type: str
101 returned: always
102 sample: 10.0.0.0/8
103 type:
104 description: Type of subnetwork
105 type: str
106 returned: always
107 sample: server
108 network_zone:
109 description: Name of network zone
110 type: str
111 returned: always
112 sample: eu-central
113 vswitch_id:
114 description: ID of the vswitch, null if not type vswitch
115 type: int
116 returned: always
117 sample: 123
118 gateway:
119 description: Gateway of the subnetwork
120 type: str
121 returned: always
122 sample: 10.0.0.1
123 """
124
125 from ansible.module_utils.basic import AnsibleModule
126
127 from ..module_utils._base import AnsibleHCloud
128 from ..module_utils._vendor.hcloud import HCloudException
129 from ..module_utils._vendor.hcloud.networks import BoundNetwork, NetworkSubnet
130
131
132 class AnsibleHCloudSubnetwork(AnsibleHCloud):
133 represent = "hcloud_subnetwork"
134
135 hcloud_network: BoundNetwork | None = None
136 hcloud_subnetwork: NetworkSubnet | None = None
137
138 def _prepare_result(self):
139 return {
140 "network": self.hcloud_network.name,
141 "ip_range": self.hcloud_subnetwork.ip_range,
142 "type": self.hcloud_subnetwork.type,
143 "network_zone": self.hcloud_subnetwork.network_zone,
144 "gateway": self.hcloud_subnetwork.gateway,
145 "vswitch_id": self.hcloud_subnetwork.vswitch_id,
146 }
147
148 def _get_network(self):
149 try:
150 self.hcloud_network = self._client_get_by_name_or_id(
151 "networks",
152 self.module.params.get("network"),
153 )
154 self.hcloud_subnetwork = None
155 except HCloudException as exception:
156 self.fail_json_hcloud(exception)
157
158 def _get_subnetwork(self):
159 subnet_ip_range = self.module.params.get("ip_range")
160 for subnetwork in self.hcloud_network.subnets:
161 if subnetwork.ip_range == subnet_ip_range:
162 self.hcloud_subnetwork = subnetwork
163
164 def _create_subnetwork(self):
165 params = {
166 "ip_range": self.module.params.get("ip_range"),
167 "type": self.module.params.get("type"),
168 "network_zone": self.module.params.get("network_zone"),
169 }
170 if self.module.params.get("type") == NetworkSubnet.TYPE_VSWITCH:
171 self.module.fail_on_missing_params(required_params=["vswitch_id"])
172 params["vswitch_id"] = self.module.params.get("vswitch_id")
173
174 if not self.module.check_mode:
175 try:
176 action = self.hcloud_network.add_subnet(subnet=NetworkSubnet(**params))
177 action.wait_until_finished()
178 except HCloudException as exception:
179 self.fail_json_hcloud(exception)
180
181 self._mark_as_changed()
182 self._get_network()
183 self._get_subnetwork()
184
185 def present_subnetwork(self):
186 self._get_network()
187 self._get_subnetwork()
188 if self.hcloud_subnetwork is None:
189 self._create_subnetwork()
190
191 def delete_subnetwork(self):
192 self._get_network()
193 self._get_subnetwork()
194 if self.hcloud_subnetwork is not None and self.hcloud_network is not None:
195 if not self.module.check_mode:
196 try:
197 action = self.hcloud_network.delete_subnet(self.hcloud_subnetwork)
198 action.wait_until_finished()
199 except HCloudException as exception:
200 self.fail_json_hcloud(exception)
201 self._mark_as_changed()
202 self.hcloud_subnetwork = None
203
204 @classmethod
205 def define_module(cls):
206 return AnsibleModule(
207 argument_spec=dict(
208 network={"type": "str", "required": True},
209 network_zone={"type": "str", "required": True},
210 type={"type": "str", "required": True, "choices": ["server", "cloud", "vswitch"]},
211 ip_range={"type": "str", "required": True},
212 vswitch_id={"type": "int"},
213 state={
214 "choices": ["absent", "present"],
215 "default": "present",
216 },
217 **super().base_module_arguments(),
218 ),
219 supports_check_mode=True,
220 )
221
222
223 def main():
224 module = AnsibleHCloudSubnetwork.define_module()
225
226 hcloud = AnsibleHCloudSubnetwork(module)
227 state = module.params["state"]
228 if state == "absent":
229 hcloud.delete_subnetwork()
230 elif state == "present":
231 hcloud.present_subnetwork()
232
233 module.exit_json(**hcloud.get_result())
234
235
236 if __name__ == "__main__":
237 main()