How to use NewIPPool method of types Package

Best K6 code snippet using types.NewIPPool

ip_pool_manager.go

Source:ip_pool_manager.go Github

copy

Full Screen

1/*2Copyright (c) 2017 VMware, Inc. All Rights Reserved.3Licensed under the Apache License, Version 2.0 (the "License");4you may not use this file except in compliance with the License.5You may obtain a copy of the License at6 http://www.apache.org/licenses/LICENSE-2.07Unless required by applicable law or agreed to in writing, software8distributed under the License is distributed on an "AS IS" BASIS,9WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.10See the License for the specific language governing permissions and11limitations under the License.12*/13package simulator14import (15 "errors"16 "fmt"17 "net"18 "strconv"19 "strings"20 "github.com/vmware/govmomi/vim25/methods"21 "github.com/vmware/govmomi/vim25/mo"22 "github.com/vmware/govmomi/vim25/soap"23 "github.com/vmware/govmomi/vim25/types"24)25var ipPool = MustNewIpPool(&types.IpPool{26 Id: 1,27 Name: "ip-pool",28 AvailableIpv4Addresses: 250,29 AvailableIpv6Addresses: 250,30 AllocatedIpv6Addresses: 0,31 AllocatedIpv4Addresses: 0,32 Ipv4Config: &types.IpPoolIpPoolConfigInfo{33 Netmask: "10.10.10.255",34 Gateway: "10.10.10.1",35 SubnetAddress: "10.10.10.0",36 Range: "10.10.10.2#250",37 },38 Ipv6Config: &types.IpPoolIpPoolConfigInfo{39 Netmask: "2001:4860:0:2001::ff",40 Gateway: "2001:4860:0:2001::1",41 SubnetAddress: "2001:4860:0:2001::0",42 Range: "2001:4860:0:2001::2#250",43 },44})45// IpPoolManager implements a simple IP Pool manager in which all pools are shared46// across different datacenters.47type IpPoolManager struct {48 mo.IpPoolManager49 pools map[int32]*IpPool50 nextPoolId int3251}52func (m *IpPoolManager) init(*Registry) {53 m.pools = map[int32]*IpPool{54 1: ipPool,55 }56 m.nextPoolId = 257}58func (m *IpPoolManager) CreateIpPool(req *types.CreateIpPool) soap.HasFault {59 body := &methods.CreateIpPoolBody{}60 id := m.nextPoolId61 var err error62 m.pools[id], err = NewIpPool(&req.Pool)63 if err != nil {64 body.Fault_ = Fault("", &types.RuntimeFault{})65 return body66 }67 m.nextPoolId++68 body.Res = &types.CreateIpPoolResponse{69 Returnval: id,70 }71 return body72}73func (m *IpPoolManager) DestroyIpPool(req *types.DestroyIpPool) soap.HasFault {74 delete(m.pools, req.Id)75 return &methods.DestroyIpPoolBody{76 Res: &types.DestroyIpPoolResponse{},77 }78}79func (m *IpPoolManager) QueryIpPools(req *types.QueryIpPools) soap.HasFault {80 pools := []types.IpPool{}81 for i := int32(1); i < m.nextPoolId; i++ {82 if p, ok := m.pools[i]; ok {83 pools = append(pools, *p.config)84 }85 }86 return &methods.QueryIpPoolsBody{87 Res: &types.QueryIpPoolsResponse{88 Returnval: pools,89 },90 }91}92func (m *IpPoolManager) UpdateIpPool(req *types.UpdateIpPool) soap.HasFault {93 body := &methods.UpdateIpPoolBody{}94 var pool *IpPool95 var err error96 var ok bool97 if pool, ok = m.pools[req.Pool.Id]; !ok {98 body.Fault_ = Fault("", &types.NotFoundFault{})99 return body100 }101 if pool.config.AllocatedIpv4Addresses+pool.config.AllocatedIpv6Addresses != 0 {102 body.Fault_ = Fault("update a pool has been used is not supported", &types.RuntimeFault{})103 return body104 }105 m.pools[req.Pool.Id], err = NewIpPool(&req.Pool)106 if err != nil {107 body.Fault_ = Fault(err.Error(), &types.RuntimeFault{})108 return body109 }110 body.Res = &types.UpdateIpPoolResponse{}111 return body112}113func (m *IpPoolManager) AllocateIpv4Address(req *types.AllocateIpv4Address) soap.HasFault {114 body := &methods.AllocateIpv4AddressBody{}115 pool, ok := m.pools[req.PoolId]116 if !ok {117 body.Fault_ = Fault("", &types.InvalidArgument{})118 return body119 }120 ip, err := pool.AllocateIPv4(req.AllocationId)121 if err != nil {122 body.Fault_ = Fault(err.Error(), &types.RuntimeFault{})123 return body124 }125 body.Res = &types.AllocateIpv4AddressResponse{126 Returnval: ip,127 }128 return body129}130func (m *IpPoolManager) AllocateIpv6Address(req *types.AllocateIpv6Address) soap.HasFault {131 body := &methods.AllocateIpv6AddressBody{}132 pool, ok := m.pools[req.PoolId]133 if !ok {134 body.Fault_ = Fault("", &types.InvalidArgument{})135 return body136 }137 ip, err := pool.AllocateIpv6(req.AllocationId)138 if err != nil {139 body.Fault_ = Fault(err.Error(), &types.RuntimeFault{})140 return body141 }142 body.Res = &types.AllocateIpv6AddressResponse{143 Returnval: ip,144 }145 return body146}147func (m *IpPoolManager) ReleaseIpAllocation(req *types.ReleaseIpAllocation) soap.HasFault {148 body := &methods.ReleaseIpAllocationBody{}149 pool, ok := m.pools[req.PoolId]150 if !ok {151 body.Fault_ = Fault("", &types.InvalidArgument{})152 return body153 }154 pool.ReleaseIpv4(req.AllocationId)155 pool.ReleaseIpv6(req.AllocationId)156 body.Res = &types.ReleaseIpAllocationResponse{}157 return body158}159func (m *IpPoolManager) QueryIPAllocations(req *types.QueryIPAllocations) soap.HasFault {160 body := &methods.QueryIPAllocationsBody{}161 pool, ok := m.pools[req.PoolId]162 if !ok {163 body.Fault_ = Fault("", &types.InvalidArgument{})164 return body165 }166 body.Res = &types.QueryIPAllocationsResponse{}167 ipv4, ok := pool.ipv4Allocation[req.ExtensionKey]168 if ok {169 body.Res.Returnval = append(body.Res.Returnval, types.IpPoolManagerIpAllocation{170 IpAddress: ipv4,171 AllocationId: req.ExtensionKey,172 })173 }174 ipv6, ok := pool.ipv6Allocation[req.ExtensionKey]175 if ok {176 body.Res.Returnval = append(body.Res.Returnval, types.IpPoolManagerIpAllocation{177 IpAddress: ipv6,178 AllocationId: req.ExtensionKey,179 })180 }181 return body182}183var (184 errNoIpAvailable = errors.New("no ip address available")185 errInvalidAllocation = errors.New("allocation id not recognized")186)187type IpPool struct {188 config *types.IpPool189 ipv4Allocation map[string]string190 ipv6Allocation map[string]string191 ipv4Pool []string192 ipv6Pool []string193}194func MustNewIpPool(config *types.IpPool) *IpPool {195 pool, err := NewIpPool(config)196 if err != nil {197 panic(err)198 }199 return pool200}201func NewIpPool(config *types.IpPool) (*IpPool, error) {202 pool := &IpPool{203 config: config,204 ipv4Allocation: make(map[string]string),205 ipv6Allocation: make(map[string]string),206 }207 return pool, pool.init()208}209func (p *IpPool) init() error {210 // IPv4 range211 if p.config.Ipv4Config != nil {212 ranges := strings.Split(p.config.Ipv4Config.Range, ",")213 for _, r := range ranges {214 sp := strings.Split(r, "#")215 if len(sp) != 2 {216 return fmt.Errorf("format of range should be ip#number; got %q", r)217 }218 ip := net.ParseIP(strings.TrimSpace(sp[0])).To4()219 if ip == nil {220 return fmt.Errorf("bad ip format: %q", sp[0])221 }222 length, err := strconv.Atoi(sp[1])223 if err != nil {224 return err225 }226 for i := 0; i < length; i++ {227 p.ipv4Pool = append(p.ipv4Pool, net.IPv4(ip[0], ip[1], ip[2], ip[3]+byte(i)).String())228 }229 }230 }231 // IPv6 range232 if p.config.Ipv6Config != nil {233 ranges := strings.Split(p.config.Ipv6Config.Range, ",")234 for _, r := range ranges {235 sp := strings.Split(r, "#")236 if len(sp) != 2 {237 return fmt.Errorf("format of range should be ip#number; got %q", r)238 }239 ip := net.ParseIP(strings.TrimSpace(sp[0])).To16()240 if ip == nil {241 return fmt.Errorf("bad ip format: %q", sp[0])242 }243 length, err := strconv.Atoi(sp[1])244 if err != nil {245 return err246 }247 for i := 0; i < length; i++ {248 var ipv6 [16]byte249 copy(ipv6[:], ip)250 ipv6[15] += byte(i)251 p.ipv6Pool = append(p.ipv6Pool, net.IP(ipv6[:]).String())252 }253 }254 }255 return nil256}257func (p *IpPool) AllocateIPv4(allocation string) (string, error) {258 if ip, ok := p.ipv4Allocation[allocation]; ok {259 return ip, nil260 }261 l := len(p.ipv4Pool)262 if l == 0 {263 return "", errNoIpAvailable264 }265 ip := p.ipv4Pool[l-1]266 p.config.AvailableIpv4Addresses--267 p.config.AllocatedIpv4Addresses++268 p.ipv4Pool = p.ipv4Pool[:l-1]269 p.ipv4Allocation[allocation] = ip270 return ip, nil271}272func (p *IpPool) ReleaseIpv4(allocation string) error {273 ip, ok := p.ipv4Allocation[allocation]274 if !ok {275 return errInvalidAllocation276 }277 delete(p.ipv4Allocation, allocation)278 p.config.AvailableIpv4Addresses++279 p.config.AllocatedIpv4Addresses--280 p.ipv4Pool = append(p.ipv4Pool, ip)281 return nil282}283func (p *IpPool) AllocateIpv6(allocation string) (string, error) {284 if ip, ok := p.ipv6Allocation[allocation]; ok {285 return ip, nil286 }287 l := len(p.ipv6Pool)288 if l == 0 {289 return "", errNoIpAvailable290 }291 ip := p.ipv6Pool[l-1]292 p.config.AvailableIpv6Addresses--293 p.config.AllocatedIpv6Addresses++294 p.ipv6Pool = p.ipv6Pool[:l-1]295 p.ipv6Allocation[allocation] = ip296 return ip, nil297}298func (p *IpPool) ReleaseIpv6(allocation string) error {299 ip, ok := p.ipv6Allocation[allocation]300 if !ok {301 return errInvalidAllocation302 }303 delete(p.ipv6Allocation, allocation)304 p.config.AvailableIpv6Addresses++305 p.config.AllocatedIpv6Addresses--306 p.ipv6Pool = append(p.ipv6Pool, ip)307 return nil308}...

Full Screen

Full Screen

NewIPPool

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 xmlFile, err := os.Open("1.xml")4 if err != nil {5 fmt.Println("Error opening file:", err)6 }7 defer xmlFile.Close()8 byteValue, _ := ioutil.ReadAll(xmlFile)9 doc, err := gokogiri.ParseXml(byteValue)10 if err != nil {11 fmt.Println("Error reading file:", err)12 }13 if err != nil {14 fmt.Println("Error finding nodes:", err)15 }16 for _, node := range nodes {17 pool := types.NewIPPool()18 pool.Add(node.Content())19 fmt.Println(pool)20 }21}

Full Screen

Full Screen

NewIPPool

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println(types.NewIPPool())4}5import (6func main() {7 fmt.Println(types.NewIPPool())8}9import (10func main() {11 fmt.Println(types.NewIPPool())12}13import (14func main() {15 fmt.Println(types.NewIPPool())16}17import (18func main() {19 fmt.Println(types.NewIPPool())20}21import (22func main() {23 fmt.Println(types.NewIPPool())24}25import (26func main() {27 fmt.Println(types.NewIPPool())28}29import (30func main() {31 fmt.Println(types.NewIPPool())32}33import (34func main() {35 fmt.Println(types.NewIPPool())36}37import (38func main() {39 fmt.Println(types.NewIPPool())40}41import (42func main() {43 fmt.Println(types.NewIPPool())44}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful