Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Functions api support #577

Merged
merged 6 commits into from
Nov 7, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 110 additions & 0 deletions functions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package godo

import (
"context"
"fmt"
"net/http"
"time"
)

const (
functionsBasePath = "/v2/functions/namespaces"
functionsNamespacePath = functionsBasePath + "/%s"
)

type FunctionsService interface {
Namespaces(context.Context) ([]FunctionsNamespace, *Response, error)
Namespace(context.Context, string) (*FunctionsNamespace, *Response, error)
ddebarros marked this conversation as resolved.
Show resolved Hide resolved
CreateNamespace(context.Context, *FunctionsNamespaceCreateOptions) (*FunctionsNamespace, *Response, error)
DeleteNamespace(context.Context, string) (*Response, error)
}

type FunctionsServiceOp struct {
client *Client
}

var _ FunctionsService = &FunctionsServiceOp{}

type namespacesRoot struct {
Namespaces []FunctionsNamespace `json:"namespaces,omitempty"`
}

type namespaceRoot struct {
Namespace *FunctionsNamespace `json:"namespace,omitempty"`
}

type FunctionsNamespace struct {
ApiHost string `json:"api_host,omitempty"`
Namespace string `json:"namespace,omitempty"`
CreatedAt time.Time `json:"created_at,omitempty"`
UpdatedAt time.Time `json:"updated_at,omitempty"`
Label string `json:"label,omitempty"`
Region string `json:"region,omitempty"`
UUID string `json:"uuid,omitempty"`
Key string `json:"key,omitempty"`
}

type FunctionsNamespaceCreateOptions struct {
ddebarros marked this conversation as resolved.
Show resolved Hide resolved
Label string `json:"label"`
Region string `json:"region"`
}

// Gets a list of namespaces
func (s *FunctionsServiceOp) Namespaces(ctx context.Context) ([]FunctionsNamespace, *Response, error) {
req, err := s.client.NewRequest(ctx, http.MethodGet, functionsBasePath, nil)
if err != nil {
return nil, nil, err
}
nsRoot := new(namespacesRoot)
resp, err := s.client.Do(ctx, req, nsRoot)
if err != nil {
return nil, resp, err
}
return nsRoot.Namespaces, resp, nil
}

// Gets a single namespace
func (s *FunctionsServiceOp) Namespace(ctx context.Context, namespace string) (*FunctionsNamespace, *Response, error) {
path := fmt.Sprintf(functionsNamespacePath, namespace)

req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
nsRoot := new(namespaceRoot)
resp, err := s.client.Do(ctx, req, nsRoot)
if err != nil {
return nil, resp, err
}
return nsRoot.Namespace, resp, nil
}

// Creates a namespace
func (s *FunctionsServiceOp) CreateNamespace(ctx context.Context, opts *FunctionsNamespaceCreateOptions) (*FunctionsNamespace, *Response, error) {
req, err := s.client.NewRequest(ctx, http.MethodPost, functionsBasePath, opts)
if err != nil {
return nil, nil, err
}
nsRoot := new(namespaceRoot)
resp, err := s.client.Do(ctx, req, nsRoot)
if err != nil {
return nil, resp, err
}
return nsRoot.Namespace, resp, nil
}

// Delete a namespace
func (s *FunctionsServiceOp) DeleteNamespace(ctx context.Context, namespace string) (*Response, error) {
path := fmt.Sprintf(functionsNamespacePath, namespace)

req, err := s.client.NewRequest(ctx, http.MethodDelete, path, nil)
if err != nil {
return nil, err
}

resp, err := s.client.Do(ctx, req, nil)
if err != nil {
return resp, err
}
return resp, nil
}
157 changes: 157 additions & 0 deletions functions_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
package godo

import (
"fmt"
"net/http"
"testing"
"time"

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

func TestFunctions_ListNamespaces(t *testing.T) {
setup()
defer teardown()

mux.HandleFunc("/v2/functions/namespaces", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, `{
"namespaces": [
{
"api_host": "https://faas.do.com",
"namespace": "123-abc",
"created_at": "2022-06-16T12:09:13Z",
"updated_at": "2022-06-16T12:09:13Z",
"label": "my-namespace-1",
"region": "nyc1",
"uuid": "",
"key": ""
},
{
"api_host": "https://faas.do.com",
"namespace": "456-abc",
"created_at": "2022-11-02T18:33:44Z",
"updated_at": "2022-11-02T18:33:44Z",
"label": "my-namespace-2",
"region": "nyc3",
"uuid": "",
"key": ""
}
]
}`)
})

namespaces, _, err := client.Functions.Namespaces(ctx)
require.NoError(t, err)

expectedNamespaces := []FunctionsNamespace{
{
ApiHost: "https://faas.do.com",
Namespace: "123-abc",
CreatedAt: time.Date(2022, 6, 16, 12, 9, 13, 0, time.UTC),
UpdatedAt: time.Date(2022, 6, 16, 12, 9, 13, 0, time.UTC),
Label: "my-namespace-1",
Region: "nyc1",
UUID: "",
Key: "",
},
{
ApiHost: "https://faas.do.com",
Namespace: "456-abc",
CreatedAt: time.Date(2022, 11, 2, 18, 33, 44, 0, time.UTC),
UpdatedAt: time.Date(2022, 11, 2, 18, 33, 44, 0, time.UTC),
Label: "my-namespace-2",
Region: "nyc3",
UUID: "",
Key: "",
},
}
assert.Equal(t, expectedNamespaces, namespaces)
}

func TestFunctions_GetNamespace(t *testing.T) {
setup()
defer teardown()

mux.HandleFunc("/v2/functions/namespaces/123-abc", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, `{
"namespace": {
"api_host": "https://faas.do.com",
"namespace": "123-abc",
"created_at": "2022-06-16T12:09:13Z",
"updated_at": "2022-06-16T12:09:13Z",
"label": "my-namespace-1",
"region": "nyc1",
"uuid": "123-456",
"key": "abc-123"
}
}`)
})

namespace, _, err := client.Functions.Namespace(ctx, "123-abc")
require.NoError(t, err)

expectedNamespace := &FunctionsNamespace{
ApiHost: "https://faas.do.com",
Namespace: "123-abc",
CreatedAt: time.Date(2022, 6, 16, 12, 9, 13, 0, time.UTC),
UpdatedAt: time.Date(2022, 6, 16, 12, 9, 13, 0, time.UTC),
Label: "my-namespace-1",
Region: "nyc1",
UUID: "123-456",
Key: "abc-123",
}
assert.Equal(t, expectedNamespace, namespace)
}

func TestFunctions_CreateNamespace(t *testing.T) {
setup()
defer teardown()

mux.HandleFunc("/v2/functions/namespaces", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodPost)
fmt.Fprint(w, `{
"namespace": {
"api_host": "https://faas.do.com",
"namespace": "123-abc",
"created_at": "2022-06-16T12:09:13Z",
"updated_at": "2022-06-16T12:09:13Z",
"label": "my-namespace-1",
"region": "nyc1",
"uuid": "123-456",
"key": "abc-123"
}
}`)
})

opts := FunctionsNamespaceCreateOptions{Label: "my-namespace-1", Region: "nyc1"}
namespace, _, err := client.Functions.CreateNamespace(ctx, &opts)
require.NoError(t, err)

expectedNamespace := &FunctionsNamespace{
ApiHost: "https://faas.do.com",
Namespace: "123-abc",
CreatedAt: time.Date(2022, 6, 16, 12, 9, 13, 0, time.UTC),
UpdatedAt: time.Date(2022, 6, 16, 12, 9, 13, 0, time.UTC),
Label: "my-namespace-1",
Region: "nyc1",
UUID: "123-456",
Key: "abc-123",
}
assert.Equal(t, expectedNamespace, namespace)
}

func TestFunctions_DeleteNamespace(t *testing.T) {
setup()
defer teardown()

mux.HandleFunc("/v2/functions/namespaces/123-abc", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodDelete)
})

_, err := client.Functions.DeleteNamespace(ctx, "123-abc")

assert.NoError(t, err)
}
3 changes: 2 additions & 1 deletion godo.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ type Client struct {
VPCs VPCsService
OneClick OneClickService
Monitoring MonitoringService
Functions FunctionsService

// Optional function called after every successful request made to the DO APIs
onRequestCompleted RequestCompletionCallback
Expand Down Expand Up @@ -248,7 +249,7 @@ func NewClient(httpClient *http.Client) *Client {
c.VPCs = &VPCsServiceOp{client: c}
c.OneClick = &OneClickServiceOp{client: c}
c.Monitoring = &MonitoringServiceOp{client: c}

c.Functions = &FunctionsServiceOp{client: c}
c.headers = make(map[string]string)

return c
Expand Down