Skip to content

Commit

Permalink
update example in readme (#150)
Browse files Browse the repository at this point in the history
  • Loading branch information
ainghazal authored Jun 26, 2024
1 parent e05caa8 commit afa658d
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,37 @@ Library Example usage:
package main

import (
"context"
"fmt"
"time"

goipam "github.com/metal-stack/go-ipam"
)

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


// The background context
bgCtx := context.Background()
// Optional with Namespace
ctx := goipam.NewContextWithNamespace(bgCtx, "tenant-a")

// Create a ipamer with in memory storage
ipam := goipam.New(bgCtx)

// Optionally, we can pass around a context for a given namespace
namespace := "tenant-a"
err := ipam.CreateNamespace(bgCtx, namespace)
if err != nil {
panic(err)
}
ctx := goipam.NewContextWithNamespace(bgCtx, namespace)
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()

// Create a prefix to manage some IPs
prefix, err := ipam.NewPrefix(ctx, "192.168.0.0/24")
if err != nil {
panic(err)
}

// Acquire and release an IP with this prefix
ip, err := ipam.AcquireIP(ctx, prefix.Cidr)
if err != nil {
panic(err)
Expand All @@ -64,11 +74,13 @@ func main() {
if err != nil {
panic(err)
}

cp1, err := ipam.AcquireChildPrefix(ctx, prefix.Cidr, 64)
if err != nil {
panic(err)
}
fmt.Printf("got Prefix: %s\n", cp1)

cp2, err := ipam.AcquireChildPrefix(ctx, prefix.Cidr, 72)
if err != nil {
panic(err)
Expand Down

0 comments on commit afa658d

Please sign in to comment.