Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix sample in readme #85

Merged
merged 1 commit into from
Jul 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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