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]) + }) +}