pcm-openstack/internal/pkg/cron/taskcron.go

113 lines
3.2 KiB
Go

package cron
import (
"fmt"
"github.com/go-resty/resty/v2"
"github.com/zeromicro/go-zero/core/logx"
coreClient "gitlink.org.cn/JointCloud/pcm-coordinator/api/client"
"gitlink.org.cn/JointCloud/pcm-coordinator/pkg/utils/httputils"
"gitlink.org.cn/JointCloud/pcm-openstack/internal/common"
"gitlink.org.cn/JointCloud/pcm-openstack/openstack"
"k8s.io/apimachinery/pkg/util/json"
"log"
"net/http"
"strconv"
"strings"
)
var AdapterId int64
var CoreUrl string
func PullTaskInfo() {
httpClient := resty.New().R()
result, _ := httpClient.SetHeader("Content-Type", "application/json").
SetQueryParam("adapterId", strconv.FormatInt(AdapterId, 10)).
Get(CoreUrl + "/pcm/v1/core/pullTaskInfo")
var resp coreClient.PullTaskInfoResp
//var respserver openstack.CreateServerResp
err := json.Unmarshal(result.Body(), &resp)
if err != nil {
return
}
if resp.VmInfoList != nil && len(resp.VmInfoList) != 0 {
var oldVmInfoList []coreClient.VmInfo
CreateServerReq := &openstack.CreateServerReq{}
for i, vmInfo := range resp.VmInfoList {
if vmInfo.Status == "Saved" {
CreateServerReq.Platform = vmInfo.Platform
CreateServerReq.CrServer.Server.Name = vmInfo.VmName
CreateServerReq.CrServer.Server.FlavorRef = vmInfo.FlavorRef
CreateServerReq.CrServer.Server.ImageRef = vmInfo.ImageRef
CreateServerReq.CrServer.Server.Networks[i].Uuid = vmInfo.NetworkUuid
respServer, err := CreateServer(CreateServerReq)
if err != nil {
log.Print(err)
}
vmInfo.ServerId = respServer.Server.Id
oldVmInfoList = append(oldVmInfoList, *vmInfo)
vmInfo.Status = "Running"
} /*else { //if state is not "Saved" ,then get state from participant and sync to core
}*/
}
}
// push submitted mark to coordinator
PushReq := coreClient.PushTaskInfoReq{
AdapterId: AdapterId,
VmInfoList: resp.VmInfoList,
}
if len(PushReq.VmInfoList) != 0 {
url := CoreUrl + "/pcm/v1/core/pushTaskInfo"
method := "POST"
jsonStr, _ := json.Marshal(PushReq)
payload := strings.NewReader(string(jsonStr))
client := &http.Client{}
req, _ := http.NewRequest(method, url, payload)
req.Header.Add("Content-Type", "application/json")
_, err := client.Do(req)
if err != nil {
return
}
}
}
func CreateServer(req *openstack.CreateServerReq) (*openstack.CreateServerResp, error) {
var resp openstack.CreateServerResp
platform, err := common.GetOpenstackConfWithPlatform(req.Platform)
token := common.GetToken(req.Platform)
fmt.Println("错误", err)
reqByte, err := json.Marshal(req.CrServer)
if err != nil {
return nil, err
}
payload := strings.NewReader(string(reqByte))
openstackUrl := platform.OpenstackComputeUrl
statusCode, body, err := httputils.HttpClientWithBodyAndCode(httputils.POST, openstackUrl+"/servers", payload, token)
fmt.Println("返回值", body)
if err != nil {
return nil, err
}
logx.Infof("pcm-openstack call createServer 返回结果! statusCode %s: ,\n body: %s ,\n err: %s", statusCode, body, err)
if statusCode == 202 {
err := json.Unmarshal(body, &resp)
if err != nil {
fmt.Println(err)
}
resp.Code = 200
resp.Msg = "Success"
} else if statusCode != 202 {
json.Unmarshal(body, &resp)
resp.Code = 400
resp.Msg = "Failure"
}
return &resp, nil
}