Skip to content

Commit

Permalink
Validate of container name is already in use
Browse files Browse the repository at this point in the history
Fixes #609
  • Loading branch information
skobyda committed Sep 7, 2023
1 parent 3bb84f4 commit a9f37bd
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
28 changes: 24 additions & 4 deletions src/ImageRunModal.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React from 'react';
import { Button } from "@patternfly/react-core/dist/esm/components/Button";
import { Checkbox } from "@patternfly/react-core/dist/esm/components/Checkbox";
import { Form, FormGroup } from "@patternfly/react-core/dist/esm/components/Form";
import { FormHelper } from "cockpit-components-form-helper.jsx";
import { FormSelect, FormSelectOption } from "@patternfly/react-core/dist/esm/components/FormSelect";
import { Grid, GridItem } from "@patternfly/react-core/dist/esm/layouts/Grid";
import { Modal } from "@patternfly/react-core/dist/esm/components/Modal";
Expand Down Expand Up @@ -258,7 +259,7 @@ export class ImageRunModal extends React.Component {
};

async onCreateClicked(runImage = false) {
if (!this.validateForm())
if (!await this.validateForm())
return;

const Dialogs = this.props.dialogs;
Expand Down Expand Up @@ -590,8 +591,17 @@ export class ImageRunModal extends React.Component {
return Object.keys(validationFailed).length == 0;
};

validateForm = () => {
const { publish, volumes, env } = this.state;
async validateContainerName(containerName) {
try {
await client.containerExists(this.isSystem(), containerName);
} catch (error) {
return;
}
return _("Name already in use");
}

async validateForm() {
const { publish, volumes, env, containerName } = this.state;
const validationFailed = { };

const publishValidation = publish.map(a => {
Expand Down Expand Up @@ -625,10 +635,12 @@ export class ImageRunModal extends React.Component {
if (Object.keys(envValidation).length > 0)
validationFailed.env = envValidation;

validationFailed.containerName = await this.validateContainerName(containerName);

this.setState({ validationFailed });

return this.isFormValid(validationFailed);
};
}

/* Updates a validation object of the whole dynamic list's form (e.g. the whole port-mapping form)
*
Expand Down Expand Up @@ -707,8 +719,16 @@ export class ImageRunModal extends React.Component {
<TextInput id='run-image-dialog-name'
className="image-name"
placeholder={_("Container name")}
validated={dialogValues.validationFailed.containerName ? "error" : "default"}
onFocus={() => utils.validationClear(dialogValues.validationFailed, "containerName", (value) => this.onValueChanged("validationFailed", value))}
onBlur={async () => {
const delta = await this.validateContainerName(dialogValues.containerName);
if (delta)
this.onValueChanged("validationFailed", { ...dialogValues.validationFailed, containerName: delta });
}}
value={dialogValues.containerName}
onChange={(_, value) => this.onValueChanged('containerName', value)} />
<FormHelper helperTextInvalid={dialogValues.validationFailed.containerName} />
</FormGroup>
<Tabs activeKey={activeTabKey} onSelect={this.handleTabClick}>
<Tab eventKey={0} title={<TabTitleText>{_("Details")}</TabTitleText>} className="pf-v5-c-form pf-m-horizontal">
Expand Down
2 changes: 2 additions & 0 deletions src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,5 @@ export const pruneUnusedImages = system => podmanJson("libpod/images/prune?all=t
export const imageHistory = (system, id) => podmanJson(`libpod/images/${id}/history`, "GET", {}, system);

export const imageExists = (system, id) => podmanCall("libpod/images/" + id + "/exists", "GET", {}, system);

export const containerExists = (system, id) => podmanCall("libpod/containers/" + id + "/exists", "GET", {}, system);

0 comments on commit a9f37bd

Please sign in to comment.