yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/bingocloud/network.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 "fmt" 19 "strings" 20 21 "yunion.io/x/pkg/errors" 22 "yunion.io/x/pkg/util/netutils" 23 24 api "yunion.io/x/cloudmux/pkg/apis/compute" 25 "yunion.io/x/cloudmux/pkg/cloudprovider" 26 "yunion.io/x/cloudmux/pkg/multicloud" 27 "yunion.io/x/onecloud/pkg/util/rbacutils" 28 ) 29 30 type SNetwork struct { 31 multicloud.SResourceBase 32 multicloud.STagBase 33 wire *SWire 34 35 CidrBlock string `json:"cidrBlock"` 36 MapPublicIPOnLaunch string `json:"mapPublicIpOnLaunch"` 37 RouterId string `json:"routerId"` 38 NetworkFrom string `json:"networkFrom"` 39 NetworkNode string `json:"networkNode"` 40 IsManagedNetwork string `json:"isManagedNetwork"` 41 SubnetName string `json:"subnetName"` 42 NetworkMask string `json:"networkMask"` 43 NetName string `json:"netName"` 44 Index string `json:"index"` 45 VpcIsPublic string `json:"vpcIsPublic"` 46 Description string `json:"description"` 47 MicroSegmentation string `json:"microSegmentation"` 48 State string `json:"state"` 49 DefaultForAz string `json:"defaultForAz"` 50 Router string `json:"router"` 51 Active string `json:"active"` 52 VpcShared string `json:"vpcShared"` 53 DvswitchName string `json:"dvswitchName"` 54 AvailableIPAddressCount string `json:"availableIpAddressCount"` 55 VlanNum string `json:"vlanNum"` 56 RouterCc2 string `json:"router_cc2"` 57 ResStatus string `json:"res_status"` 58 IPAddressCount string `json:"ipAddressCount"` 59 RouterCc string `json:"router_cc"` 60 VpcId string `json:"vpcId"` 61 NetworkTo string `json:"networkTo"` 62 Broadcast string `json:"broadcast"` 63 UserName string `json:"userName"` 64 IPVersion string `json:"ipVersion"` 65 AvailabilityZone string `json:"availabilityZone"` 66 Network string `json:"network"` 67 RouterMac string `json:"router_mac"` 68 CheckIP string `json:"checkIp"` 69 FloatingPool string `json:"floatingPool"` 70 StaticPool string `json:"staticPool"` 71 DvsPortGroup string `json:"dvsPortGroup"` 72 SubnetId string `json:"subnetId"` 73 } 74 75 func (self *SNetwork) GetId() string { 76 return self.SubnetId 77 } 78 79 func (self *SNetwork) GetGlobalId() string { 80 return self.SubnetId 81 } 82 83 func (self *SNetwork) GetName() string { 84 if len(self.SubnetName) > 0 { 85 return self.SubnetName 86 } 87 return self.SubnetId 88 } 89 90 func (self *SNetwork) Delete() error { 91 return cloudprovider.ErrNotImplemented 92 } 93 94 func (self *SNetwork) GetAllocTimeoutSeconds() int { 95 return 300 96 } 97 98 func (self *SNetwork) GetGateway() string { 99 prefix, err := netutils.NewIPV4Prefix(self.CidrBlock) 100 if err == nil { 101 return prefix.Address.StepUp().String() 102 } 103 return "" 104 } 105 106 func (self *SNetwork) GetIpStart() string { 107 pref, _ := netutils.NewIPV4Prefix(self.CidrBlock) 108 startIp := pref.Address.NetAddr(pref.MaskLen) // 0 109 startIp = startIp.StepUp() // 1 110 return startIp.String() 111 } 112 113 func (self *SNetwork) GetIpEnd() string { 114 pref, _ := netutils.NewIPV4Prefix(self.CidrBlock) 115 endIp := pref.Address.BroadcastAddr(pref.MaskLen) // 255 116 endIp = endIp.StepDown() // 254 117 endIp = endIp.StepDown() // 253 118 endIp = endIp.StepDown() // 252 119 return endIp.String() 120 } 121 122 func (self *SNetwork) GetIpMask() int8 { 123 pref, _ := netutils.NewIPV4Prefix(self.CidrBlock) 124 return pref.MaskLen 125 } 126 127 func (self *SNetwork) GetIWire() cloudprovider.ICloudWire { 128 return self.wire 129 } 130 131 func (self *SNetwork) GetProjectId() string { 132 return "" 133 } 134 135 func (self *SNetwork) GetPublicScope() rbacutils.TRbacScope { 136 return rbacutils.ScopeDomain 137 } 138 139 func (self *SNetwork) GetServerType() string { 140 return api.NETWORK_TYPE_GUEST 141 } 142 143 func (self *SNetwork) GetStatus() string { 144 return strings.ToLower(self.State) 145 } 146 147 func (self *SWire) CreateINetwork(opts *cloudprovider.SNetworkCreateOptions) (cloudprovider.ICloudNetwork, error) { 148 return nil, cloudprovider.ErrNotImplemented 149 } 150 151 func (self *SWire) GetINetworks() ([]cloudprovider.ICloudNetwork, error) { 152 networks, err := self.vpc.region.GetNetworks("", self.cluster.ClusterId, self.vpc.VpcId) 153 if err != nil { 154 return nil, err 155 } 156 ret := []cloudprovider.ICloudNetwork{} 157 for i := range networks { 158 networks[i].wire = self 159 ret = append(ret, &networks[i]) 160 } 161 return ret, nil 162 } 163 164 func (self *SWire) GetINetworkById(id string) (cloudprovider.ICloudNetwork, error) { 165 networks, err := self.GetINetworks() 166 if err != nil { 167 return nil, err 168 } 169 for i := range networks { 170 if networks[i].GetGlobalId() == id { 171 return networks[i], nil 172 } 173 } 174 return nil, errors.Wrapf(cloudprovider.ErrNotFound, id) 175 } 176 177 func (self *SRegion) GetNetworks(id, clusterId, vpcId string) ([]SNetwork, error) { 178 params := map[string]string{} 179 if len(id) > 0 { 180 params["subnetId"] = id 181 } 182 idx := 1 183 if len(clusterId) > 0 { 184 params[fmt.Sprintf("Filter.%d.Name", idx)] = "availability-zone" 185 params[fmt.Sprintf("Filter.%d.Value.1", idx)] = clusterId 186 idx++ 187 } 188 if len(vpcId) > 0 { 189 params[fmt.Sprintf("Filter.%d.Name", idx)] = "vpc-id" 190 params[fmt.Sprintf("Filter.%d.Value.1", idx)] = vpcId 191 idx++ 192 } 193 resp, err := self.invoke("DescribeSubnets", params) 194 if err != nil { 195 return nil, err 196 } 197 networks := []SNetwork{} 198 return networks, resp.Unmarshal(&networks, "subnetSet") 199 }