pcm-octopus/internal/logic/getresourcespecslogic.go

93 lines
2.3 KiB
Go

package logic
import (
"context"
"errors"
"github.com/zeromicro/go-zero/core/logx"
"gitlink.org.cn/JointCloud/pcm-octopus/internal/common"
"gitlink.org.cn/JointCloud/pcm-octopus/internal/svc"
"gitlink.org.cn/JointCloud/pcm-octopus/octopus"
"log"
)
type GetResourceSpecsLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewGetResourceSpecsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetResourceSpecsLogic {
return &GetResourceSpecsLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
// ResourceSpecService
func (l *GetResourceSpecsLogic) GetResourceSpecs(in *octopus.GetResourceSpecsReq) (*octopus.GetResourceSpecsResp, error) {
resp := &octopus.GetResourceSpecsResp{}
var url_prefix = common.OctopusUrls[in.Platform]
var reqUrl = url_prefix + l.svcCtx.Config.OctopusApi.GetResourceSpecs
token := common.GetToken(in.Platform)
if token == "" {
log.Println("获取token失败, platform : ", in.Platform)
return nil, errors.New("获取token失败")
}
switch in.Platform {
case common.Hanwuji:
specResp := common.HanwujiResp{}
req := common.GetRestyRequest()
_, err := req.
SetHeader("Authorization", "Bearer "+token).
SetQueryString("resourcePool=" + in.ResourcePool).
SetResult(&specResp).
Get(reqUrl)
if err != nil {
return nil, err
}
for _, elem := range specResp.Payload.MapResourceSpecIdList.Train.ResourceSpecs {
var spec octopus.ResourceSpecs
spec.Id = elem.Id
spec.Name = elem.Name
spec.Price = float64(elem.Price)
resp.TrainResourceSpecs = append(resp.TrainResourceSpecs, &spec)
}
case common.Suiyuan:
specResp := common.SuiyuanResp{}
req := common.GetRestyRequest()
_, err := req.
SetHeader("Authorization", "Bearer "+token).
SetQueryString("resourcePool=" + in.ResourcePool).
SetResult(&specResp).
Get(reqUrl)
if err != nil {
return nil, err
}
for _, elem := range specResp.Payload.MapResourceSpecIdList.Train.ResourceSpecs {
var spec octopus.ResourceSpecs
spec.Id = elem.Id
spec.Name = elem.Name
spec.Price = float64(elem.Price)
resp.TrainResourceSpecs = append(resp.TrainResourceSpecs, &spec)
}
default:
return nil, errors.New("转换资源规格失败")
}
if len(resp.TrainResourceSpecs) == 0 {
resp.Success = false
} else {
resp.Success = true
}
return resp, nil
}