forked from yunionio/cloudmux
/
route_table.go
245 lines (210 loc) · 6.54 KB
/
route_table.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
// Copyright 2019 Yunion
//
// 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 qcloud
import (
"fmt"
"github.com/nyl1001/pkg/errors"
"github.com/nyl1001/pkg/jsonutils"
api "github.com/nyl1001/cloudmux/pkg/apis/compute"
"github.com/nyl1001/cloudmux/pkg/cloudprovider"
"github.com/nyl1001/cloudmux/pkg/multicloud"
)
type SRouteSet struct {
multicloud.SResourceBase
QcloudTags
RouteID int `json:"RouteId"`
RouteItemID string `json:"RouteItemId"`
DestinationIpv6CidrBlock string `json:"DestinationIpv6CidrBlock,omitempty"`
GatewayType string `json:"GatewayType"`
GatewayID string `json:"GatewayId"`
RouteDescription string `json:"RouteDescription"`
DestinationCidrBlock string `json:"DestinationCidrBlock,omitempty"`
RouteType string `json:"RouteType"`
}
type SRouteTableSet struct {
multicloud.SResourceBase
QcloudTags
vpc *SVpc
VpcID string `json:"VpcId"`
RouteTableID string `json:"RouteTableId"`
RouteTableName string `json:"RouteTableName"`
AssociationSet []RouteTableAssociationSet `json:"AssociationSet"`
RouteSet []SRouteSet `json:"RouteSet"`
Main bool `json:"Main"`
CreatedTime string `json:"CreatedTime"`
}
type RouteTableAssociationSet struct {
SubnetId string
RouteTableId string
}
func (self *SRegion) DescribeRouteTables(vpcId string, routetables []string, offset int, limit int) ([]SRouteTableSet, int, error) {
if limit > 50 || limit <= 0 {
limit = 50
}
params := make(map[string]string)
params["Limit"] = fmt.Sprintf("%d", limit)
params["Offset"] = fmt.Sprintf("%d", offset)
for i := range routetables {
params[fmt.Sprintf("RouteTableIds.%d", i)] = routetables[i]
}
if len(vpcId) > 0 {
params["Filters.0.Name"] = "vpc-id"
params["Filters.0.Values.0"] = vpcId
}
body, err := self.vpcRequest("DescribeRouteTables", params)
if err != nil {
return nil, 0, errors.Wrapf(err, ` self.vpcRequest("DescribeRouteTables", %s)`, jsonutils.Marshal(params))
}
routeTables := make([]SRouteTableSet, 0)
err = body.Unmarshal(&routeTables, "RouteTableSet")
if err != nil {
return nil, 0, errors.Wrapf(err, "body.Unmarshal(RouteTableSet)[%s]", body.String())
}
total, _ := body.Float("TotalCount")
return routeTables, int(total), nil
}
func (self *SRegion) GetAllRouteTables(vpcId string, routetables []string) ([]SRouteTableSet, error) {
routeTables := make([]SRouteTableSet, 0)
for {
part, total, err := self.DescribeRouteTables(vpcId, routetables, len(routeTables), 50)
if err != nil {
return nil, errors.Wrap(err, "self.DescribeRouteTables(vpcId, routetables, offset, limit)")
}
routeTables = append(routeTables, part...)
if len(routeTables) >= total {
break
}
}
return routeTables, nil
}
func (self *SRouteTableSet) GetId() string {
return self.RouteTableID
}
func (self *SRouteTableSet) GetName() string {
return self.RouteTableName
}
func (self *SRouteTableSet) GetGlobalId() string {
return self.RouteTableID
}
func (self *SRouteTableSet) GetStatus() string {
return api.ROUTE_TABLE_AVAILABLE
}
func (self *SRouteTableSet) Refresh() error {
return nil
}
func (self *SRouteTableSet) IsEmulated() bool {
return false
}
func (self *SRouteTableSet) GetAssociations() []cloudprovider.RouteTableAssociation {
result := []cloudprovider.RouteTableAssociation{}
for i := range self.AssociationSet {
association := cloudprovider.RouteTableAssociation{
AssociationType: cloudprovider.RouteTableAssociaToSubnet,
AssociatedResourceId: self.AssociationSet[i].SubnetId,
}
result = append(result, association)
}
return result
}
func (self *SRouteTableSet) GetDescription() string {
return ""
}
func (self *SRouteTableSet) GetRegionId() string {
return self.vpc.GetRegion().GetId()
}
func (self *SRouteTableSet) GetVpcId() string {
return self.vpc.GetId()
}
func (self *SRouteTableSet) GetType() cloudprovider.RouteTableType {
if self.Main {
return cloudprovider.RouteTableTypeSystem
}
return cloudprovider.RouteTableTypeCustom
}
func (self *SRouteTableSet) GetIRoutes() ([]cloudprovider.ICloudRoute, error) {
result := []cloudprovider.ICloudRoute{}
for i := range self.RouteSet {
result = append(result, &self.RouteSet[i])
}
return result, nil
}
func (self *SRouteTableSet) CreateRoute(route cloudprovider.RouteSet) error {
return cloudprovider.ErrNotSupported
}
func (self *SRouteTableSet) UpdateRoute(route cloudprovider.RouteSet) error {
return cloudprovider.ErrNotSupported
}
func (self *SRouteTableSet) RemoveRoute(route cloudprovider.RouteSet) error {
return cloudprovider.ErrNotSupported
}
func (self *SRouteSet) GetId() string {
return self.RouteItemID
}
func (self *SRouteSet) GetName() string {
return ""
}
func (self *SRouteSet) GetGlobalId() string {
return self.RouteItemID
}
func (self *SRouteSet) GetStatus() string {
return api.ROUTE_ENTRY_STATUS_AVAILIABLE
}
func (self *SRouteSet) Refresh() error {
return nil
}
func (self *SRouteSet) IsEmulated() bool {
return false
}
func (self *SRouteSet) GetType() string {
switch self.RouteType {
case "USER":
return api.ROUTE_ENTRY_TYPE_CUSTOM
case "NETD":
return api.ROUTE_ENTRY_TYPE_SYSTEM
case "CCN":
return api.ROUTE_ENTRY_TYPE_PROPAGATE
default:
return api.ROUTE_ENTRY_TYPE_SYSTEM
}
}
func (self *SRouteSet) GetCidr() string {
return self.DestinationCidrBlock
}
func (self *SRouteSet) GetNextHopType() string {
switch self.GatewayType {
case "CVM":
return api.NEXT_HOP_TYPE_INSTANCE
case "VPN":
return api.NEXT_HOP_TYPE_VPN
case "DIRECTCONNECT":
return api.NEXT_HOP_TYPE_DIRECTCONNECTION
case "PEERCONNECTION":
return api.NEXT_HOP_TYPE_VPCPEERING
case "SSLVPN":
return api.NEXT_HOP_TYPE_VPN
case "NAT":
return api.NEXT_HOP_TYPE_NAT
case "NORMAL_CVM":
return api.NEXT_HOP_TYPE_INSTANCE
case "EIP":
return api.NEXT_HOP_TYPE_EIP
case "CCN":
return api.NEXT_HOP_TYPE_INTERVPCNETWORK
default:
return ""
}
}
func (self *SRouteSet) GetNextHop() string {
return self.GatewayID
}