yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/bingocloud/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 bingocloud 16 17 import ( 18 "yunion.io/x/pkg/errors" 19 20 api "yunion.io/x/cloudmux/pkg/apis/compute" 21 "yunion.io/x/cloudmux/pkg/cloudprovider" 22 "yunion.io/x/cloudmux/pkg/multicloud" 23 ) 24 25 type SVpc struct { 26 multicloud.SVpc 27 multicloud.STagBase 28 29 region *SRegion 30 31 RequestId string `json:"requestId"` 32 VlanNum string `json:"vlanNum"` 33 AsGateway string `json:"asGateway"` 34 DhcpOptionsId string `json:"dhcpOptionsId"` 35 VpcName string `json:"vpcName"` 36 OwnerId string `json:"ownerId"` 37 WanCode string `json:"wanCode"` 38 Shared string `json:"shared"` 39 SubnetPolicy string `json:"subnetPolicy"` 40 Description string `json:"description"` 41 VpcId string `json:"vpcId"` 42 IsPublicNetwork string `json:"isPublicNetwork"` 43 GatewayId string `json:"gatewayId"` 44 IsDefault string `json:"isDefault"` 45 Provider string `json:"provider"` 46 State string `json:"state"` 47 CidrBlock string `json:"cidrBlock"` 48 InstanceTenancy string `json:"instanceTenancy"` 49 } 50 51 func (self *SVpc) GetId() string { 52 return self.VpcId 53 } 54 55 func (self *SVpc) GetGlobalId() string { 56 return self.VpcId 57 } 58 59 func (self *SVpc) GetName() string { 60 return self.VpcName 61 } 62 63 func (self *SVpc) Delete() error { 64 return cloudprovider.ErrNotImplemented 65 } 66 67 func (self *SVpc) GetCidrBlock() string { 68 return self.CidrBlock 69 } 70 71 func (self *SVpc) GetIRouteTableById(id string) (cloudprovider.ICloudRouteTable, error) { 72 return nil, cloudprovider.ErrNotImplemented 73 } 74 75 func (self *SVpc) GetIRouteTables() ([]cloudprovider.ICloudRouteTable, error) { 76 return nil, cloudprovider.ErrNotImplemented 77 } 78 79 func (self *SVpc) GetISecurityGroups() ([]cloudprovider.ICloudSecurityGroup, error) { 80 part, nextToken, err := self.region.GetSecurityGroups("", "", "") 81 if err != nil { 82 return nil, err 83 } 84 groups := []SSecurityGroup{} 85 groups = append(groups, part...) 86 for len(nextToken) > 0 { 87 part, nextToken, err = self.region.GetSecurityGroups("", "", nextToken) 88 if err != nil { 89 return nil, err 90 } 91 groups = append(groups, part...) 92 } 93 ret := []cloudprovider.ICloudSecurityGroup{} 94 for i := range groups { 95 groups[i].region = self.region 96 ret = append(ret, &groups[i]) 97 } 98 return ret, nil 99 } 100 101 func (self *SVpc) GetIsDefault() bool { 102 return self.IsDefault == "true" 103 } 104 105 func (self *SVpc) GetRegion() cloudprovider.ICloudRegion { 106 return self.region 107 } 108 109 func (self *SVpc) GetStatus() string { 110 switch self.State { 111 case "available": 112 return api.VPC_STATUS_AVAILABLE 113 default: 114 return self.State 115 } 116 } 117 118 func (self *SRegion) GetVpcs(id string) ([]SVpc, error) { 119 params := map[string]string{} 120 if len(id) > 0 { 121 params["vpcId"] = id 122 } 123 resp, err := self.invoke("DescribeVpcs", params) 124 if err != nil { 125 return nil, err 126 } 127 vpcs := []SVpc{} 128 return vpcs, resp.Unmarshal(&vpcs, "vpcSet") 129 } 130 131 func (self *SRegion) GetIVpcs() ([]cloudprovider.ICloudVpc, error) { 132 vpcs, err := self.GetVpcs("") 133 if err != nil { 134 return nil, errors.Wrapf(err, "GetVpcs") 135 } 136 ret := []cloudprovider.ICloudVpc{} 137 for i := range vpcs { 138 vpcs[i].region = self 139 ret = append(ret, &vpcs[i]) 140 } 141 return ret, nil 142 }