yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/ctyun/vpc.go (about) 1 // Copyright 2019 Yunion 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package ctyun 16 17 import ( 18 "yunion.io/x/jsonutils" 19 "yunion.io/x/pkg/errors" 20 21 api "yunion.io/x/cloudmux/pkg/apis/compute" 22 "yunion.io/x/cloudmux/pkg/cloudprovider" 23 "yunion.io/x/cloudmux/pkg/multicloud" 24 ) 25 26 type SVpc struct { 27 multicloud.SVpc 28 CtyunTags 29 30 region *SRegion 31 32 iwires []cloudprovider.ICloudWire 33 secgroups []cloudprovider.ICloudSecurityGroup 34 35 ResVpcID string `json:"resVpcId"` 36 Name string `json:"name"` 37 CIDR string `json:"cidr"` 38 ZoneID string `json:"zoneId"` 39 ZoneName string `json:"zoneName"` 40 VpcStatus string `json:"vpcStatus"` 41 RegionID string `json:"regionId"` 42 CreateDate int64 `json:"createDate"` 43 } 44 45 func (self *SVpc) addWire(wire *SWire) { 46 if self.iwires == nil { 47 self.iwires = make([]cloudprovider.ICloudWire, 0) 48 } 49 self.iwires = append(self.iwires, wire) 50 } 51 52 func (self *SVpc) GetId() string { 53 return self.ResVpcID 54 } 55 56 func (self *SVpc) GetName() string { 57 if len(self.Name) > 0 { 58 return self.Name 59 } 60 return self.ResVpcID 61 } 62 63 func (self *SVpc) GetGlobalId() string { 64 return self.GetId() 65 } 66 67 func (self *SVpc) GetStatus() string { 68 return api.VPC_STATUS_AVAILABLE 69 } 70 71 func (self *SVpc) Refresh() error { 72 new, err := self.region.GetVpc(self.GetId()) 73 if err != nil { 74 return err 75 } 76 return jsonutils.Update(self, new) 77 } 78 79 func (self *SVpc) IsEmulated() bool { 80 return false 81 } 82 83 func (self *SVpc) GetRegion() cloudprovider.ICloudRegion { 84 return self.region 85 } 86 87 func (self *SVpc) GetIsDefault() bool { 88 return false 89 } 90 91 func (self *SVpc) GetCidrBlock() string { 92 return self.CIDR 93 } 94 95 func (self *SVpc) GetIWires() ([]cloudprovider.ICloudWire, error) { 96 if self.iwires == nil { 97 err := self.fetchNetworks() 98 if err != nil { 99 return nil, err 100 } 101 } 102 return self.iwires, nil 103 } 104 105 // http://ctyun-api-url/apiproxy/v3/getSecurityGroupRules 106 // http://ctyun-api-url/apiproxy/v3/getSecurityGroups 107 func (self *SVpc) GetISecurityGroups() ([]cloudprovider.ICloudSecurityGroup, error) { 108 if self.secgroups == nil { 109 err := self.fetchSecurityGroups() 110 if err != nil { 111 return nil, err 112 } 113 } 114 return self.secgroups, nil 115 } 116 117 func (self *SVpc) GetIRouteTables() ([]cloudprovider.ICloudRouteTable, error) { 118 rts := []cloudprovider.ICloudRouteTable{} 119 return rts, nil 120 } 121 122 func (self *SVpc) GetIRouteTableById(routeTableId string) (cloudprovider.ICloudRouteTable, error) { 123 return nil, cloudprovider.ErrNotSupported 124 } 125 126 func (self *SVpc) Delete() error { 127 return self.region.DeleteVpc(self.GetId()) 128 } 129 130 func (self *SVpc) GetIWireById(wireId string) (cloudprovider.ICloudWire, error) { 131 if self.iwires == nil { 132 err := self.fetchNetworks() 133 if err != nil { 134 return nil, err 135 } 136 } 137 for i := 0; i < len(self.iwires); i += 1 { 138 if self.iwires[i].GetGlobalId() == wireId { 139 return self.iwires[i], nil 140 } 141 } 142 return nil, cloudprovider.ErrNotFound 143 } 144 145 func (self *SVpc) fetchSecurityGroups() error { 146 secgroups, err := self.region.GetSecurityGroups("") 147 if err != nil { 148 return err 149 } 150 151 self.secgroups = make([]cloudprovider.ICloudSecurityGroup, len(secgroups)) 152 for i := 0; i < len(secgroups); i++ { 153 self.secgroups[i] = &secgroups[i] 154 } 155 156 return nil 157 } 158 159 func (self *SVpc) getWireByRegionId(regionId string) *SWire { 160 if len(regionId) == 0 { 161 return nil 162 } 163 164 for i := 0; i < len(self.iwires); i++ { 165 wire := self.iwires[i].(*SWire) 166 167 if wire.region.GetId() == regionId { 168 return wire 169 } 170 } 171 172 return nil 173 } 174 175 func (self *SVpc) fetchNetworks() error { 176 networks, err := self.region.GetNetwroks(self.GetId()) 177 if err != nil { 178 return err 179 } 180 181 if len(networks) == 0 { 182 self.iwires = []cloudprovider.ICloudWire{&SWire{region: self.region, vpc: self}} 183 return nil 184 } 185 186 for i := 0; i < len(networks); i += 1 { 187 wire := self.getWireByRegionId(self.region.GetId()) 188 networks[i].wire = wire 189 wire.addNetwork(&networks[i]) 190 } 191 192 return nil 193 } 194 195 func (self *SRegion) GetVpc(vpcId string) (*SVpc, error) { 196 params := map[string]string{ 197 "vpcId": vpcId, 198 "regionId": self.GetId(), 199 } 200 201 vpc := &SVpc{} 202 resp, err := self.client.DoGet("/apiproxy/v3/queryVPCDetail", params) 203 if err != nil { 204 return nil, errors.Wrap(err, "SRegion.GetVpc.DoGet") 205 } 206 207 if id, _ := resp.GetString("returnObj", "resVpcId"); len(id) == 0 { 208 return nil, errors.Wrap(cloudprovider.ErrNotFound, "SRegion.GetVpc.GetID") 209 } 210 211 err = resp.Unmarshal(vpc, "returnObj") 212 if err != nil { 213 return nil, errors.Wrap(err, "SRegion.GetVpc.Unmarshal") 214 } 215 216 vpc.region = self 217 return vpc, nil 218 } 219 220 func (self *SRegion) DeleteVpc(vpcId string) error { 221 params := map[string]jsonutils.JSONObject{ 222 "regionId": jsonutils.NewString(self.GetId()), 223 "vpcId": jsonutils.NewString(vpcId), 224 } 225 226 _, err := self.client.DoPost("/apiproxy/v3/deleteVPC", params) 227 if err != nil { 228 return errors.Wrap(err, "SRegion.DeleteVpc.DoPost") 229 } 230 231 return nil 232 }