Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4810 from invincibleJai/feat-apiserversource
adds support for apiServerSource
- Loading branch information
Showing
15 changed files
with
266 additions
and
54 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
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
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
98 changes: 98 additions & 0 deletions
98
frontend/packages/knative-plugin/src/components/add/event-sources/ApiServerSection.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,98 @@ | ||
import * as React from 'react'; | ||
import * as _ from 'lodash'; | ||
import * as fuzzy from 'fuzzysearch'; | ||
import { useFormikContext, FormikValues } from 'formik'; | ||
import { FormGroup } from '@patternfly/react-core'; | ||
import { ServiceAccountModel } from '@console/internal/models'; | ||
import { NameValueEditor } from '@console/internal/components/utils/name-value-editor'; | ||
import { ResourceDropdownField, DropdownField, getFieldId } from '@console/shared'; | ||
import FormSection from '@console/dev-console/src/components/import/section/FormSection'; | ||
|
||
interface ApiServerSectionProps { | ||
namespace: string; | ||
} | ||
|
||
const ApiServerSection: React.FC<ApiServerSectionProps> = ({ namespace }) => { | ||
const { values, setFieldValue, setFieldTouched, validateForm } = useFormikContext<FormikValues>(); | ||
const initVal = values?.data?.apiserversource?.resources || []; | ||
const initialValueResources = !_.isEmpty(initVal) | ||
? initVal.map((val) => _.values(val)) | ||
: [['', '']]; | ||
const [nameValue, setNameValue] = React.useState(initialValueResources); | ||
const autocompleteFilter = (strText, item): boolean => fuzzy(strText, item?.props?.name); | ||
const handleNameValuePairs = React.useCallback( | ||
({ nameValuePairs }) => { | ||
const updatedNameValuePairs = _.compact( | ||
nameValuePairs.map(([name, value]) => { | ||
if (value.length) { | ||
return { apiVersion: name, kind: value }; | ||
} | ||
return null; | ||
}), | ||
); | ||
setNameValue(nameValuePairs); | ||
setFieldValue('data.apiserversource.resources', updatedNameValuePairs); | ||
}, | ||
[setFieldValue], | ||
); | ||
const onChange = React.useCallback( | ||
(selectedValue) => { | ||
if (selectedValue) { | ||
setFieldTouched('data.apiserversource.serviceAccountName', true); | ||
setFieldValue('data.apiserversource.serviceAccountName', selectedValue); | ||
validateForm(); | ||
} | ||
}, | ||
[setFieldValue, setFieldTouched, validateForm], | ||
); | ||
const resources = [ | ||
{ | ||
isList: true, | ||
kind: ServiceAccountModel.kind, | ||
namespace, | ||
prop: ServiceAccountModel.id, | ||
optional: true, | ||
}, | ||
]; | ||
const modeItems = { | ||
Ref: 'Ref', | ||
Resource: 'Resource', | ||
}; | ||
const fieldId = getFieldId(values.type, 'res-input'); | ||
return ( | ||
<FormSection title="ApiServerSource"> | ||
<FormGroup fieldId={fieldId} label="Resource" isRequired> | ||
<NameValueEditor | ||
nameValuePairs={nameValue} | ||
valueString="kind" | ||
nameString="apiVersion" | ||
addString="Add Resource" | ||
readOnly={false} | ||
allowSorting={false} | ||
updateParentData={handleNameValuePairs} | ||
/> | ||
</FormGroup> | ||
<DropdownField | ||
name="data.apiserversource.mode" | ||
label="Mode" | ||
items={modeItems} | ||
title={modeItems.Ref} | ||
fullWidth | ||
/> | ||
<ResourceDropdownField | ||
name="data.apiserversource.serviceAccountName" | ||
label="Service Account Name" | ||
resources={resources} | ||
dataSelector={['metadata', 'name']} | ||
fullWidth | ||
placeholder="Select Service Account Name" | ||
onChange={onChange} | ||
autocompleteFilter={autocompleteFilter} | ||
autoSelect | ||
showBadge | ||
/> | ||
</FormSection> | ||
); | ||
}; | ||
|
||
export default ApiServerSection; |
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
38 changes: 38 additions & 0 deletions
38
...kages/knative-plugin/src/components/add/event-sources/__tests__/ApiServerSection.spec.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,38 @@ | ||
import * as React from 'react'; | ||
import { shallow } from 'enzyme'; | ||
import { ResourceDropdownField } from '@console/shared'; | ||
import { NameValueEditor } from '@console/internal/components/utils/name-value-editor'; | ||
import FormSection from '@console/dev-console/src/components/import/section/FormSection'; | ||
import ApiServerSection from '../ApiServerSection'; | ||
|
||
jest.mock('formik', () => ({ | ||
useField: jest.fn(() => [{}, {}]), | ||
useFormikContext: jest.fn(() => ({ | ||
setFieldValue: jest.fn(), | ||
setFieldTouched: jest.fn(), | ||
validateForm: jest.fn(), | ||
values: { | ||
type: 'ApiServerSource', | ||
}, | ||
})), | ||
getFieldId: jest.fn(), | ||
})); | ||
describe('ApiServerSection', () => { | ||
it('should render FormSection', () => { | ||
const wrapper = shallow(<ApiServerSection namespace="test-project" />); | ||
expect(wrapper.find(FormSection)).toHaveLength(1); | ||
expect(wrapper.find(FormSection).props().title).toBe('ApiServerSource'); | ||
}); | ||
|
||
it('should render NameValueEditor', () => { | ||
const wrapper = shallow(<ApiServerSection namespace="test-project" />); | ||
const nameValueEditorField = wrapper.find(NameValueEditor); | ||
expect(nameValueEditorField).toHaveLength(1); | ||
expect(nameValueEditorField.props().nameString).toBe('apiVersion'); | ||
expect(nameValueEditorField.props().valueString).toBe('kind'); | ||
}); | ||
it('should render ResourceDropdownField', () => { | ||
const wrapper = shallow(<ApiServerSection namespace="test-project" />); | ||
expect(wrapper.find(ResourceDropdownField)).toHaveLength(1); | ||
}); | ||
}); |
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
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
Oops, something went wrong.