Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 95 additions & 0 deletions bcs-services/bcs-platform-manager/pkg/api/cloudvpc/cloudvpc.go
Original file line number Diff line number Diff line change
@@ -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
}
8 changes: 8 additions & 0 deletions bcs-services/bcs-platform-manager/pkg/api/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ 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/config"
"github.com/Tencent/bk-bcs/bcs-services/bcs-platform-manager/pkg/rest"
Expand Down Expand Up @@ -118,6 +119,13 @@ func registerRoutes() http.Handler {
route.Get("/containers", rest.Handle(pod.GetPodContainers))
route.Post("/containers", rest.Handle(pod.CreateContainers))
})

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))
})
return r
}

Expand Down
2 changes: 1 addition & 1 deletion bcs-services/bcs-platform-manager/pkg/component/bcs/bcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
204 changes: 204 additions & 0 deletions bcs-services/bcs-platform-manager/pkg/component/bcs/cloudvpc.go
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
32 changes: 26 additions & 6 deletions bcs-services/bcs-platform-manager/pkg/component/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand All @@ -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
Expand All @@ -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
}
Expand Down
Loading