From 7192d5c4bb5918e038111460439dbd60784b04ed Mon Sep 17 00:00:00 2001 From: Ryan Petrello Date: Tue, 29 Aug 2017 17:03:14 -0400 Subject: [PATCH] include extra_credentials data in summary fields for JT/Job detail view see: https://github.com/ansible/ansible-tower/issues/7521 --- awx/api/serializers.py | 35 +++++++++++++++++-- .../functional/api/test_rbac_displays.py | 1 + .../tests/unit/api/serializers/conftest.py | 3 +- .../test_job_template_serializers.py | 1 + 4 files changed, 37 insertions(+), 3 deletions(-) diff --git a/awx/api/serializers.py b/awx/api/serializers.py index f8050da8a6..e3aa182a53 100644 --- a/awx/api/serializers.py +++ b/awx/api/serializers.py @@ -89,8 +89,8 @@ SUMMARIZABLE_FK_FIELDS = { 'project': DEFAULT_SUMMARY_FIELDS + ('status', 'scm_type'), 'source_project': DEFAULT_SUMMARY_FIELDS + ('status', 'scm_type'), 'project_update': DEFAULT_SUMMARY_FIELDS + ('status', 'failed',), - 'credential': DEFAULT_SUMMARY_FIELDS + ('kind', 'cloud'), - 'vault_credential': DEFAULT_SUMMARY_FIELDS + ('kind', 'cloud'), + 'credential': DEFAULT_SUMMARY_FIELDS + ('kind', 'cloud', 'credential_type_id'), + 'vault_credential': DEFAULT_SUMMARY_FIELDS + ('kind', 'cloud', 'credential_type_id'), 'job': DEFAULT_SUMMARY_FIELDS + ('status', 'failed', 'elapsed'), 'job_template': DEFAULT_SUMMARY_FIELDS, 'workflow_job_template': DEFAULT_SUMMARY_FIELDS, @@ -2490,6 +2490,22 @@ class JobTemplateSerializer(JobTemplateMixin, UnifiedJobTemplateSerializer, JobO def validate_extra_vars(self, value): return vars_validate_or_raise(value) + def get_summary_fields(self, obj): + summary_fields = super(JobTemplateSerializer, self).get_summary_fields(obj) + if 'pk' in self.context['view'].kwargs and self.version > 1: # TODO: remove version check in 3.3 + extra_creds = [] + for cred in obj.extra_credentials.all(): + extra_creds.append({ + 'id': cred.pk, + 'name': cred.name, + 'description': cred.description, + 'kind': cred.kind, + 'credential_type_id': cred.credential_type_id + }) + summary_fields['extra_credentials'] = extra_creds + return summary_fields + + class JobSerializer(UnifiedJobSerializer, JobOptionsSerializer): @@ -2577,6 +2593,21 @@ class JobSerializer(UnifiedJobSerializer, JobOptionsSerializer): ret['extra_vars'] = obj.display_extra_vars() return ret + def get_summary_fields(self, obj): + summary_fields = super(JobSerializer, self).get_summary_fields(obj) + if 'pk' in self.context['view'].kwargs and self.version > 1: # TODO: remove version check in 3.3 + extra_creds = [] + for cred in obj.extra_credentials.all(): + extra_creds.append({ + 'id': cred.pk, + 'name': cred.name, + 'description': cred.description, + 'kind': cred.kind, + 'credential_type_id': cred.credential_type_id + }) + summary_fields['extra_credentials'] = extra_creds + return summary_fields + class JobCancelSerializer(JobSerializer): diff --git a/awx/main/tests/functional/api/test_rbac_displays.py b/awx/main/tests/functional/api/test_rbac_displays.py index 523e057465..688d4cf174 100644 --- a/awx/main/tests/functional/api/test_rbac_displays.py +++ b/awx/main/tests/functional/api/test_rbac_displays.py @@ -73,6 +73,7 @@ class TestJobTemplateCopyEdit: fake_view = FakeView() fake_view.request = request + fake_view.kwargs = {'pk': '42'} context = {} context['view'] = fake_view context['request'] = request diff --git a/awx/main/tests/unit/api/serializers/conftest.py b/awx/main/tests/unit/api/serializers/conftest.py index 9aa601d591..477c273bf9 100644 --- a/awx/main/tests/unit/api/serializers/conftest.py +++ b/awx/main/tests/unit/api/serializers/conftest.py @@ -1,4 +1,4 @@ - +import mock import pytest @@ -39,6 +39,7 @@ def get_summary_fields_assert(): def get_summary_fields_mock_and_run(): def fn(serializer_class, model_obj): serializer = serializer_class() + serializer.context['view'] = mock.Mock(kwargs={}) return serializer.get_summary_fields(model_obj) return fn diff --git a/awx/main/tests/unit/api/serializers/test_job_template_serializers.py b/awx/main/tests/unit/api/serializers/test_job_template_serializers.py index 11ddf3a2aa..ab69aeb375 100644 --- a/awx/main/tests/unit/api/serializers/test_job_template_serializers.py +++ b/awx/main/tests/unit/api/serializers/test_job_template_serializers.py @@ -108,6 +108,7 @@ class TestJobTemplateSerializerGetSummaryFields(): request.user = user view = JobTemplateDetail() view.request = request + view.kwargs = {} serializer.context['view'] = view with mocker.patch("awx.api.serializers.role_summary_fields_generator", return_value='Can eat pie'):