Skip to content

Commit

Permalink
add katello_content_view_filter module
Browse files Browse the repository at this point in the history
  • Loading branch information
sean797 committed Jul 25, 2018
1 parent 2dedb38 commit 8311f5d
Show file tree
Hide file tree
Showing 34 changed files with 12,093 additions and 5 deletions.
48 changes: 43 additions & 5 deletions module_utils/ansible_nailgun_cement.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,18 @@
from nailgun.entities import (
_check_for_value,
Entity,
AbstractContentViewFilter,
CommonParameter,
ContentView,
ContentViewFilterRule,
ContentViewVersion,
Domain,
Errata,
LifecycleEnvironment,
Location,
OperatingSystem,
Organization,
PackageGroup,
Ping,
Product,
Realm,
Expand Down Expand Up @@ -229,12 +233,12 @@ def update_fields(new, old, fields):


# Common functionality to manipulate entities
def naildown_entity_state(entity_class, entity_dict, entity, state, module):
changed, _ = naildown_entity(entity_class, entity_dict, entity, state, module)
def naildown_entity_state(entity_class, entity_dict, entity, state, module, check_missing=[]):
changed, _ = naildown_entity(entity_class, entity_dict, entity, state, module, check_missing)
return changed


def naildown_entity(entity_class, entity_dict, entity, state, module):
def naildown_entity(entity_class, entity_dict, entity, state, module, check_missing):
""" Ensure that a given entity has a certain state """
changed, changed_entity = False, entity
if state == 'present_with_defaults':
Expand All @@ -244,7 +248,7 @@ def naildown_entity(entity_class, entity_dict, entity, state, module):
if entity is None:
changed, changed_entity = create_entity(entity_class, entity_dict, module)
else:
changed, changed_entity = update_entity(entity, entity_dict, module)
changed, changed_entity = update_entity(entity, entity_dict, module, check_missing)
elif state == 'absent':
if entity is not None:
changed, changed_entity = delete_entity(entity, module)
Expand Down Expand Up @@ -304,7 +308,7 @@ def fields_equal(value1, value2):
return value1 == value2


def update_entity(old_entity, entity_dict, module):
def update_entity(old_entity, entity_dict, module, check_missing):
try:
volatile_entity = old_entity.read()
result = volatile_entity
Expand All @@ -313,6 +317,17 @@ def update_entity(old_entity, entity_dict, module):
if key in entity_dict and not fields_equal(value, entity_dict[key]):
volatile_entity.__setattr__(key, entity_dict[key])
fields.append(key)
# check_missing is a special case, Foreman sometimes returns different values
# depending on what 'type' of same object you are requesting. Content View
# Filters are a prime example. We list these attributes in `check_missing`
# so we can ensure the entity is as the user specified.
if key not in entity_dict and key in check_missing:
volatile_entity.__setattr__(key, None)
fields.append(key)
for key in check_missing:
if key in entity_dict and key not in volatile_entity.get_values():
volatile_entity.__setattr__(key, entity_dict[key])
fields.append(key)
if len(fields) > 0:
if not module.check_mode:
result = volatile_entity.update(fields)
Expand All @@ -333,6 +348,16 @@ def delete_entity(entity, module):
return True, None


def find_package_group(module, name, failsafe=False):
package_group = PackageGroup().search(set(), {'search': 'name="{}"'.format(name)})
return handle_find_response(module, package_group, message="No package group found for %s" % name, failsafe=failsafe)


def find_errata(module, id, organization, failsafe=False):
errata = Errata().search(set(), {'search': 'id="{}"'.format(id)})
return handle_find_response(module, errata, message="No errata found for %s" % id, failsafe=failsafe)


def find_content_view(module, name, organization, failsafe=False):
content_view = ContentView(name=name, organization=organization)
return handle_find_response(module, content_view.search(), message="No content view found for %s" % name, failsafe=failsafe)
Expand All @@ -349,6 +374,19 @@ def find_content_view_version(module, content_view, environment=None, version=No
format(content_view.name, version), failsafe=failsafe)


def find_content_view_filter_rule(module, content_view_filter, name=False, errata=False, failsafe=False):
if errata is not False:
content_view_filter_rule = ContentViewFilterRule(content_view_filter=content_view_filter, errata=errata).search()
else:
content_view_filter_rule = ContentViewFilterRule(name=name, content_view_filter=content_view_filter).search()
return handle_find_response(module, content_view_filter_rule, message="No content view filter rule found for %s" % name or errata, failsafe=failsafe)


def find_content_view_filter(module, name, content_view, failsafe=False):
content_view_filter = AbstractContentViewFilter(name=name, content_view=content_view)
return handle_find_response(module, content_view_filter.search(), message="No content view filter found for %s" % name, failsafe=failsafe)


def find_organizations(module, organizations):
return list(map(lambda organization: find_organization(module, organization), organizations))

Expand Down
Loading

0 comments on commit 8311f5d

Please sign in to comment.