63 lines
1.7 KiB
Go
63 lines
1.7 KiB
Go
package logic
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"gitlink.org.cn/JointCloud/pcm-coordinator/pkg/utils/httputils"
|
|
"gitlink.org.cn/JointCloud/pcm-openstack/internal/common"
|
|
"gitlink.org.cn/JointCloud/pcm-openstack/internal/svc"
|
|
"gitlink.org.cn/JointCloud/pcm-openstack/openstack"
|
|
"strings"
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
"k8s.io/apimachinery/pkg/util/json"
|
|
)
|
|
|
|
type CreateServerLogic struct {
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
logx.Logger
|
|
}
|
|
|
|
func NewCreateServerLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateServerLogic {
|
|
return &CreateServerLogic{
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
Logger: logx.WithContext(ctx),
|
|
}
|
|
}
|
|
|
|
func (l *CreateServerLogic) CreateServer(in *openstack.CreateServerReq) (*openstack.CreateServerResp, error) {
|
|
// todo: add your logic here and delete this line
|
|
var resp openstack.CreateServerResp
|
|
platform, err := common.GetOpenstackConfWithPlatform(in.Platform)
|
|
token := common.GetToken(in.Platform)
|
|
fmt.Println("错误", err)
|
|
reqByte, err := json.Marshal(in.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
|
|
|
|
}
|