diff --git a/pkg/client/client_test.go b/pkg/client/client_test.go index e55b8aa7e..afadbf481 100644 --- a/pkg/client/client_test.go +++ b/pkg/client/client_test.go @@ -59,7 +59,8 @@ func TestGetLoadBalancer(t *testing.T) { "id": "loadori-origin", "name": "origin", "target": "1.2.3.4", - "portNumber": 80 + "portNumber": 80, + "weight": 100 } } ] @@ -94,6 +95,7 @@ func TestGetLoadBalancer(t *testing.T) { assert.Equal(t, "origin", lb.Ports.Edges[0].Node.Pools[0].Origins.Edges[0].Node.Name) assert.Equal(t, "1.2.3.4", lb.Ports.Edges[0].Node.Pools[0].Origins.Edges[0].Node.Target) assert.Equal(t, int64(80), lb.Ports.Edges[0].Node.Pools[0].Origins.Edges[0].Node.PortNumber) + assert.Equal(t, int64(100), lb.Ports.Edges[0].Node.Pools[0].Origins.Edges[0].Node.Weight) require.Len(t, lb.IPAddresses, 2) assert.Equal(t, "ipamipa-randovalue", lb.IPAddresses[0].ID) @@ -105,6 +107,58 @@ func TestGetLoadBalancer(t *testing.T) { assert.True(t, lb.IPAddresses[1].Reserved) }) + t.Run("successful query with metadata status", func(t *testing.T) { + respJSON := `{ + "data": { + "loadBalancer": { + "id": "loadbal-testing", + "name": "some lb", + "location": { + "id": "lctnloc-testing" + }, + "metadata": { + "id": "metadat-testing", + "nodeID": "loadbal-testing", + "statuses": { + "edges": [ + { + "node": { + "source": "lctnloc-testing", + "statusNamespaceID": "metasns-testing", + "id": "metasts-testing", + "data": { + "status": "creating" + } + } + } + ] + } + } + } + } +}` + + cli.gqlCli = mustNewGQLTestClient(respJSON, http.StatusOK) + lb, err := cli.GetLoadBalancer(context.Background(), "loadbal-randovalue") + require.NoError(t, err) + require.NotNil(t, lb) + + assert.Equal(t, "loadbal-testing", lb.ID) + assert.Equal(t, "some lb", lb.Name) + assert.Equal(t, "lctnloc-testing", lb.Location.ID) + + assert.Equal(t, "metadat-testing", lb.Metadata.ID) + assert.Equal(t, "loadbal-testing", lb.Metadata.NodeID) + assert.Len(t, lb.IPAddresses, 0) + assert.Len(t, lb.Ports.Edges, 0) + + require.Len(t, lb.Metadata.Statuses.Edges, 1) + assert.Equal(t, "lctnloc-testing", lb.Metadata.Statuses.Edges[0].Node.Source) + assert.Equal(t, "metasts-testing", lb.Metadata.Statuses.Edges[0].Node.ID) + assert.Equal(t, "metasns-testing", lb.Metadata.Statuses.Edges[0].Node.StatusNamespaceID) + assert.JSONEq(t, `{"status": "creating"}`, string(lb.Metadata.Statuses.Edges[0].Node.Data)) + }) + t.Run("unauthorized", func(t *testing.T) { respJSON := `{"message":"invalid or expired jwt"}` diff --git a/pkg/client/types.go b/pkg/client/types.go index dd4033463..0d87d52a6 100644 --- a/pkg/client/types.go +++ b/pkg/client/types.go @@ -1,69 +1,72 @@ package client +import "encoding/json" + // OriginNode is a struct that represents the OriginNode GraphQL type type OriginNode struct { - ID string - Name string - Target string - PortNumber int64 - Weight int64 - Active bool + ID string `graphql:"id" json:"id"` + Name string `graphql:"name" json:"name"` + Target string `graphql:"target" json:"target"` + PortNumber int64 `graphql:"portNumber" json:"portNumber"` + Weight int64 `graphql:"weight" json:"weight"` + Active bool `graphql:"active" json:"active"` } // OriginEdges is a struct that represents the OriginEdges GraphQL type type OriginEdges struct { - Node OriginNode + Node OriginNode `graphql:"node" json:"node"` } // Origins is a struct that represents the Origins GraphQL type type Origins struct { - Edges []OriginEdges + Edges []OriginEdges `graphql:"edges" json:"edges"` } // Pool is a struct that represents the Pool GraphQL type type Pool struct { - ID string - Name string - Protocol string - Origins Origins + ID string `graphql:"id"` + Name string `graphql:"name" json:"name"` + Protocol string `graphql:"protocol" json:"protocol"` + Origins Origins `graphql:"origins" json:"origins"` } // PortNode is a struct that represents the PortNode GraphQL type type PortNode struct { - ID string - Name string - Number int64 - Pools []Pool + ID string `graphql:"id" json:"id"` + Name string `graphql:"name" json:"name"` + Number int64 `graphql:"number" json:"number"` + Pools []Pool `graphql:"pools" json:"pools"` } // PortEdges is a struct that represents the PortEdges GraphQL type type PortEdges struct { - Node PortNode + Node PortNode `graphql:"node" json:"node"` } // Ports is a struct that represents the Ports GraphQL type type Ports struct { - Edges []PortEdges + Edges []PortEdges `graphql:"edges" json:"edges"` } // OwnerNode is a struct that represents the OwnerNode GraphQL type type OwnerNode struct { - ID string + ID string `graphql:"id" json:"id"` } // LocationNode is a struct that represents the LocationNode GraphQL type type LocationNode struct { - ID string + ID string `graphql:"id" json:"id"` } // LoadBalancer is a struct that represents the LoadBalancer GraphQL type type LoadBalancer struct { - ID string - Name string - Owner OwnerNode - Location LocationNode - IPAddresses []IPAddress `graphql:"IPAddresses" json:"IPAddresses"` - Ports Ports + ID string `graphql:"id" json:"id"` + Name string `graphql:"name" json:"name"` + Owner OwnerNode `graphql:"owner" json:"owner"` + Location LocationNode `graphql:"location" json:"location"` + IPAddresses []IPAddress `graphql:"IPAddresses" json:"IPAddresses"` + Metadata Metadata `graphql:"metadata" json:"metadata"` + Ports Ports `graphql:"ports" json:"ports"` } // GetLoadBalancer is a struct that represents the GetLoadBalancer GraphQL query @@ -73,9 +76,34 @@ type GetLoadBalancer struct { // IPAddress is a struct that represents the IPAddress GraphQL type type IPAddress struct { - ID string - IP string - Reserved bool + ID string `graphql:"id" json:"id"` + IP string `graphql:"ip" json:"ip"` + Reserved bool `graphql:"reserved" json:"reserved"` +} + +// MetadataStatusNode is a struct that represents the Metadata status node GraphQL type +type MetadataStatusNode struct { + ID string `graphql:"id" json:"id"` + Data json.RawMessage `graphql:"data"` + Source string `graphql:"source" json:"source"` + StatusNamespaceID string `graphql:"statusNamespaceID" json:"statusNamespaceID"` +} + +// MetadataStatusEdges is a struct that represents the Metadata status edges GraphQL type +type MetadataStatusEdges struct { + Node MetadataStatusNode `graphql:"node" json:"node"` +} + +// MetadataStatuses is a struct that represents the Metadata statuses GraphQL type +type MetadataStatuses struct { + Edges []MetadataStatusEdges `graphql:"edges" json:"edges"` +} + +// Metadata is a struct that represents the metadata GraphQL type +type Metadata struct { + ID string `graphql:"id" json:"id"` + NodeID string `graphql:"nodeID" json:"nodeID"` + Statuses MetadataStatuses `graphql:"statuses" json:"statuses"` } // Readable version of the above: @@ -84,7 +112,24 @@ type IPAddress struct { // ID string // Owner string // Name string -// IPAddressableFragment +// IPAddresses { +// id string +// ip string +// } +// metadata struct { +// id string +// nodeID string +// statuses struct { +// edges []struct { +// node struct { +// source string +// statusNamespaceID string +// id string +// data json bytes +// } +// } +// } +// } // Ports struct { // Edges []struct { // Node struct {