Skip to content

Commit

Permalink
Fix sample in readme (#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
majst01 committed Jul 26, 2022
1 parent 8466513 commit 6884838
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
15 changes: 9 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,32 +27,35 @@ package main

import (
"fmt"
"time"
goipam "github.com/metal-stack/go-ipam"
)

func main() {
// create a ipamer with in memory storage
ipam := goipam.New()

prefix, err := ipam.NewPrefix("192.168.0.0/24")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
prefix, err := ipam.NewPrefix(ctx, "192.168.0.0/24")
if err != nil {
panic(err)
}

ip, err := ipam.AcquireIP(prefix.Cidr)
ip, err := ipam.AcquireIP(ctx, prefix.Cidr)
if err != nil {
panic(err)
}
fmt.Printf("got IP: %s\n", ip.IP)

prefix, err = ipam.ReleaseIP(ip)
prefix, err = ipam.ReleaseIP(ctx, ip)
if err != nil {
panic(err)
}
fmt.Printf("IP: %s released.\n", ip.IP)

// Now a IPv6 Super Prefix with Child Prefixes
prefix, err = ipam.NewPrefix("2001:aabb::/48")
prefix, err = ipam.NewPrefix(ctx, "2001:aabb::/48")
if err != nil {
panic(err)
}
Expand All @@ -61,12 +64,12 @@ func main() {
panic(err)
}
fmt.Printf("got Prefix: %s\n", cp1)
cp2, err := ipam.AcquireChildPrefix(prefix.Cidr, 72)
cp2, err := ipam.AcquireChildPrefix(ctx, prefix.Cidr, 72)
if err != nil {
panic(err)
}
fmt.Printf("got Prefix: %s\n", cp2)
ip21, err := ipam.AcquireIP(cp2.Cidr)
ip21, err := ipam.AcquireIP(ctx, cp2.Cidr)
if err != nil {
panic(err)
}
Expand Down
4 changes: 3 additions & 1 deletion ipam_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import (
"context"
"fmt"
"strings"
"time"
)

func ExampleIpamer_NewPrefix() {
ctx := context.Background()
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

ipamer := New()
prefix, err := ipamer.NewPrefix(ctx, "192.168.0.0/24")
Expand Down

0 comments on commit 6884838

Please sign in to comment.