-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
220 additions
and
2 deletions.
There are no files selected for viewing
218 changes: 218 additions & 0 deletions
218
..._ui/public/application/components/builtin_action_types/resilient/resilient_connectors.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,218 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
import React, { useCallback } from 'react'; | ||
|
||
import { | ||
EuiFieldText, | ||
EuiFlexGroup, | ||
EuiFlexItem, | ||
EuiFormRow, | ||
EuiFieldPassword, | ||
EuiSpacer, | ||
} from '@elastic/eui'; | ||
|
||
import { isEmpty } from 'lodash'; | ||
import { ActionConnectorFieldsProps } from '../../../../types'; | ||
import * as i18n from './translations'; | ||
import { ResilientActionConnector, CasesConfigurationMapping } from './types'; | ||
import { connectorConfiguration } from './config'; | ||
import { FieldMapping } from './case_mappings/field_mapping'; | ||
|
||
const ResilientConnectorFields: React.FC<ActionConnectorFieldsProps<ResilientActionConnector>> = ({ | ||
action, | ||
editActionSecrets, | ||
editActionConfig, | ||
errors, | ||
consumer, | ||
readOnly, | ||
docLinks, | ||
}) => { | ||
// TODO: remove incidentConfiguration later, when Case ServiceNow will move their fields to the level of action execution | ||
const { apiUrl, orgId, incidentConfiguration, isCaseOwned } = action.config; | ||
const mapping = incidentConfiguration ? incidentConfiguration.mapping : []; | ||
|
||
const isApiUrlInvalid: boolean = errors.apiUrl.length > 0 && apiUrl != null; | ||
|
||
const { apiKeyId, apiKeySecret } = action.secrets; | ||
|
||
const isOrgIdInvalid: boolean = errors.orgId.length > 0 && orgId != null; | ||
const isApiKeyInvalid: boolean = errors.apiKeyId.length > 0 && apiKeyId != null; | ||
const isApiKeySecretInvalid: boolean = errors.apiKeySecret.length > 0 && apiKeySecret != null; | ||
|
||
// TODO: remove this block later, when Case ServiceNow will move their fields to the level of action execution | ||
if (consumer === 'case') { | ||
if (isEmpty(mapping)) { | ||
editActionConfig('incidentConfiguration', { | ||
mapping: createDefaultMapping(connectorConfiguration.fields as any), | ||
}); | ||
} | ||
if (!isCaseOwned) { | ||
editActionConfig('isCaseOwned', true); | ||
} | ||
} | ||
|
||
const handleOnChangeActionConfig = useCallback( | ||
(key: string, value: string) => editActionConfig(key, value), | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
[] | ||
); | ||
|
||
const handleOnChangeSecretConfig = useCallback( | ||
(key: string, value: string) => editActionSecrets(key, value), | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
[] | ||
); | ||
|
||
const handleOnChangeMappingConfig = useCallback( | ||
(newMapping: CasesConfigurationMapping[]) => | ||
editActionConfig('incidentConfiguration', { | ||
...action.config.incidentConfiguration, | ||
mapping: newMapping, | ||
}), | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
[action.config] | ||
); | ||
|
||
return ( | ||
<> | ||
<EuiFlexGroup> | ||
<EuiFlexItem> | ||
<EuiFormRow | ||
id="apiUrl" | ||
fullWidth | ||
error={errors.apiUrl} | ||
isInvalid={isApiUrlInvalid} | ||
label={i18n.API_URL_LABEL} | ||
> | ||
<EuiFieldText | ||
fullWidth | ||
isInvalid={isApiUrlInvalid} | ||
name="apiUrl" | ||
readOnly={readOnly} | ||
value={apiUrl || ''} // Needed to prevent uncontrolled input error when value is undefined | ||
data-test-subj="apiUrlFromInput" | ||
placeholder="https://<site-url>" | ||
onChange={(evt) => handleOnChangeActionConfig('apiUrl', evt.target.value)} | ||
onBlur={() => { | ||
if (!apiUrl) { | ||
editActionConfig('apiUrl', ''); | ||
} | ||
}} | ||
/> | ||
</EuiFormRow> | ||
</EuiFlexItem> | ||
</EuiFlexGroup> | ||
<EuiSpacer size="m" /> | ||
<EuiFlexGroup> | ||
<EuiFlexItem> | ||
<EuiFormRow | ||
id="connector-resilient-orgId-key" | ||
fullWidth | ||
error={errors.orgId} | ||
isInvalid={isOrgIdInvalid} | ||
label={i18n.ORG_ID_LABEL} | ||
> | ||
<EuiFieldText | ||
fullWidth | ||
isInvalid={isOrgIdInvalid} | ||
name="connector-resilient-org-id" | ||
value={orgId || ''} // Needed to prevent uncontrolled input error when value is undefined | ||
data-test-subj="connector-resilient-org-id-form-input" | ||
onChange={(evt) => handleOnChangeActionConfig('orgId ', evt.target.value)} | ||
onBlur={() => { | ||
if (!orgId) { | ||
editActionConfig('orgId ', ''); | ||
} | ||
}} | ||
/> | ||
</EuiFormRow> | ||
</EuiFlexItem> | ||
</EuiFlexGroup> | ||
<EuiSpacer size="m" /> | ||
<EuiFlexGroup> | ||
<EuiFlexItem> | ||
<EuiFormRow | ||
id="connector-resilient-apiKeyId" | ||
fullWidth | ||
error={errors.apiKeyId} | ||
isInvalid={isApiKeyInvalid} | ||
label={i18n.API_KEY_ID_LABEL} | ||
> | ||
<EuiFieldText | ||
fullWidth | ||
isInvalid={isApiKeyInvalid} | ||
readOnly={readOnly} | ||
name="connector-resilient-apiKeyId" | ||
value={apiKeyId || ''} // Needed to prevent uncontrolled input error when value is undefined | ||
data-test-subj="connector-resilient-apiKeyId-form-input" | ||
onChange={(evt) => handleOnChangeSecretConfig('apiKeyId', evt.target.value)} | ||
onBlur={() => { | ||
if (!apiKeyId) { | ||
editActionSecrets('apiKeyId', ''); | ||
} | ||
}} | ||
/> | ||
</EuiFormRow> | ||
</EuiFlexItem> | ||
</EuiFlexGroup> | ||
<EuiSpacer size="m" /> | ||
<EuiFlexGroup> | ||
<EuiFlexItem> | ||
<EuiFormRow | ||
id="connector-resilient-apiKeySecret" | ||
fullWidth | ||
error={errors.apiKeySecret} | ||
isInvalid={isApiKeySecretInvalid} | ||
label={i18n.API_KEY_SECRET_LABEL} | ||
> | ||
<EuiFieldPassword | ||
fullWidth | ||
readOnly={readOnly} | ||
isInvalid={isApiKeySecretInvalid} | ||
name="connector-resilient-apiKeySecret" | ||
value={apiKeySecret || ''} // Needed to prevent uncontrolled input error when value is undefined | ||
data-test-subj="connector-resilient-apiKeySecret-form-input" | ||
onChange={(evt) => handleOnChangeSecretConfig('apiKeySecret', evt.target.value)} | ||
onBlur={() => { | ||
if (!apiKeySecret) { | ||
editActionSecrets('apiKeySecret', ''); | ||
} | ||
}} | ||
/> | ||
</EuiFormRow> | ||
</EuiFlexItem> | ||
</EuiFlexGroup> | ||
{consumer === 'case' && ( // TODO: remove this block later, when Case ServiceNow will move their fields to the level of action execution | ||
<> | ||
<EuiSpacer size="l" /> | ||
<EuiFlexGroup> | ||
<EuiFlexItem data-test-subj="case-resilient-mappings"> | ||
<FieldMapping | ||
disabled={true} | ||
connectorActionTypeId={connectorConfiguration.id} | ||
mapping={mapping as CasesConfigurationMapping[]} | ||
onChangeMapping={handleOnChangeMappingConfig} | ||
/> | ||
</EuiFlexItem> | ||
</EuiFlexGroup> | ||
</> | ||
)} | ||
</> | ||
); | ||
}; | ||
|
||
export const createDefaultMapping = (fields: Record<string, any>): CasesConfigurationMapping[] => | ||
Object.keys(fields).map( | ||
(key) => | ||
({ | ||
source: fields[key].defaultSourceField, | ||
target: key, | ||
actionType: fields[key].defaultActionType, | ||
} as CasesConfigurationMapping) | ||
); | ||
|
||
// eslint-disable-next-line import/no-default-export | ||
export { ResilientConnectorFields as default }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters