Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preselect default galaxy cred when creating new org #10395

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion awx/ui_next/src/api/Base.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import axios from 'axios';

import { SESSION_TIMEOUT_KEY } from '../constants';
import { encodeQueryString } from '../util/qs';
import debounce from '../util/debounce';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { PageSection, Card } from '@patternfly/react-core';
import { CardBody } from '../../../components/Card';
import ContentError from '../../../components/ContentError';
import ContentLoading from '../../../components/ContentLoading';

import {
CredentialInputSourcesAPI,
CredentialTypesAPI,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,40 @@
import React, { useState } from 'react';
import React, { useCallback, useEffect, useState } from 'react';
import { useHistory } from 'react-router-dom';
import { PageSection, Card } from '@patternfly/react-core';

import { OrganizationsAPI } from '../../../api';
import useRequest from '../../../util/useRequest';
import { CredentialsAPI, OrganizationsAPI } from '../../../api';
import { CardBody } from '../../../components/Card';
import ContentError from '../../../components/ContentError';
import ContentLoading from '../../../components/ContentLoading';
import OrganizationForm from '../shared/OrganizationForm';

function OrganizationAdd() {
const history = useHistory();
const [formError, setFormError] = useState(null);

const {
isLoading,
error: defaultGalaxyCredentialError,
request: fetchDefaultGalaxyCredential,
result: defaultGalaxyCredential,
} = useRequest(
useCallback(async () => {
const {
data: { results },
} = await CredentialsAPI.read({
credential_type__kind: 'galaxy',
managed_by_tower: true,
});

return results[0] || null;
}, []),
null
);

useEffect(() => {
fetchDefaultGalaxyCredential();
}, [fetchDefaultGalaxyCredential]);

const handleSubmit = async (values, groupsToAssociate) => {
try {
const { data: response } = await OrganizationsAPI.create({
Expand All @@ -35,6 +60,30 @@ function OrganizationAdd() {
history.push('/organizations');
};

if (defaultGalaxyCredentialError) {
return (
<PageSection>
<Card>
<CardBody>
<ContentError error={defaultGalaxyCredentialError} />
</CardBody>
</Card>
</PageSection>
);
}

if (isLoading) {
return (
<PageSection>
<Card>
<CardBody>
<ContentLoading />
</CardBody>
</Card>
</PageSection>
);
}

return (
<PageSection>
<Card>
Expand All @@ -43,6 +92,7 @@ function OrganizationAdd() {
onSubmit={handleSubmit}
onCancel={handleCancel}
submitError={formError}
defaultGalaxyCredential={defaultGalaxyCredential}
/>
</CardBody>
</Card>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,28 @@ import {
waitForElement,
} from '../../../../testUtils/enzymeHelpers';
import OrganizationAdd from './OrganizationAdd';
import { OrganizationsAPI } from '../../../api';
import { CredentialsAPI, OrganizationsAPI } from '../../../api';

jest.mock('../../../api');

describe('<OrganizationAdd />', () => {
beforeEach(() => {
CredentialsAPI.read.mockResolvedValue({
data: {
results: [
{
id: 2,
type: 'credential',
name: 'Ansible Galaxy',
credential_type: 18,
managed_by_tower: true,
kind: 'galaxy_api_token',
},
],
},
});
});

test('onSubmit should post to api', async () => {
const updatedOrgData = {
name: 'new name',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ function OrganizationForm({
onCancel,
onSubmit,
submitError,
defaultGalaxyCredential,
...rest
}) {
const [contentError, setContentError] = useState(null);
Expand Down Expand Up @@ -181,7 +182,9 @@ function OrganizationForm({
name: organization.name,
description: organization.description,
max_hosts: organization.max_hosts || '0',
galaxy_credentials: organization.galaxy_credentials || [],
galaxy_credentials:
organization.galaxy_credentials ||
(defaultGalaxyCredential ? [defaultGalaxyCredential] : []),
default_environment:
organization.summary_fields?.default_environment || null,
}}
Expand Down Expand Up @@ -209,13 +212,15 @@ function OrganizationForm({
}

OrganizationForm.propTypes = {
defaultGalaxyCredential: PropTypes.shape(),
organization: PropTypes.shape(),
onSubmit: PropTypes.func.isRequired,
onCancel: PropTypes.func.isRequired,
submitError: PropTypes.shape(),
};

OrganizationForm.defaultProps = {
defaultGalaxyCredential: null,
organization: {
id: '',
name: '',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,32 @@ describe('<OrganizationForm />', () => {
jest.clearAllMocks();
});

test('should render default galaxy credential when passed', async () => {
let wrapper;
await act(async () => {
wrapper = mountWithContexts(
<OrganizationForm
onSubmit={jest.fn()}
onCancel={jest.fn()}
me={meConfig.me}
defaultGalaxyCredential={{
id: 2,
type: 'credential',
name: 'Ansible Galaxy',
credential_type: 18,
managed_by_tower: true,
kind: 'galaxy_api_token',
}}
/>,
{
context: { network },
}
);
});
await waitForElement(wrapper, 'CredentialLookup', el => el.length === 1);
expect(wrapper.find('CredentialLookup Chip span')).toHaveLength(1);
});

test('should request related instance groups from api', async () => {
let wrapper;
await act(async () => {
Expand Down