From 5238d7212ecc75f27660d428df97ed5bdb7ad997 Mon Sep 17 00:00:00 2001 From: Rigel Di Scala Date: Mon, 3 Aug 2020 22:32:36 +0200 Subject: [PATCH] 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. --- awx/api/serializers.py | 6 ++++++ .../api/pages/workflow_job_templates.py | 21 +++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/awx/api/serializers.py b/awx/api/serializers.py index be6a9d640b9d..f97548be3039 100644 --- a/awx/api/serializers.py +++ b/awx/api/serializers.py @@ -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): diff --git a/awxkit/awxkit/api/pages/workflow_job_templates.py b/awxkit/awxkit/api/pages/workflow_job_templates.py index 17f3b5634273..c134196dca1c 100644 --- a/awxkit/awxkit/api/pages/workflow_job_templates.py +++ b/awxkit/awxkit/api/pages/workflow_job_templates.py @@ -15,6 +15,27 @@ class WorkflowJobTemplate(HasCopy, HasCreate, HasNotifications, HasSurvey, Unifi optional_dependencies = [Organization] NATURAL_KEY = ('organization', 'name') + @property + def related(self): + """Augment the related namespace with the inventory. + + This provides a workaround for API instances that do not provide + a reference to this workflow's associated inventory, if defined, + in the related namespace. + + See issue #7798. + """ + 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