From 04c2b0ae4963be4e66e47ef008f41de8cbc0172d Mon Sep 17 00:00:00 2001 From: Hussain Khalil Date: Thu, 23 Feb 2023 16:02:02 -0500 Subject: [PATCH] Add validation warning for user data Co-authored-by: Hussain Khalil --- .../features/linodes/UserDataAccordion.tsx | 37 ++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/packages/manager/src/features/linodes/UserDataAccordion.tsx b/packages/manager/src/features/linodes/UserDataAccordion.tsx index 206863e80e7..fd61a819a73 100644 --- a/packages/manager/src/features/linodes/UserDataAccordion.tsx +++ b/packages/manager/src/features/linodes/UserDataAccordion.tsx @@ -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 { @@ -23,8 +25,26 @@ const useStyles = makeStyles(() => ({ const UserDataAccordion: React.FC = (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{' '} @@ -47,11 +67,26 @@ const UserDataAccordion: React.FC = (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 ? ( + + This user data may not be in a format accepted by cloud-init. See{' '} + {' '} + for more information. + + ) : null} ); };