mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-09-19 02:37:24 +08:00
fix: optimized huawei security group get and create operation (#7522)
Co-authored-by: Qu Xuan <quxuan@yunionyun.com>
This commit is contained in:
@@ -141,17 +141,13 @@ func (ce *HuaweiClientError) Error() string {
|
||||
}
|
||||
|
||||
func (ce *HuaweiClientError) ParseErrorFromJsonResponse(statusCode int, body jsonutils.JSONObject) error {
|
||||
err := body.Unmarshal(ce)
|
||||
if err != nil {
|
||||
ce.err = errors.Wrapf(err, "body.Unmarshal(%s)", body.String())
|
||||
ce.Code = statusCode
|
||||
ce.Details = body.String()
|
||||
return ce
|
||||
if body != nil {
|
||||
body.Unmarshal(ce)
|
||||
}
|
||||
if ce.Code == 0 {
|
||||
ce.Code = statusCode
|
||||
}
|
||||
if len(ce.Details) == 0 {
|
||||
if len(ce.Details) == 0 && body != nil {
|
||||
ce.Details = body.String()
|
||||
}
|
||||
return ce
|
||||
@@ -182,10 +178,6 @@ func (self *SBaseManager) jsonRequest(request requests.IRequest) (http.Header, j
|
||||
}
|
||||
}
|
||||
|
||||
if self.debug {
|
||||
log.Debugf("url: %s", request.BuildUrl())
|
||||
}
|
||||
|
||||
client := httputils.NewJsonClient(self.httpClient)
|
||||
req := httputils.NewJsonRequest(httputils.THttpMethod(request.GetMethod()), request.BuildUrl(), jsonBody)
|
||||
req.SetHeader(header)
|
||||
@@ -196,12 +188,11 @@ func (self *SBaseManager) jsonRequest(request requests.IRequest) (http.Header, j
|
||||
for {
|
||||
h, b, e := client.Send(ctx, req, resp, self.debug)
|
||||
if e == nil {
|
||||
if self.debug {
|
||||
log.Debugf("response: %s body: %s", h, b)
|
||||
}
|
||||
return h, b, e
|
||||
return h, b, nil
|
||||
}
|
||||
|
||||
log.Errorf("[%s] %s body: %v error: %v", req.GetHttpMethod(), req.GetUrl(), jsonBody, e)
|
||||
|
||||
switch err := e.(type) {
|
||||
case *HuaweiClientError:
|
||||
if (err.Code == 499 || err.Code == 429) && retry > 0 && request.GetMethod() == "GET" {
|
||||
|
||||
@@ -537,16 +537,19 @@ func (self *SRegion) CreateISecurityGroup(conf *cloudprovider.SecurityGroupCreat
|
||||
|
||||
// https://support.huaweicloud.com/api-vpc/zh-cn_topic_0020090608.html
|
||||
func (self *SRegion) CreateIVpc(name string, desc string, cidr string) (cloudprovider.ICloudVpc, error) {
|
||||
params := jsonutils.NewDict()
|
||||
vpcObj := jsonutils.NewDict()
|
||||
vpcObj.Add(jsonutils.NewString(name), "name")
|
||||
vpcObj.Add(jsonutils.NewString(cidr), "cidr")
|
||||
params.Add(vpcObj, "vpc")
|
||||
return self.CreateVpc(name, cidr, desc)
|
||||
}
|
||||
|
||||
vpc := SVpc{}
|
||||
err := DoCreate(self.ecsClient.Vpcs.Create, params, &vpc)
|
||||
vpc.region = self
|
||||
return &vpc, err
|
||||
func (self *SRegion) CreateVpc(name, cidr, desc string) (*SVpc, error) {
|
||||
params := map[string]interface{}{
|
||||
"vpc": map[string]string{
|
||||
"name": name,
|
||||
"cidr": cidr,
|
||||
"description": desc,
|
||||
},
|
||||
}
|
||||
vpc := &SVpc{region: self}
|
||||
return vpc, DoCreate(self.ecsClient.Vpcs.Create, jsonutils.Marshal(params), vpc)
|
||||
}
|
||||
|
||||
// https://support.huaweicloud.com/api-vpc/zh-cn_topic_0020090596.html
|
||||
|
||||
@@ -31,6 +31,7 @@ import (
|
||||
"yunion.io/x/jsonutils"
|
||||
"yunion.io/x/pkg/errors"
|
||||
"yunion.io/x/pkg/util/secrules"
|
||||
"yunion.io/x/pkg/utils"
|
||||
|
||||
api "yunion.io/x/onecloud/pkg/apis/compute"
|
||||
"yunion.io/x/onecloud/pkg/cloudprovider"
|
||||
@@ -227,7 +228,7 @@ func (self *SRegion) GetSecurityGroupDetails(secGroupId string) (*SSecurityGroup
|
||||
// https://support.huaweicloud.com/api-vpc/zh-cn_topic_0020090617.html
|
||||
func (self *SRegion) GetSecurityGroups(vpcId string, name string) ([]SSecurityGroup, error) {
|
||||
querys := map[string]string{}
|
||||
if len(vpcId) > 0 {
|
||||
if len(vpcId) > 0 && !utils.IsInStringArray(vpcId, []string{"default", api.NORMAL_VPC_ID}) { // vpc_id = default or normal 时报错 '{"code":"VPC.0601","message":"Query security groups error vpcId is invalid."}'
|
||||
querys["vpc_id"] = vpcId
|
||||
}
|
||||
|
||||
|
||||
@@ -30,4 +30,28 @@ func init() {
|
||||
printList(vpcs, 0, 0, 0, nil)
|
||||
return nil
|
||||
})
|
||||
|
||||
type VpcCreateOptions struct {
|
||||
NAME string
|
||||
CIDR string
|
||||
Desc string
|
||||
}
|
||||
|
||||
shellutils.R(&VpcCreateOptions{}, "vpc-create", "Create vpc", func(cli *huawei.SRegion, args *VpcCreateOptions) error {
|
||||
vpc, err := cli.CreateVpc(args.NAME, args.CIDR, args.Desc)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
printObject(vpc)
|
||||
return nil
|
||||
})
|
||||
|
||||
type VpcIdOption struct {
|
||||
ID string
|
||||
}
|
||||
|
||||
shellutils.R(&VpcIdOption{}, "vpc-delete", "Delete vpc", func(cli *huawei.SRegion, args *VpcIdOption) error {
|
||||
return cli.DeleteVpc(args.ID)
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
@@ -225,6 +225,18 @@ func (self *SRegion) getVpc(vpcId string) (*SVpc, error) {
|
||||
}
|
||||
|
||||
func (self *SRegion) DeleteVpc(vpcId string) error {
|
||||
if vpcId != "default" {
|
||||
secgroups, err := self.GetSecurityGroups(vpcId, "")
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "GetSecurityGroups")
|
||||
}
|
||||
for _, secgroup := range secgroups {
|
||||
err = self.DeleteSecurityGroup(secgroup.ID)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "DeleteSecurityGroup(%s)", secgroup.ID)
|
||||
}
|
||||
}
|
||||
}
|
||||
return DoDelete(self.ecsClient.Vpcs.Delete, vpcId, nil, nil)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user