-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add event source mappings in Rulebook Activation form (#2907)
- Loading branch information
Showing
6 changed files
with
388 additions
and
33 deletions.
There are no files selected for viewing
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,9 @@ | ||
import { Source } from './generated/eda-api'; | ||
export type EdaSource = Source; | ||
|
||
export type EdaSourceEventMapping = { | ||
source_name: string; | ||
webhook_id: string; | ||
webhook_name: string; | ||
rulebook_hash: string; | ||
}; |
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
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
108 changes: 108 additions & 0 deletions
108
frontend/eda/rulebook-activations/components/SourceEventMapFields.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,108 @@ | ||
import { Button, FormFieldGroupExpandable, FormFieldGroupHeader } from '@patternfly/react-core'; | ||
import { TrashIcon } from '@patternfly/react-icons'; | ||
import { useTranslation } from 'react-i18next'; | ||
import { useFormContext, useWatch } from 'react-hook-form'; | ||
import { PageFormTextArea } from '../../../../framework'; | ||
import { EdaSource, EdaSourceEventMapping } from '../../interfaces/EdaSource'; | ||
import { edaAPI } from '../../common/eda-utils'; | ||
import { EdaResult } from '../../interfaces/EdaResult'; | ||
import { EdaRulebook } from '../../interfaces/EdaRulebook'; | ||
import { useGet } from '../../../common/crud/useGet'; | ||
import { EdaWebhook } from '../../interfaces/EdaWebhook'; | ||
import { PageFormSingleSelect } from '../../../../framework/PageForm/Inputs/PageFormSingleSelect'; | ||
|
||
export function SourceEventMapFields(props: { | ||
index: number; | ||
rulebook: EdaRulebook; | ||
source_mappings: EdaSourceEventMapping; | ||
onDelete: (id: number) => void; | ||
}) { | ||
const { t } = useTranslation(); | ||
const { index, onDelete } = props; | ||
const { register, setValue } = useFormContext(); | ||
|
||
const { data: sources } = useGet<EdaResult<EdaSource>>( | ||
edaAPI`/rulebooks/` + `${props?.rulebook?.id}/sources/?page=1&page_size=200` | ||
); | ||
const { data: events } = useGet<EdaResult<EdaWebhook>>(edaAPI`/webhooks/?page=1&page_size=200`); | ||
|
||
const selectedSource = useWatch({ name: `mappings.${index}.source_name` }) as string; | ||
let srcIndex = -1; | ||
if (sources?.results) { | ||
srcIndex = sources.results.findIndex((source) => source.name === selectedSource); | ||
|
||
if (srcIndex > -1) { | ||
setValue(`${index}.source_info`, sources.results[srcIndex].source_info); | ||
setValue(`mappings.${index}.rulebook_hash`, sources.results[srcIndex].rulebook_hash); | ||
} | ||
} | ||
const selectedEvent = useWatch({ name: `mappings.${index}.webhook_id` }) as number; | ||
let evIndex = -1; | ||
if (events?.results) { | ||
evIndex = events.results.findIndex((event) => event.id === selectedEvent); | ||
|
||
if (evIndex > -1) { | ||
setValue(`mappings.${index}.webhook_name`, events.results[evIndex].name); | ||
} | ||
} | ||
|
||
return ( | ||
<FormFieldGroupExpandable | ||
isExpanded | ||
header={ | ||
<FormFieldGroupHeader | ||
titleText={{ text: t('Mapping ') + `${index + 1}`, id: `Mapping ${index}` }} | ||
data-cy={'mapping-header-' + `${index}`} | ||
actions={ | ||
<Button | ||
id={`map-delete-${index}`} | ||
icon={<TrashIcon />} | ||
aria-label={t('Delete map')} | ||
onClick={() => onDelete(index)} | ||
variant="plain" | ||
/> | ||
} | ||
/> | ||
} | ||
> | ||
<PageFormSingleSelect | ||
name={`mappings.${index}.source_name`} | ||
label={t('Source')} | ||
placeholder={t('Select source')} | ||
isRequired | ||
labelHelp={t('Sources in the rulebook.')} | ||
labelHelpTitle={t('Sources')} | ||
options={ | ||
sources?.results | ||
? sources.results.map((item: { name: string }) => ({ | ||
label: item.name, | ||
value: item.name, | ||
})) | ||
: [] | ||
} | ||
/> | ||
<PageFormSingleSelect | ||
name={`mappings.${index}.webhook_id`} | ||
label={t('Event stream')} | ||
placeholder={t('Select event stream')} | ||
isRequired | ||
labelHelp={t('Event stream to swap with the source.')} | ||
labelHelpTitle={t('Event streams')} | ||
options={ | ||
events?.results | ||
? events.results.map((item: { name: string; id: number }) => ({ | ||
label: item.name, | ||
value: item.id, | ||
})) | ||
: [] | ||
} | ||
/> | ||
<PageFormTextArea | ||
name={`${index}.source_info`} | ||
label={t('Preview of source from rulebook')} | ||
isReadOnly | ||
/> | ||
<input type="hidden" {...register(`mappings.${index}.rulebook_hash`)} /> | ||
</FormFieldGroupExpandable> | ||
); | ||
} |
78 changes: 78 additions & 0 deletions
78
frontend/eda/rulebook-activations/components/SourceEventStreamMapping.cy.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,78 @@ | ||
import { edaAPI } from '../../common/eda-utils'; | ||
import { SourceEventStreamMappingModal } from './SourceEventStreamMapping'; | ||
|
||
describe('SourceEventStreamMapping.cy.ts', () => { | ||
beforeEach(() => { | ||
cy.intercept( | ||
{ method: 'GET', url: edaAPI`/rulebooks/1/sources/?page=1&page_size=200` }, | ||
{ | ||
count: 3, | ||
next: null, | ||
previous: null, | ||
page_size: 10, | ||
page: 1, | ||
results: [ | ||
{ | ||
name: '__SOURCE_1', | ||
source_info: 'ansible.eda.sample1:\n sample1: 10\n', | ||
rulebook_hash: 'hash_1', | ||
}, | ||
{ | ||
name: '__SOURCE_2', | ||
source_info: 'ansible.eda.sample2:\n sample2: 10\n', | ||
rulebook_hash: 'hash_2', | ||
}, | ||
{ | ||
name: '__SOURCE_3', | ||
source_info: 'ansible.eda.sample3:\n sample3: 10\n', | ||
rulebook_hash: 'hash_3', | ||
}, | ||
], | ||
} | ||
); | ||
cy.intercept( | ||
{ method: 'GET', url: edaAPI`/webhooks/*` }, | ||
{ | ||
fixture: 'edaWebhooks.json', | ||
} | ||
); | ||
}); | ||
|
||
it('Renders the correct rulebook activations columns', () => { | ||
cy.mount( | ||
<SourceEventStreamMappingModal | ||
rulebook={{ | ||
id: 1, | ||
name: 'sample_rulebook.yml', | ||
description: '', | ||
rulesets: | ||
'---\n- name: Test run job templates\n hosts: all\n sources:\n - ansible.eda.range:\n limit: 10\n rules:\n - name: "1 job template"\n condition: event.i >= 0\n action:\n run_job_template:\n name: Demo Job Template\n organization: Default\n job_args:\n extra_vars:\n hello: Fred\n retries: 1\n delay: 10\n', | ||
project_id: 2, | ||
organization_id: 1, | ||
created_at: '2024-08-07T22:29:42.081976Z', | ||
modified_at: '2024-08-07T22:29:42.081983Z', | ||
}} | ||
mappings={undefined} | ||
setSourceMappings={() => undefined} | ||
/> | ||
); | ||
cy.get('.pf-v5-c-modal-box__title-text').should('contain', 'Event streams'); | ||
cy.contains( | ||
'Event streams represent server side webhooks which ease the routing issues related to running webhooks individually in a container or a pod. ' + | ||
'You can swap the sources in your rulebook with a matching event stream. Typically the sources to swap out are of the type ansible.eda.rulebook, ' + | ||
'but you may also be able to swap out your own webhook source plugins. The swapping process replaces just the source type and args and leaves your ' + | ||
'filters intact. We swap out the webhook source type with a source of type ansible.eda.pg_listener.' | ||
).should('be.visible'); | ||
cy.get('[data-cy="rulebook"]').should('contain.text', 'sample_rulebook.yml'); | ||
cy.get('[data-cy="number-of-sources"]').should('contain.text', '3'); | ||
cy.get('[data-cy="add_event_stream"]').should('contain.text', 'Add event stream'); | ||
cy.clickButton(/^Add event stream$/); | ||
cy.get('[data-cy="mapping-header-0"]').should('contain.text', 'Mapping 1'); | ||
cy.get('[data-cy="mappings-0-source-name"]').click(); | ||
cy.get('#--source-1 > .pf-v5-c-menu__item-main > .pf-v5-c-menu__item-text').click(); | ||
cy.get('[data-cy="0-source-info"]').should( | ||
'contain.text', | ||
'ansible.eda.sample1:\n sample1: 10\n' | ||
); | ||
}); | ||
}); |
Oops, something went wrong.