Skip to content

Commit

Permalink
Add validation warning for user data
Browse files Browse the repository at this point in the history
Co-authored-by: Hussain Khalil <hussain@sanpilot.co>
  • Loading branch information
hkhalil-akamai and SanPilot committed Feb 23, 2023
1 parent 60ebae0 commit 04c2b0a
Showing 1 changed file with 36 additions and 1 deletion.
37 changes: 36 additions & 1 deletion packages/manager/src/features/linodes/UserDataAccordion.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import * as React from 'react';
import Accordion from 'src/components/Accordion';
import { makeStyles } from 'src/components/core/styles';
import Typography from 'src/components/core/Typography';
import ExternalLink from 'src/components/ExternalLink';
import HelpIcon from 'src/components/HelpIcon';
import Notice from 'src/components/Notice';
import TextField from 'src/components/TextField';

interface Props {
Expand All @@ -23,8 +25,26 @@ const useStyles = makeStyles(() => ({

const UserDataAccordion: React.FC<Props> = (props) => {
const { disabled, userData, onChange } = props;
const [formatWarning, setFormatWarning] = React.useState(false);

const classes = useStyles();

const checkFormat = (userData: string, isOnChange: boolean) => {
const userDataLower = userData.toLowerCase();
if (
userData.length > 0 &&
!userDataLower.startsWith('#cloud-config') &&
!userDataLower.startsWith('content-type: text/') &&
!userDataLower.startsWith('#!')
) {
if (!isOnChange) {
setFormatWarning(true);
}
} else {
setFormatWarning(false);
}
};

const accordionHeading = (
<>
Add User Data{' '}
Expand All @@ -47,11 +67,26 @@ const UserDataAccordion: React.FC<Props> = (props) => {
multiline
rows={1}
expand
onChange={(e) => onChange(e.target.value)}
onChange={(e) => {
checkFormat(e.target.value, true);
onChange(e.target.value);
}}
value={userData}
disabled={Boolean(disabled)}
onBlur={(e) => checkFormat(e.target.value, false)}
data-qa-user-data-input
/>
{formatWarning ? (
<Notice warning important>
This user data may not be in a format accepted by cloud-init. See{' '}
<ExternalLink
text="cloud-init documentation"
link="https://cloudinit.readthedocs.io/en/latest/explanation/format.html"
fixedIcon
/>{' '}
for more information.
</Notice>
) : null}
</Accordion>
);
};
Expand Down

0 comments on commit 04c2b0a

Please sign in to comment.