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 Aug 8, 2018
1 parent acb51d9 commit 812ba16
Show file tree
Hide file tree
Showing 34 changed files with 12,094 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 @@ -10,14 +10,18 @@
_check_for_value,
ActivationKey,
Entity,
AbstractContentViewFilter,
CommonParameter,
ContentView,
ContentViewFilterRule,
ContentViewVersion,
Domain,
Errata,
LifecycleEnvironment,
Location,
OperatingSystem,
Organization,
PackageGroup,
Ping,
Product,
Realm,
Expand Down Expand Up @@ -231,12 +235,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 @@ -246,7 +250,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 == 'copied':
new_entity = entity_class(name=entity_dict['new_name'], organization=entity_dict['organization']).search()
if entity is not None and len(new_entity) == 0:
Expand Down Expand Up @@ -323,7 +327,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 @@ -332,6 +336,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 @@ -357,6 +372,16 @@ def find_activation_key(module, name, organization, failsafe=False):
return handle_find_response(module, activation_key.search(), message="No activation key found for %s" % name, failsafe=failsafe)


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 @@ -373,6 +398,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 812ba16

Please sign in to comment.