From 9962c6c4a82d1be1add437056bb800f62e893fd8 Mon Sep 17 00:00:00 2001 From: ssongliu Date: Tue, 11 Oct 2022 19:47:16 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=20inspect=20?= =?UTF-8?q?=E8=AF=A6=E6=83=85=E6=98=BE=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/api/v1/container.go | 17 +++--- backend/app/dto/container.go | 5 ++ backend/app/service/container.go | 42 +++++++++------ backend/router/ro_container.go | 2 +- frontend/src/api/interface/container.ts | 4 ++ frontend/src/api/modules/container.ts | 4 +- .../src/views/container/container/index.vue | 19 ++++--- .../src/views/container/network/index.vue | 54 ++++++++++++++++--- frontend/src/views/container/volume/index.vue | 54 ++++++++++++++++--- 9 files changed, 148 insertions(+), 53 deletions(-) diff --git a/backend/app/api/v1/container.go b/backend/app/api/v1/container.go index 5a2ba4710..11be46fe2 100644 --- a/backend/app/api/v1/container.go +++ b/backend/app/api/v1/container.go @@ -1,8 +1,6 @@ package v1 import ( - "errors" - "github.com/1Panel-dev/1Panel/app/api/v1/helper" "github.com/1Panel-dev/1Panel/app/dto" "github.com/1Panel-dev/1Panel/constant" @@ -49,13 +47,18 @@ func (b *BaseApi) ContainerOperation(c *gin.Context) { helper.SuccessWithData(c, nil) } -func (b *BaseApi) ContainerDetail(c *gin.Context) { - id, ok := c.Params.Get("id") - if !ok { - helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, errors.New("error id in path")) +func (b *BaseApi) Inspect(c *gin.Context) { + var req dto.InspectReq + if err := c.ShouldBindJSON(&req); err != nil { + helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) return } - result, err := containerService.ContainerInspect(id) + if err := global.VALID.Struct(req); err != nil { + helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) + return + } + + result, err := containerService.Inspect(req) if err != nil { helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) return diff --git a/backend/app/dto/container.go b/backend/app/dto/container.go index d13d68454..40e287b93 100644 --- a/backend/app/dto/container.go +++ b/backend/app/dto/container.go @@ -7,6 +7,11 @@ type PageContainer struct { Status string `json:"status" validate:"required,oneof=all running"` } +type InspectReq struct { + ID string `json:"id"` + Type string `json:"type"` +} + type ContainerInfo struct { ContainerID string `json:"containerID"` Name string `json:"name"` diff --git a/backend/app/service/container.go b/backend/app/service/container.go index 6b41184a9..52d28bedc 100644 --- a/backend/app/service/container.go +++ b/backend/app/service/container.go @@ -27,7 +27,7 @@ type IContainerService interface { PageVolume(req dto.PageInfo) (int64, interface{}, error) ContainerOperation(req dto.ContainerOperation) error ContainerLogs(param dto.ContainerLog) (string, error) - ContainerInspect(id string) (string, error) + Inspect(req dto.InspectReq) (string, error) DeleteNetwork(req dto.BatchDelete) error CreateNetwork(req dto.NetworkCreat) error DeleteVolume(req dto.BatchDelete) error @@ -77,6 +77,30 @@ func (u *ContainerService) Page(req dto.PageContainer) (int64, interface{}, erro return int64(total), backDatas, nil } +func (u *ContainerService) Inspect(req dto.InspectReq) (string, error) { + client, err := docker.NewDockerClient() + if err != nil { + return "", err + } + var inspectInfo interface{} + switch req.Type { + case "container": + inspectInfo, err = client.ContainerInspect(context.Background(), req.ID) + case "network": + inspectInfo, err = client.NetworkInspect(context.TODO(), req.ID, types.NetworkInspectOptions{}) + case "volume": + inspectInfo, err = client.VolumeInspect(context.TODO(), req.ID) + } + if err != nil { + return "", err + } + bytes, err := json.Marshal(inspectInfo) + if err != nil { + return "", err + } + return string(bytes), nil +} + func (u *ContainerService) ContainerOperation(req dto.ContainerOperation) error { var err error ctx := context.Background() @@ -105,22 +129,6 @@ func (u *ContainerService) ContainerOperation(req dto.ContainerOperation) error return err } -func (u *ContainerService) ContainerInspect(id string) (string, error) { - client, err := docker.NewDockerClient() - if err != nil { - return "", err - } - inspect, err := client.ContainerInspect(context.Background(), id) - if err != nil { - return "", err - } - bytes, err := json.Marshal(inspect) - if err != nil { - return "", err - } - return string(bytes), err -} - func (u *ContainerService) ContainerLogs(req dto.ContainerLog) (string, error) { var ( options types.ContainerLogsOptions diff --git a/backend/router/ro_container.go b/backend/router/ro_container.go index 9c9ba8014..20252f5fb 100644 --- a/backend/router/ro_container.go +++ b/backend/router/ro_container.go @@ -21,7 +21,7 @@ func (s *ContainerRouter) InitContainerRouter(Router *gin.RouterGroup) { baseApi := v1.ApiGroupApp.BaseApi { baRouter.POST("/search", baseApi.SearchContainer) - baRouter.GET("/detail/:id", baseApi.ContainerDetail) + baRouter.POST("/inspect", baseApi.Inspect) withRecordRouter.POST("operate", baseApi.ContainerOperation) withRecordRouter.POST("/log", baseApi.ContainerLogs) diff --git a/frontend/src/api/interface/container.ts b/frontend/src/api/interface/container.ts index 0f69a4cb2..93b8e7294 100644 --- a/frontend/src/api/interface/container.ts +++ b/frontend/src/api/interface/container.ts @@ -16,6 +16,10 @@ export namespace Container { containerID: string; mode: string; } + export interface ContainerInspect { + id: string; + type: string; + } export interface ImageInfo { id: string; diff --git a/frontend/src/api/modules/container.ts b/frontend/src/api/modules/container.ts index b3486d201..0e0626aca 100644 --- a/frontend/src/api/modules/container.ts +++ b/frontend/src/api/modules/container.ts @@ -14,8 +14,8 @@ export const ContainerOperator = (params: Container.ContainerOperate) => { return http.post(`/containers/operate`, params); }; -export const getContainerInspect = (containerID: string) => { - return http.get(`/containers/detail/${containerID}`); +export const inspect = (params: Container.ContainerInspect) => { + return http.post(`/containers/inspect`, params); }; // image diff --git a/frontend/src/views/container/container/index.vue b/frontend/src/views/container/container/index.vue index 13743cfe3..b1eb07ff9 100644 --- a/frontend/src/views/container/container/index.vue +++ b/frontend/src/views/container/container/index.vue @@ -37,7 +37,11 @@ min-width="100" prop="name" fix - /> + > + + { dialogCreateRef.value!.acceptParams(); }; -const onDetail = async (row: Container.ContainerInfo) => { - const res = await getContainerInspect(row.containerID); +const onInspect = async (id: string) => { + const res = await inspect({ id: id, type: 'container' }); detailInfo.value = JSON.stringify(JSON.parse(res.data), null, 2); detailVisiable.value = true; }; + const onLog = async (row: Container.ContainerInfo) => { logSearch.container = row.name; logSearch.containerID = row.containerID; @@ -371,12 +376,6 @@ const buttons = [ onLog(row); }, }, - { - label: i18n.global.t('commons.button.view'), - click: (row: Container.ContainerInfo) => { - onDetail(row); - }, - }, ]; onMounted(() => { diff --git a/frontend/src/views/container/network/index.vue b/frontend/src/views/container/network/index.vue index 9c1bd87c5..d00085f73 100644 --- a/frontend/src/views/container/network/index.vue +++ b/frontend/src/views/container/network/index.vue @@ -11,13 +11,11 @@ - + + + @@ -53,13 +78,20 @@