Skip to content

Commit

Permalink
Add ip address metadata types; update metadata helper (#284)
Browse files Browse the repository at this point in the history
* add ip address metadata types

Signed-off-by: Matt Siwiec <[email protected]>

* flexible metadata helper

Signed-off-by: Matt Siwiec <[email protected]>

* add load-balancer-api metadata source

Signed-off-by: Matt Siwiec <[email protected]>

---------

Signed-off-by: Matt Siwiec <[email protected]>
  • Loading branch information
rizzza authored Dec 1, 2023
1 parent 3d091d9 commit 63fdc7a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
11 changes: 8 additions & 3 deletions pkg/metadata/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,23 @@ 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
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 {
Expand Down
12 changes: 8 additions & 4 deletions pkg/metadata/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 63fdc7a

Please sign in to comment.