60 lines
1.5 KiB
Go
60 lines
1.5 KiB
Go
package logic
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"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"
|
|
)
|
|
|
|
type CreateSubnetLogic struct {
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
logx.Logger
|
|
}
|
|
|
|
func NewCreateSubnetLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateSubnetLogic {
|
|
return &CreateSubnetLogic{
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
Logger: logx.WithContext(ctx),
|
|
}
|
|
}
|
|
|
|
func (l *CreateSubnetLogic) CreateSubnet(in *openstack.CreateSubnetReq) (*openstack.CreateSubnetResp, error) {
|
|
// todo: add your logic here and delete this line
|
|
var resp openstack.CreateSubnetResp
|
|
platform, err := common.GetOpenstackConfWithPlatform(in.Platform)
|
|
reqByte, err := json.Marshal(in)
|
|
token := common.GetToken(in.Platform)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
payload := strings.NewReader(string(reqByte))
|
|
openstackUrl := platform.OpenstackNetworkUrl
|
|
statusCode, body, err := httputils.HttpClientWithBodyAndCode(httputils.POST, openstackUrl+"/v2.0/subnets", payload, token)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if statusCode == 201 {
|
|
err := json.Unmarshal(body, &resp)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
}
|
|
resp.Code = 200
|
|
resp.Msg = "Success"
|
|
} else if statusCode != 201 {
|
|
json.Unmarshal(body, &resp)
|
|
resp.Code = 400
|
|
resp.Msg = "Failure"
|
|
}
|
|
return &resp, nil
|
|
}
|