From bd891b18470aa11a696a1b01453276cc96ea7169 Mon Sep 17 00:00:00 2001 From: zhangwei <894646498@qq.com> Date: Thu, 18 May 2023 10:05:45 +0800 Subject: [PATCH] =?UTF-8?q?=E9=95=9C=E5=83=8F=E5=88=97=E8=A1=A8=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E6=8B=BC=E6=8E=A5=E5=9C=B0=E5=9D=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Former-commit-id: e502be4a9dea6dd417d834fd67880adaf891e4f7 --- adaptor/PCM-CORE/api/desc/pcm.api | 2 - .../PCM-CORE/api/desc/storage/pcm-storage.api | 2 +- .../handler/image/imagetagshandler.go | 25 --------- .../PCM-CORE/api/internal/handler/routes.go | 5 -- .../internal/logic/image/imagelistlogic.go | 53 +++++++++++-------- .../internal/logic/image/imagetagslogic.go | 48 ----------------- adaptor/PCM-CORE/api/internal/types/types.go | 2 +- 7 files changed, 34 insertions(+), 103 deletions(-) delete mode 100644 adaptor/PCM-CORE/api/internal/handler/image/imagetagshandler.go delete mode 100644 adaptor/PCM-CORE/api/internal/logic/image/imagetagslogic.go diff --git a/adaptor/PCM-CORE/api/desc/pcm.api b/adaptor/PCM-CORE/api/desc/pcm.api index 80e4288d..ce3f80bd 100644 --- a/adaptor/PCM-CORE/api/desc/pcm.api +++ b/adaptor/PCM-CORE/api/desc/pcm.api @@ -184,6 +184,4 @@ service pcm { @handler imageListHandler get /image/list () returns (imageListResp) - @handler imageTagsHandler - get /image/tags (imageTagsReq) returns (imageTagsResp) } \ No newline at end of file diff --git a/adaptor/PCM-CORE/api/desc/storage/pcm-storage.api b/adaptor/PCM-CORE/api/desc/storage/pcm-storage.api index d9d7bd46..87117496 100644 --- a/adaptor/PCM-CORE/api/desc/storage/pcm-storage.api +++ b/adaptor/PCM-CORE/api/desc/storage/pcm-storage.api @@ -112,7 +112,7 @@ type ( type ( imageTagsReq { - name string `json:"name"` + name string `form:"name"` } imageTagsResp { name string `json:"name"` diff --git a/adaptor/PCM-CORE/api/internal/handler/image/imagetagshandler.go b/adaptor/PCM-CORE/api/internal/handler/image/imagetagshandler.go deleted file mode 100644 index 36f177f8..00000000 --- a/adaptor/PCM-CORE/api/internal/handler/image/imagetagshandler.go +++ /dev/null @@ -1,25 +0,0 @@ -package image - -import ( - "PCM/common/result" - "net/http" - - "PCM/adaptor/PCM-CORE/api/internal/logic/image" - "PCM/adaptor/PCM-CORE/api/internal/svc" - "PCM/adaptor/PCM-CORE/api/internal/types" - "github.com/zeromicro/go-zero/rest/httpx" -) - -func ImageTagsHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { - return func(w http.ResponseWriter, r *http.Request) { - var req types.ImageTagsReq - if err := httpx.Parse(r, &req); err != nil { - httpx.ErrorCtx(r.Context(), w, err) - return - } - - l := image.NewImageTagsLogic(r.Context(), svcCtx) - resp, err := l.ImageTags(&req) - result.HttpResult(r, w, resp, err) - } -} diff --git a/adaptor/PCM-CORE/api/internal/handler/routes.go b/adaptor/PCM-CORE/api/internal/handler/routes.go index 5c9bc8ef..734d774d 100644 --- a/adaptor/PCM-CORE/api/internal/handler/routes.go +++ b/adaptor/PCM-CORE/api/internal/handler/routes.go @@ -271,11 +271,6 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { Path: "/image/list", Handler: image.ImageListHandler(serverCtx), }, - { - Method: http.MethodGet, - Path: "/image/tags", - Handler: image.ImageTagsHandler(serverCtx), - }, }, rest.WithPrefix("/pcm/v1"), ) diff --git a/adaptor/PCM-CORE/api/internal/logic/image/imagelistlogic.go b/adaptor/PCM-CORE/api/internal/logic/image/imagelistlogic.go index cab880f0..ce5422c4 100644 --- a/adaptor/PCM-CORE/api/internal/logic/image/imagelistlogic.go +++ b/adaptor/PCM-CORE/api/internal/logic/image/imagelistlogic.go @@ -1,13 +1,12 @@ package image import ( + "PCM/adaptor/PCM-CORE/api/internal/svc" + "PCM/adaptor/PCM-CORE/api/internal/types" + "PCM/common/tool" "context" "encoding/json" "fmt" - "net/http" - - "PCM/adaptor/PCM-CORE/api/internal/svc" - "PCM/adaptor/PCM-CORE/api/internal/types" "github.com/zeromicro/go-zero/core/logx" ) @@ -17,6 +16,11 @@ type ImageListLogic struct { svcCtx *svc.ServiceContext } +type ImageTags struct { + Name string `json:"name"` + Tags []string `json:"tags" copier:"tags"` +} + func NewImageListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ImageListLogic { return &ImageListLogic{ Logger: logx.WithContext(ctx), @@ -27,23 +31,30 @@ func NewImageListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ImageLi func (l *ImageListLogic) ImageList() (resp *types.ImageListResp, err error) { - client := &http.Client{} + // 获取镜像列表 url := fmt.Sprintf("%s/repository/%s/v2/_catalog", l.svcCtx.Config.NexusUrl, "pcm") - req, err := http.NewRequest("GET", url, nil) - if err != nil { - return nil, err + bytes, err := tool.HttpClient("GET", url, nil, "") + json.Unmarshal(bytes, &resp) + result := types.ImageListResp{} + for _, image := range resp.Repositories { + // 获取镜像的tag列表 + url := fmt.Sprintf("%s/repository/%s/v2/%s/tags/list", l.svcCtx.Config.NexusUrl, "pcm", image) + bytes, err := tool.HttpClient("GET", url, nil, "") + if err != nil { + return nil, err + } + tags := ImageTags{} + json.Unmarshal(bytes, &tags) + // 拼接镜像名称 + for _, tag := range tags.Tags { + var name string + if tag != "latest" { + name = "hub.jcce.dev:18445/" + image + ":v" + tag + } else { + name = "hub.jcce.dev:18445/" + image + } + result.Repositories = append(result.Repositories, name) + } } - - response, err := client.Do(req) - if err != nil { - return nil, err - } - defer response.Body.Close() - - if response.StatusCode != 200 { - } - - json.NewDecoder(response.Body).Decode(&resp) - - return resp, nil + return &result, nil } diff --git a/adaptor/PCM-CORE/api/internal/logic/image/imagetagslogic.go b/adaptor/PCM-CORE/api/internal/logic/image/imagetagslogic.go deleted file mode 100644 index 72484687..00000000 --- a/adaptor/PCM-CORE/api/internal/logic/image/imagetagslogic.go +++ /dev/null @@ -1,48 +0,0 @@ -package image - -import ( - "context" - "encoding/json" - "fmt" - "net/http" - - "PCM/adaptor/PCM-CORE/api/internal/svc" - "PCM/adaptor/PCM-CORE/api/internal/types" - - "github.com/zeromicro/go-zero/core/logx" -) - -type ImageTagsLogic struct { - logx.Logger - ctx context.Context - svcCtx *svc.ServiceContext -} - -func NewImageTagsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ImageTagsLogic { - return &ImageTagsLogic{ - Logger: logx.WithContext(ctx), - ctx: ctx, - svcCtx: svcCtx, - } -} - -func (l *ImageTagsLogic) ImageTags(req *types.ImageTagsReq) (resp *types.ImageTagsResp, err error) { - client := &http.Client{} - url := fmt.Sprintf("%s/repository/%s/v2/%s/tags/list", l.svcCtx.Config.NexusUrl, "pcm", req.Name) - httpReq, err := http.NewRequest("GET", url, nil) - if err != nil { - return nil, err - } - - response, err := client.Do(httpReq) - if err != nil { - return nil, err - } - defer response.Body.Close() - - json.NewDecoder(response.Body).Decode(&resp) - - if response.StatusCode != 200 { - } - return resp, nil -} diff --git a/adaptor/PCM-CORE/api/internal/types/types.go b/adaptor/PCM-CORE/api/internal/types/types.go index 0469939b..6210a5af 100644 --- a/adaptor/PCM-CORE/api/internal/types/types.go +++ b/adaptor/PCM-CORE/api/internal/types/types.go @@ -1853,7 +1853,7 @@ type ImageListResp struct { } type ImageTagsReq struct { - Name string `json:"name"` + Name string `form:"name"` } type ImageTagsResp struct {