From 63fdc7abfac509690c2125e024be2b68f28eebe7 Mon Sep 17 00:00:00 2001 From: Matt Siwiec Date: Fri, 1 Dec 2023 09:04:49 -0700 Subject: [PATCH] Add ip address metadata types; update metadata helper (#284) * add ip address metadata types Signed-off-by: Matt Siwiec * flexible metadata helper Signed-off-by: Matt Siwiec * add load-balancer-api metadata source Signed-off-by: Matt Siwiec --------- Signed-off-by: Matt Siwiec --- pkg/metadata/metadata.go | 11 ++++++++--- pkg/metadata/metadata_test.go | 12 ++++++++---- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/pkg/metadata/metadata.go b/pkg/metadata/metadata.go index e79bbd2e8..5f6108952 100644 --- a/pkg/metadata/metadata.go +++ b/pkg/metadata/metadata.go @@ -19,6 +19,11 @@ const ( LoadBalancerStateActive LoadBalancerState = "active" LoadBalancerStateDeleted LoadBalancerState = "deleted" LoadBalancerStateUpdating LoadBalancerState = "updating" + + LoadBalancerStateIPAssigned LoadBalancerState = "ip-address.assigned" + LoadBalancerStateIPUnassigned LoadBalancerState = "ip-address.unassigned" + + LoadBalancerAPISource string = "load-balancer-api" ) // LoadBalancerStatus is the status of a load balancer @@ -26,11 +31,11 @@ type LoadBalancerStatus struct { State LoadBalancerState `json:"state"` } -// GetLoadbalancerStatus returns the status of a load balancer -func GetLoadbalancerStatus(metadataStatuses client.MetadataStatuses, statusNamespaceID gidx.PrefixedID) (*LoadBalancerStatus, error) { +// GetLoadbalancerStatus searches through the list of metadata status for the requested status of a load balancer using namespace and source +func GetLoadbalancerStatus(metadataStatuses client.MetadataStatuses, statusNamespaceID gidx.PrefixedID, source string) (*LoadBalancerStatus, error) { if metadataStatuses.TotalCount > 0 { for _, s := range metadataStatuses.Edges { - if s.Node.StatusNamespaceID == statusNamespaceID.String() { + if s.Node.StatusNamespaceID == statusNamespaceID.String() && s.Node.Source == source { status := &LoadBalancerStatus{} if err := json.Unmarshal(s.Node.Data, status); err != nil { diff --git a/pkg/metadata/metadata_test.go b/pkg/metadata/metadata_test.go index fe8dd9014..150b989af 100644 --- a/pkg/metadata/metadata_test.go +++ b/pkg/metadata/metadata_test.go @@ -19,18 +19,20 @@ func TestGetLoadbalancerStatus(t *testing.T) { Node: client.MetadataStatusNode{ StatusNamespaceID: "metasns-loadbalancer-status", Data: json.RawMessage(`{"state": "active"}`), + Source: "load-balancer-api", }, }, { Node: client.MetadataStatusNode{ StatusNamespaceID: "metasns-some-other-namespace", Data: json.RawMessage(`{"key": "value"}`), + Source: "some-other-source", }, }, }, } - status, err := GetLoadbalancerStatus(statuses, "metasns-loadbalancer-status") + status, err := GetLoadbalancerStatus(statuses, "metasns-loadbalancer-status", "load-balancer-api") require.Nil(t, err) assert.Equal(t, LoadBalancerStateActive, status.State) }) @@ -43,12 +45,13 @@ func TestGetLoadbalancerStatus(t *testing.T) { Node: client.MetadataStatusNode{ StatusNamespaceID: "metasns-loadbalancer-status", Data: json.RawMessage(`{"state"}`), + Source: "load-balancer-api", }, }, }, } - status, err := GetLoadbalancerStatus(statuses, "metasns-loadbalancer-status") + status, err := GetLoadbalancerStatus(statuses, "metasns-loadbalancer-status", "load-balancer-api") require.NotNil(t, err) require.Nil(t, status) assert.ErrorIs(t, err, ErrInvalidStatusData) @@ -60,7 +63,7 @@ func TestGetLoadbalancerStatus(t *testing.T) { Edges: []client.MetadataStatusEdges{}, } - status, err := GetLoadbalancerStatus(statuses, "metasns-loadbalancer-status") + status, err := GetLoadbalancerStatus(statuses, "metasns-loadbalancer-status", "load-balancer-api") require.NotNil(t, err) require.Nil(t, status) assert.ErrorIs(t, err, ErrStatusNotFound) @@ -74,12 +77,13 @@ func TestGetLoadbalancerStatus(t *testing.T) { Node: client.MetadataStatusNode{ StatusNamespaceID: "metasns-loadbalancer-status", Data: json.RawMessage(``), + Source: "load-balancer-api", }, }, }, } - status, err := GetLoadbalancerStatus(statuses, "metasns-loadbalancer-status") + status, err := GetLoadbalancerStatus(statuses, "metasns-loadbalancer-status", "load-balancer-api") assert.NotNil(t, err) assert.Nil(t, status) assert.ErrorIs(t, err, ErrInvalidStatusData)