Debian Codegraph @

ansible_collections/community/general/plugins/modules/ovh_ip_loadbalancing_backend.py

raw
1 #!/usr/bin/python
2
3 # Copyright Ansible Project
4 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
5 # SPDX-License-Identifier: GPL-3.0-or-later
6
7 from __future__ import annotations
8
9 DOCUMENTATION = r"""
10 module: ovh_ip_loadbalancing_backend
11 short_description: Manage OVH IP LoadBalancing backends
12 description:
13 - Manage OVH (French European hosting provider) LoadBalancing IP backends.
14 author: Pascal Heraud (@pascalheraud)
15 notes:
16 - Uses the Python OVH API U(https://github.com/ovh/python-ovh). You have to create an application (a key and secret) with
17 a consumer key as described into U(https://docs.ovh.com/gb/en/customer/first-steps-with-ovh-api/).
18 requirements:
19 - ovh > 0.3.5
20 extends_documentation_fragment:
21 - community.general._attributes
22 attributes:
23 check_mode:
24 support: none
25 diff_mode:
26 support: none
27 options:
28 name:
29 required: true
30 description:
31 - Name of the LoadBalancing internal name (V(ip-X.X.X.X)).
32 type: str
33 backend:
34 required: true
35 description:
36 - The IP address of the backend to update / modify / delete.
37 type: str
38 state:
39 default: present
40 choices: ['present', 'absent']
41 description:
42 - Determines whether the backend is to be created/modified or deleted.
43 type: str
44 probe:
45 default: 'none'
46 choices: ['none', 'http', 'icmp', 'oco']
47 description:
48 - Determines the type of probe to use for this backend.
49 type: str
50 weight:
51 default: 8
52 description:
53 - Determines the weight for this backend.
54 type: int
55 endpoint:
56 required: true
57 description:
58 - The endpoint to use (for instance V(ovh-eu)).
59 type: str
60 application_key:
61 required: true
62 description:
63 - The applicationKey to use.
64 type: str
65 application_secret:
66 required: true
67 description:
68 - The application secret to use.
69 type: str
70 consumer_key:
71 required: true
72 description:
73 - The consumer key to use.
74 type: str
75 timeout:
76 default: 120
77 description:
78 - The timeout in seconds used to wait for a task to be completed.
79 type: int
80 """
81
82 EXAMPLES = r"""
83 - name: Adds or modify the backend '212.1.1.1' to a loadbalancing 'ip-1.1.1.1'
84 ovh_ip_loadbalancing:
85 name: ip-1.1.1.1
86 backend: 212.1.1.1
87 state: present
88 probe: none
89 weight: 8
90 endpoint: ovh-eu
91 application_key: yourkey
92 application_secret: yoursecret
93 consumer_key: yourconsumerkey
94
95 - name: Removes a backend '212.1.1.1' from a loadbalancing 'ip-1.1.1.1'
96 ovh_ip_loadbalancing:
97 name: ip-1.1.1.1
98 backend: 212.1.1.1
99 state: absent
100 endpoint: ovh-eu
101 application_key: yourkey
102 application_secret: yoursecret
103 consumer_key: yourconsumerkey
104 """
105
106 import time
107
108 try:
109 import ovh
110 import ovh.exceptions
111 from ovh.exceptions import APIError
112
113 HAS_OVH = True
114 except ImportError:
115 HAS_OVH = False
116
117 from ansible.module_utils.basic import AnsibleModule
118
119
120 def getOvhClient(ansibleModule):
121 endpoint = ansibleModule.params.get("endpoint")
122 application_key = ansibleModule.params.get("application_key")
123 application_secret = ansibleModule.params.get("application_secret")
124 consumer_key = ansibleModule.params.get("consumer_key")
125
126 return ovh.Client(
127 endpoint=endpoint,
128 application_key=application_key,
129 application_secret=application_secret,
130 consumer_key=consumer_key,
131 )
132
133
134 def waitForNoTask(client, name, timeout):
135 currentTimeout = timeout
136 while len(client.get(f"/ip/loadBalancing/{name}/task")) > 0:
137 time.sleep(1) # Delay for 1 sec
138 currentTimeout -= 1
139 if currentTimeout < 0:
140 return False
141 return True
142
143
144 def main():
145 module = AnsibleModule(
146 argument_spec=dict(
147 name=dict(required=True),
148 backend=dict(required=True),
149 weight=dict(default=8, type="int"),
150 probe=dict(default="none", choices=["none", "http", "icmp", "oco"]),
151 state=dict(default="present", choices=["present", "absent"]),
152 endpoint=dict(required=True),
153 application_key=dict(required=True, no_log=True),
154 application_secret=dict(required=True, no_log=True),
155 consumer_key=dict(required=True, no_log=True),
156 timeout=dict(default=120, type="int"),
157 )
158 )
159
160 if not HAS_OVH:
161 module.fail_json(msg="ovh-api python module is required to run this module")
162
163 # Get parameters
164 name = module.params.get("name")
165 state = module.params.get("state")
166 backend = module.params.get("backend")
167 weight = module.params.get("weight")
168 probe = module.params.get("probe")
169 timeout = module.params.get("timeout")
170
171 # Connect to OVH API
172 client = getOvhClient(module)
173
174 # Check that the load balancing exists
175 try:
176 loadBalancings = client.get("/ip/loadBalancing")
177 except APIError as apiError:
178 module.fail_json(
179 msg=f"Unable to call OVH API for getting the list of loadBalancing, check application key, secret, consumerkey and parameters. "
180 f"Error returned by OVH API was : {apiError}"
181 )
182
183 if name not in loadBalancings:
184 module.fail_json(msg=f"IP LoadBalancing {name} does not exist")
185
186 # Check that no task is pending before going on
187 try:
188 if not waitForNoTask(client, name, timeout):
189 module.fail_json(
190 msg=f"Timeout of {timeout} seconds while waiting for no pending tasks before executing the module "
191 )
192 except APIError as apiError:
193 module.fail_json(
194 msg=f"Unable to call OVH API for getting the list of pending tasks of the loadBalancing, check application key, secret, consumerkey and "
195 f"parameters. Error returned by OVH API was : {apiError}"
196 )
197
198 try:
199 backends = client.get(f"/ip/loadBalancing/{name}/backend")
200 except APIError as apiError:
201 module.fail_json(
202 msg=(
203 "Unable to call OVH API for getting the list of backends "
204 "of the loadBalancing, check application key, secret, consumerkey "
205 f"and parameters. Error returned by OVH API was : {apiError}"
206 )
207 )
208
209 backendExists = backend in backends
210 moduleChanged = False
211 if state == "absent":
212 if backendExists:
213 # Remove backend
214 try:
215 client.delete(f"/ip/loadBalancing/{name}/backend/{backend}")
216 if not waitForNoTask(client, name, timeout):
217 module.fail_json(
218 msg=f"Timeout of {timeout} seconds while waiting for completion of removing backend task"
219 )
220 except APIError as apiError:
221 module.fail_json(
222 msg=f"Unable to call OVH API for deleting the backend, check application key, secret, consumerkey and parameters. "
223 f"Error returned by OVH API was : {apiError}"
224 )
225 moduleChanged = True
226 else:
227 if backendExists:
228 # Get properties
229 try:
230 backendProperties = client.get(f"/ip/loadBalancing/{name}/backend/{backend}")
231 except APIError as apiError:
232 module.fail_json(
233 msg=f"Unable to call OVH API for getting the backend properties, check application key, secret, consumerkey and parameters. "
234 f"Error returned by OVH API was : {apiError}"
235 )
236
237 if backendProperties["weight"] != weight:
238 # Change weight
239 try:
240 client.post(f"/ip/loadBalancing/{name}/backend/{backend}/setWeight", weight=weight)
241 if not waitForNoTask(client, name, timeout):
242 module.fail_json(
243 msg=f"Timeout of {timeout} seconds while waiting for completion of setWeight to backend task"
244 )
245 except APIError as apiError:
246 module.fail_json(
247 msg=f"Unable to call OVH API for updating the weight of the backend, check application key, secret, consumerkey and parameters. "
248 f"Error returned by OVH API was : {apiError}"
249 )
250 moduleChanged = True
251
252 if backendProperties["probe"] != probe:
253 # Change probe
254 backendProperties["probe"] = probe
255 try:
256 client.put(f"/ip/loadBalancing/{name}/backend/{backend}", probe=probe)
257 if not waitForNoTask(client, name, timeout):
258 module.fail_json(
259 msg=f"Timeout of {timeout} seconds while waiting for completion of setProbe to backend task"
260 )
261 except APIError as apiError:
262 module.fail_json(
263 msg=f"Unable to call OVH API for updating the probe of the backend, check application key, secret, consumerkey and parameters. "
264 f"Error returned by OVH API was : {apiError}"
265 )
266 moduleChanged = True
267
268 else:
269 # Creates backend
270 try:
271 try:
272 client.post(f"/ip/loadBalancing/{name}/backend", ipBackend=backend, probe=probe, weight=weight)
273 except APIError as apiError:
274 module.fail_json(
275 msg=f"Unable to call OVH API for creating the backend, check application key, secret, consumerkey and parameters. "
276 f"Error returned by OVH API was : {apiError}"
277 )
278
279 if not waitForNoTask(client, name, timeout):
280 module.fail_json(
281 msg=f"Timeout of {timeout} seconds while waiting for completion of backend creation task"
282 )
283 except APIError as apiError:
284 module.fail_json(
285 msg=f"Unable to call OVH API for creating the backend, check application key, secret, consumerkey and parameters. "
286 f"Error returned by OVH API was : {apiError}"
287 )
288 moduleChanged = True
289
290 module.exit_json(changed=moduleChanged)
291
292
293 if __name__ == "__main__":
294 main()