96 lines
2.2 KiB
Go
96 lines
2.2 KiB
Go
package logic
|
||
|
||
import (
|
||
"context"
|
||
"encoding/json"
|
||
"github.com/go-resty/resty/v2"
|
||
"gitlink.org.cn/JointCloud/pcm-ac/hpcAC"
|
||
"gitlink.org.cn/JointCloud/pcm-ac/internal/svc"
|
||
"io"
|
||
"net/http"
|
||
"time"
|
||
|
||
"github.com/zeromicro/go-zero/core/logx"
|
||
)
|
||
|
||
type GetACTokenLogic struct {
|
||
ctx context.Context
|
||
svcCtx *svc.ServiceContext
|
||
logx.Logger
|
||
}
|
||
|
||
func NewGetACTokenLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetACTokenLogic {
|
||
return &GetACTokenLogic{
|
||
ctx: ctx,
|
||
svcCtx: svcCtx,
|
||
Logger: logx.WithContext(ctx),
|
||
}
|
||
}
|
||
|
||
// GetACToken 曙光ac获取token
|
||
func (l *GetACTokenLogic) GetACToken(in *hpcAC.ACTokenReq) (*hpcAC.TokenResp, error) {
|
||
|
||
var respAC hpcAC.ACTokenResp
|
||
var resp hpcAC.TokenResp
|
||
token := l.svcCtx.Config.ShuguangConf.Token
|
||
|
||
httpClient := http.Client{Timeout: time.Duration(3) * time.Second}
|
||
|
||
req, _ := http.NewRequest("GET", l.svcCtx.Config.ShuguangConf.StateUrl, nil)
|
||
req.Header.Add("token", token)
|
||
respState, _ := httpClient.Do(req)
|
||
var res hpcAC.ACTokenState
|
||
body, _ := io.ReadAll(respState.Body)
|
||
err := json.Unmarshal(body, &res)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
defer func(Body io.ReadCloser) {
|
||
err := Body.Close()
|
||
if err != nil {
|
||
|
||
}
|
||
}(respState.Body)
|
||
|
||
//获取状态返回0则token可用,返回为10008或者token为空则重新读取
|
||
if res.Code == "0" {
|
||
var acDataToken hpcAC.ACTokenData
|
||
acDataToken.Token = token
|
||
resp.Data = &acDataToken
|
||
resp.Msg = res.Msg
|
||
resp.Code = "200"
|
||
} else if token == "" || res.Code == "10008" {
|
||
client := resty.New()
|
||
queryParams := map[string]string{
|
||
"user": l.svcCtx.Config.ShuguangConf.User,
|
||
"password": l.svcCtx.Config.ShuguangConf.Password,
|
||
"orgId": l.svcCtx.Config.ShuguangConf.OrgId,
|
||
}
|
||
_, err := client.R().
|
||
SetHeaders(queryParams).
|
||
SetResult(&respAC).
|
||
Post(l.svcCtx.Config.ShuguangConf.TokenUrl)
|
||
|
||
if err != nil {
|
||
logx.WithContext(l.ctx).Errorf("ACAuth err: %s", err.Error())
|
||
return nil, err
|
||
}
|
||
|
||
//这里固定使用clusterid 11112
|
||
for _, dt := range respAC.Data {
|
||
if dt.ClusterId == "11276" {
|
||
token = dt.Token
|
||
}
|
||
}
|
||
l.svcCtx.Config.ShuguangConf.Token = token
|
||
resp.Code = respAC.Code
|
||
resp.Msg = respAC.Msg
|
||
var acDataToken hpcAC.ACTokenData
|
||
acDataToken.Token = token
|
||
resp.Data = &acDataToken
|
||
}
|
||
|
||
return &resp, nil
|
||
}
|