Preselect default galaxy cred when creating new org

This commit is contained in:
mabashian
2021-06-08 15:19:36 -04:00
parent 2ad84b60b3
commit d6a06c40f1
6 changed files with 103 additions and 7 deletions

View File

@@ -1,5 +1,4 @@
import axios from 'axios'; import axios from 'axios';
import { SESSION_TIMEOUT_KEY } from '../constants'; import { SESSION_TIMEOUT_KEY } from '../constants';
import { encodeQueryString } from '../util/qs'; import { encodeQueryString } from '../util/qs';
import debounce from '../util/debounce'; import debounce from '../util/debounce';

View File

@@ -4,7 +4,6 @@ import { PageSection, Card } from '@patternfly/react-core';
import { CardBody } from '../../../components/Card'; import { CardBody } from '../../../components/Card';
import ContentError from '../../../components/ContentError'; import ContentError from '../../../components/ContentError';
import ContentLoading from '../../../components/ContentLoading'; import ContentLoading from '../../../components/ContentLoading';
import { import {
CredentialInputSourcesAPI, CredentialInputSourcesAPI,
CredentialTypesAPI, CredentialTypesAPI,

View File

@@ -1,15 +1,40 @@
import React, { useState } from 'react'; import React, { useCallback, useEffect, useState } from 'react';
import { useHistory } from 'react-router-dom'; import { useHistory } from 'react-router-dom';
import { PageSection, Card } from '@patternfly/react-core'; import { PageSection, Card } from '@patternfly/react-core';
import useRequest from '../../../util/useRequest';
import { OrganizationsAPI } from '../../../api'; import { CredentialsAPI, OrganizationsAPI } from '../../../api';
import { CardBody } from '../../../components/Card'; import { CardBody } from '../../../components/Card';
import ContentError from '../../../components/ContentError';
import ContentLoading from '../../../components/ContentLoading';
import OrganizationForm from '../shared/OrganizationForm'; import OrganizationForm from '../shared/OrganizationForm';
function OrganizationAdd() { function OrganizationAdd() {
const history = useHistory(); const history = useHistory();
const [formError, setFormError] = useState(null); 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) => { const handleSubmit = async (values, groupsToAssociate) => {
try { try {
const { data: response } = await OrganizationsAPI.create({ const { data: response } = await OrganizationsAPI.create({
@@ -35,6 +60,30 @@ function OrganizationAdd() {
history.push('/organizations'); 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 ( return (
<PageSection> <PageSection>
<Card> <Card>
@@ -43,6 +92,7 @@ function OrganizationAdd() {
onSubmit={handleSubmit} onSubmit={handleSubmit}
onCancel={handleCancel} onCancel={handleCancel}
submitError={formError} submitError={formError}
defaultGalaxyCredential={defaultGalaxyCredential}
/> />
</CardBody> </CardBody>
</Card> </Card>

View File

@@ -6,11 +6,28 @@ import {
waitForElement, waitForElement,
} from '../../../../testUtils/enzymeHelpers'; } from '../../../../testUtils/enzymeHelpers';
import OrganizationAdd from './OrganizationAdd'; import OrganizationAdd from './OrganizationAdd';
import { OrganizationsAPI } from '../../../api'; import { CredentialsAPI, OrganizationsAPI } from '../../../api';
jest.mock('../../../api'); jest.mock('../../../api');
describe('<OrganizationAdd />', () => { 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 () => { test('onSubmit should post to api', async () => {
const updatedOrgData = { const updatedOrgData = {
name: 'new name', name: 'new name',

View File

@@ -117,6 +117,7 @@ function OrganizationForm({
onCancel, onCancel,
onSubmit, onSubmit,
submitError, submitError,
defaultGalaxyCredential,
...rest ...rest
}) { }) {
const [contentError, setContentError] = useState(null); const [contentError, setContentError] = useState(null);
@@ -181,7 +182,9 @@ function OrganizationForm({
name: organization.name, name: organization.name,
description: organization.description, description: organization.description,
max_hosts: organization.max_hosts || '0', max_hosts: organization.max_hosts || '0',
galaxy_credentials: organization.galaxy_credentials || [], galaxy_credentials:
organization.galaxy_credentials ||
(defaultGalaxyCredential ? [defaultGalaxyCredential] : []),
default_environment: default_environment:
organization.summary_fields?.default_environment || null, organization.summary_fields?.default_environment || null,
}} }}
@@ -209,6 +212,7 @@ function OrganizationForm({
} }
OrganizationForm.propTypes = { OrganizationForm.propTypes = {
defaultGalaxyCredential: PropTypes.shape(),
organization: PropTypes.shape(), organization: PropTypes.shape(),
onSubmit: PropTypes.func.isRequired, onSubmit: PropTypes.func.isRequired,
onCancel: PropTypes.func.isRequired, onCancel: PropTypes.func.isRequired,
@@ -216,6 +220,7 @@ OrganizationForm.propTypes = {
}; };
OrganizationForm.defaultProps = { OrganizationForm.defaultProps = {
defaultGalaxyCredential: null,
organization: { organization: {
id: '', id: '',
name: '', name: '',

View File

@@ -47,6 +47,32 @@ describe('<OrganizationForm />', () => {
jest.clearAllMocks(); 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 () => { test('should request related instance groups from api', async () => {
let wrapper; let wrapper;
await act(async () => { await act(async () => {