Add UI bits

This commit is contained in:
Bill Nottingham 2021-04-09 17:58:49 -04:00
parent be18803250
commit b6ced2b8dc
4 changed files with 156 additions and 0 deletions

View File

@ -20,6 +20,7 @@ import {
AzureSubForm,
EC2SubForm,
GCESubForm,
InsightsSubForm,
OpenStackSubForm,
SCMSubForm,
SatelliteSubForm,
@ -179,6 +180,13 @@ const InventorySourceFormFields = ({
sourceOptions={sourceOptions}
/>
),
insights: (
<InsightsSubForm
autoPopulateCredential={
!source?.id || source?.source !== 'insights'
}
/>
),
openstack: (
<OpenStackSubForm
autoPopulateCredential={

View File

@ -0,0 +1,82 @@
import React, { useCallback } from 'react';
import { useField, useFormikContext } from 'formik';
import { t, Trans } from '@lingui/macro';
import CredentialLookup from '../../../../components/Lookup/CredentialLookup';
import {
OptionsField,
VerbosityField,
EnabledVarField,
EnabledValueField,
HostFilterField,
SourceVarsField,
} from './SharedFields';
import { required } from '../../../../util/validators';
import getDocsBaseUrl from '../../../../util/getDocsBaseUrl';
import { useConfig } from '../../../../contexts/Config';
const InsightsSubForm = ({ autoPopulateCredential }) => {
const { setFieldValue, setFieldTouched } = useFormikContext();
const [credentialField, credentialMeta, credentialHelpers] = useField(
'credential'
);
const config = useConfig();
const handleCredentialUpdate = useCallback(
value => {
setFieldValue('credential', value);
setFieldTouched('credential', true, false);
},
[setFieldValue, setFieldTouched]
);
const pluginLink = `${getDocsBaseUrl(
config
)}/html/userguide/inventories.html#inventory-plugins`;
const configLink =
'https://docs.ansible.com/ansible/latest/collections/redhatinsights/insights/insights_inventory.html';
return (
<>
<CredentialLookup
credentialTypeNamespace="insights"
label={t`Credential`}
helperTextInvalid={credentialMeta.error}
isValid={!credentialMeta.touched || !credentialMeta.error}
onBlur={() => credentialHelpers.setTouched()}
onChange={handleCredentialUpdate}
value={credentialField.value}
required
autoPopulate={autoPopulateCredential}
validate={required(t`Select a value for this field`)}
/>
<VerbosityField />
<HostFilterField />
<EnabledVarField />
<EnabledValueField />
<OptionsField />
<SourceVarsField
popoverContent={
<>
<Trans>
Enter variables to configure the inventory source. For a detailed
description of how to configure this plugin, see{' '}
<a href={pluginLink} target="_blank" rel="noopener noreferrer">
Inventory Plugins
</a>{' '}
in the documentation and the{' '}
<a href={configLink} target="_blank" rel="noopener noreferrer">
Insights
</a>{' '}
plugin configuration guide.
</Trans>
<br />
<br />
</>
}
/>
</>
);
};
export default InsightsSubForm;

View File

@ -0,0 +1,65 @@
import React from 'react';
import { act } from 'react-dom/test-utils';
import { Formik } from 'formik';
import { mountWithContexts } from '../../../../../testUtils/enzymeHelpers';
import InsightsSubForm from './InsightsSubForm';
import { CredentialsAPI } from '../../../../api';
jest.mock('../../../../api');
const initialValues = {
credential: null,
overwrite: false,
overwrite_vars: false,
source_path: '',
source_project: null,
source_script: null,
source_vars: '---\n',
update_cache_timeout: 0,
update_on_launch: true,
update_on_project_update: false,
verbosity: 1,
};
describe('<InsightsSubForm />', () => {
let wrapper;
beforeEach(async () => {
CredentialsAPI.read.mockResolvedValue({
data: { count: 0, results: [] },
});
await act(async () => {
wrapper = mountWithContexts(
<Formik initialValues={initialValues}>
<InsightsSubForm />
</Formik>
);
});
});
afterAll(() => {
jest.clearAllMocks();
});
test('should render subform fields', () => {
expect(wrapper.find('FormGroup[label="Credential"]')).toHaveLength(1);
expect(wrapper.find('FormGroup[label="Verbosity"]')).toHaveLength(1);
expect(wrapper.find('FormGroup[label="Update options"]')).toHaveLength(1);
expect(
wrapper.find('FormGroup[label="Cache timeout (seconds)"]')
).toHaveLength(1);
expect(
wrapper.find('VariablesField[label="Source variables"]')
).toHaveLength(1);
});
test('should make expected api calls', () => {
expect(CredentialsAPI.read).toHaveBeenCalledTimes(1);
expect(CredentialsAPI.read).toHaveBeenCalledWith({
credential_type__namespace: 'insights',
order_by: 'name',
page: 1,
page_size: 5,
});
});
});

View File

@ -1,6 +1,7 @@
export { default as AzureSubForm } from './AzureSubForm';
export { default as EC2SubForm } from './EC2SubForm';
export { default as GCESubForm } from './GCESubForm';
export { default as InsightsSubForm } from './InsightsSubForm';
export { default as OpenStackSubForm } from './OpenStackSubForm';
export { default as SCMSubForm } from './SCMSubForm';
export { default as SatelliteSubForm } from './SatelliteSubForm';