Debian Codegraph @

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

raw symbols SCIP
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 # (c) 2018 Manuel Bonk (ATIX AG)
4 #
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 from __future__ import absolute_import, division, print_function
19 __metaclass__ = type
20
21
22 DOCUMENTATION = '''
23 ---
24 module: installation_medium
25 version_added: 1.0.0
26 short_description: Manage Installation Media
27 description:
28 - Create, update, and delete Installation Media
29 author:
30 - "Manuel Bonk(@manuelbonk) ATIX AG"
31 options:
32 name:
33 description:
34 - The full installation medium name.
35 - The special name "*" (only possible as parameter) is used to perform bulk actions (modify, delete) on all existing partition tables.
36 required: true
37 type: str
38 updated_name:
39 description: New full installation medium name. When this parameter is set, the module will not be idempotent.
40 type: str
41 os_family:
42 description:
43 - The OS family the template shall be assigned with.
44 - If no os_family is set but a operatingsystem, the value will be derived from it.
45 path:
46 description: Path to the installation medium
47 type: str
48 extends_documentation_fragment:
49 - theforeman.foreman.foreman
50 - theforeman.foreman.foreman.entity_state_with_defaults
51 - theforeman.foreman.foreman.taxonomy
52 - theforeman.foreman.foreman.os_family
53 - theforeman.foreman.foreman.operatingsystems
54 '''
55
56 EXAMPLES = '''
57 - name: create new debian medium
58 theforeman.foreman.installation_medium:
59 name: "wheezy"
60 locations:
61 - "Munich"
62 organizations:
63 - "ACME"
64 operatingsystems:
65 - "Debian"
66 path: "http://debian.org/mirror/"
67 server_url: "https://foreman.example.com"
68 username: "admin"
69 password: "changeme"
70 state: present
71 '''
72
73 RETURN = '''
74 entity:
75 description: Final state of the affected entities grouped by their type.
76 returned: success
77 type: dict
78 contains:
79 media:
80 description: List of installation media.
81 type: list
82 elements: dict
83 '''
84
85 from ansible_collections.theforeman.foreman.plugins.module_utils.foreman_helper import ForemanTaxonomicEntityAnsibleModule, OS_LIST
86
87
88 class ForemanInstallationMediumModule(ForemanTaxonomicEntityAnsibleModule):
89 pass
90
91
92 def main():
93 module = ForemanInstallationMediumModule(
94 argument_spec=dict(
95 updated_name=dict(),
96 state=dict(default='present', choices=['present', 'present_with_defaults', 'absent']),
97 ),
98 foreman_spec=dict(
99 name=dict(required=True),
100 operatingsystems=dict(type='entity_list'),
101 os_family=dict(choices=OS_LIST),
102 path=dict(),
103 ),
104 entity_opts=dict(
105 resource_type='media',
106 ),
107 entity_name='medium',
108 )
109
110 module_params = module.foreman_params
111 entity = None
112
113 name = module_params['name']
114
115 affects_multiple = name == '*'
116 # sanitize user input, filter unuseful configuration combinations with 'name: *'
117 if affects_multiple:
118 if module.state == 'present_with_defaults':
119 module.fail_json(msg="'state: present_with_defaults' and 'name: *' cannot be used together")
120 if module.params['updated_name']:
121 module.fail_json(msg="updated_name not allowed if 'name: *'!")
122 if module.desired_absent:
123 further_params = set(module_params.keys()) - {'name', 'entity'}
124 if further_params:
125 module.fail_json(msg='When deleting all installation media, there is no need to specify further parameters: %s ' % further_params)
126
127 with module.api_connection():
128 if affects_multiple:
129 module.set_entity('entity', None) # prevent lookup
130 entities = module.list_resource('media')
131 if not entities:
132 # Nothing to do shortcut to exit
133 module.exit_json()
134 if not module.desired_absent: # not 'thin'
135 entities = [module.show_resource('media', entity['id']) for entity in entities]
136 module.auto_lookup_entities()
137 module_params.pop('name')
138 for entity in entities:
139 module.ensure_entity('media', module_params, entity)
140 else:
141 entity = module.lookup_entity('entity')
142 if not module.desired_absent and 'operatingsystems' in module_params:
143 operatingsystems = module.lookup_entity('operatingsystems')
144 if len(operatingsystems) == 1 and 'os_family' not in module_params and entity is None:
145 module_params['os_family'] = module.show_resource('operatingsystems', operatingsystems[0]['id'])['family']
146
147 module.run()
148
149
150 if __name__ == '__main__':
151 main()