yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/ucloud/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 ucloud 16 17 import ( 18 "fmt" 19 20 "yunion.io/x/jsonutils" 21 "yunion.io/x/log" 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 // https://docs.ucloud.cn/api/vpc2.0-api/describe_subnet 31 type SNetwork struct { 32 multicloud.SResourceBase 33 UcloudTags 34 wire *SWire 35 36 CreateTime int64 `json:"CreateTime"` 37 Gateway string `json:"Gateway"` 38 HasNATGW bool `json:"HasNATGW"` 39 Name string `json:"Name"` 40 Netmask string `json:"Netmask"` 41 Remark string `json:"Remark"` 42 RouteTableID string `json:"RouteTableId"` 43 Subnet string `json:"Subnet"` 44 SubnetID string `json:"SubnetId"` 45 SubnetName string `json:"SubnetName"` 46 SubnetType int `json:"SubnetType"` 47 Tag string `json:"Tag"` 48 VPCID string `json:"VPCId"` 49 VPCName string `json:"VPCName"` 50 VRouterID string `json:"VRouterId"` 51 Zone string `json:"Zone"` 52 } 53 54 func (self *SNetwork) GetProjectId() string { 55 return self.wire.region.client.projectId 56 } 57 58 func (self *SNetwork) GetId() string { 59 return self.SubnetID 60 } 61 62 func (self *SNetwork) GetName() string { 63 if len(self.SubnetName) > 0 { 64 return self.SubnetName 65 } 66 67 return self.GetId() 68 } 69 70 func (self *SNetwork) GetGlobalId() string { 71 return self.GetId() 72 } 73 74 func (self *SNetwork) GetStatus() string { 75 return api.NETWORK_STATUS_AVAILABLE 76 } 77 78 func (self *SNetwork) Refresh() error { 79 log.Debugf("network refresh %s", self.GetId()) 80 new, err := self.wire.region.getNetwork(self.GetId()) 81 if err != nil { 82 return err 83 } 84 return jsonutils.Update(self, new) 85 } 86 87 func (self *SNetwork) IsEmulated() bool { 88 return false 89 } 90 91 func (self *SNetwork) GetIWire() cloudprovider.ICloudWire { 92 return self.wire 93 } 94 95 func (self *SNetwork) GetIpStart() string { 96 pref, _ := netutils.NewIPV4Prefix(self.Subnet + "/" + self.Netmask) 97 startIp := pref.Address.NetAddr(pref.MaskLen) // 0 98 startIp = startIp.StepUp() // 1 gateway 99 startIp = startIp.StepUp() // 2 100 return startIp.String() 101 } 102 103 func (self *SNetwork) GetIpEnd() string { 104 pref, _ := netutils.NewIPV4Prefix(self.Subnet + "/" + self.Netmask) 105 endIp := pref.Address.BroadcastAddr(pref.MaskLen) // 255 106 endIp = endIp.StepDown() // 254 107 return endIp.String() 108 } 109 110 func (self *SNetwork) GetIpMask() int8 { 111 pref, _ := netutils.NewIPV4Prefix(self.Subnet + "/" + self.Netmask) 112 return pref.MaskLen 113 } 114 115 func (self *SNetwork) GetGateway() string { 116 return self.Gateway 117 } 118 119 func (self *SNetwork) GetServerType() string { 120 return api.NETWORK_TYPE_GUEST 121 } 122 123 func (self *SNetwork) GetIsPublic() bool { 124 return true 125 } 126 127 func (self *SNetwork) GetPublicScope() rbacutils.TRbacScope { 128 return rbacutils.ScopeDomain 129 } 130 131 func (self *SNetwork) Delete() error { 132 return self.wire.region.DeleteNetwork(self.GetId()) 133 } 134 135 func (self *SNetwork) GetAllocTimeoutSeconds() int { 136 return 120 // 2 minutes 137 } 138 139 // https://docs.ucloud.cn/api/vpc2.0-api/describe_subnet 140 func (self *SRegion) getNetwork(networkId string) (*SNetwork, error) { 141 if len(networkId) == 0 { 142 return nil, fmt.Errorf("getNetwork network id should not be empty") 143 } 144 networks := make([]SNetwork, 0) 145 146 params := NewUcloudParams() 147 params.Set("SubnetId", networkId) 148 err := self.DoListAll("DescribeSubnet", params, &networks) 149 if err != nil { 150 return nil, err 151 } 152 153 if len(networks) == 1 { 154 network := networks[0] 155 vpc, err := self.getVpc(network.VPCID) 156 if err != nil { 157 return nil, err 158 } 159 network.wire = &SWire{region: self, vpc: vpc, inetworks: []cloudprovider.ICloudNetwork{&network}} 160 return &network, nil 161 } else if len(networks) == 0 { 162 return nil, cloudprovider.ErrNotFound 163 } else { 164 return nil, fmt.Errorf("getNetwork %s %d found", networkId, len(networks)) 165 } 166 } 167 168 // https://docs.ucloud.cn/api/vpc2.0-api/delete_subnet 169 func (self *SRegion) DeleteNetwork(networkId string) error { 170 params := NewUcloudParams() 171 params.Set("SubnetId", networkId) 172 173 return self.DoAction("DeleteSubnet", params, nil) 174 } 175 176 // https://docs.ucloud.cn/api/vpc2.0-api/create_subnet 177 func (self *SRegion) CreateNetwork(vpcId string, name string, cidr string, desc string) (*SNetwork, error) { 178 ip, mask, err := netutils.ParsePrefix(cidr) 179 if err != nil { 180 return nil, fmt.Errorf("CreateINetwork invalid cidr %s", cidr) 181 } 182 183 params := NewUcloudParams() 184 params.Set("VPCId", vpcId) 185 params.Set("Subnet", ip.String()) 186 params.Set("Netmask", int(mask)) 187 params.Set("SubnetName", name) 188 params.Set("Remark", desc) 189 190 type SNet struct { 191 SubnetId string 192 } 193 194 net := SNet{} 195 err = self.DoAction("CreateSubnet", params, &net) 196 if err != nil { 197 return nil, err 198 } 199 200 return self.getNetwork(net.SubnetId) 201 }