-
Notifications
You must be signed in to change notification settings - Fork 593
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add credential management section in start pipeline modal. #4955
Merged
openshift-merge-robot
merged 2 commits into
openshift:master
from
karthikjeeyar:vikramraj-4868
Apr 16, 2020
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
15 changes: 15 additions & 0 deletions
15
...nd/packages/dev-console/src/components/pipelines/pipeline-form/PipelineSecretSection.scss
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,15 @@ | ||
.odc-pipeline-secret-section { | ||
margin-top: var(--pf-global--spacer--sm); | ||
padding-left: var(--pf-global--spacer--lg); | ||
&__secret-form { | ||
border: 1px dashed var(--pf-global--BorderColor--100); | ||
padding: var(--pf-global--spacer--md); | ||
} | ||
&__secret-action { | ||
margin-top: var(--pf-global--spacer--sm); | ||
padding-left: 0; | ||
} | ||
} | ||
.odc-pipeline-expandable-content { | ||
margin-top: var(--pf-global--spacer--sm); | ||
} |
101 changes: 101 additions & 0 deletions
101
...end/packages/dev-console/src/components/pipelines/pipeline-form/PipelineSecretSection.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,101 @@ | ||
import * as React from 'react'; | ||
import { Formik, useField, useFormikContext, FormikValues } from 'formik'; | ||
import { PlusCircleIcon } from '@patternfly/react-icons'; | ||
import { Button } from '@patternfly/react-core'; | ||
import { ExpandCollapse } from '@console/internal/components/utils'; | ||
import { SecretType } from '@console/internal/components/secrets/create-secret'; | ||
import { SecretModel } from '@console/internal/models'; | ||
import { k8sCreate } from '@console/internal/module/k8s'; | ||
import SecretForm from './SecretForm'; | ||
import { | ||
associateServiceAccountToSecret, | ||
getSecretAnnotations, | ||
} from '../../../utils/pipeline-utils'; | ||
import SecretsList from './SecretsList'; | ||
import { advancedSectionValidationSchema } from './pipelineForm-validation-utils'; | ||
import { SecretAnnotationId } from '../const'; | ||
|
||
import './PipelineSecretSection.scss'; | ||
|
||
const initialValues = { | ||
secretName: '', | ||
annotations: { key: SecretAnnotationId.Image, value: '' }, | ||
type: SecretType.dockerconfigjson, | ||
formData: {}, | ||
}; | ||
|
||
type PipelineSecretSectionProps = { | ||
namespace: string; | ||
}; | ||
|
||
const PipelineSecretSection: React.FC<PipelineSecretSectionProps> = ({ namespace }) => { | ||
const [secretOpenField] = useField<boolean>('secretOpen'); | ||
const { setFieldValue } = useFormikContext<FormikValues>(); | ||
|
||
const handleSubmit = (values, actions) => { | ||
actions.setSubmitting(true); | ||
const newSecret = { | ||
apiVersion: SecretModel.apiVersion, | ||
kind: SecretModel.kind, | ||
metadata: { | ||
name: values.secretName, | ||
namespace, | ||
annotations: getSecretAnnotations(values.annotations), | ||
}, | ||
type: values.type, | ||
stringData: values.formData, | ||
}; | ||
k8sCreate(SecretModel, newSecret) | ||
.then((resp) => { | ||
actions.setSubmitting(false); | ||
setFieldValue(secretOpenField.name, false); | ||
associateServiceAccountToSecret(resp, namespace); | ||
}) | ||
.catch((err) => { | ||
actions.setSubmitting(false); | ||
setFieldValue(secretOpenField.name, false); | ||
actions.setStatus({ submitError: err.message }); | ||
}); | ||
}; | ||
|
||
const handleReset = (values, actions) => { | ||
actions.resetForm({ values: initialValues, status: {} }); | ||
setFieldValue(secretOpenField.name, false); | ||
}; | ||
|
||
return ( | ||
<ExpandCollapse textExpanded="Hide Credential Options" textCollapsed="Show Credential Options"> | ||
<div className="odc-pipeline-secret-section"> | ||
<p>The following secrets are available for all pipelines in this namespace:</p> | ||
<div className="odc-pipeline-secret-section__secrets"> | ||
<SecretsList namespace={namespace} /> | ||
{secretOpenField.value ? ( | ||
<div className="odc-pipeline-secret-section__secret-form"> | ||
<Formik | ||
initialValues={initialValues} | ||
validationSchema={advancedSectionValidationSchema} | ||
onSubmit={handleSubmit} | ||
onReset={handleReset} | ||
> | ||
{(props) => <SecretForm {...props} />} | ||
</Formik> | ||
</div> | ||
) : ( | ||
<Button | ||
variant="link" | ||
onClick={() => { | ||
setFieldValue(secretOpenField.name, true); | ||
}} | ||
className="odc-pipeline-secret-section__secret-action" | ||
icon={<PlusCircleIcon />} | ||
> | ||
Add Secret | ||
</Button> | ||
)} | ||
</div> | ||
</div> | ||
</ExpandCollapse> | ||
); | ||
}; | ||
|
||
export default PipelineSecretSection; |
38 changes: 38 additions & 0 deletions
38
frontend/packages/dev-console/src/components/pipelines/pipeline-form/SecretAnnotation.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 { TextInputTypes } from '@patternfly/react-core'; | ||
import { InputField, DropdownField } from '@console/shared'; | ||
import { SecretAnnotationType } from '../const'; | ||
|
||
type SecretAnnotationParam = { | ||
fieldName: string; | ||
isReadOnly?: boolean; | ||
}; | ||
|
||
const SecretAnnotation: React.FC<SecretAnnotationParam> = (props) => { | ||
const { fieldName, isReadOnly = false } = props; | ||
return ( | ||
<div className="row"> | ||
<div className="col-lg-6"> | ||
<DropdownField | ||
name={`${fieldName}.key`} | ||
items={SecretAnnotationType} | ||
label="Access to" | ||
disabled={isReadOnly} | ||
fullWidth | ||
required | ||
/> | ||
</div> | ||
<div className="col-lg-6"> | ||
<InputField | ||
name={`${fieldName}.value`} | ||
type={TextInputTypes.text} | ||
isReadOnly={isReadOnly} | ||
label="Server URL" | ||
required | ||
/> | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default SecretAnnotation; |
8 changes: 8 additions & 0 deletions
8
frontend/packages/dev-console/src/components/pipelines/pipeline-form/SecretForm.scss
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,8 @@ | ||
.odc-secret-form { | ||
&__title { | ||
margin-top: 0; | ||
} | ||
&__help-text { | ||
margin: var(--pf-global--spacer--md) 0; | ||
} | ||
} |
136 changes: 136 additions & 0 deletions
136
frontend/packages/dev-console/src/components/pipelines/pipeline-form/SecretForm.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,136 @@ | ||
import * as React from 'react'; | ||
import * as _ from 'lodash'; | ||
import { FormikValues, useFormikContext } from 'formik'; | ||
import { ActionGroup, Button, ButtonVariant, TextInputTypes } from '@patternfly/react-core'; | ||
import { CheckIcon, CloseIcon } from '@patternfly/react-icons'; | ||
import { ButtonBar } from '@console/internal/components/utils'; | ||
import { | ||
SecretType, | ||
BasicAuthSubform, | ||
SSHAuthSubform, | ||
CreateConfigSubform, | ||
} from '@console/internal/components/secrets/create-secret'; | ||
import { DropdownField, InputField } from '@console/shared'; | ||
import SecretAnnotation from './SecretAnnotation'; | ||
import './SecretForm.scss'; | ||
|
||
const authTypes = { | ||
[SecretType.dockerconfigjson]: 'Image Registry Credentials', | ||
[SecretType.basicAuth]: 'Basic Authentication', | ||
[SecretType.sshAuth]: 'SSH Key', | ||
}; | ||
|
||
const renderSecretForm = ( | ||
type: SecretType, | ||
stringData: { | ||
[key: string]: any; | ||
}, | ||
onDataChanged: (value: string) => void, | ||
) => { | ||
switch (type) { | ||
case SecretType.basicAuth: | ||
return ( | ||
<BasicAuthSubform onChange={onDataChanged} stringData={stringData[SecretType.basicAuth]} /> | ||
); | ||
case SecretType.sshAuth: | ||
return ( | ||
<SSHAuthSubform onChange={onDataChanged} stringData={stringData[SecretType.sshAuth]} /> | ||
); | ||
case SecretType.dockerconfigjson: | ||
return ( | ||
<CreateConfigSubform | ||
onChange={onDataChanged} | ||
stringData={stringData[SecretType.dockerconfigjson]} | ||
/> | ||
); | ||
default: | ||
return null; | ||
} | ||
}; | ||
|
||
const SecretForm: React.FC<FormikValues> = ({ | ||
handleSubmit, | ||
handleReset, | ||
status, | ||
isSubmitting, | ||
}) => { | ||
const { values, setFieldValue } = useFormikContext<FormikValues>(); | ||
const [stringData, setStringData] = React.useState({ | ||
[SecretType.basicAuth]: {}, | ||
[SecretType.sshAuth]: {}, | ||
[SecretType.dockerconfigjson]: {}, | ||
}); | ||
|
||
const setValues = (type: SecretType) => { | ||
if (type === SecretType.dockerconfigjson) { | ||
setFieldValue( | ||
'formData', | ||
_.mapValues({ '.dockerconfigjson': stringData[type] }, JSON.stringify), | ||
); | ||
} else { | ||
setFieldValue('formData', stringData[type]); | ||
} | ||
}; | ||
|
||
const onDataChanged = (value: string) => { | ||
setStringData(_.merge(stringData, { [values.type]: value })); | ||
setValues(values.type); | ||
}; | ||
|
||
return ( | ||
<div className="odc-secret-form"> | ||
<h1 className="odc-secret-form__title">Create Secret</h1> | ||
<p className="odc-secret-form__help-text help-block"> | ||
Source secrets let you authenticate against a Git server. | ||
</p> | ||
<div className="form-group"> | ||
<InputField | ||
type={TextInputTypes.text} | ||
required | ||
name="secretName" | ||
label="Secret Name" | ||
helpText="Unique name of the new secret." | ||
/> | ||
</div> | ||
<div className="form-group"> | ||
<SecretAnnotation fieldName="annotations" /> | ||
</div> | ||
<div className="form-group"> | ||
<DropdownField | ||
name="type" | ||
label="Authentication Type" | ||
items={authTypes} | ||
title={authTypes[values.type]} | ||
onChange={(type: SecretType) => setValues(type)} | ||
fullWidth | ||
required | ||
/> | ||
</div> | ||
{renderSecretForm(values.type, stringData, onDataChanged)} | ||
<ButtonBar errorMessage={status && status.submitError} inProgress={isSubmitting}> | ||
<ActionGroup className="pf-c-form pf-c-form__actions--right pf-c-form__group--no-top-margin"> | ||
<Button | ||
type="button" | ||
variant={ButtonVariant.link} | ||
onClick={handleSubmit} | ||
className="odc-pipeline-resource-param__action-btn" | ||
aria-label="create" | ||
> | ||
<CheckIcon /> | ||
</Button> | ||
<Button | ||
type="button" | ||
className="odc-pipeline-resource-param__action-btn" | ||
variant={ButtonVariant.plain} | ||
onClick={handleReset} | ||
aria-label="close" | ||
> | ||
<CloseIcon /> | ||
</Button> | ||
</ActionGroup> | ||
</ButtonBar> | ||
</div> | ||
); | ||
}; | ||
|
||
export default SecretForm; |
5 changes: 5 additions & 0 deletions
5
frontend/packages/dev-console/src/components/pipelines/pipeline-form/SecretsList.scss
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,5 @@ | ||
.odc-secrets-list { | ||
&__secretsItem { | ||
margin-top: var(--pf-global--spacer--sm); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Definitely see an issue with form-inside-form concept that Vikram did. I told him the same on his PR. I have since refactored PipelineResources to bubble to the surface of the modal... so we may need to do that here as well... but let me continue reviewing. This may become a tech debt item; doubtful we'll do it part of this PR either way.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sub task to track : https://issues.redhat.com/browse/ODC-3549
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Personally, I don't see an issue with
Formik
insideFormik
pattern (not form-inside-form) in these cases. There are several use cases where people have used this pattern and its beneficial when you want to have a separation of concerns between the forms. Parent form doesn't care about all the details of creation of a secret here, it just needs a reference to the created secret. So, the inner secrets form creates all the necessary resources and gives back the reference. We use similar pattern in the Add flows where there is a create secret modal that does the exact same thing.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rohitkrai03 Technically no, I agree. There are use cases for this. However, this isn't "sub form submit => one property in parent form" - which if it was, I think we'd have a use-case to enjoy this.
We may not need to kill Formik usage itself, but the concept of creating a resource in the middle of a form... especially one in a modal... leads the user to manipulating resources in a context they can "cancel" -- which naturally won't delete the resources.
Personally, I think we can roll it all up to the parent form... but I can see more use-cases, now that I think of it, of having intermediate sub-forms that don't do any "actions" but instead their form submit is just a field update to their parent form.