镜像列表接口拼接地址
Former-commit-id: e502be4a9dea6dd417d834fd67880adaf891e4f7
This commit is contained in:
parent
10ed9b35e9
commit
bd891b1847
|
@ -184,6 +184,4 @@ service pcm {
|
||||||
@handler imageListHandler
|
@handler imageListHandler
|
||||||
get /image/list () returns (imageListResp)
|
get /image/list () returns (imageListResp)
|
||||||
|
|
||||||
@handler imageTagsHandler
|
|
||||||
get /image/tags (imageTagsReq) returns (imageTagsResp)
|
|
||||||
}
|
}
|
|
@ -112,7 +112,7 @@ type (
|
||||||
|
|
||||||
type (
|
type (
|
||||||
imageTagsReq {
|
imageTagsReq {
|
||||||
name string `json:"name"`
|
name string `form:"name"`
|
||||||
}
|
}
|
||||||
imageTagsResp {
|
imageTagsResp {
|
||||||
name string `json:"name"`
|
name string `json:"name"`
|
||||||
|
|
|
@ -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)
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -271,11 +271,6 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
|
||||||
Path: "/image/list",
|
Path: "/image/list",
|
||||||
Handler: image.ImageListHandler(serverCtx),
|
Handler: image.ImageListHandler(serverCtx),
|
||||||
},
|
},
|
||||||
{
|
|
||||||
Method: http.MethodGet,
|
|
||||||
Path: "/image/tags",
|
|
||||||
Handler: image.ImageTagsHandler(serverCtx),
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
rest.WithPrefix("/pcm/v1"),
|
rest.WithPrefix("/pcm/v1"),
|
||||||
)
|
)
|
||||||
|
|
|
@ -1,13 +1,12 @@
|
||||||
package image
|
package image
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"PCM/adaptor/PCM-CORE/api/internal/svc"
|
||||||
|
"PCM/adaptor/PCM-CORE/api/internal/types"
|
||||||
|
"PCM/common/tool"
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
|
||||||
|
|
||||||
"PCM/adaptor/PCM-CORE/api/internal/svc"
|
|
||||||
"PCM/adaptor/PCM-CORE/api/internal/types"
|
|
||||||
"github.com/zeromicro/go-zero/core/logx"
|
"github.com/zeromicro/go-zero/core/logx"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -17,6 +16,11 @@ type ImageListLogic struct {
|
||||||
svcCtx *svc.ServiceContext
|
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 {
|
func NewImageListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ImageListLogic {
|
||||||
return &ImageListLogic{
|
return &ImageListLogic{
|
||||||
Logger: logx.WithContext(ctx),
|
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) {
|
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")
|
url := fmt.Sprintf("%s/repository/%s/v2/_catalog", l.svcCtx.Config.NexusUrl, "pcm")
|
||||||
req, err := http.NewRequest("GET", url, nil)
|
bytes, err := tool.HttpClient("GET", url, nil, "")
|
||||||
if err != nil {
|
json.Unmarshal(bytes, &resp)
|
||||||
return nil, err
|
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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
return &result, nil
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
|
||||||
}
|
|
|
@ -1853,7 +1853,7 @@ type ImageListResp struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type ImageTagsReq struct {
|
type ImageTagsReq struct {
|
||||||
Name string `json:"name"`
|
Name string `form:"name"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type ImageTagsResp struct {
|
type ImageTagsResp struct {
|
||||||
|
|
Loading…
Reference in New Issue