diff --git a/bcs-services/bcs-platform-manager/pkg/api/cloudvpc/cloudvpc.go b/bcs-services/bcs-platform-manager/pkg/api/cloudvpc/cloudvpc.go new file mode 100644 index 0000000000..6bc019df65 --- /dev/null +++ b/bcs-services/bcs-platform-manager/pkg/api/cloudvpc/cloudvpc.go @@ -0,0 +1,95 @@ +/* + * Tencent is pleased to support the open source community by making Blueking Container Service available. + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * Licensed under the MIT License (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * http://opensource.org/licenses/MIT + * Unless required by applicable law or agreed to in writing, software distributed under + * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, + * either express or implied. See the License for the specific language governing permissions and + * limitations under the License. + */ + +// Package cloudvpc cloudvpc operate +package cloudvpc + +import ( + "context" + + "github.com/Tencent/bk-bcs/bcs-services/bcs-platform-manager/pkg/component/bcs" +) + +// ListCloudVPCReq list cloud vpc request +type ListCloudVPCReq struct { + CloudID string `json:"cloudID" in:"query=cloudID"` + Region string `json:"region" in:"query=region"` + VpcID string `json:"vpcID" in:"query=vpcID"` + NetworkType string `json:"networkType" in:"query=networkType"` + BusinessID string `json:"businessID" in:"query=businessID"` +} + +// DeleteCloudVPCReq delete cloud vpc request +type DeleteCloudVPCReq struct { + CloudID string `json:"cloudID" in:"query=cloudID"` + VpcID string `json:"vpcID" in:"query=vpcID"` +} + +// ListCloudVPC 获取VPC列表 +// @Summary 获取VPC列表 +// @Tags Logs +// @Produce json +// @Success 200 {array} k8sclient.Container +// @Router /cloudvpc [get] +func ListCloudVPC(c context.Context, req *ListCloudVPCReq) (*[]*bcs.CloudVPC, error) { + vpcs, err := bcs.ListCloudVPC(req.CloudID, req.Region, req.VpcID, req.NetworkType, req.BusinessID) + if err != nil { + return nil, err + } + + return &vpcs, nil +} + +// CreateCloudVPC 创建VPC +// @Summary 创建VPC +// @Tags Logs +// @Produce json +// @Success 200 {array} k8sclient.Container +// @Router /cloudvpc [post] +func CreateCloudVPC(c context.Context, req *bcs.CreateCloudVPCReq) (*bool, error) { + result, err := bcs.CreateCloudVPC(req) + if err != nil { + return nil, err + } + + return &result, nil +} + +// UpdateCloudVPC 更新VPC +// @Summary 更新VPC +// @Tags Logs +// @Produce json +// @Success 200 {array} k8sclient.Container +// @Router /cloudvpc [put] +func UpdateCloudVPC(c context.Context, req *bcs.UpdateCloudVPCReq) (*bcs.CloudVPC, error) { + vpc, err := bcs.UpdateCloudVPC(req) + if err != nil { + return nil, err + } + + return vpc, nil +} + +// DeleteCloudVPC 删除VPC +// @Summary 更新VPC +// @Tags Logs +// @Produce json +// @Success 200 {array} k8sclient.Container +// @Router /cloudvpc [delete] +func DeleteCloudVPC(c context.Context, req *DeleteCloudVPCReq) (*bcs.CloudVPC, error) { + result, err := bcs.DeleteCloudVPC(req.CloudID, req.VpcID) + if err != nil { + return nil, err + } + + return result, nil +} diff --git a/bcs-services/bcs-platform-manager/pkg/api/routes.go b/bcs-services/bcs-platform-manager/pkg/api/routes.go index 62bb17df51..2a1397d19b 100644 --- a/bcs-services/bcs-platform-manager/pkg/api/routes.go +++ b/bcs-services/bcs-platform-manager/pkg/api/routes.go @@ -23,7 +23,9 @@ import ( "github.com/go-chi/chi/v5" httpSwagger "github.com/swaggo/http-swagger" + "github.com/Tencent/bk-bcs/bcs-services/bcs-platform-manager/pkg/api/cloudvpc" "github.com/Tencent/bk-bcs/bcs-services/bcs-platform-manager/pkg/api/pod" + "github.com/Tencent/bk-bcs/bcs-services/bcs-platform-manager/pkg/api/templateconfig" "github.com/Tencent/bk-bcs/bcs-services/bcs-platform-manager/pkg/config" "github.com/Tencent/bk-bcs/bcs-services/bcs-platform-manager/pkg/rest" "github.com/Tencent/bk-bcs/bcs-services/bcs-platform-manager/pkg/rest/middleware" @@ -110,7 +112,6 @@ func (a *APIServer) newRoutes() http.Handler { func registerRoutes() http.Handler { r := chi.NewRouter() // 日志相关接口 - r.Route("/projects/{projectId}/clusters/{clusterId}", func(route chi.Router) { route.Use(middleware.AuthenticationRequired, middleware.ProjectParse, middleware.ClusterAuthorization) route.Use(middleware.Tracing, middleware.Audit) @@ -118,6 +119,22 @@ func registerRoutes() http.Handler { route.Get("/containers", rest.Handle(pod.GetPodContainers)) route.Post("/containers", rest.Handle(pod.CreateContainers)) }) + + // vpc 相关接口 + r.Route("/cloudvpc", func(route chi.Router) { + route.Use(middleware.AuthenticationRequired, middleware.Tracing, middleware.Audit) + + route.Post("/", rest.Handle(cloudvpc.CreateCloudVPC)) + route.Put("/", rest.Handle(cloudvpc.UpdateCloudVPC)) + }) + + // templateconfig 相关接口 + r.Route("/templateconfigs", func(route chi.Router) { + route.Use(middleware.AuthenticationRequired, middleware.Tracing, middleware.Audit) + + route.Post("/", rest.Handle(templateconfig.CreateTemplateConfig)) + route.Delete("/{templateConfigID}", rest.Handle(templateconfig.DeleteTemplateConfig)) + }) return r } diff --git a/bcs-services/bcs-platform-manager/pkg/api/templateconfig/templateconfig.go b/bcs-services/bcs-platform-manager/pkg/api/templateconfig/templateconfig.go new file mode 100644 index 0000000000..80376a76af --- /dev/null +++ b/bcs-services/bcs-platform-manager/pkg/api/templateconfig/templateconfig.go @@ -0,0 +1,81 @@ +/* + * Tencent is pleased to support the open source community by making Blueking Container Service available. + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * Licensed under the MIT License (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * http://opensource.org/licenses/MIT + * Unless required by applicable law or agreed to in writing, software distributed under + * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, + * either express or implied. See the License for the specific language governing permissions and + * limitations under the License. + */ + +// Package templateconfig templateconfig operate +package templateconfig + +import ( + "context" + + "github.com/Tencent/bk-bcs/bcs-services/bcs-platform-manager/pkg/component/bcs" +) + +// DeleteTemplateConfigReq delete template config request +type DeleteTemplateConfigReq struct { + TemplateConfigID string `json:"cloudID" in:"path=templateConfigID"` + BusinessID string `json:"businessID" in:"query=businessID"` + ProjectID string `json:"projectID" in:"query=projectID"` +} + +// ListTemplateConfigReq list template config request +type ListTemplateConfigReq struct { + BusinessID string `json:"businessID" in:"query=businessID"` + ProjectID string `json:"projectID" in:"query=projectID"` + ClusterID string `json:"clusterID" in:"query=clusterID"` + Provider string `json:"provider" in:"query=provider"` + ConfigType string `json:"configType" in:"query=configType"` +} + +// CreateTemplateConfig 创建TemplateConfig +// @Summary 创建TemplateConfig +// @Tags Logs +// @Produce json +// @Success 200 {array} k8sclient.Container +// @Router /templateConfig [post] +func CreateTemplateConfig(c context.Context, req *bcs.CreateTemplateConfigReq) (*bool, error) { + result, err := bcs.CreateTemplateConfig(req) + if err != nil { + return nil, err + } + + return &result, nil +} + +// DeleteTemplateConfig 删除TemplateConfig +// @Summary 删除TemplateConfig +// @Tags Logs +// @Produce json +// @Success 200 {array} k8sclient.Container +// @Router /templateConfig/{templateConfigID} [delete] +func DeleteTemplateConfig(c context.Context, req *DeleteTemplateConfigReq) (*bool, error) { + result, err := bcs.DeleteTemplateConfig(req.TemplateConfigID, req.BusinessID, req.ProjectID) + if err != nil { + return nil, err + } + + return &result, nil +} + +// ListTemplateConfig 获取TemplateConfig列表 +// @Summary 获取TemplateConfig列表 +// @Tags Logs +// @Produce json +// @Success 200 {array} k8sclient.Container +// @Router /templateConfig/{templateConfigID} [get] +func ListTemplateConfig(c context.Context, req *ListTemplateConfigReq) (*[]*bcs.TemplateConfigInfo, error) { + result, err := bcs.ListTemplateConfig(req.BusinessID, req.ProjectID, req.ClusterID, req.Provider, req.ConfigType) + if err != nil { + return nil, err + } + + return &result, nil +} diff --git a/bcs-services/bcs-platform-manager/pkg/component/bcs/bcs.go b/bcs-services/bcs-platform-manager/pkg/component/bcs/bcs.go index 596d7ab633..fc21d5d32d 100644 --- a/bcs-services/bcs-platform-manager/pkg/component/bcs/bcs.go +++ b/bcs-services/bcs-platform-manager/pkg/component/bcs/bcs.go @@ -104,7 +104,7 @@ func ListClusters() { } var result []*Cluster - if err = component.UnmarshalBKResult(resp, &result); err != nil { + if err = component.UnmarshalBKData(resp, &result); err != nil { blog.Errorf("unmarshal clusters error, %s", err.Error()) return } diff --git a/bcs-services/bcs-platform-manager/pkg/component/bcs/cloudvpc.go b/bcs-services/bcs-platform-manager/pkg/component/bcs/cloudvpc.go new file mode 100644 index 0000000000..336423c84f --- /dev/null +++ b/bcs-services/bcs-platform-manager/pkg/component/bcs/cloudvpc.go @@ -0,0 +1,204 @@ +/* + * Tencent is pleased to support the open source community by making Blueking Container Service available. + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * Licensed under the MIT License (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * http://opensource.org/licenses/MIT + * Unless required by applicable law or agreed to in writing, software distributed under + * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, + * either express or implied. See the License for the specific language governing permissions and + * limitations under the License. + */ + +// Package bcs cloudvpc操作 +package bcs + +import ( + "fmt" + + "github.com/Tencent/bk-bcs/bcs-common/common/blog" + + "github.com/Tencent/bk-bcs/bcs-services/bcs-platform-manager/pkg/component" + "github.com/Tencent/bk-bcs/bcs-services/bcs-platform-manager/pkg/config" +) + +// CidrState cidr state +type CidrState struct { + Cidr string `json:"cidr"` + Block bool `json:"block"` +} + +// Cidr cidr +type Cidr struct { + Cidrs []*CidrState `json:"cidrs"` + ReservedIPNum uint32 `json:"reservedIPNum"` + ReservedCidrs []string `json:"reservedCidrs"` +} + +// CloudVPC for cloud vpc +type CloudVPC struct { + CloudID string `json:"cloudID"` + Region string `json:"region"` + RegionName string `json:"regionName"` + NetworkType string `json:"networkType"` + VpcID string `json:"vpcID"` + VpcName string `json:"vpcName"` + Available string `json:"available"` + Extra string `json:"extra"` + Creator string `json:"creator"` + Updater string `json:"updater"` + CreatTime string `json:"creatTime"` + UpdateTime string `json:"updateTime"` + ReservedIPNum uint32 `json:"reservedIPNum"` + BusinessID string `json:"businessID"` + Overlay *Cidr `json:"overlay"` + Underlay *Cidr `json:"underlay"` +} + +// CreateCloudVPCReq create cloud vpc request +type CreateCloudVPCReq struct { + CloudID string `json:"cloudID" validate:"required"` + NetworkType string `json:"networkType"` + Region string `json:"region"` + RegionName string `json:"regionName"` + VpcName string `json:"vpcName"` + VpcID string `json:"vpcID" validate:"required"` + Available string `json:"available"` + Extra string `json:"extra"` + Creator string `json:"creator"` + ReservedIPNum uint32 `json:"reservedIPNum"` + BusinessID string `json:"businessID"` + Overlay *Cidr `json:"overlay"` + Underlay *Cidr `json:"underlay"` +} + +// UpdateCloudVPCReq update cloud vpc request +type UpdateCloudVPCReq struct { + CloudID string `json:"cloudID" validate:"required"` + NetworkType string `json:"networkType"` + Region string `json:"region"` + RegionName string `json:"regionName"` + VpcName string `json:"vpcName"` + VpcID string `json:"vpcID" validate:"required"` + Available string `json:"available"` + Updater string `json:"updater"` + ReservedIPNum uint32 `json:"reservedIPNum"` + BusinessID string `json:"businessID"` + Overlay *Cidr `json:"overlay"` + Underlay *Cidr `json:"underlay"` +} + +// ListCloudVPC 获取cloud vpc列表 +func ListCloudVPC(cloudID, region, vpcID, networkType, businessID string) ([]*CloudVPC, error) { + url := fmt.Sprintf("%s/bcsapi/v4/clustermanager/v1/cloudvpc", config.G.BCS.Host) + + queryParams := make(map[string]string) + if cloudID != "" { + queryParams["cloudID"] = cloudID + } + if region != "" { + queryParams["region"] = region + } + if vpcID != "" { + queryParams["vpcID"] = vpcID + } + if networkType != "" { + queryParams["networkType"] = networkType + } + if businessID != "" { + queryParams["businessID"] = businessID + } + + resp, err := component.GetClient().R(). + SetAuthToken(config.G.BCS.Token). + SetQueryParams(queryParams). + Get(url) + + if err != nil { + blog.Errorf("list cloud vpc error, %s", err.Error()) + return nil, err + } + + var result []*CloudVPC + + fmt.Printf("list cloud vpc response: %s", resp.String()) + if err = component.UnmarshalBKData(resp, &result); err != nil { + blog.Errorf("unmarshal cloud vpc error, %s", err.Error()) + return nil, err + } + + return result, nil +} + +// CreateCloudVPC 创建cloud vpc +func CreateCloudVPC(vpc *CreateCloudVPCReq) (bool, error) { + url := fmt.Sprintf("%s/bcsapi/v4/clustermanager/v1/cloudvpc", config.G.BCS.Host) + + resp, err := component.GetClient().R(). + SetAuthToken(config.G.BCS.Token). + SetBody(vpc). + Post(url) + + if err != nil { + blog.Errorf("create cloud vpc error, %s", err.Error()) + return false, err + } + + var result bool + fmt.Printf("create cloud vpc response: %s", resp.String()) + if err = component.UnmarshalBKResult(resp, &result); err != nil { + blog.Errorf("unmarshal cloud vpc error, %s", err.Error()) + return false, err + } + + return result, nil +} + +// UpdateCloudVPC 更新cloud vpc +func UpdateCloudVPC(req *UpdateCloudVPCReq) (*CloudVPC, error) { + url := fmt.Sprintf("%s/bcsapi/v4/clustermanager/v1/cloudvpc/%s/%s", config.G.BCS.Host, req.CloudID, req.VpcID) + + resp, err := component.GetClient().R(). + SetAuthToken(config.G.BCS.Token). + SetBody(req). + Put(url) + + if err != nil { + blog.Errorf("update cloud vpc error, %s", err.Error()) + return nil, err + } + + var result CloudVPC + + fmt.Printf("update cloud vpc response: %s", resp.String()) + if err = component.UnmarshalBKData(resp, &result); err != nil { + blog.Errorf("unmarshal cloud vpc error, %s", err.Error()) + return nil, err + } + + return &result, nil +} + +// DeleteCloudVPC 删除cloud vpc +func DeleteCloudVPC(cloudID, vpcID string) (*CloudVPC, error) { + url := fmt.Sprintf("%s/bcsapi/v4/clustermanager/v1/cloudvpc/%s/%s", config.G.BCS.Host, cloudID, vpcID) + + resp, err := component.GetClient().R(). + SetAuthToken(config.G.BCS.Token). + Delete(url) + + if err != nil { + blog.Errorf("delete cloud vpc error, %s", err.Error()) + return nil, err + } + + var result CloudVPC + + fmt.Printf("delete cloud vpc response: %s", resp.String()) + if err = component.UnmarshalBKData(resp, &result); err != nil { + blog.Errorf("unmarshal cloud vpc error, %s", err.Error()) + return nil, err + } + + return &result, nil +} diff --git a/bcs-services/bcs-platform-manager/pkg/component/bcs/project.go b/bcs-services/bcs-platform-manager/pkg/component/bcs/project.go index c3c76bb952..8bd062b54f 100644 --- a/bcs-services/bcs-platform-manager/pkg/component/bcs/project.go +++ b/bcs-services/bcs-platform-manager/pkg/component/bcs/project.go @@ -70,7 +70,7 @@ func GetProject(ctx context.Context, bcsConf *config.BCSConf, projectIDOrCode st } project := new(Project) - if err := component.UnmarshalBKResult(resp, project); err != nil { + if err := component.UnmarshalBKData(resp, project); err != nil { return nil, err } diff --git a/bcs-services/bcs-platform-manager/pkg/component/bcs/templateconfig.go b/bcs-services/bcs-platform-manager/pkg/component/bcs/templateconfig.go new file mode 100644 index 0000000000..3750ca74f3 --- /dev/null +++ b/bcs-services/bcs-platform-manager/pkg/component/bcs/templateconfig.go @@ -0,0 +1,163 @@ +/* + * Tencent is pleased to support the open source community by making Blueking Container Service available. + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * Licensed under the MIT License (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * http://opensource.org/licenses/MIT + * Unless required by applicable law or agreed to in writing, software distributed under + * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, + * either express or implied. See the License for the specific language governing permissions and + * limitations under the License. + */ + +// Package bcs templateconfig操作 +package bcs + +import ( + "fmt" + + "github.com/Tencent/bk-bcs/bcs-common/common/blog" + + "github.com/Tencent/bk-bcs/bcs-services/bcs-platform-manager/pkg/component" + "github.com/Tencent/bk-bcs/bcs-services/bcs-platform-manager/pkg/config" +) + +// EnvCidrStep env cidr step +type EnvCidrStep struct { + Env string `json:"env"` + Step uint32 `json:"step"` +} + +// CloudNetworkTemplateConfig cloud network template config +type CloudNetworkTemplateConfig struct { + CidrSteps []*EnvCidrStep `json:"cidrSteps"` + ServiceSteps []uint32 `json:"serviceSteps"` + PerNodePodNum []uint32 `json:"perNodePodNum"` + UnderlaySteps []uint32 `json:"underlaySteps"` + UnderlayAutoSteps []uint32 `json:"underlayAutoSteps"` +} + +// CloudTemplateConfig cloud template config +type CloudTemplateConfig struct { + CloudNetworkTemplateConfig *CloudNetworkTemplateConfig `json:"cloudNetworkTemplateConfig"` +} + +// CreateTemplateConfigReq create template config req +type CreateTemplateConfigReq struct { + BusinessID string `json:"businessID"` + ProjectID string `json:"projectID"` + ClusterID string `json:"clusterID"` + Provider string `json:"provider"` + ConfigType string `json:"configType"` + CloudTemplateConfig *CloudTemplateConfig `json:"cloudTemplateConfig"` +} + +// TemplateConfigInfo template config info +type TemplateConfigInfo struct { + TemplateConfigID string `json:"templateConfigID"` + BusinessID string `json:"businessID"` + ProjectID string `json:"projectID"` + ClusterID string `json:"clusterID"` + Provider string `json:"provider"` + ConfigType string `json:"configType"` + CloudTemplateConfig *CloudTemplateConfig `json:"cloudTemplateConfig"` + Creator string `json:"creator"` + Updater string `json:"updater"` + CreateTime string `json:"createTime"` + UpdateTime string `json:"updateTime"` +} + +// CreateTemplateConfig 创建template config +func CreateTemplateConfig(templateconfig *CreateTemplateConfigReq) (bool, error) { + url := fmt.Sprintf("%s/bcsapi/v4/clustermanager/v1/templateconfigs", config.G.BCS.Host) + + resp, err := component.GetClient().R(). + SetAuthToken(config.G.BCS.Token). + SetBody(templateconfig). + Post(url) + + if err != nil { + blog.Errorf("create template config error, %s", err.Error()) + return false, err + } + + var result bool + fmt.Printf("create template config response: %s", resp.String()) + if err = component.UnmarshalBKResult(resp, &result); err != nil { + blog.Errorf("unmarshal template config error, %s", err.Error()) + return false, err + } + + return result, nil +} + +// ListTemplateConfig 列出template config +func ListTemplateConfig(businessID, projectID, clusterID, provider, configType string) ([]*TemplateConfigInfo, error) { + url := fmt.Sprintf("%s/bcsapi/v4/clustermanager/v1/templateconfigs", config.G.BCS.Host) + + queryParams := make(map[string]string) + if businessID != "" { + queryParams["businessID"] = businessID + } + if projectID != "" { + queryParams["projectID"] = projectID + } + if clusterID != "" { + queryParams["clusterID"] = clusterID + } + if provider != "" { + queryParams["provider"] = provider + } + if configType != "" { + queryParams["configType"] = configType + } + resp, err := component.GetClient().R(). + SetAuthToken(config.G.BCS.Token). + SetQueryParams(queryParams). + Get(url) + + if err != nil { + blog.Errorf("list template config error, %s", err.Error()) + return nil, err + } + + var result []*TemplateConfigInfo + fmt.Printf("list template config response: %s", resp.String()) + if err = component.UnmarshalBKData(resp, &result); err != nil { + blog.Errorf("unmarshal template config error, %s", err.Error()) + return nil, err + } + + return result, nil +} + +// DeleteTemplateConfig 删除template config +func DeleteTemplateConfig(templateConfigID, businessID, projectID string) (bool, error) { + url := fmt.Sprintf("%s/bcsapi/v4/clustermanager/v1/templateconfigs/%s", config.G.BCS.Host, templateConfigID) + + queryParams := make(map[string]string) + if businessID != "" { + queryParams["businessID"] = businessID + } + if projectID != "" { + queryParams["projectID"] = projectID + } + resp, err := component.GetClient().R(). + SetAuthToken(config.G.BCS.Token). + SetQueryParams(queryParams). + Delete(url) + + if err != nil { + blog.Errorf("delete template config error, %s", err.Error()) + return false, err + } + + var result bool + fmt.Printf("delete template config response: %s", resp.String()) + if err = component.UnmarshalBKResult(resp, &result); err != nil { + blog.Errorf("unmarshal template config error, %s", err.Error()) + return false, err + } + + return result, nil +} diff --git a/bcs-services/bcs-platform-manager/pkg/component/client.go b/bcs-services/bcs-platform-manager/pkg/component/client.go index a9ef4e252c..cb9f9ed7bc 100644 --- a/bcs-services/bcs-platform-manager/pkg/component/client.go +++ b/bcs-services/bcs-platform-manager/pkg/component/client.go @@ -258,13 +258,14 @@ func GetBKAPIAuthorization(username string) (string, error) { // BKResult 蓝鲸返回规范的结构体 type BKResult struct { - Code interface{} `json:"code"` - Message string `json:"message"` - Data interface{} `json:"data"` + Code any `json:"code"` + Message string `json:"message"` + Data any `json:"data"` + Result any `json:"result"` } -// UnmarshalBKResult 反序列化为蓝鲸返回规范 -func UnmarshalBKResult(resp *resty.Response, data interface{}) error { +// UnmarshalBKData 反序列化为蓝鲸返回规范 +func UnmarshalBKData(resp *resty.Response, data any) error { if resp.StatusCode() != http.StatusOK { return errors.Errorf("http code %d != 200", resp.StatusCode()) } @@ -282,6 +283,25 @@ func UnmarshalBKResult(resp *resty.Response, data interface{}) error { return nil } +// UnmarshalBKResult 反序列化为蓝鲸返回规范 +func UnmarshalBKResult(resp *resty.Response, result any) error { + if resp.StatusCode() != http.StatusOK { + return errors.Errorf("http code %d != 200", resp.StatusCode()) + } + + // 部分接口,如 usermanager 返回的content-type不是json, 需要手动Unmarshal + bkResult := &BKResult{Result: result} + if err := json.Unmarshal(resp.Body(), bkResult); err != nil { + return err + } + + if err := bkResult.ValidateCode(); err != nil { + return err + } + + return nil +} + // ValidateCode 返回结果是否OK func (r *BKResult) ValidateCode() error { var resultCode int @@ -302,7 +322,7 @@ func (r *BKResult) ValidateCode() error { } if resultCode != 0 { - return errors.Errorf("resp code %d != 0, %s", resultCode, r.Message) + return errors.Errorf("%s", r.Message) } return nil }