diff --git a/README.md b/README.md index 9231f43..004e828 100644 --- a/README.md +++ b/README.md @@ -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) @@ -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)