yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/nutanix/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 nutanix 16 17 import ( 18 "fmt" 19 "strings" 20 21 "yunion.io/x/jsonutils" 22 "yunion.io/x/pkg/errors" 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 multicloud.STagBase 34 wire *SWire 35 36 Range string 37 } 38 39 func (self *SNetwork) GetName() string { 40 if len(self.Range) > 0 { 41 return self.Range 42 } 43 return self.wire.GetName() 44 } 45 46 func (self *SNetwork) GetId() string { 47 if len(self.Range) > 0 { 48 return self.Range 49 } 50 return self.wire.GetId() 51 } 52 53 func (self *SNetwork) GetGlobalId() string { 54 if len(self.Range) > 0 { 55 return self.Range 56 } 57 return self.wire.GetGlobalId() 58 } 59 60 func (self *SNetwork) IsEmulated() bool { 61 return len(self.Range) == 0 62 } 63 64 func (self *SNetwork) Refresh() error { 65 vpc, err := self.wire.vpc.region.GetVpc(self.wire.vpc.GetGlobalId()) 66 if err != nil { 67 return err 68 } 69 for _, pool := range vpc.IPConfig.Pool { 70 if pool.Range == self.Range { 71 return nil 72 } 73 } 74 return cloudprovider.ErrNotFound 75 } 76 77 func (self *SNetwork) Delete() error { 78 if len(self.Range) == 0 { 79 return nil 80 } 81 return self.wire.vpc.region.DeleteNetwork(self.wire.vpc.UUID, self.Range) 82 } 83 84 func (self *SNetwork) GetAllocTimeoutSeconds() int { 85 return 120 // 2 minutes 86 } 87 88 func (self *SNetwork) GetGateway() string { 89 return self.wire.vpc.IPConfig.DefaultGateway 90 } 91 92 func (self *SNetwork) GetIWire() cloudprovider.ICloudWire { 93 return self.wire 94 } 95 96 func (self *SNetwork) GetIpStart() string { 97 if info := strings.Split(self.Range, " "); len(info) == 2 { 98 return info[0] 99 } 100 cidr := self.wire.vpc.GetCidrBlock() 101 if len(cidr) == 0 { 102 cidr = "0.0.0.0/0" 103 } 104 _range, _ := netutils.NewIPV4Prefix(cidr) 105 return _range.ToIPRange().StartIp().StepUp().String() 106 } 107 108 func (self *SNetwork) GetIpEnd() string { 109 if info := strings.Split(self.Range, " "); len(info) == 2 { 110 return info[1] 111 } 112 cidr := self.wire.vpc.GetCidrBlock() 113 if len(cidr) == 0 { 114 cidr = "0.0.0.0/0" 115 } 116 _range, _ := netutils.NewIPV4Prefix(cidr) 117 return _range.ToIPRange().EndIp().StepDown().String() 118 } 119 120 func (self *SNetwork) Contains(_ip string) bool { 121 start, _ := netutils.NewIPV4Addr(self.GetIpStart()) 122 end, _ := netutils.NewIPV4Addr(self.GetIpEnd()) 123 ip, _ := netutils.NewIPV4Addr(_ip) 124 return netutils.NewIPV4AddrRange(start, end).Contains(ip) 125 } 126 127 func (self *SNetwork) GetIpMask() int8 { 128 pref, _ := netutils.NewIPV4Prefix(self.wire.vpc.GetCidrBlock()) 129 return pref.MaskLen 130 } 131 132 func (self *SNetwork) GetProjectId() string { 133 return "" 134 } 135 136 func (self *SNetwork) GetPublicScope() rbacutils.TRbacScope { 137 return rbacutils.ScopeDomain 138 } 139 140 func (self *SNetwork) GetServerType() string { 141 return api.NETWORK_TYPE_GUEST 142 } 143 144 func (self *SNetwork) GetStatus() string { 145 return api.NETWORK_STATUS_AVAILABLE 146 } 147 148 func (self *SRegion) CreateNetwork(vpcId string, opts *cloudprovider.SNetworkCreateOptions) (*SNetwork, error) { 149 vpc, err := self.GetVpc(vpcId) 150 if err != nil { 151 return nil, errors.Wrapf(err, "GetVpc") 152 } 153 cidr, _ := netutils.NewIPV4Prefix(opts.Cidr) 154 _range := fmt.Sprintf("%s %s", cidr.ToIPRange().StartIp().StepUp(), cidr.ToIPRange().EndIp().StepDown()) 155 pool := SPool{Range: _range} 156 vpc.IPConfig.Pool = append(vpc.IPConfig.Pool, pool) 157 err = self.update("networks", vpcId, jsonutils.Marshal(vpc), nil) 158 if err != nil { 159 return nil, err 160 } 161 wire := vpc.getWire() 162 return &SNetwork{wire: wire, Range: _range}, nil 163 } 164 165 func (self *SRegion) DeleteNetwork(vpcId string, _range string) error { 166 vpc, err := self.GetVpc(vpcId) 167 if err != nil { 168 return err 169 } 170 pools, find := []SPool{}, false 171 for i := range vpc.IPConfig.Pool { 172 if vpc.IPConfig.Pool[i].Range == _range { 173 find = true 174 continue 175 } 176 pools = append(pools, vpc.IPConfig.Pool[i]) 177 } 178 if !find { 179 return nil 180 } 181 vpc.IPConfig.Pool = pools 182 return self.update("networks", vpcId, jsonutils.Marshal(vpc), nil) 183 }