Skip to content

Commit

Permalink
use status struct
Browse files Browse the repository at this point in the history
Signed-off-by: Matt Siwiec <rizzza@users.noreply.github.com>
  • Loading branch information
rizzza committed Nov 16, 2023
1 parent b8f798d commit 7258010
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 82 deletions.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ require (
github.com/lib/pq v1.10.9
github.com/mattn/go-sqlite3 v1.14.18
github.com/mitchellh/go-homedir v1.1.0
github.com/spf13/cobra v1.7.0
github.com/spf13/cobra v1.8.0
github.com/spf13/viper v1.17.0
github.com/stretchr/testify v1.8.4
github.com/testcontainers/testcontainers-go v0.26.0
Expand Down Expand Up @@ -59,6 +59,7 @@ require (
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/go-openapi/inflect v0.19.0 // indirect
github.com/gofrs/uuid v4.4.0+incompatible // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
github.com/golang-jwt/jwt/v5 v5.0.0 // indirect
Expand Down
10 changes: 5 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7
github.com/coreos/go-systemd v0.0.0-20190719114852-fd7a80b32e1f/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
github.com/cpuguy83/dockercfg v0.3.1 h1:/FpZ+JaygUR/lZP2NlFI2DVfrOEMAIKP5wWEJdoYe9E=
github.com/cpuguy83/dockercfg v0.3.1/go.mod h1:sugsbF4//dDlL/i+S+rtpIWp+5h0BHJHfjj5/jFyUJc=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7DoTY=
github.com/creack/pty v1.1.18 h1:n56/Zwd5o6whRC5PMGretI4IdRLlmBXYNjScPaBgsbY=
github.com/creack/pty v1.1.18/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr4O4=
Expand Down Expand Up @@ -188,8 +188,8 @@ github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MG
github.com/gofrs/flock v0.8.1 h1:+gYjHKf32LDeiEEFhQaotPbLuUXjY5ZqxKgXy7n59aw=
github.com/gofrs/flock v0.8.1/go.mod h1:F1TvTiK9OcQqauNUHlbJvyl9Qa1QvF/gOUDKA14jxHU=
github.com/gofrs/uuid v4.0.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
github.com/gofrs/uuid v4.3.1+incompatible h1:0/KbAdpx3UXAx1kEOWHJeOkpbgRFGHVgv+CFIY7dBJI=
github.com/gofrs/uuid v4.3.1+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
github.com/gofrs/uuid v4.4.0+incompatible h1:3qXRTX8/NbyulANqlc0lchS1gqAVxRgsuW1YrTJupqA=
github.com/gofrs/uuid v4.4.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
github.com/golang-jwt/jwt v3.2.2+incompatible h1:IfV12K8xAKAnZqdXVzCZ+TOjboZ2keLg81eXfW3O+oY=
Expand Down Expand Up @@ -512,8 +512,8 @@ github.com/spf13/afero v1.10.0 h1:EaGW2JJh15aKOejeuJ+wpFSHnbd7GE6Wvp3TsNhb6LY=
github.com/spf13/afero v1.10.0/go.mod h1:UBogFpq8E9Hx+xc5CNTTEpTnuHVmXDwZcZcE1eb/UhQ=
github.com/spf13/cast v1.5.1 h1:R+kOtfhWQE6TVQzY+4D7wJLBgkdVasCEFxSUBYBYIlA=
github.com/spf13/cast v1.5.1/go.mod h1:b9PdjNptOpzXr7Rq1q9gJML/2cdGQAo69NKzQ10KN48=
github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I=
github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0=
github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/spf13/viper v1.17.0 h1:I5txKw7MJasPL/BrfkbA0Jyo/oELqVmux4pR/UxOMfI=
Expand Down
9 changes: 6 additions & 3 deletions internal/graphapi/loadbalancer.resolvers.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 11 additions & 7 deletions internal/graphapi/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ package graphapi
import (
"context"
"encoding/json"
"fmt"

metadata "go.infratographer.com/metadata-api/pkg/client"
metacli "go.infratographer.com/metadata-api/pkg/client"
"go.infratographer.com/x/gidx"

lbstate "go.infratographer.com/load-balancer-api/pkg/metadata"
metastatus "go.infratographer.com/load-balancer-api/pkg/metadata"

"go.infratographer.com/load-balancer-api/internal/config"
)
Expand All @@ -17,21 +16,26 @@ const metadataStatusSource = "load-balancer-api"

// Metadata interface for the metadata client
type Metadata interface {
StatusUpdate(ctx context.Context, input *metadata.StatusUpdateInput) (*metadata.StatusUpdate, error)
StatusUpdate(ctx context.Context, input *metacli.StatusUpdateInput) (*metacli.StatusUpdate, error)
}

// LoadBalancerStatusUpdate updates the state of a load balancer in the metadata service
func (r Resolver) LoadBalancerStatusUpdate(ctx context.Context, loadBalancerID gidx.PrefixedID, state lbstate.LoadBalancerState) error {
func (r Resolver) LoadBalancerStatusUpdate(ctx context.Context, loadBalancerID gidx.PrefixedID, status *metastatus.LoadBalancerStatus) error {
if r.metadata == nil {
r.logger.Warnln("metadata client not configured")
return nil
}

if _, err := r.metadata.StatusUpdate(ctx, &metadata.StatusUpdateInput{
jsonBytes, err := json.Marshal(status)
if err != nil {
return err
}

if _, err := r.metadata.StatusUpdate(ctx, &metacli.StatusUpdateInput{
NodeID: loadBalancerID.String(),
NamespaceID: config.AppConfig.Metadata.StatusNamespaceID.String(),
Source: metadataStatusSource,
Data: json.RawMessage(fmt.Sprintf(`{"state": "%s"}`, state)),
Data: json.RawMessage(jsonBytes),
}); err != nil {
return err
}
Expand Down
9 changes: 6 additions & 3 deletions internal/graphapi/origin.resolvers.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions internal/graphapi/pool.resolvers.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions internal/graphapi/port.resolvers.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions pkg/metadata/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package metadata

import "errors"

var (
// ErrStatusNotFound is returned when a status is not found in the payload
ErrStatusNotFound = errors.New("status not found")

// ErrInvalidStatusData is returned when the status json data is invalid
ErrInvalidStatusData = errors.New("invalid status json data")
)
55 changes: 13 additions & 42 deletions pkg/metadata/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ package metadata

import (
"encoding/json"
"errors"
"fmt"

"go.infratographer.com/x/gidx"

"go.infratographer.com/load-balancer-api/pkg/client"
)

// LoadBalancerState is the state of a load balancer
// LoadBalancerState state of a load balancer
type LoadBalancerState string

// load balancer states
Expand All @@ -22,52 +21,24 @@ const (
LoadBalancerStateUpdating LoadBalancerState = "updating"
)

var (
// ErrStatusNotFound is returned when a status is not found in the payload
ErrStatusNotFound = errors.New("status not found")

// ErrInvalidStatusData is returned when the status json data is invalid
ErrInvalidStatusData = errors.New("invalid status json data")
)

// ErrUnknownLoadBalancerState is returned when the load balancer state is unknown
type ErrUnknownLoadBalancerState struct {
State string
// LoadBalancerStatus is the status of a load balancer
type LoadBalancerStatus struct {
State LoadBalancerState `json:"state"`
}

func (e ErrUnknownLoadBalancerState) Error() string {
return "unknown load balancer state: " + e.State
}
// GetLoadbalancerStatus returns the status of a load balancer
func GetLoadbalancerStatus(metadataStatuses client.MetadataStatuses, statusNamespaceID gidx.PrefixedID) (*LoadBalancerStatus, error) {
for _, s := range metadataStatuses.Edges {
if s.Node.StatusNamespaceID == statusNamespaceID.String() {
status := &LoadBalancerStatus{}

// GetLoadbalancerState returns the status of a load balancer
func GetLoadbalancerState(metadataStatuses client.MetadataStatuses, statusNamespaceID gidx.PrefixedID) (LoadBalancerState, error) {
for _, status := range metadataStatuses.Edges {
if status.Node.StatusNamespaceID == statusNamespaceID.String() {
// we've found the loadbalancer status namespace we are looking fah
data := map[string]string{}
if err := json.Unmarshal(status.Node.Data, &data); err != nil {
// bad data stored in metadata-api, drop the message and move on
return "", fmt.Errorf("%w: %s", ErrInvalidStatusData, err)
if err := json.Unmarshal(s.Node.Data, status); err != nil {
return nil, fmt.Errorf("%w: %s", ErrInvalidStatusData, err)
}

if state, ok := data["state"]; ok {
switch state {
case string(LoadBalancerStateCreating):
fallthrough
case string(LoadBalancerStateTerminating):
fallthrough
case string(LoadBalancerStateActive):
fallthrough
case string(LoadBalancerStateDeleted):
fallthrough
case string(LoadBalancerStateUpdating):
return LoadBalancerState(state), nil
default:
return "", ErrUnknownLoadBalancerState{State: state}
}
}
return status, nil
}
}

return "", ErrStatusNotFound
return nil, ErrStatusNotFound
}
31 changes: 16 additions & 15 deletions pkg/metadata/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"go.infratographer.com/load-balancer-api/pkg/client"
)

func TestGetLoadbalancerState(t *testing.T) {
func TestGetLoadbalancerStatus(t *testing.T) {
t.Run("valid status", func(t *testing.T) {
statuses := client.MetadataStatuses{
Edges: []client.MetadataStatusEdges{
Expand All @@ -28,9 +29,9 @@ func TestGetLoadbalancerState(t *testing.T) {
},
}

state, err := GetLoadbalancerState(statuses, "metasns-loadbalancer-status")
assert.Nil(t, err)
assert.Equal(t, LoadBalancerStateActive, state)
status, err := GetLoadbalancerStatus(statuses, "metasns-loadbalancer-status")
require.Nil(t, err)
assert.Equal(t, LoadBalancerStateActive, status.State)
})

t.Run("bad json data", func(t *testing.T) {
Expand All @@ -45,9 +46,9 @@ func TestGetLoadbalancerState(t *testing.T) {
},
}

state, err := GetLoadbalancerState(statuses, "metasns-loadbalancer-status")
assert.NotNil(t, err)
assert.Empty(t, state)
status, err := GetLoadbalancerStatus(statuses, "metasns-loadbalancer-status")
require.NotNil(t, err)
require.Nil(t, status)
assert.ErrorIs(t, err, ErrInvalidStatusData)
})

Expand All @@ -56,27 +57,27 @@ func TestGetLoadbalancerState(t *testing.T) {
Edges: []client.MetadataStatusEdges{},
}

state, err := GetLoadbalancerState(statuses, "metasns-loadbalancer-status")
assert.NotNil(t, err)
assert.Empty(t, state)
status, err := GetLoadbalancerStatus(statuses, "metasns-loadbalancer-status")
require.NotNil(t, err)
require.Nil(t, status)
assert.ErrorIs(t, err, ErrStatusNotFound)
})

t.Run("unknown state", func(t *testing.T) {
t.Run("no status data", func(t *testing.T) {
statuses := client.MetadataStatuses{
Edges: []client.MetadataStatusEdges{
{
Node: client.MetadataStatusNode{
StatusNamespaceID: "metasns-loadbalancer-status",
Data: json.RawMessage(`{"state": "unknown"}`),
Data: json.RawMessage(``),
},
},
},
}

state, err := GetLoadbalancerState(statuses, "metasns-loadbalancer-status")
status, err := GetLoadbalancerStatus(statuses, "metasns-loadbalancer-status")
assert.NotNil(t, err)
assert.Empty(t, state)
assert.ErrorIs(t, err, ErrUnknownLoadBalancerState{State: "unknown"})
assert.Nil(t, status)
assert.ErrorIs(t, err, ErrInvalidStatusData)
})
}

0 comments on commit 7258010

Please sign in to comment.