-
Notifications
You must be signed in to change notification settings - Fork 190
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Vincent T <vtaylor@microsoft.com>
- Loading branch information
Showing
7 changed files
with
217 additions
and
7 deletions.
There are no files selected for viewing
161 changes: 161 additions & 0 deletions
161
frontend/src/components/namespace/CreateNamespaceButton.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,161 @@ | ||
import { | ||
Box, | ||
Button, | ||
Dialog, | ||
DialogActions, | ||
DialogContent, | ||
DialogTitle, | ||
TextField, | ||
} from '@mui/material'; | ||
import React, { useState } from 'react'; | ||
import { useTranslation } from 'react-i18next'; | ||
import { useDispatch } from 'react-redux'; | ||
import { getCluster } from '../../lib/cluster'; | ||
import { post } from '../../lib/k8s/apiProxy'; | ||
import Namespace from '../../lib/k8s/namespace'; | ||
import { clusterAction } from '../../redux/clusterActionSlice'; | ||
import { EventStatus, HeadlampEventType, useEventCallback } from '../../redux/headlampEventSlice'; | ||
import { ActionButton, AuthVisible } from '../common'; | ||
|
||
export function CreateNamespaceButton() { | ||
const { t } = useTranslation(['glossary', 'translation']); | ||
const [namespaceName, setNamespaceName] = useState(''); | ||
const [namespaceDialogOpen, setNamespaceDialogOpen] = useState(false); | ||
const dispatchCreateEvent = useEventCallback(HeadlampEventType.CREATE_RESOURCE); | ||
const dispatch = useDispatch(); | ||
|
||
function createNewNamespace() { | ||
function validateNamespaceName(namespaceName: string) { | ||
const namespaceRegex = /^[a-z]([-a-z0-9]*[a-z0-9])?$/; | ||
|
||
// Check if the namespace name is too long | ||
if (namespaceName.length > 63) { | ||
alert(t('translation|Invalid namespace name: name must be 63 characters or shorter')); | ||
return false; | ||
} | ||
|
||
// Check if the namespace name starts with a lowercase letter | ||
const startsLowercase = /^[a-z]/.test(namespaceName); | ||
if (!startsLowercase) { | ||
alert(t('translation|Invalid namespace name: name must start with a lowercase letter')); | ||
return false; | ||
} | ||
|
||
// Check if the namespace name ends with an alphanumeric character | ||
const endsAlphanumeric = /[a-z0-9]$/.test(namespaceName); | ||
if (!endsAlphanumeric) { | ||
alert( | ||
t('translation|Invalid namespace name: name must end with an alphanumeric character') | ||
); | ||
return false; | ||
} | ||
|
||
// Check if the namespace name contains only lowercase letters, numbers, or hyphens | ||
const validChars = namespaceRegex.test(namespaceName); | ||
if (!validChars) { | ||
alert( | ||
t( | ||
'translation|Invalid namespace name: name must consist of lower case alphanumeric characters or -' | ||
) | ||
); | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
if (!validateNamespaceName(namespaceName)) { | ||
return; | ||
} | ||
|
||
const cluster = getCluster(); | ||
const newNamespaceData = { | ||
apiVersion: 'v1', | ||
kind: 'Namespace', | ||
metadata: { | ||
name: namespaceName, | ||
}, | ||
}; | ||
const newNamespaceName = newNamespaceData.metadata.name; | ||
|
||
async function namespaceRequest() { | ||
try { | ||
const response = await post('/api/v1/namespaces', newNamespaceData, true, { | ||
cluster: `${cluster}`, | ||
}); | ||
return response; | ||
} catch (error) { | ||
console.error('Error creating namespace', error); | ||
throw error; | ||
} | ||
} | ||
|
||
setNamespaceDialogOpen(false); | ||
dispatch( | ||
clusterAction(() => namespaceRequest(), { | ||
startMessage: t('translation|Creating namespace {{ name }}…', { name: newNamespaceName }), | ||
cancelledMessage: t('translation|Cancelled creation of {{ name }}.', { | ||
name: newNamespaceName, | ||
}), | ||
successMessage: t('translation|Created namespace {{ name }}.', { | ||
name: newNamespaceName, | ||
}), | ||
errorMessage: t('translation|Error creating namespace {{ name }}.', { | ||
name: newNamespaceName, | ||
}), | ||
cancelCallback: () => { | ||
setNamespaceDialogOpen(true); | ||
}, | ||
}) | ||
); | ||
} | ||
|
||
return ( | ||
<AuthVisible item={Namespace} authVerb="create"> | ||
<ActionButton | ||
color="primary" | ||
description={t('translation|Create')} | ||
icon={'mdi:plus-circle'} | ||
onClick={() => { | ||
setNamespaceDialogOpen(true); | ||
}} | ||
/> | ||
|
||
<Dialog open={namespaceDialogOpen} onClose={() => setNamespaceDialogOpen(false)}> | ||
<DialogTitle>{t('translation|Create Namespace')}</DialogTitle> | ||
<DialogContent> | ||
<Box component="form" style={{ width: '20vw', maxWidth: '20vw' }}> | ||
<TextField | ||
margin="dense" | ||
id="name" | ||
label={t('translation|Name')} | ||
type="text" | ||
fullWidth | ||
value={namespaceName} | ||
onChange={event => setNamespaceName(event.target.value)} | ||
/> | ||
</Box> | ||
</DialogContent> | ||
<DialogActions> | ||
<Button | ||
onClick={() => { | ||
setNamespaceDialogOpen(false); | ||
}} | ||
> | ||
{t('translation|Cancel')} | ||
</Button> | ||
<Button | ||
onClick={() => { | ||
createNewNamespace(); | ||
dispatchCreateEvent({ | ||
status: EventStatus.CONFIRMED, | ||
}); | ||
}} | ||
> | ||
{t('translation|Create')} | ||
</Button> | ||
</DialogActions> | ||
</Dialog> | ||
</AuthVisible> | ||
); | ||
} |
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
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