Skip to content

Commit

Permalink
Add EE to the settings page
Browse files Browse the repository at this point in the history
Allow a system admin to set the global default execution environment.

See: ansible#9088

This PR is also addressing the issue: ansible#9669
  • Loading branch information
nixocio committed Mar 23, 2021
1 parent 646c9cc commit d4841bb
Show file tree
Hide file tree
Showing 8 changed files with 254 additions and 33 deletions.
2 changes: 1 addition & 1 deletion awx/main/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@
default=None,
queryset=ExecutionEnvironment.objects.all(),
label=_('Global default execution environment'),
help_text=_('.'),
help_text=_('The Execution Environment to be used when one has not been configured for a job template.'),
category=_('System'),
category_slug='system',
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import ContentError from '../../../../components/ContentError';
import ContentLoading from '../../../../components/ContentLoading';
import { DetailList } from '../../../../components/DetailList';
import RoutedTabs from '../../../../components/RoutedTabs';
import { SettingsAPI } from '../../../../api';
import { SettingsAPI, ExecutionEnvironmentsAPI } from '../../../../api';
import useRequest from '../../../../util/useRequest';
import { useConfig } from '../../../../contexts/Config';
import { useSettings } from '../../../../contexts/Settings';
Expand All @@ -23,7 +23,15 @@ function MiscSystemDetail({ i18n }) {
const { isLoading, error, request, result: system } = useRequest(
useCallback(async () => {
const { data } = await SettingsAPI.readCategory('all');

let DEFAULT_EXECUTION_ENVIRONMENT = '';
if (data.DEFAULT_EXECUTION_ENVIRONMENT) {
const {
data: { name },
} = await ExecutionEnvironmentsAPI.readDetail(
data.DEFAULT_EXECUTION_ENVIRONMENT
);
DEFAULT_EXECUTION_ENVIRONMENT = name;
}
const {
OAUTH2_PROVIDER: {
ACCESS_TOKEN_EXPIRE_SECONDS,
Expand All @@ -49,19 +57,17 @@ function MiscSystemDetail({ i18n }) {
'SESSION_COOKIE_AGE',
'TOWER_URL_BASE'
);

const systemData = {
...pluckedSystemData,
ACCESS_TOKEN_EXPIRE_SECONDS,
REFRESH_TOKEN_EXPIRE_SECONDS,
AUTHORIZATION_CODE_EXPIRE_SECONDS,
DEFAULT_EXECUTION_ENVIRONMENT,
};

const {
OAUTH2_PROVIDER: OAUTH2_PROVIDER_OPTIONS,
...options
} = allOptions;

const systemOptions = {
...options,
ACCESS_TOKEN_EXPIRE_SECONDS: {
Expand All @@ -80,7 +86,6 @@ function MiscSystemDetail({ i18n }) {
label: i18n._(t`Authorization Code Expiration`),
},
};

const mergedData = {};
Object.keys(systemData).forEach(key => {
mergedData[key] = systemOptions[key];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
waitForElement,
} from '../../../../../testUtils/enzymeHelpers';
import { SettingsProvider } from '../../../../contexts/Settings';
import { SettingsAPI } from '../../../../api';
import { SettingsAPI, ExecutionEnvironmentsAPI } from '../../../../api';
import {
assertDetail,
assertVariableDetail,
Expand All @@ -14,13 +14,14 @@ import mockAllOptions from '../../shared/data.allSettingOptions.json';
import MiscSystemDetail from './MiscSystemDetail';

jest.mock('../../../../api/models/Settings');
jest.mock('../../../../api/models/ExecutionEnvironments');

SettingsAPI.readCategory.mockResolvedValue({
data: {
ALLOW_OAUTH2_FOR_EXTERNAL_USERS: false,
AUTH_BASIC_ENABLED: true,
AUTOMATION_ANALYTICS_GATHER_INTERVAL: 14400,
AUTOMATION_ANALYTICS_URL: 'https://example.com',
CUSTOM_VENV_PATHS: [],
INSIGHTS_TRACKING_STATE: false,
LOGIN_REDIRECT_OVERRIDE: 'https://redirect.com',
MANAGE_ORGANIZATION_AUTH: true,
Expand All @@ -36,6 +37,16 @@ SettingsAPI.readCategory.mockResolvedValue({
SESSIONS_PER_USER: -1,
SESSION_COOKIE_AGE: 30000000000,
TOWER_URL_BASE: 'https://towerhost',
DEFAULT_EXECUTION_ENVIRONMENT: 1,
},
});

ExecutionEnvironmentsAPI.readDetail.mockResolvedValue({
data: {
id: 1,
name: 'Foo',
image: 'quay.io/ansible/awx-ee',
pull: 'missing',
},
});

Expand Down Expand Up @@ -110,6 +121,71 @@ describe('<MiscSystemDetail />', () => {
assertDetail(wrapper, 'Red Hat customer username', 'mock name');
assertDetail(wrapper, 'Refresh Token Expiration', '3 seconds');
assertVariableDetail(wrapper, 'Remote Host Headers', '[]');
assertDetail(wrapper, 'Global default execution environment', 'Foo');
});

test('should render execution environment as not configured', async () => {
ExecutionEnvironmentsAPI.readDetail.mockResolvedValue({
data: {},
});
const newWrapper = mountWithContexts(
<SettingsProvider
value={{
...mockAllOptions.actions,
DEFAULT_EXECUTION_ENVIRONMENT: null,
}}
>
<MiscSystemDetail />
</SettingsProvider>
);
await waitForElement(newWrapper, 'ContentLoading', el => el.length === 0);

assertDetail(newWrapper, 'Access Token Expiration', '1 seconds');
assertDetail(newWrapper, 'All Users Visible to Organization Admins', 'On');
assertDetail(
newWrapper,
'Allow External Users to Create OAuth2 Tokens',
'Off'
);
assertDetail(newWrapper, 'Authorization Code Expiration', '2 seconds');
assertDetail(
newWrapper,
'Automation Analytics Gather Interval',
'14400 seconds'
);
assertDetail(
newWrapper,
'Automation Analytics upload URL',
'https://example.com'
);
assertDetail(newWrapper, 'Base URL of the Tower host', 'https://towerhost');
assertDetail(newWrapper, 'Enable HTTP Basic Auth', 'On');
assertDetail(newWrapper, 'Gather data for Automation Analytics', 'Off');
assertDetail(newWrapper, 'Idle Time Force Log Out', '30000000000 seconds');
assertDetail(
newWrapper,
'Login redirect override URL',
'https://redirect.com'
);
assertDetail(
newWrapper,
'Maximum number of simultaneous logged in sessions',
'-1'
);
assertDetail(
newWrapper,
'Organization Admins Can Manage Users and Teams',
'On'
);
assertDetail(newWrapper, 'Red Hat customer password', 'Encrypted');
assertDetail(newWrapper, 'Red Hat customer username', 'mock name');
assertDetail(newWrapper, 'Refresh Token Expiration', '3 seconds');
assertVariableDetail(newWrapper, 'Remote Host Headers', '[]');
assertDetail(
newWrapper,
'Global default execution environment',
'Not configured'
);
});

test('should hide edit button from non-superusers', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, { useCallback, useEffect } from 'react';
import { useHistory } from 'react-router-dom';
import { withI18n } from '@lingui/react';
import { t } from '@lingui/macro';
import { Formik } from 'formik';
import { Formik, useField } from 'formik';
import { Form } from '@patternfly/react-core';
import { CardBody } from '../../../../components/Card';
import ContentError from '../../../../components/ContentError';
Expand All @@ -20,8 +20,31 @@ import {
} from '../../shared';
import useModal from '../../../../util/useModal';
import useRequest from '../../../../util/useRequest';
import { SettingsAPI } from '../../../../api';
import { SettingsAPI, ExecutionEnvironmentsAPI } from '../../../../api';
import { pluck, formatJson } from '../../shared/settingUtils';
import { ExecutionEnvironmentLookup } from '../../../../components/Lookup';

const ExecutionEnvironmentLookupField = () => {
const [
executionEnvironmentField,
executionEnvironmentMeta,
executionEnvironmentHelpers,
] = useField({
name: 'DEFAULT_EXECUTION_ENVIRONMENT',
});

return (
<ExecutionEnvironmentLookup
helperTextInvalid={executionEnvironmentMeta.error}
isValid={
!executionEnvironmentMeta.touched || !executionEnvironmentMeta.error
}
onBlur={() => executionEnvironmentHelpers.setTouched()}
value={executionEnvironmentField.value}
onChange={value => executionEnvironmentHelpers.setValue(value)}
/>
);
};

function MiscSystemEdit({ i18n }) {
const history = useHistory();
Expand All @@ -44,7 +67,6 @@ function MiscSystemEdit({ i18n }) {
'AUTH_BASIC_ENABLED',
'AUTOMATION_ANALYTICS_GATHER_INTERVAL',
'AUTOMATION_ANALYTICS_URL',
'CUSTOM_VENV_PATHS',
'INSIGHTS_TRACKING_STATE',
'LOGIN_REDIRECT_OVERRIDE',
'MANAGE_ORGANIZATION_AUTH',
Expand All @@ -55,7 +77,8 @@ function MiscSystemEdit({ i18n }) {
'REMOTE_HOST_HEADERS',
'SESSIONS_PER_USER',
'SESSION_COOKIE_AGE',
'TOWER_URL_BASE'
'TOWER_URL_BASE',
'DEFAULT_EXECUTION_ENVIRONMENT'
);

const systemData = {
Expand Down Expand Up @@ -128,6 +151,7 @@ function MiscSystemEdit({ i18n }) {
AUTHORIZATION_CODE_EXPIRE_SECONDS,
...formData
} = form;

await submitForm({
...formData,
REMOTE_HOST_HEADERS: formatJson(formData.REMOTE_HOST_HEADERS),
Expand All @@ -136,6 +160,8 @@ function MiscSystemEdit({ i18n }) {
REFRESH_TOKEN_EXPIRE_SECONDS,
AUTHORIZATION_CODE_EXPIRE_SECONDS,
},
DEFAULT_EXECUTION_ENVIRONMENT:
formData.DEFAULT_EXECUTION_ENVIRONMENT?.id || null,
});
};

Expand Down Expand Up @@ -178,16 +204,51 @@ function MiscSystemEdit({ i18n }) {
return acc;
}, {});

const executionEnvironmentId =
system?.DEFAULT_EXECUTION_ENVIRONMENT?.value || null;

const {
isLoading: isLoadingExecutionEnvironment,
error: errorExecutionEnvironment,
request: fetchExecutionEnvironment,
result: executionEnvironment,
} = useRequest(
useCallback(async () => {
if (!executionEnvironmentId) {
return '';
}
const { data } = await ExecutionEnvironmentsAPI.readDetail(
executionEnvironmentId
);
return data;
}, [executionEnvironmentId])
);

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

return (
<CardBody>
{isLoading && <ContentLoading />}
{!isLoading && error && <ContentError error={error} />}
{!isLoading && system && (
<Formik initialValues={initialValues(system)} onSubmit={handleSubmit}>
{(isLoading || isLoadingExecutionEnvironment) && <ContentLoading />}
{!(isLoading || isLoadingExecutionEnvironment) && error && (
<ContentError error={error || errorExecutionEnvironment} />
)}
{!(isLoading || isLoadingExecutionEnvironment) && system && (
<Formik
initialValues={{
...initialValues(system),
DEFAULT_EXECUTION_ENVIRONMENT: executionEnvironment
? { id: executionEnvironment.id, name: executionEnvironment.name }
: null,
}}
onSubmit={handleSubmit}
>
{formik => {
return (
<Form autoComplete="off" onSubmit={formik.handleSubmit}>
<FormColumnLayout>
<ExecutionEnvironmentLookupField />
<InputField
name="TOWER_URL_BASE"
config={system.TOWER_URL_BASE}
Expand Down
Loading

0 comments on commit d4841bb

Please sign in to comment.