Skip to content

Commit

Permalink
Serialize Workflow Job Template inventories by natural key - related a…
Browse files Browse the repository at this point in the history
…nsible#7798

This changeset introduces two changes:

 1. Update the API representation of Workflow Job Templates to use the
 natural key of the Inventory type instead of its id;
 2. Override the related property of the CLI's WorkflowJobTemplate page
 type to patch the related references during the export process,
 allowing the resource to be serialised using the natural key of the
 Inventory type instead of the id.

Change n.2 is a workaround that is used when exporting resources from
AWX/Tower instances that don't have change n.1. It can be removed in the
future.
  • Loading branch information
rigeldiscala committed Aug 3, 2020
1 parent b11908e commit ad0c9f1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
6 changes: 6 additions & 0 deletions awx/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3404,6 +3404,12 @@ def get_related(self, obj):
res['organization'] = self.reverse('api:organization_detail', kwargs={'pk': obj.organization.pk})
if obj.webhook_credential_id:
res['webhook_credential'] = self.reverse('api:credential_detail', kwargs={'pk': obj.webhook_credential_id})
if obj.inventory:
res['inventory'] = self.reverse(
'api:inventory_detail', kwargs={
'pk': obj.inventory.pk
}
)
return res

def validate_extra_vars(self, value):
Expand Down
13 changes: 13 additions & 0 deletions awxkit/awxkit/api/pages/workflow_job_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,19 @@ class WorkflowJobTemplate(HasCopy, HasCreate, HasNotifications, HasSurvey, Unifi
optional_dependencies = [Organization]
NATURAL_KEY = ('organization', 'name')

@property
def related(self):
related_data = self.__getattr__('related')
if 'inventory' not in related_data:
inventory_id = self.json['inventory']
if inventory_id:
endpoint_url = '/api/v2/inventories/{}/'.format(inventory_id)
related_data['inventory'] = page.TentativePage(
endpoint_url,
self.connection
)
return related_data

def launch(self, payload={}):
"""Launch using related->launch endpoint."""
# get related->launch
Expand Down

0 comments on commit ad0c9f1

Please sign in to comment.