Skip to content

Commit

Permalink
Rework the export of full dependent objects
Browse files Browse the repository at this point in the history
and include hosts and groups in the export.
  • Loading branch information
jbradberry authored and ryanpetrello committed Oct 30, 2020
1 parent df87496 commit 607bc07
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 12 deletions.
50 changes: 38 additions & 12 deletions awxkit/awxkit/api/pages/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,38 @@
'NotificationTemplates',
'WorkflowJobTemplateNodes',
'Credentials',
'Hosts',
'Groups',
]


EXPORTABLE_DEPENDENT_OBJECTS = [
'Labels',
'SurveySpec',
'Schedules',
# WFJT Nodes are a special case, we want full data for the create
# view and natural keys for the attach views.
'WorkflowJobTemplateNodes',
# These are special-case related objects, where we want only in this
# case to export a full object instead of a natural key reference.
DEPENDENT_EXPORT = [
('JobTemplate', 'labels'),
('JobTemplate', 'survey_spec'),
('JobTemplate', 'schedules'),
('WorkflowJobTemplate', 'labels'),
('WorkflowJobTemplate', 'survey_spec'),
('WorkflowJobTemplate', 'schedules'),
('WorkflowJobTemplate', 'workflow_nodes'),
('Project', 'schedules'),
('InventorySource', 'schedules'),
('Inventory', 'groups'),
('Inventory', 'hosts'),
]


# This is for related views where it is unneeded to export anything,
# such as because it is a calculated subset of objects covered by a
# different view.
DEPENDENT_NONEXPORT = [
('InventorySource', 'groups'),
('InventorySource', 'hosts'),
('Inventory', 'root_groups'),
('Group', 'all_hosts'),
('Group', 'potential_children'),
('Host', 'all_groups'),
]


Expand All @@ -61,6 +83,9 @@ def _export(self, _page, post_fields):
if _page.json.get('managed_by_tower'):
log.debug("%s is managed by Tower, skipping.", _page.endpoint)
return None
# Drop any hosts, groups, or inventories that were pulled in programmatically by an inventory source.
if _page.json.get('has_inventory_sources'):
return None
if post_fields is None: # Deprecated endpoint or insufficient permissions
log.error("Object export failed: %s", _page.endpoint)
return None
Expand Down Expand Up @@ -107,8 +132,9 @@ def _export(self, _page, post_fields):

rel = rel_endpoint._create()
is_relation = rel.__class__.__name__ in EXPORTABLE_RELATIONS
is_dependent = rel.__class__.__name__ in EXPORTABLE_DEPENDENT_OBJECTS
if not (is_relation or is_dependent):
is_dependent = (_page.__item_class__.__name__, key) in DEPENDENT_EXPORT
is_blocked = (_page.__item_class__.__name__, key) in DEPENDENT_NONEXPORT
if is_blocked or not (is_relation or is_dependent):
continue

rel_post_fields = utils.get_post_fields(rel_endpoint, self._cache)
Expand All @@ -117,10 +143,10 @@ def _export(self, _page, post_fields):
continue
is_attach = 'id' in rel_post_fields # This is not a create-only endpoint.

if is_relation and is_attach:
by_natural_key = True
elif is_dependent:
if is_dependent:
by_natural_key = False
elif is_relation and is_attach and not is_blocked:
by_natural_key = True
else:
continue

Expand Down
2 changes: 2 additions & 0 deletions awxkit/awxkit/api/pages/inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ class Group(HasCreate, HasVariables, base.Base):

dependencies = [Inventory]
optional_dependencies = [Credential, InventoryScript]
NATURAL_KEY = ('name', 'inventory')

@property
def is_root_group(self):
Expand Down Expand Up @@ -367,6 +368,7 @@ class Groups(page.PageList, Group):
class Host(HasCreate, HasVariables, base.Base):

dependencies = [Inventory]
NATURAL_KEY = ('name', 'inventory')

def payload(self, inventory, **kwargs):
payload = PseudoNamespace(
Expand Down

0 comments on commit 607bc07

Please sign in to comment.