yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/hcs/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 hcs 16 17 import ( 18 "fmt" 19 20 "yunion.io/x/jsonutils" 21 "yunion.io/x/pkg/errors" 22 23 api "yunion.io/x/cloudmux/pkg/apis/compute" 24 "yunion.io/x/cloudmux/pkg/cloudprovider" 25 "yunion.io/x/cloudmux/pkg/multicloud" 26 ) 27 28 type SVpc struct { 29 multicloud.SVpc 30 HcsTags 31 32 region *SRegion 33 34 Id string 35 Name string 36 CIDR string 37 Status string 38 EnterpriseProjectId string 39 } 40 41 func (self *SRegion) GetVpcs() ([]SVpc, error) { 42 ret := []SVpc{} 43 return ret, self.vpcList("vpcs", nil, &ret) 44 } 45 46 func (self *SVpc) GetId() string { 47 return self.Id 48 } 49 50 func (self *SVpc) GetName() string { 51 if len(self.Name) > 0 { 52 return self.Name 53 } 54 return self.Id 55 } 56 57 func (self *SVpc) GetGlobalId() string { 58 return self.Id 59 } 60 61 func (self *SVpc) GetStatus() string { 62 return api.VPC_STATUS_AVAILABLE 63 } 64 65 func (self *SVpc) Refresh() error { 66 ret, err := self.region.GetVpc(self.GetId()) 67 if err != nil { 68 return err 69 } 70 return jsonutils.Update(self, ret) 71 } 72 73 func (self *SVpc) GetRegion() cloudprovider.ICloudRegion { 74 return self.region 75 } 76 77 func (self *SVpc) GetIsDefault() bool { 78 return false 79 } 80 81 func (self *SVpc) GetCidrBlock() string { 82 return self.CIDR 83 } 84 85 func (self *SVpc) GetIWires() ([]cloudprovider.ICloudWire, error) { 86 wire := &SWire{region: self.region, vpc: self} 87 return []cloudprovider.ICloudWire{wire}, nil 88 } 89 90 func (self *SVpc) GetIWireById(id string) (cloudprovider.ICloudWire, error) { 91 wires, err := self.GetIWires() 92 if err != nil { 93 return nil, err 94 } 95 for i := range wires { 96 if wires[i].GetGlobalId() == id { 97 return wires[i], nil 98 } 99 } 100 return nil, cloudprovider.ErrNotFound 101 } 102 103 func (self *SVpc) GetISecurityGroups() ([]cloudprovider.ICloudSecurityGroup, error) { 104 secgroups, err := self.region.GetSecurityGroups(self.Id) 105 if err != nil { 106 return nil, err 107 } 108 ret := []cloudprovider.ICloudSecurityGroup{} 109 for i := range secgroups { 110 secgroups[i].region = self.region 111 ret = append(ret, &secgroups[i]) 112 } 113 return ret, nil 114 } 115 116 func (self *SVpc) GetIRouteTables() ([]cloudprovider.ICloudRouteTable, error) { 117 rtbs, err := self.region.GetRouteTables(self.Id) 118 if err != nil { 119 return nil, err 120 } 121 ret := []cloudprovider.ICloudRouteTable{} 122 for i := range rtbs { 123 rtbs[i].vpc = self 124 ret = append(ret, &rtbs[i]) 125 } 126 return ret, nil 127 } 128 129 func (self *SVpc) GetIRouteTableById(routeTableId string) (cloudprovider.ICloudRouteTable, error) { 130 rtb, err := self.region.GetRouteTable(routeTableId) 131 if err != nil { 132 return nil, err 133 } 134 rtb.vpc = self 135 return rtb, nil 136 } 137 138 func (self *SVpc) Delete() error { 139 return self.region.DeleteVpc(self.GetId()) 140 } 141 142 func (self *SVpc) GetINatGateways() ([]cloudprovider.ICloudNatGateway, error) { 143 nats, err := self.region.GetNatGateways(self.GetId()) 144 if err != nil { 145 return nil, err 146 } 147 ret := make([]cloudprovider.ICloudNatGateway, len(nats)) 148 for i := 0; i < len(nats); i++ { 149 nats[i].region = self.region 150 ret[i] = &nats[i] 151 } 152 return ret, nil 153 } 154 155 func (self *SVpc) GetICloudVpcPeeringConnections() ([]cloudprovider.ICloudVpcPeeringConnection, error) { 156 svpcPCs, err := self.getVpcPeeringConnections() 157 if err != nil { 158 return nil, errors.Wrap(err, "self.getVpcPeeringConnections()") 159 } 160 ivpcPCs := []cloudprovider.ICloudVpcPeeringConnection{} 161 for i := range svpcPCs { 162 ivpcPCs = append(ivpcPCs, &svpcPCs[i]) 163 } 164 return ivpcPCs, nil 165 } 166 167 func (self *SVpc) GetICloudAccepterVpcPeeringConnections() ([]cloudprovider.ICloudVpcPeeringConnection, error) { 168 svpcPCs, err := self.getAccepterVpcPeeringConnections() 169 if err != nil { 170 return nil, errors.Wrap(err, "self.getAccepterVpcPeeringConnections()") 171 } 172 ivpcPCs := []cloudprovider.ICloudVpcPeeringConnection{} 173 for i := range svpcPCs { 174 ivpcPCs = append(ivpcPCs, &svpcPCs[i]) 175 } 176 return ivpcPCs, nil 177 } 178 179 func (self *SVpc) GetICloudVpcPeeringConnectionById(id string) (cloudprovider.ICloudVpcPeeringConnection, error) { 180 svpcPC, err := self.getVpcPeeringConnectionById(id) 181 if err != nil { 182 return nil, errors.Wrapf(err, "self.getVpcPeeringConnectionById(%s)", id) 183 } 184 return svpcPC, nil 185 } 186 187 func (self *SVpc) CreateICloudVpcPeeringConnection(opts *cloudprovider.VpcPeeringConnectionCreateOptions) (cloudprovider.ICloudVpcPeeringConnection, error) { 188 svpcPC, err := self.region.CreateVpcPeering(self.GetId(), opts) 189 if err != nil { 190 return nil, errors.Wrapf(err, "self.region.CreateVpcPeering(%s,%s)", self.GetId(), jsonutils.Marshal(opts).String()) 191 } 192 svpcPC.vpc = self 193 return svpcPC, nil 194 } 195 196 func (self *SVpc) AcceptICloudVpcPeeringConnection(id string) error { 197 vpcPC, err := self.getVpcPeeringConnectionById(id) 198 if err != nil { 199 return errors.Wrapf(err, "self.getVpcPeeringConnectionById(%s)", id) 200 } 201 if vpcPC.GetStatus() == api.VPC_PEERING_CONNECTION_STATUS_ACTIVE { 202 return nil 203 } 204 if vpcPC.GetStatus() == api.VPC_PEERING_CONNECTION_STATUS_UNKNOWN { 205 return errors.Wrapf(cloudprovider.ErrInvalidStatus, "vpcPC: %s", jsonutils.Marshal(vpcPC).String()) 206 } 207 err = self.region.AcceptVpcPeering(id) 208 if err != nil { 209 return errors.Wrapf(err, "self.region.AcceptVpcPeering(%s)", id) 210 } 211 return nil 212 } 213 214 func (self *SVpc) GetAuthorityOwnerId() string { 215 return self.region.client.projectId 216 } 217 218 func (self *SRegion) GetVpc(id string) (*SVpc, error) { 219 vpc := &SVpc{region: self} 220 res := fmt.Sprintf("vpcs/%s", id) 221 return vpc, self.vpcGet(res, vpc) 222 } 223 224 func (self *SRegion) DeleteVpc(id string) error { 225 if id != "default" { 226 secgroups, err := self.GetSecurityGroups(id) 227 if err != nil { 228 return errors.Wrap(err, "GetSecurityGroups") 229 } 230 for _, secgroup := range secgroups { 231 err = self.DeleteSecurityGroup(secgroup.Id) 232 if err != nil { 233 return errors.Wrapf(err, "DeleteSecurityGroup(%s)", secgroup.Id) 234 } 235 } 236 } 237 res := fmt.Sprintf("vpcs/%s", id) 238 return self.vpcDelete(res) 239 } 240 241 func (self *SRegion) CreateVpc(name, cidr, desc string) (*SVpc, error) { 242 params := map[string]interface{}{ 243 "vpc": map[string]string{ 244 "name": name, 245 "cidr": cidr, 246 "description": desc, 247 }, 248 } 249 vpc := &SVpc{region: self} 250 return vpc, self.vpcCreate("vpcs", params, vpc) 251 } 252 253 func (self *SRegion) GetIVpcs() ([]cloudprovider.ICloudVpc, error) { 254 vpcs, err := self.GetVpcs() 255 if err != nil { 256 return nil, err 257 } 258 ret := []cloudprovider.ICloudVpc{} 259 for i := range vpcs { 260 vpcs[i].region = self 261 ret = append(ret, &vpcs[i]) 262 } 263 return ret, nil 264 } 265 266 func (self *SRegion) GetIVpcById(id string) (cloudprovider.ICloudVpc, error) { 267 vpc, err := self.GetVpc(id) 268 if err != nil { 269 return nil, err 270 } 271 vpc.region = self 272 return vpc, nil 273 } 274 275 func (self *SVpc) getVpcPeeringConnections() ([]SVpcPeering, error) { 276 svpcPeerings, err := self.region.GetVpcPeerings(self.GetId()) 277 if err != nil { 278 return nil, errors.Wrapf(err, "self.region.GetVpcPeerings(%s)", self.GetId()) 279 } 280 vpcPCs := []SVpcPeering{} 281 for i := range svpcPeerings { 282 if svpcPeerings[i].GetVpcId() == self.GetId() { 283 svpcPeerings[i].vpc = self 284 vpcPCs = append(vpcPCs, svpcPeerings[i]) 285 } 286 } 287 return vpcPCs, nil 288 } 289 290 func (self *SVpc) getAccepterVpcPeeringConnections() ([]SVpcPeering, error) { 291 svpcPeerings, err := self.region.GetVpcPeerings(self.GetId()) 292 if err != nil { 293 return nil, errors.Wrapf(err, "self.region.GetVpcPeerings(%s)", self.GetId()) 294 } 295 vpcPCs := []SVpcPeering{} 296 for i := range svpcPeerings { 297 if svpcPeerings[i].GetPeerVpcId() == self.GetId() { 298 svpcPeerings[i].vpc = self 299 vpcPCs = append(vpcPCs, svpcPeerings[i]) 300 } 301 } 302 return vpcPCs, nil 303 } 304 305 func (self *SVpc) getVpcPeeringConnectionById(id string) (*SVpcPeering, error) { 306 svpcPC, err := self.region.GetVpcPeering(id) 307 if err != nil { 308 return nil, errors.Wrapf(err, "self.region.GetVpcPeering(%s)", id) 309 } 310 svpcPC.vpc = self 311 return svpcPC, nil 312 }