From 6dd93fd49c04b81db291b7bcf31f9cbcb90f54f0 Mon Sep 17 00:00:00 2001 From: rene-at-dell <89469836+rene-at-dell@users.noreply.github.com> Date: Fri, 1 Oct 2021 01:17:11 -0400 Subject: [PATCH] Enable retrieving existing Prefix CIDRs from the Ipamer (#69) --- ipam.go | 2 ++ prefix.go | 5 +++++ prefix_test.go | 17 +++++++++++++++++ 3 files changed, 24 insertions(+) diff --git a/ipam.go b/ipam.go index 35261fa..3dd93f8 100644 --- a/ipam.go +++ b/ipam.go @@ -32,6 +32,8 @@ type Ipamer interface { // PrefixesOverlapping will check if one ore more prefix of newPrefixes is overlapping // with one of existingPrefixes PrefixesOverlapping(existingPrefixes []string, newPrefixes []string) error + // ReadAllPrefixCidrs retrieves all existing Prefix CIDRs from the underlying storage + ReadAllPrefixCidrs() ([]string, error) } type ipamer struct { diff --git a/prefix.go b/prefix.go index a601a80..ecb209e 100644 --- a/prefix.go +++ b/prefix.go @@ -484,6 +484,11 @@ func (i *ipamer) newPrefix(cidr, parentCidr string) (*Prefix, error) { return p, nil } +// ReadAllPrefixCidrs retrieves all existing Prefix CIDRs from the underlying storage +func (i *ipamer) ReadAllPrefixCidrs() ([]string, error) { + return i.storage.ReadAllPrefixCidrs() +} + func (p *Prefix) String() string { return p.Cidr } diff --git a/prefix_test.go b/prefix_test.go index 55440a7..6bfdc6f 100644 --- a/prefix_test.go +++ b/prefix_test.go @@ -1456,3 +1456,20 @@ func TestAcquireIPParallel(t *testing.T) { } }) } + +func TestIpamer_ReadAllPrefixCidrs(t *testing.T) { + + testWithBackends(t, func(t *testing.T, ipam *ipamer) { + const cidr = "192.168.0.0/20" + + prefix, err := ipam.NewPrefix(cidr) + require.Nil(t, err) + require.NotNil(t, prefix) + + cidrs, err := ipam.ReadAllPrefixCidrs() + require.Nil(t, err) + require.NotNil(t, cidrs) + require.Equal(t, 1, len(cidrs)) + require.Equal(t, cidr, cidrs[0]) + }) +}