Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions changelogs/fragments/deprecate-cv-lce-params.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
minor_changes:
- host, hostgroup - internally convert ``content_view``/``lifecycle_environment``
to content view environment ID for compatibility with newer Katello API versions
(https://github.com/theforeman/foreman-ansible-modules/pull/1977)
68 changes: 67 additions & 1 deletion plugins/module_utils/foreman_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,11 +312,13 @@ def __init__(self, **kwargs):
kickstart_repository=dict(type='entity', scope=['organization'], optional_scope=['lifecycle_environment', 'content_view'],
resource_type='repositories'),
content_view=dict(type='entity', scope=['organization'], optional_scope=['lifecycle_environment']),
content_view_environment_id=dict(type='int', invisible=True),
content_view_environment_ids=dict(type='list', elements='int', invisible=True),
activation_keys=dict(no_log=False),
)
foreman_spec.update(kwargs.pop('foreman_spec', {}))
required_plugins = kwargs.pop('required_plugins', []) + [
('katello', ['activation_keys', 'content_source', 'lifecycle_environment', 'kickstart_repository', 'content_view']),
('katello', ['activation_keys', 'content_source', 'lifecycle_environment', 'kickstart_repository', 'content_view', 'content_view_environment_id']),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
('katello', ['activation_keys', 'content_source', 'lifecycle_environment', 'kickstart_repository', 'content_view', 'content_view_environment_id']),
('katello', ['activation_keys', 'content_source', 'lifecycle_environment', 'kickstart_repository', 'content_view']),

given it's not exposed to the user, we need to flag them as "plugin required"

('openscap', ['openscap_proxy']),
]
mutually_exclusive = kwargs.pop('mutually_exclusive', []) + [['medium', 'kickstart_repository']]
Expand All @@ -326,6 +328,9 @@ def run(self, **kwargs):
entity = self.lookup_entity('entity')

if not self.desired_absent:
if 'content_view' in self.foreman_params or 'lifecycle_environment' in self.foreman_params:
self._convert_cv_lce_to_cve(entity)

if 'activation_keys' in self.foreman_params:
if 'parameters' not in self.foreman_params:
parameters = [param for param in (entity or {}).get('parameters', []) if param['name'] != 'kt_activation_keys']
Expand All @@ -343,6 +348,67 @@ def run(self, **kwargs):

return super(HostMixin, self).run(**kwargs)

def _convert_cv_lce_to_cve(self, entity):
resource = inflector.pluralize(self.entity_name)
_filtered, unsupported = self.foremanapi.validate_payload(resource, 'create', {'content_view_id': 1})
if 'content_view_id' not in unsupported:
return

if entity:
entity_cves = entity.get('content_view_environments', [])
if len(entity_cves) > 1:
self.fail_json(
msg="This {0} has multiple content view environments. "
"The 'content_view' and 'lifecycle_environment' parameters "
"cannot safely update it — they would overwrite the existing "
"multi-CV assignment.".format(self.entity_name)
)

self.lookup_entity('content_view')
self.lookup_entity('lifecycle_environment')

cv = self.foreman_params.get('content_view')
lce = self.foreman_params.get('lifecycle_environment')
Comment on lines +367 to +371

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
self.lookup_entity('content_view')
self.lookup_entity('lifecycle_environment')
cv = self.foreman_params.get('content_view')
lce = self.foreman_params.get('lifecycle_environment')
cv = self.lookup_entity('content_view')
lce = self.lookup_entity('lifecycle_environment')


cv_id = cv['id'] if cv else None
lce_id = lce['id'] if lce else None

if entity and cv_id is None:
cv_id = entity.get('content_view_id')
if cv_id is None:
entity_cves = entity.get('content_view_environments', [])
if entity_cves:
cv_id = entity_cves[0].get('content_view', {}).get('id')
if entity and lce_id is None:
lce_id = entity.get('lifecycle_environment_id')
Comment thread
jeremylenz marked this conversation as resolved.
if lce_id is None:
entity_cves = entity.get('content_view_environments', [])
if entity_cves:
lce_id = entity_cves[0].get('lifecycle_environment', {}).get('id')

if cv_id is None or lce_id is None:
self.fail_json(msg="Both 'content_view' and 'lifecycle_environment' must be provided together.")

org_id = self.lookup_entity('organization')['id'] if 'organization' in self.foreman_params else None
if org_id is None and entity:
org_id = entity.get('organization_id')

cve = self.find_content_view_environment(cv_id, lce_id, org_id)

self.foreman_spec['content_view']['ensure'] = False
self.foreman_spec['lifecycle_environment']['ensure'] = False

if resource == 'hosts':
current_cve_ids = []
if entity:
current_cve_ids = [e['id'] for e in entity.get('content_facet_attributes', {}).get('content_view_environments', [])]
if [cve['id']] != current_cve_ids:
self.foreman_params['content_view_environment_ids'] = [cve['id']]
else:
current_cve_id = entity.get('content_view_environment_id') if entity else None
if cve['id'] != current_cve_id:
self.foreman_params['content_view_environment_id'] = cve['id']


class ForemanAnsibleModule(AnsibleModule):
""" Baseclass for all foreman related Ansible modules.
Expand Down