-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(instances) add image creation from instance snapshot
Signed-off-by: David Edler <david.edler@canonical.com>
- Loading branch information
Showing
12 changed files
with
241 additions
and
23 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
49 changes: 49 additions & 0 deletions
49
src/pages/instances/actions/snapshots/CreateImageFromInstanceSnapshotBtn.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,49 @@ | ||
import { FC } from "react"; | ||
import usePortal from "react-useportal"; | ||
import { Button, Icon } from "@canonical/react-components"; | ||
import { LxdInstance, LxdInstanceSnapshot } from "types/instance"; | ||
import CreateImageFromInstanceSnapshotForm from "pages/instances/forms/CreateImageFromInstanceSnapshotForm"; | ||
|
||
interface Props { | ||
instance: LxdInstance; | ||
snapshot: LxdInstanceSnapshot; | ||
isDeleting: boolean; | ||
isRestoring: boolean; | ||
} | ||
|
||
const CreateImageFromInstanceSnapshotBtn: FC<Props> = ({ | ||
instance, | ||
snapshot, | ||
isDeleting, | ||
isRestoring, | ||
}) => { | ||
const { openPortal, closePortal, isOpen, Portal } = usePortal(); | ||
|
||
return ( | ||
<> | ||
{isOpen && ( | ||
<Portal> | ||
<CreateImageFromInstanceSnapshotForm | ||
close={closePortal} | ||
instance={instance} | ||
snapshot={snapshot} | ||
/> | ||
</Portal> | ||
)} | ||
<Button | ||
appearance="base" | ||
hasIcon | ||
dense={true} | ||
disabled={isDeleting || isRestoring} | ||
onClick={openPortal} | ||
type="button" | ||
aria-label="Create image" | ||
title="Create image" | ||
> | ||
<Icon name="export" /> | ||
</Button> | ||
</> | ||
); | ||
}; | ||
|
||
export default CreateImageFromInstanceSnapshotBtn; |
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
136 changes: 136 additions & 0 deletions
136
src/pages/instances/forms/CreateImageFromInstanceSnapshotForm.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 { FC } from "react"; | ||
import { LxdInstance, LxdInstanceSnapshot } from "types/instance"; | ||
import { useEventQueue } from "context/eventQueue"; | ||
import { useFormik } from "formik"; | ||
import { useToastNotification } from "context/toastNotificationProvider"; | ||
import { createImageAlias, createImageFromInstanceSnapshot } from "api/images"; | ||
import { | ||
ActionButton, | ||
Button, | ||
Form, | ||
Input, | ||
Modal, | ||
} from "@canonical/react-components"; | ||
import * as Yup from "yup"; | ||
import { Link } from "react-router-dom"; | ||
|
||
interface Props { | ||
instance: LxdInstance; | ||
snapshot: LxdInstanceSnapshot; | ||
close: () => void; | ||
} | ||
|
||
const CreateImageFromInstanceSnapshotForm: FC<Props> = ({ | ||
instance, | ||
snapshot, | ||
close, | ||
}) => { | ||
const eventQueue = useEventQueue(); | ||
const toastNotify = useToastNotification(); | ||
|
||
const notifySuccess = () => { | ||
const created = ( | ||
<Link to={`/ui/project/${instance.project}/images`}>created</Link> | ||
); | ||
toastNotify.success( | ||
<> | ||
Image {created} from snapshot <b>{snapshot.name}</b>. | ||
</>, | ||
); | ||
}; | ||
|
||
const formik = useFormik<{ alias: string; isPublic: boolean }>({ | ||
initialValues: { | ||
alias: "", | ||
isPublic: false, | ||
}, | ||
validationSchema: Yup.object().shape({ | ||
alias: Yup.string(), | ||
}), | ||
onSubmit: (values) => { | ||
const alias = values.alias; | ||
|
||
createImageFromInstanceSnapshot(instance, snapshot, values.isPublic) | ||
.then((operation) => { | ||
toastNotify.info( | ||
<> | ||
Creation of image from snapshot <b>{snapshot.name}</b> started. | ||
</>, | ||
); | ||
close(); | ||
eventQueue.set( | ||
operation.metadata.id, | ||
(event) => { | ||
if (alias) { | ||
const fingerprint = event.metadata.metadata?.fingerprint ?? ""; | ||
void createImageAlias(fingerprint, alias).then(notifySuccess); | ||
} else { | ||
notifySuccess(); | ||
} | ||
}, | ||
(msg) => { | ||
toastNotify.failure( | ||
`Image creation from snapshot "${snapshot.name}" failed.`, | ||
new Error(msg), | ||
); | ||
}, | ||
); | ||
}) | ||
.catch((e) => { | ||
toastNotify.failure( | ||
`Image creation from snapshot "${snapshot.name}" failed.`, | ||
e, | ||
); | ||
}); | ||
}, | ||
}); | ||
|
||
return ( | ||
<Modal | ||
close={close} | ||
title="Create image from instance snapshot" | ||
buttonRow={ | ||
<> | ||
<Button | ||
appearance="base" | ||
className="u-no-margin--bottom" | ||
type="button" | ||
onClick={close} | ||
> | ||
Cancel | ||
</Button> | ||
<ActionButton | ||
appearance="positive" | ||
className="u-no-margin--bottom" | ||
loading={formik.isSubmitting} | ||
disabled={!formik.isValid} | ||
onClick={() => void formik.submitForm()} | ||
> | ||
Create image | ||
</ActionButton> | ||
</> | ||
} | ||
> | ||
<Form onSubmit={formik.handleSubmit}> | ||
<Input type="text" label="Instance" value={instance.name} disabled /> | ||
<Input type="text" label="Snapshot" value={snapshot.name} disabled /> | ||
<Input | ||
{...formik.getFieldProps("alias")} | ||
type="text" | ||
label="Alias" | ||
error={formik.touched.alias ? formik.errors.alias : null} | ||
/> | ||
<Input | ||
{...formik.getFieldProps("isPublic")} | ||
type="checkbox" | ||
label="Make the image publicly available" | ||
error={formik.touched.isPublic ? formik.errors.isPublic : null} | ||
/> | ||
{/* hidden submit to enable enter key in inputs */} | ||
<Input type="submit" hidden value="Hidden input" /> | ||
</Form> | ||
</Modal> | ||
); | ||
}; | ||
|
||
export default CreateImageFromInstanceSnapshotForm; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,6 +42,7 @@ | |
|
||
.individual-notification { | ||
margin: 0.5rem 0; | ||
overflow: hidden; | ||
} | ||
|
||
.dismiss { | ||
|
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.