yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/incloudsphere/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 incloudsphere 16 17 import ( 18 "fmt" 19 "net/url" 20 "strings" 21 22 "yunion.io/x/jsonutils" 23 "yunion.io/x/pkg/util/netutils" 24 25 api "yunion.io/x/cloudmux/pkg/apis/compute" 26 "yunion.io/x/cloudmux/pkg/cloudprovider" 27 "yunion.io/x/cloudmux/pkg/multicloud" 28 "yunion.io/x/onecloud/pkg/util/rbacutils" 29 ) 30 31 type SNetwork struct { 32 multicloud.SResourceBase 33 InCloudSphereTags 34 35 wire *SWire 36 37 Id string `json:"id"` 38 Name string `json:"name"` 39 ResourceId string `json:"resourceId"` 40 Vlan int `json:"vlan"` 41 VlanFlag bool `json:"vlanFlag"` 42 Mtu string `json:"mtu"` 43 Type string `json:"type"` 44 VswitchDto SWire `json:"vswitchDto"` 45 //PortDtos []string `json:"portDtos"` 46 VMDtos string `json:"vmDtos"` 47 VnicDtos string `json:"vnicDtos"` 48 Vmcount int `json:"vmcount"` 49 Vniccount int `json:"vniccount"` 50 ConnectMode string `json:"connectMode"` 51 // 约定未开启dhcp时,描述信息里面放置<gateway>/<netmask> 52 Description string `json:"description"` 53 UplinkRate int `json:"uplinkRate"` 54 UplinkBurst int `json:"uplinkBurst"` 55 DownlinkRate int `json:"downlinkRate"` 56 DownlinkBurst int `json:"downlinkBurst"` 57 QosEnabled bool `json:"qosEnabled"` 58 DataServiceType string `json:"dataServiceType"` 59 UserVlan string `json:"userVlan"` 60 TpidType string `json:"tpidType"` 61 PermitDel bool `json:"permitDel"` 62 Cidr string `json:"cidr"` 63 Gateway string `json:"gateway"` 64 DhcpEnabled bool `json:"dhcpEnabled"` 65 DNS string `json:"dns"` 66 DataCenterDto SZone `json:"dataCenterDto"` 67 NetworkTopoly bool `json:"networkTopoly"` 68 } 69 70 func (self *SNetwork) GetCidr() string { 71 if len(self.Cidr) > 0 { 72 return self.Cidr 73 } 74 _, err := netutils.NewIPV4Prefix(self.Description) 75 if err == nil { 76 return self.Description 77 } 78 return "0.0.0.0/0" 79 } 80 81 func (self *SNetwork) GetName() string { 82 return self.Name 83 } 84 85 func (self *SNetwork) GetId() string { 86 return self.Id 87 } 88 89 func (self *SNetwork) GetGlobalId() string { 90 return self.GetId() 91 } 92 93 func (self *SNetwork) Refresh() error { 94 ret, err := self.wire.region.GetNetwork(self.Id) 95 if err != nil { 96 return err 97 } 98 return jsonutils.Update(self, ret) 99 } 100 101 func (self *SNetwork) Delete() error { 102 return cloudprovider.ErrNotFound 103 } 104 105 func (self *SNetwork) GetAllocTimeoutSeconds() int { 106 return 120 // 2 minutes 107 } 108 109 func (self *SNetwork) GetGateway() string { 110 if len(self.Gateway) > 0 { 111 return self.Gateway 112 } 113 if len(self.Description) > 0 && strings.Contains(self.Description, "/") { 114 info := strings.Split(self.Description, "/") 115 ip, err := netutils.NewIPV4Addr(info[0]) 116 if err == nil { 117 return ip.String() 118 } 119 } 120 return self.Gateway 121 } 122 123 func (self *SNetwork) GetIWire() cloudprovider.ICloudWire { 124 return self.wire 125 } 126 127 func (self *SNetwork) GetIpStart() string { 128 _range, err := netutils.NewIPV4Prefix(self.GetCidr()) 129 if err != nil { 130 return "" 131 } 132 return _range.ToIPRange().StartIp().StepUp().String() 133 } 134 135 func (self *SNetwork) GetIpEnd() string { 136 _range, err := netutils.NewIPV4Prefix(self.GetCidr()) 137 if err != nil { 138 return "" 139 } 140 return _range.ToIPRange().EndIp().StepDown().String() 141 } 142 143 func (self *SNetwork) Contains(_ip string) bool { 144 start, _ := netutils.NewIPV4Addr(self.GetIpStart()) 145 end, _ := netutils.NewIPV4Addr(self.GetIpEnd()) 146 ip, _ := netutils.NewIPV4Addr(_ip) 147 return netutils.NewIPV4AddrRange(start, end).Contains(ip) 148 } 149 150 func (self *SNetwork) GetIpMask() int8 { 151 pref, err := netutils.NewIPV4Prefix(self.GetCidr()) 152 if err != nil { 153 return 0 154 } 155 return pref.MaskLen 156 } 157 158 func (self *SNetwork) GetProjectId() string { 159 return "" 160 } 161 162 func (self *SNetwork) GetPublicScope() rbacutils.TRbacScope { 163 return rbacutils.ScopeDomain 164 } 165 166 func (self *SNetwork) GetServerType() string { 167 return api.NETWORK_TYPE_GUEST 168 } 169 170 func (self *SNetwork) GetStatus() string { 171 return api.NETWORK_STATUS_AVAILABLE 172 } 173 174 func (self *SRegion) GetNetworks(vsId string) ([]SNetwork, error) { 175 ret := []SNetwork{} 176 res := fmt.Sprintf("/vswitchs/%s/networks", vsId) 177 return ret, self.list(res, url.Values{}, &ret) 178 } 179 180 func (self *SRegion) GetNetwork(id string) (*SNetwork, error) { 181 ret := &SNetwork{} 182 res := fmt.Sprintf("/networks/%s", id) 183 return ret, self.get(res, url.Values{}, ret) 184 }