forked from kubernetes/autoscaler
/
subnet.go
147 lines (130 loc) · 3.88 KB
/
subnet.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
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 vpc
import (
"bytes"
"encoding/json"
"fmt"
"k8s.io/autoscaler/cluster-autoscaler/cloudprovider/baiducloud/baiducloud-sdk-go/bce"
)
// Subnet define subnet of vpc
type Subnet struct {
SubnetID string `json:"subnetId"`
Name string `json:"name"`
ZoneName string `json:"zoneName"`
Cidr string `json:"cidr"`
VpcID string `json:"vpcId"`
SubnetType string `json:"subnetType"`
Description string `json:"description"`
}
// CreateSubnetArgs define args create a subnet
type CreateSubnetArgs struct {
Name string `json:"name"`
ZoneName string `json:"zoneName"`
Cidr string `json:"cidr"`
VpcID string `json:"vpcId"`
SubnetType string `json:"subnetType,omitempty"`
Description string `json:"description,omitempty"`
}
// CreateSubnetResponse define response of creating a subnet
type CreateSubnetResponse struct {
SubnetID string `json:"subnetId"`
}
// ListSubnetResponse json
type ListSubnetResponse struct {
Marker string `json:"marker"`
IsTruncated bool `json:"isTruncated"`
NextMarker string `json:"nextMarker"`
MaxKeys int `json:"maxKeys"`
Subnets []*Subnet `json:"subnets"`
}
// DescribeSubnetResponse json
type DescribeSubnetResponse struct {
Subnet *Subnet `json:"subnet"`
}
// CreateSubnet 在VPC中创建子网
func (c *Client) CreateSubnet(args *CreateSubnetArgs) (string, error) {
if args == nil {
return "", fmt.Errorf("CreateSubnet failed: CreateSubnetArgs is nil")
}
params := map[string]string{
"clientToken": c.GenerateClientToken(),
}
postContent, err := json.Marshal(args)
if err != nil {
return "", err
}
req, err := bce.NewRequest("POST", c.GetURL("v1/subnet", params), bytes.NewBuffer(postContent))
if err != nil {
return "", err
}
resp, err := c.SendRequest(req, nil)
if err != nil {
return "", err
}
bodyContent, err := resp.GetBodyContent()
if err != nil {
return "", err
}
var createSubnetResponse *CreateSubnetResponse
err = json.Unmarshal(bodyContent, &createSubnetResponse)
if err != nil {
return "", err
}
return createSubnetResponse.SubnetID, nil
}
// ListSubnet 查询指定VPC的所有子网列表信息
func (c *Client) ListSubnet(params map[string]string) ([]*Subnet, error) {
req, err := bce.NewRequest("GET", c.GetURL("v1/subnet", params), nil)
if err != nil {
return nil, err
}
resp, err := c.SendRequest(req, nil)
if err != nil {
return nil, err
}
bodyContent, err := resp.GetBodyContent()
if err != nil {
return nil, err
}
var listSubnetResponse *ListSubnetResponse
err = json.Unmarshal(bodyContent, &listSubnetResponse)
if err != nil {
return nil, err
}
return listSubnetResponse.Subnets, nil
}
// DescribeSubnet 查询指定子网的详细信息
func (c *Client) DescribeSubnet(subnetId string) (*Subnet, error) {
if len(subnetId) == 0 {
return nil, fmt.Errorf("DescribeSubnet failed, subnetId must not be empty")
}
req, err := bce.NewRequest("GET", c.GetURL("v1/subnet/"+subnetId, nil), nil)
if err != nil {
return nil, err
}
resp, err := c.SendRequest(req, nil)
if err != nil {
return nil, err
}
bodyContent, err := resp.GetBodyContent()
if err != nil {
return nil, err
}
var describeSubnetResponse *DescribeSubnetResponse
err = json.Unmarshal(bodyContent, &describeSubnetResponse)
if err != nil {
return nil, err
}
return describeSubnetResponse.Subnet, nil
}