From 4f132e302f9b69e9e0a2eb3187686ab96e5c609b Mon Sep 17 00:00:00 2001 From: nixocio Date: Mon, 16 Nov 2020 14:23:34 -0500 Subject: Mark organization as a required field for Galaxy crendentials Mark organization as a required field for Galaxy credential type. See: https://github.com/ansible/awx/issues/8276 --- .../screens/Credential/shared/CredentialForm.jsx | 17 ++++- .../Credential/shared/CredentialForm.test.jsx | 21 +++++ .../Credential/shared/data.credentialTypes.json | 51 ++++++++++++- .../Credential/shared/data.galaxyCredential.json | 89 ++++++++++++++++++++++ 4 files changed, 176 insertions(+), 2 deletions(-) create mode 100644 awx/ui_next/src/screens/Credential/shared/data.galaxyCredential.json diff --git a/awx/ui_next/src/screens/Credential/shared/CredentialForm.jsx b/awx/ui_next/src/screens/Credential/shared/CredentialForm.jsx index c3d6856136..5d5382cbe0 100644 --- a/awx/ui_next/src/screens/Credential/shared/CredentialForm.jsx +++ b/awx/ui_next/src/screens/Credential/shared/CredentialForm.jsx @@ -22,12 +22,26 @@ function CredentialFormFields({ initialValues, }) { const { setFieldValue } = useFormikContext(); - const [orgField, orgMeta, orgHelpers] = useField('organization'); + const [credTypeField, credTypeMeta, credTypeHelpers] = useField({ name: 'credential_type', validate: required(i18n._(t`Select a value for this field`), i18n), }); + const isGalaxyCredential = + !!credTypeField.value && + credentialTypes[credTypeField.value].kind === 'galaxy'; + + const [orgField, orgMeta, orgHelpers] = useField({ + name: 'organization', + validate: + isGalaxyCredential && + required( + i18n._(t`Galaxy credentials must be owned by an Organization.`), + i18n + ), + }); + const credentialTypeOptions = Object.keys(credentialTypes) .map(key => { return { @@ -108,6 +122,7 @@ function CredentialFormFields({ value={orgField.value} touched={orgMeta.touched} error={orgMeta.error} + required={isGalaxyCredential} /> ', () => { gceFieldExpects(); }); + + test('should display from fields for galaxy/automation hub credentials', async () => { + await act(async () => { + wrapper = mountWithContexts( + + ); + }); + expect(wrapper.find('FormGroup[label="Name"]').length).toBe(1); + expect(wrapper.find('FormGroup[label="Description"]').length).toBe(1); + expect(wrapper.find('FormGroup[label="Organization"]').length).toBe(1); + expect( + wrapper.find('FormGroup[label="Organization"]').prop('isRequired') + ).toBe(true); + expect(wrapper.find('FormGroup[label="Credential Type"]').length).toBe(1); + }); }); }); diff --git a/awx/ui_next/src/screens/Credential/shared/data.credentialTypes.json b/awx/ui_next/src/screens/Credential/shared/data.credentialTypes.json index 98db5efeab..b7b3189951 100644 --- a/awx/ui_next/src/screens/Credential/shared/data.credentialTypes.json +++ b/awx/ui_next/src/screens/Credential/shared/data.credentialTypes.json @@ -1269,5 +1269,54 @@ "required": ["vault_password"] }, "injectors": {} - } + }, + { + "id": 42, + "type": "credential_type", + "url": "/api/v2/credential_types/42/", + "related": { + "credentials": "/api/v2/credential_types/42/credentials/", + "activity_stream": "/api/v2/credential_types/42/activity_stream/" + }, + "summary_fields": { + "user_capabilities": { + "edit": true, + "delete": true + } + }, + "created": "2020-11-11T00:18:31.928286Z", + "modified": "2020-11-11T00:19:00.851597Z", + "name": "Ansible Galaxy/Automation Hub API Token", + "description": "", + "kind": "galaxy", + "namespace": "galaxy_api_token", + "managed_by_tower": true, + "inputs": { + "fields": [ + { + "id": "url", + "label": "Galaxy Server URL", + "type": "string", + "help_text": "The URL of the Galaxy instance to connect to." + }, + { + "id": "auth_url", + "label": "Auth Server URL", + "type": "string", + "help_text": "The URL of a Keycloak server token_endpoint, if using SSO auth." + }, + { + "id": "token", + "label": "API Token", + "type": "string", + "secret": true, + "help_text": "A token to use for authentication against the Galaxy instance." + } + ], + "required": [ + "url" + ] + }, + "injectors": {} +} ] diff --git a/awx/ui_next/src/screens/Credential/shared/data.galaxyCredential.json b/awx/ui_next/src/screens/Credential/shared/data.galaxyCredential.json new file mode 100644 index 0000000000..a8c7184548 --- /dev/null +++ b/awx/ui_next/src/screens/Credential/shared/data.galaxyCredential.json @@ -0,0 +1,89 @@ +{ + "id": 5, + "type": "credential", + "url": "/api/v2/credentials/5/", + "related": { + "named_url": "/api/v2/credentials/Foo++Ansible Galaxy%2FAutomation Hub API Token+galaxy++Default/", + "created_by": "/api/v2/users/1/", + "modified_by": "/api/v2/users/1/", + "organization": "/api/v2/organizations/1/", + "activity_stream": "/api/v2/credentials/5/activity_stream/", + "access_list": "/api/v2/credentials/5/access_list/", + "object_roles": "/api/v2/credentials/5/object_roles/", + "owner_users": "/api/v2/credentials/5/owner_users/", + "owner_teams": "/api/v2/credentials/5/owner_teams/", + "copy": "/api/v2/credentials/5/copy/", + "input_sources": "/api/v2/credentials/5/input_sources/", + "credential_type": "/api/v2/credential_types/42/" + }, + "summary_fields": { + "organization": { + "id": 1, + "name": "Baz", + "description": "" + }, + "credential_type": { + "id": 42, + "name": "Ansible Galaxy/Automation Hub API Token", + "description": "" + }, + "created_by": { + "id": 1, + "username": "admin", + "first_name": "", + "last_name": "" + }, + "modified_by": { + "id": 1, + "username": "admin", + "first_name": "", + "last_name": "" + }, + "object_roles": { + "admin_role": { + "description": "Can manage all aspects of the credential", + "name": "Admin", + "id": 109 + }, + "use_role": { + "description": "Can use the credential in a job template", + "name": "Use", + "id": 110 + }, + "read_role": { + "description": "May view settings for the credential", + "name": "Read", + "id": 111 + } + }, + "user_capabilities": { + "edit": true, + "delete": true, + "copy": true, + "use": true + }, + "owners": [ + { + "id": 1, + "type": "organization", + "name": "Default", + "description": "", + "url": "/api/v2/organizations/1/" + } + ] + }, + "created": "2020-11-16T21:33:39.385284Z", + "modified": "2020-11-16T21:33:39.385311Z", + "name": "Foo", + "description": "Bar", + "organization": 1, + "credential_type": 42, + "managed_by_tower": false, + "inputs": { + "url": "https://localhost.com", + "auth_url": "" + }, + "kind": "galaxy_api_token", + "cloud": false, + "kubernetes": false +} \ No newline at end of file -- cgit v1.2.3