Skip to content

Commit 32c08a0

Browse files
rdiscalajbradberry
authored andcommitted
Serialize Workflow Job Template inventories by natural key - related #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.
1 parent a170040 commit 32c08a0

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

awx/api/serializers.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3438,6 +3438,12 @@ def get_related(self, obj):
34383438
res['organization'] = self.reverse('api:organization_detail', kwargs={'pk': obj.organization.pk})
34393439
if obj.webhook_credential_id:
34403440
res['webhook_credential'] = self.reverse('api:credential_detail', kwargs={'pk': obj.webhook_credential_id})
3441+
if obj.inventory:
3442+
res['inventory'] = self.reverse(
3443+
'api:inventory_detail', kwargs={
3444+
'pk': obj.inventory.pk
3445+
}
3446+
)
34413447
return res
34423448

34433449
def validate_extra_vars(self, value):

awxkit/awxkit/api/pages/workflow_job_templates.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,27 @@ class WorkflowJobTemplate(HasCopy, HasCreate, HasNotifications, HasSurvey, Unifi
1515
optional_dependencies = [Organization]
1616
NATURAL_KEY = ('organization', 'name')
1717

18+
@property
19+
def related(self):
20+
"""Augment the related namespace with the inventory.
21+
22+
This provides a workaround for API instances that do not provide
23+
a reference to this workflow's associated inventory, if defined,
24+
in the related namespace.
25+
26+
See issue #7798.
27+
"""
28+
related_data = self.__getattr__('related')
29+
if 'inventory' not in related_data:
30+
inventory_id = self.json['inventory']
31+
if inventory_id:
32+
endpoint_url = '/api/v2/inventories/{}/'.format(inventory_id)
33+
related_data['inventory'] = page.TentativePage(
34+
endpoint_url,
35+
self.connection
36+
)
37+
return related_data
38+
1839
def launch(self, payload={}):
1940
"""Launch using related->launch endpoint."""
2041
# get related->launch

0 commit comments

Comments
 (0)