Debian Codegraph @

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

raw symbols SCIP
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 # (c) 2018, Sean O'Keeffe <[email protected]>
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: content_view_version
25 version_added: 1.0.0
26 short_description: Manage Content View Versions
27 description:
28 - Publish, Promote or Remove a Content View Version
29 author: Sean O'Keeffe (@sean797)
30 notes:
31 - You cannot use this to remove a Content View Version from a Lifecycle environment, you should promote another version first.
32 - For idempotency you must specify either C(version) or C(current_lifecycle_environment).
33 options:
34 content_view:
35 description:
36 - Name of the content view
37 required: true
38 type: str
39 description:
40 description:
41 - Description of the Content View Version
42 type: str
43 version:
44 description:
45 - The content view version number (i.e. 1.0)
46 type: str
47 lifecycle_environments:
48 description:
49 - The lifecycle environments the Content View Version should be in.
50 type: list
51 elements: str
52 force_promote:
53 description:
54 - Force content view promotion and bypass lifecycle environment restriction
55 default: false
56 type: bool
57 aliases:
58 - force
59 force_yum_metadata_regeneration:
60 description:
61 - Force metadata regeneration when performing Publish and Promote tasks
62 type: bool
63 default: false
64 current_lifecycle_environment:
65 description:
66 - The lifecycle environment that is already associated with the content view version
67 - Helpful for promoting a content view version
68 type: str
69 extends_documentation_fragment:
70 - theforeman.foreman.foreman
71 - theforeman.foreman.foreman.entity_state
72 - theforeman.foreman.foreman.organization
73 '''
74
75 EXAMPLES = '''
76 - name: "Ensure content view version 2.0 is in Test & Pre Prod"
77 theforeman.foreman.content_view_version:
78 username: "admin"
79 password: "changeme"
80 server_url: "https://foreman.example.com"
81 content_view: "CV 1"
82 organization: "Default Organization"
83 version: "2.0"
84 lifecycle_environments:
85 - Test
86 - Pre Prod
87
88 - name: "Ensure content view version in Test is also in Pre Prod"
89 theforeman.foreman.content_view_version:
90 username: "admin"
91 password: "changeme"
92 server_url: "https://foreman.example.com"
93 content_view: "CV 1"
94 organization: "Default Organization"
95 current_lifecycle_environment: Test
96 lifecycle_environments:
97 - Pre Prod
98
99 - name: "Publish a content view, not idempotent"
100 theforeman.foreman.content_view_version:
101 username: "admin"
102 password: "changeme"
103 server_url: "https://foreman.example.com"
104 content_view: "CV 1"
105 organization: "Default Organization"
106
107 - name: "Publish a content view and promote that version to Library & Dev, not idempotent"
108 theforeman.foreman.content_view_version:
109 username: "admin"
110 password: "changeme"
111 server_url: "https://foreman.example.com"
112 content_view: "CV 1"
113 organization: "Default Organization"
114 lifecycle_environments:
115 - Library
116 - Dev
117
118 - name: "Ensure content view version 1.0 doesn't exist"
119 theforeman.foreman.content_view_version:
120 username: "admin"
121 password: "changeme"
122 server_url: "https://foreman.example.com"
123 content_view: "Web Servers"
124 organization: "Default Organization"
125 version: "1.0"
126 state: absent
127
128 # Obtain information about a Content View and its versions
129 - name: find all CVs
130 theforeman.foreman.resource_info:
131 username: "admin"
132 password: "changeme"
133 server_url: "https://foreman.example.com"
134 organization: "Default Organization"
135 resource: content_views
136 search: 'name="Example Content"'
137 register: example_content
138
139 # Obtain more details about all versions of a specific Content View
140 - name: "find content view versions of {{ cv_id }}"
141 theforeman.foreman.resource_info:
142 username: "admin"
143 password: "changeme"
144 server_url: "https://foreman.example.com"
145 organization: "Default Organization"
146 resource: content_view_versions
147 params:
148 content_view_id: "{{ example_content.resources[0].id }}"
149 register: version_information
150 '''
151
152 RETURN = '''
153 entity:
154 description: Final state of the affected entities grouped by their type.
155 returned: success
156 type: dict
157 contains:
158 content_view_versions:
159 description: List of content view versions.
160 type: list
161 elements: dict
162 '''
163
164
165 import re
166 from ansible_collections.theforeman.foreman.plugins.module_utils.foreman_helper import KatelloEntityAnsibleModule, _flatten_entity
167
168
169 def promote_content_view_version(module, content_view_version, environments, force, force_yum_metadata_regeneration):
170 current_environment_ids = {environment['id'] for environment in content_view_version['environments']}
171 desired_environment_ids = {environment['id'] for environment in environments}
172 promote_to_environment_ids = list(desired_environment_ids - current_environment_ids)
173
174 if promote_to_environment_ids:
175 payload = {
176 'id': content_view_version['id'],
177 'environment_ids': promote_to_environment_ids,
178 'force': force,
179 'force_yum_metadata_regeneration': force_yum_metadata_regeneration,
180 }
181
182 module.resource_action('content_view_versions', 'promote', params=payload)
183
184
185 class KatelloContentViewVersionModule(KatelloEntityAnsibleModule):
186 pass
187
188
189 def main():
190 module = KatelloContentViewVersionModule(
191 foreman_spec=dict(
192 content_view=dict(type='entity', required=True, scope=['organization']),
193 description=dict(),
194 version=dict(),
195 lifecycle_environments=dict(type='entity_list', scope=['organization'], flat_name='environments'),
196 force_promote=dict(type='bool', aliases=['force'], default=False),
197 force_yum_metadata_regeneration=dict(type='bool', default=False),
198 current_lifecycle_environment=dict(type='entity', resource_type='lifecycle_environments', scope=['organization']),
199 ),
200 mutually_exclusive=[['current_lifecycle_environment', 'version']],
201 )
202
203 module.task_timeout = 180 * 60
204
205 if 'version' in module.foreman_params and not re.match(r'^\d+\.\d+$', module.foreman_params['version']):
206 try:
207 major_version = int(module.foreman_params['version'])
208 module.foreman_params['version'] = "{0}.0".format(major_version)
209 except ValueError:
210 module.fail_json("The 'version' needs to be in the format 'X.Y', not '{0}'".format(module.foreman_params['version']))
211
212 with module.api_connection():
213 scope = module.scope_for('organization')
214 content_view = module.lookup_entity('content_view')
215
216 if 'current_lifecycle_environment' in module.foreman_params:
217 search_scope = {'content_view_id': content_view['id'], 'environment_id': module.lookup_entity('current_lifecycle_environment')['id']}
218 content_view_version = module.find_resource('content_view_versions', search=None, params=search_scope)
219 elif 'version' in module.foreman_params:
220 search = "content_view_id={0},version={1}".format(content_view['id'], module.foreman_params['version'])
221 content_view_version = module.find_resource('content_view_versions', search=search, failsafe=True)
222 else:
223 content_view_version = None
224 module.set_entity('entity', content_view_version)
225
226 if module.desired_absent:
227 module.ensure_entity('content_view_versions', None, content_view_version, params=scope)
228 else:
229 module.auto_lookup_entities()
230
231 # only need to record if not desired_absent, as ensure_entity already does all the proper recording for us
232 content_view_version_record = _flatten_entity(content_view_version, module.foreman_spec)
233 content_view_version_record['environments'] = [environment['name'] for environment in content_view_version_record.get('environments', [])]
234 module.record_before('content_view_versions', content_view_version_record)
235
236 if content_view_version is None:
237 payload = {
238 'id': content_view['id'],
239 }
240 if 'description' in module.foreman_params:
241 payload['description'] = module.foreman_params['description']
242 if 'force_yum_metadata_regeneration' in module.foreman_params:
243 payload['force_yum_metadata_regeneration'] = module.foreman_params['force_yum_metadata_regeneration']
244 if 'version' in module.foreman_params:
245 split_version = list(map(int, str(module.foreman_params['version']).split('.')))
246 payload['major'] = split_version[0]
247 payload['minor'] = split_version[1]
248
249 response = module.resource_action('content_views', 'publish', params=payload)
250 # workaround for https://projects.theforeman.org/issues/28138
251 if not module.check_mode:
252 content_view_version_id = response['output'].get('content_view_version_id') or response['input'].get('content_view_version_id')
253 content_view_version = module.show_resource('content_view_versions', content_view_version_id)
254 else:
255 content_view_version = {'id': -1, 'environments': []}
256
257 if 'lifecycle_environments' in module.foreman_params:
258 promote_content_view_version(
259 module,
260 content_view_version,
261 module.foreman_params['lifecycle_environments'],
262 force=module.foreman_params['force_promote'],
263 force_yum_metadata_regeneration=module.foreman_params['force_yum_metadata_regeneration'],
264 )
265
266 content_view_version['environments'] = module.foreman_params['lifecycle_environments']
267
268 content_view_version_record = _flatten_entity(content_view_version, module.foreman_spec)
269 content_view_version_record['environments'] = [environment['name'] for environment in content_view_version_record.get('environments', [])]
270 module.record_after('content_view_versions', content_view_version_record)
271 module.record_after_full('content_view_versions', content_view_version)
272
273
274 if __name__ == '__main__':
275 main()