From 0f1f58b6d8d2ec1ea401c230e85bb1724b2a426a Mon Sep 17 00:00:00 2001 From: qiwang <1364512070@qq.com> Date: Wed, 3 Jul 2024 11:19:48 +0800 Subject: [PATCH] finish vm create task --- internal/pkg/cron/taskcron.go | 76 ++++++++++++++++++++++------------- 1 file changed, 48 insertions(+), 28 deletions(-) diff --git a/internal/pkg/cron/taskcron.go b/internal/pkg/cron/taskcron.go index 5bf3801..e52addc 100644 --- a/internal/pkg/cron/taskcron.go +++ b/internal/pkg/cron/taskcron.go @@ -28,8 +28,8 @@ func PullTaskInfo(svc *svc.ServiceContext) { fmt.Println("CoreUrl:", CoreUrl) httpClient := resty.New().R() result, _ := httpClient.SetHeader("Content-Type", "application/json"). - SetQueryParam("adapterId", strconv.FormatInt(1706858330967773113, 10)). - Get("http://127.0.0.1:8999" + "/pcm/v1/core/pullTaskInfo") + SetQueryParam("adapterId", strconv.FormatInt(AdapterId, 10)). + Get(CoreUrl + "/pcm/v1/core/pullTaskInfo") var resp coreClient.PullTaskInfoResp err := json.Unmarshal(result.Body(), &resp) @@ -41,14 +41,15 @@ func PullTaskInfo(svc *svc.ServiceContext) { var oldVmInfoList []coreClient.VmInfo CreateServerReq := common.CreateSerReq{} - for i, vmInfo := range resp.VmInfoList { + for _, vmInfo := range resp.VmInfoList { if vmInfo.Status == "Saved" { - CreateServerReq.CrServer.Server.Networks = make([]common.Networks, len(resp.VmInfoList)) + network := common.Networks{} 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.Uuid + network.Uuid = vmInfo.Uuid + CreateServerReq.CrServer.Server.Networks = append(CreateServerReq.CrServer.Server.Networks, network) CreateServerReq.CrServer.Server.MinCount = vmInfo.MinCount respServer, err := CreateServer(CreateServerReq) if err != nil { @@ -57,6 +58,27 @@ func PullTaskInfo(svc *svc.ServiceContext) { vmInfo.ServerId = respServer.Server.Id oldVmInfoList = append(oldVmInfoList, *vmInfo) vmInfo.Status = "Running" + + // 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 := bytes.NewBuffer(jsonStr) + client := &http.Client{} + req, _ := http.NewRequest(method, url, payload) + req.Header.Add("Content-Type", "application/json") + resp, err := client.Do(req) + if err != nil { + return + } + fmt.Print(resp) + } } else if vmInfo.Status == "Running" || vmInfo.Status == "BUILD" { //if state is not "Running" ,then get state from participant and sync to core GetServersDetailedByIdReq := common.GetServersDetailedByIdReq{} GetServersDetailedByIdReq.ServerId = vmInfo.ServerId @@ -67,34 +89,32 @@ func PullTaskInfo(svc *svc.ServiceContext) { continue } vmInfo.Status = GetServersDetailedByIdResp.Server.Status - } else { - return + + // 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 := bytes.NewBuffer(jsonStr) + client := &http.Client{} + req, _ := http.NewRequest(method, url, payload) + req.Header.Add("Content-Type", "application/json") + resp, err := client.Do(req) + if err != nil { + return + } + fmt.Print(resp) + } } } } - // push submitted mark to coordinator - PushReq := coreClient.PushTaskInfoReq{ - AdapterId: 1706858330967773113, - VmInfoList: resp.VmInfoList, - } - - if len(PushReq.VmInfoList) != 0 { - url := "http://127.0.0.1:8999" + "/pcm/v1/core/pushTaskInfo" - method := "POST" - jsonStr, _ := json.Marshal(PushReq) - payload := bytes.NewBuffer(jsonStr) - client := &http.Client{} - req, _ := http.NewRequest(method, url, payload) - req.Header.Add("Content-Type", "application/json") - resp, err := client.Do(req) - if err != nil { - return - } - fmt.Print(resp) - } - } func CreateServer(req common.CreateSerReq) (common.CreateServerResp, error) {