Browse Source

fix: 解决容器编辑无法编辑名称的问题 (#1546)

pull/1548/head
ssongliu 1 year ago committed by GitHub
parent
commit
12eeb6503c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      backend/app/dto/container.go
  2. 20
      backend/app/service/container.go
  3. 1
      frontend/src/api/interface/container.ts
  4. 8
      frontend/src/views/container/container/operate/index.vue

1
backend/app/dto/container.go

@ -38,6 +38,7 @@ type ResourceLimit struct {
}
type ContainerOperate struct {
ContainerID string `json:"containerID"`
Name string `json:"name"`
Image string `json:"image"`
PublishAllPorts bool `json:"publishAllPorts"`

20
backend/app/service/container.go

@ -285,6 +285,11 @@ func (u *ContainerService) ContainerCreate(req dto.ContainerOperate) error {
if err != nil {
return err
}
ctx := context.Background()
newContainer, _ := client.ContainerInspect(ctx, req.Name)
if len(newContainer.ID) != 0 {
return buserr.New(constant.ErrContainerName)
}
var config container.Config
var hostConf container.HostConfig
@ -294,7 +299,6 @@ func (u *ContainerService) ContainerCreate(req dto.ContainerOperate) error {
global.LOG.Infof("new container info %s has been made, now start to create", req.Name)
ctx := context.Background()
if !checkImageExist(client, req.Image) {
if err := pullImages(ctx, client, req.Image); err != nil {
return err
@ -325,6 +329,7 @@ func (u *ContainerService) ContainerInfo(req dto.OperationWithName) (*dto.Contai
}
var data dto.ContainerOperate
data.ContainerID = oldContainer.ID
data.Name = strings.ReplaceAll(oldContainer.Name, "/", "")
data.Image = oldContainer.Config.Image
data.Cmd = oldContainer.Config.Cmd
@ -372,7 +377,12 @@ func (u *ContainerService) ContainerUpdate(req dto.ContainerOperate) error {
return err
}
ctx := context.Background()
oldContainer, err := client.ContainerInspect(ctx, req.Name)
newContainer, _ := client.ContainerInspect(ctx, req.Name)
if len(newContainer.ID) != 0 && newContainer.ID != req.ContainerID {
return buserr.New(constant.ErrContainerName)
}
oldContainer, err := client.ContainerInspect(ctx, req.ContainerID)
if err != nil {
return err
}
@ -386,7 +396,7 @@ func (u *ContainerService) ContainerUpdate(req dto.ContainerOperate) error {
if err := loadConfigInfo(req, config, hostConf); err != nil {
return err
}
if err := client.ContainerRemove(ctx, req.Name, types.ContainerRemoveOptions{Force: true}); err != nil {
if err := client.ContainerRemove(ctx, req.ContainerID, types.ContainerRemoveOptions{Force: true}); err != nil {
return err
}
@ -460,6 +470,10 @@ func (u *ContainerService) ContainerOperation(req dto.ContainerOperation) error
case constant.ContainerOpUnpause:
err = client.ContainerUnpause(ctx, req.Name)
case constant.ContainerOpRename:
newContainer, _ := client.ContainerInspect(ctx, req.NewName)
if len(newContainer.ID) != 0 {
return buserr.New(constant.ErrContainerName)
}
err = client.ContainerRename(ctx, req.Name, req.NewName)
case constant.ContainerOpRemove:
err = client.ContainerRemove(ctx, req.Name, types.ContainerRemoveOptions{RemoveVolumes: true, Force: true})

1
frontend/src/api/interface/container.ts

@ -17,6 +17,7 @@ export namespace Container {
memory: number;
}
export interface ContainerHelper {
containerID: string;
name: string;
image: string;
cmdStr: string;

8
frontend/src/views/container/container/operate/index.vue

@ -1,7 +1,7 @@
<template>
<el-drawer v-model="drawerVisiable" :destroy-on-close="true" :close-on-click-modal="false" size="50%">
<template #header>
<DrawerHeader :header="title" :back="handleClose" />
<DrawerHeader :header="title" :resource="dialogData.rowData?.name" :back="handleClose" />
</template>
<el-form
ref="formRef"
@ -379,7 +379,8 @@ const onSubmit = async (formEl: FormInstance | undefined) => {
confirmButtonText: i18n.global.t('commons.button.confirm'),
cancelButtonText: i18n.global.t('commons.button.cancel'),
},
).then(async () => {
)
.then(async () => {
await updateContainer(dialogData.value.rowData!)
.then(() => {
loading.value = false;
@ -390,6 +391,9 @@ const onSubmit = async (formEl: FormInstance | undefined) => {
.catch(() => {
loading.value = false;
});
})
.catch(() => {
loading.value = false;
});
}
});

Loading…
Cancel
Save