Debian Codegraph @

ansible_collections/theforeman/foreman/plugins/modules/compute_resource.py

raw symbols SCIP
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 # (c) Philipp Joos 2017
4 # (c) Baptiste Agasse 2019
5 # (c) Mark Hlawatschek 2020
6 #
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20 from __future__ import absolute_import, division, print_function
21 __metaclass__ = type
22
23
24 DOCUMENTATION = '''
25 ---
26 module: compute_resource
27 version_added: 1.0.0
28 short_description: Manage Compute Resources
29 description:
30 - Create, update, and delete Compute Resources
31 author:
32 - "Philipp Joos (@philippj)"
33 - "Baptiste Agasse (@bagasse)"
34 - "Manisha Singhal (@Manisha15) ATIX AG"
35 - "Mark Hlawatschek (@hlawatschek) ATIX AG"
36 options:
37 name:
38 description: compute resource name
39 required: true
40 type: str
41 updated_name:
42 description: new compute resource name
43 required: false
44 type: str
45 description:
46 description: compute resource description
47 required: false
48 type: str
49 provider:
50 description: Compute resource provider. Required if I(state=present_with_defaults).
51 required: false
52 choices: ["vmware", "libvirt", "ovirt", "proxmox", "EC2", "AzureRm", "GCE", "Openstack"]
53 type: str
54 provider_params:
55 description: Parameter specific to compute resource provider. Required if I(state=present_with_defaults).
56 required: false
57 type: dict
58 suboptions:
59 url:
60 description:
61 - URL of the compute resource
62 type: str
63 user:
64 description:
65 - Username for the compute resource connection, not valid for I(provider=libvirt)
66 type: str
67 password:
68 description:
69 - Password for the compute resource connection, not valid for I(provider=libvirt)
70 type: str
71 region:
72 description:
73 - AWS region, AZURE region
74 type: str
75 tenant:
76 description:
77 - AzureRM tenant, Openstack project name
78 type: str
79 app_ident:
80 description:
81 - AzureRM client id
82 type: str
83 datacenter:
84 description:
85 - Datacenter the compute resource is in, not valid for I(provider=libvirt)
86 type: str
87 display_type:
88 description:
89 - Display type to use for the remote console, only valid for I(provider=libvirt)
90 type: str
91 use_v4:
92 description:
93 - Use oVirt API v4, only valid for I(provider=ovirt)
94 type: bool
95 ovirt_quota:
96 description:
97 - oVirt quota ID, only valid for I(provider=ovirt)
98 type: str
99 project:
100 description:
101 - Project id for I(provider=GCE)
102 type: str
103 email:
104 description:
105 - Email for I(provider=GCE)
106 type: str
107 key_path:
108 description:
109 - Certificate path for I(provider=GCE)
110 type: str
111 zone:
112 description:
113 - zone for I(provider=GCE)
114 type: str
115 cloud:
116 description:
117 - cloud for I(provider=AzureRm)
118 type: str
119 choices:
120 - azure
121 - azureusgovernment
122 - azurechina
123 - azuregermancloud
124 version_added: 2.1.0
125 sub_id:
126 description:
127 - Subscription ID for I(provider=AzureRm)
128 type: str
129 version_added: 2.1.0
130 ssl_verify_peer:
131 description:
132 - verify ssl from provider I(provider=proxmox)
133 type: bool
134 caching_enabled:
135 description:
136 - enable caching for I(provider=vmware)
137 type: bool
138 set_console_password:
139 description:
140 - Set a randomly generated password on the display connection for I(provider=vmware) and I(provider=libvirt)
141 type: bool
142 version_added: 2.0.0
143 keyboard_layout:
144 description:
145 - Default VNC Keyboard for I(provider=ovirt)
146 type: str
147 version_added: 2.0.0
148 choices:
149 - 'ar'
150 - 'da'
151 - 'de'
152 - 'de-ch'
153 - 'en-gb'
154 - 'en-us'
155 - 'es'
156 - 'et'
157 - 'fi'
158 - 'fo'
159 - 'fr'
160 - 'fr-be'
161 - 'fr-ca'
162 - 'fr-ch'
163 - 'hr'
164 - 'hu'
165 - 'is'
166 - 'it'
167 - 'ja'
168 - 'lt'
169 - 'lv'
170 - 'mk'
171 - 'nl'
172 - 'nl-be'
173 - 'no'
174 - 'pl'
175 - 'pt'
176 - 'pt-br'
177 - 'ru'
178 - 'sl'
179 - 'sv'
180 - 'th'
181 - 'tr'
182 public_key:
183 description:
184 - X509 Certification Authorities, only valid for I(provider=ovirt)
185 type: str
186 version_added: 2.0.0
187 domain:
188 description:
189 - Auth domain for users, only valid for I(provider=Openstack)
190 type: str
191 project_domain_name:
192 description:
193 - Project domain name, only valid for I(provider=Openstack)
194 type: str
195 project_domain_id:
196 description:
197 - Project domain id, only valid for I(provider=Openstack)
198 type: str
199 extends_documentation_fragment:
200 - theforeman.foreman.foreman
201 - theforeman.foreman.foreman.entity_state_with_defaults
202 - theforeman.foreman.foreman.taxonomy
203 '''
204
205 EXAMPLES = '''
206 - name: Create libvirt compute resource
207 theforeman.foreman.compute_resource:
208 name: example_compute_resource
209 locations:
210 - Munich
211 organizations:
212 - ACME
213 provider: libvirt
214 provider_params:
215 url: qemu+ssh://[email protected]/system
216 display_type: spice
217 server_url: "https://foreman.example.com"
218 username: "admin"
219 password: "changeme"
220 state: present
221
222 - name: Update libvirt compute resource
223 theforeman.foreman.compute_resource:
224 name: example_compute_resource
225 description: updated compute resource
226 locations:
227 - Munich
228 organizations:
229 - ACME
230 provider: libvirt
231 provider_params:
232 url: qemu+ssh://[email protected]/system
233 display_type: spice
234 server_url: "https://foreman.example.com"
235 username: "admin"
236 password: "changeme"
237 state: present
238
239 - name: Delete libvirt compute resource
240 theforeman.foreman.compute_resource:
241 name: example_compute_resource
242 server_url: "https://foreman.example.com"
243 username: "admin"
244 password: "changeme"
245 state: absent
246
247 - name: Create vmware compute resource
248 theforeman.foreman.compute_resource:
249 name: example_compute_resource
250 locations:
251 - Munich
252 organizations:
253 - ACME
254 provider: vmware
255 provider_params:
256 caching_enabled: false
257 url: vsphere.example.com
258 user: admin
259 password: secret
260 datacenter: ax01
261 server_url: "https://foreman.example.com"
262 username: "admin"
263 password: "changeme"
264 state: present
265
266 - name: Create ovirt compute resource
267 theforeman.foreman.compute_resource:
268 name: ovirt_compute_resource
269 locations:
270 - France/Toulouse
271 organizations:
272 - Example Org
273 provider: ovirt
274 provider_params:
275 url: ovirt.example.com
276 user: [email protected]
277 password: ovirtsecret
278 datacenter: aa92fb54-0736-4066-8fa8-b8b9e3bd75ac
279 ovirt_quota: 24868ab9-c2a1-47c3-87e7-706f17d215ac
280 use_v4: true
281 server_url: "https://foreman.example.com"
282 username: "admin"
283 password: "changeme"
284 state: present
285
286 - name: Create proxmox compute resource
287 theforeman.foreman.compute_resource:
288 name: proxmox_compute_resource
289 locations:
290 - Munich
291 organizations:
292 - ACME
293 provider: proxmox
294 provider_params:
295 url: https://proxmox.example.com:8006/api2/json
296 user: root@pam
297 password: secretpassword
298 ssl_verify_peer: true
299 server_url: "https://foreman.example.com"
300 username: "admin"
301 password: "changeme"
302 state: present
303
304 - name: create EC2 compute resource
305 theforeman.foreman.compute_resource:
306 name: EC2_compute_resource
307 description: EC2
308 locations:
309 - AWS
310 organizations:
311 - ACME
312 provider: EC2
313 provider_params:
314 user: AWS_ACCESS_KEY
315 password: AWS_SECRET_KEY
316 region: eu-west-1
317 server_url: "https://foreman.example.com"
318 username: "admin"
319 password: "changeme"
320 state: present
321
322 - name: create Azure compute resource
323 theforeman.foreman.compute_resource:
324 name: AzureRm_compute_resource
325 description: AzureRm
326 locations:
327 - Azure
328 organizations:
329 - ACME
330 provider: AzureRm
331 provider_params:
332 sub_id: SUBSCRIPTION_ID
333 tenant: TENANT_ID
334 app_ident: CLIENT_ID
335 password: CLIENT_SECRET
336 region: westeurope
337 server_url: "https://foreman.example.com"
338 username: "admin"
339 password: "changeme"
340 state: present
341
342 - name: create GCE compute resource
343 theforeman.foreman.compute_resource:
344 name: GCE compute resource
345 description: Google Cloud Engine
346 locations:
347 - GCE
348 organizations:
349 - ACME
350 provider: GCE
351 provider_params:
352 project: orcharhino
353 email: [email protected]
354 key_path: "/usr/share/foreman/gce_orcharhino_key.json"
355 zone: europe-west3-b
356 server_url: "https://foreman.example.com"
357 username: "admin"
358 password: "changeme"
359 state: present
360
361 - name: create Openstack compute resource
362 theforeman.foreman.compute_resource:
363 name: Openstack compute resource
364 description: Openstack
365 locations:
366 - DC1
367 organizations:
368 - ACME
369 provider: Openstack
370 provider_params:
371 url: "https://Openstack.example.com:5000/v3"
372 user: admin
373 password: secret
374 tenant: "MyProject"
375 domain: "example.com"
376 project_domain_name: "example.com"
377 project_domain_id: "0123456789a0123456789b0123456789"
378 server_url: "https://foreman.example.com"
379 username: "admin"
380 password: "changeme"
381 state: present
382 '''
383
384 RETURN = '''
385 entity:
386 description: Final state of the affected entities grouped by their type.
387 returned: success
388 type: dict
389 contains:
390 compute_resources:
391 description: List of compute resources.
392 type: list
393 elements: dict
394 '''
395
396
397 from ansible_collections.theforeman.foreman.plugins.module_utils.foreman_helper import ForemanTaxonomicEntityAnsibleModule
398
399
400 def get_provider_info(provider):
401 provider_name = provider.lower()
402
403 if provider_name == 'libvirt':
404 return 'Libvirt', ['url', 'display_type', 'set_console_password']
405
406 elif provider_name == 'ovirt':
407 return 'Ovirt', ['url', 'user', 'password', 'datacenter', 'use_v4', 'ovirt_quota', 'keyboard_layout', 'public_key']
408
409 elif provider_name == 'proxmox':
410 return 'Proxmox', ['url', 'user', 'password', 'ssl_verify_peer']
411
412 elif provider_name == 'vmware':
413 return 'Vmware', ['url', 'user', 'password', 'datacenter', 'caching_enabled', 'set_console_password']
414
415 elif provider_name == 'ec2':
416 return 'EC2', ['user', 'password', 'region']
417
418 elif provider_name == 'azurerm':
419 return 'AzureRm', ['user', 'password', 'tenant', 'region', 'app_ident', 'cloud', 'sub_id']
420
421 elif provider_name == 'gce':
422 return 'GCE', ['project', 'email', 'key_path', 'zone']
423
424 elif provider_name == 'openstack':
425 return 'Openstack', ['url', 'user', 'password', 'tenant', 'domain', 'project_domain_name', 'project_domain_id']
426
427 else:
428 return '', []
429
430
431 class ForemanComputeResourceModule(ForemanTaxonomicEntityAnsibleModule):
432 pass
433
434
435 def main():
436 module = ForemanComputeResourceModule(
437 foreman_spec=dict(
438 name=dict(required=True),
439 updated_name=dict(),
440 description=dict(),
441 provider=dict(choices=['vmware', 'libvirt', 'ovirt', 'proxmox', 'EC2', 'AzureRm', 'GCE', 'Openstack']),
442 display_type=dict(invisible=True),
443 datacenter=dict(invisible=True),
444 url=dict(invisible=True),
445 caching_enabled=dict(invisible=True),
446 user=dict(invisible=True),
447 password=dict(invisible=True),
448 region=dict(invisible=True),
449 tenant=dict(invisible=True),
450 app_ident=dict(invisible=True),
451 use_v4=dict(invisible=True),
452 ovirt_quota=dict(invisible=True),
453 project=dict(invisible=True),
454 email=dict(invisible=True),
455 key_path=dict(invisible=True),
456 zone=dict(invisible=True),
457 cloud=dict(invisible=True),
458 ssl_verify_peer=dict(invisible=True),
459 set_console_password=dict(invisible=True),
460 keyboard_layout=dict(invisible=True),
461 public_key=dict(invisible=True),
462 sub_id=dict(invisible=True),
463 domain=dict(invisible=True),
464 project_domain_name=dict(invisible=True),
465 project_domain_id=dict(invisible=True),
466 ),
467 argument_spec=dict(
468 provider_params=dict(type='dict', options=dict(
469 url=dict(),
470 display_type=dict(),
471 user=dict(),
472 password=dict(no_log=True),
473 region=dict(),
474 tenant=dict(),
475 app_ident=dict(),
476 datacenter=dict(),
477 caching_enabled=dict(type='bool'),
478 use_v4=dict(type='bool'),
479 ovirt_quota=dict(),
480 project=dict(),
481 email=dict(),
482 key_path=dict(no_log=False),
483 zone=dict(),
484 cloud=dict(choices=['azure', 'azureusgovernment', 'azurechina', 'azuregermancloud']),
485 ssl_verify_peer=dict(type='bool'),
486 set_console_password=dict(type='bool'),
487 keyboard_layout=dict(choices=['ar', 'de-ch', 'es', 'fo', 'fr-ca', 'hu', 'ja', 'mk', 'no', 'pt-br', 'sv', 'da', 'en-gb', 'et', 'fr', 'fr-ch',
488 'is', 'lt', 'nl', 'pl', 'ru', 'th', 'de', 'en-us', 'fi', 'fr-be', 'hr', 'it', 'lv', 'nl-be', 'pt', 'sl', 'tr']),
489 public_key=dict(),
490 sub_id=dict(),
491 domain=dict(),
492 project_domain_name=dict(),
493 project_domain_id=dict(),
494 ),
495 mutually_exclusive=[['user', 'sub_id']],
496 ),
497 state=dict(type='str', default='present', choices=['present', 'absent', 'present_with_defaults']),
498 ),
499 required_if=[
500 ['state', 'present_with_defaults', ['provider', 'provider_params']],
501 ],
502 )
503
504 if not module.desired_absent:
505 if 'provider' in module.foreman_params:
506 module.foreman_params['provider'], provider_param_keys = get_provider_info(provider=module.foreman_params['provider'])
507 provider_params = module.foreman_params.pop('provider_params', {})
508
509 if module.foreman_params['provider'] == 'AzureRm' and 'user' in provider_params:
510 provider_params['sub_id'] = provider_params.pop('user')
511 for key in provider_param_keys:
512 if key in provider_params:
513 module.foreman_params[key] = provider_params.pop(key)
514 if provider_params:
515 module.fail_json(msg="Provider {0} does not support the following given parameters: {1}".format(
516 module.foreman_params['provider'], list(provider_params.keys())))
517
518 with module.api_connection():
519 entity = module.lookup_entity('entity')
520 if not module.desired_absent and 'provider' not in module.foreman_params and entity is None:
521 module.fail_json(msg='To create a compute resource a valid provider must be supplied')
522
523 module.run()
524
525
526 if __name__ == '__main__':
527 main()