Merge pull request 'finish vm create task' (#22) from qiwang/pcm-openstack:master into master

This commit is contained in:
qiwang 2024-07-03 11:22:19 +08:00
commit e6c8a1e9e0
1 changed files with 48 additions and 28 deletions

View File

@ -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) {