61 lines
1.6 KiB
Go
61 lines
1.6 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"
|
|
|
|
"k8s.io/apimachinery/pkg/util/json"
|
|
"strings"
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
)
|
|
|
|
type UpdateServerLogic struct {
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
logx.Logger
|
|
}
|
|
|
|
func NewUpdateServerLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateServerLogic {
|
|
return &UpdateServerLogic{
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
Logger: logx.WithContext(ctx),
|
|
}
|
|
}
|
|
|
|
func (l *UpdateServerLogic) UpdateServer(in *openstack.UpdateServerReq) (*openstack.UpdateServerResp, error) {
|
|
// todo: add your logic here and delete this line
|
|
var resp openstack.UpdateServerResp
|
|
platform, err := common.GetOpenstackConfWithPlatform(in.Platform)
|
|
openstackUrl := platform.OpenstackComputeUrl
|
|
println(in.ServerId)
|
|
reqByte, err := json.Marshal(in.ServerUpdate)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
payload := strings.NewReader(string(reqByte))
|
|
token := common.GetToken(in.Platform)
|
|
statusCode, body, err := httputils.HttpClientWithBodyAndCode(httputils.PUT, openstackUrl+"/servers/"+in.ServerId, payload, token)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if statusCode == 200 {
|
|
err := json.Unmarshal(body, &resp)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
}
|
|
resp.Code = 200
|
|
resp.Msg = "Success"
|
|
} else if statusCode != 200 {
|
|
json.Unmarshal(body, &resp)
|
|
resp.Code = 400
|
|
resp.Msg = "Failure"
|
|
}
|
|
return &resp, nil
|
|
}
|