Skip to content
This repository has been archived by the owner on Dec 16, 2020. It is now read-only.

Commit

Permalink
Update faas-provider to 0.14.0
Browse files Browse the repository at this point in the history
**What**
- Update the faas-provider to 0.14.0
- Update the types used in the secrets handler to use the Secret struct
  from the faas-provider
- Remove the FunctionDeleteRequest import so that we can remove the
  gateway from the vendor list. This decouples the two projects better
  and removes a dependency change that would require updating  the gateway
  provider version to update this project as well
- Test the DeleteHandler
- Rename the proivder info variables to use the same names as in
  faas-netes and faas-containerd, making the info handler code more
  portable

Relates to openfaas/faas-provider#38

Signed-off-by: Lucas Roesler <[email protected]>
  • Loading branch information
LucasRoesler authored and alexellis committed Feb 2, 2020
1 parent ee713b2 commit bb8b883
Show file tree
Hide file tree
Showing 16 changed files with 244 additions and 199 deletions.
15 changes: 3 additions & 12 deletions Gopkg.lock

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

2 changes: 1 addition & 1 deletion Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

[[constraint]]
name = "github.com/openfaas/faas-provider"
version = "0.13.2"
version = "0.14.0"

[[constraint]]
name = "github.com/docker/go-units"
Expand Down
18 changes: 14 additions & 4 deletions handlers/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,26 @@ import (

"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/client"
"github.com/openfaas/faas/gateway/requests"
"github.com/docker/docker/api/types/swarm"
)

type deleteFunctionRequest struct {
FunctionName string `json:"functionName"`
}

// ServiceDeleter is the sub-interface of client.ServiceAPIClient that is required for deleting
// a OpenFaaS Function. This interface is satisfied by *client.Client
type ServiceDeleter interface {
ServiceList(ctx context.Context, options types.ServiceListOptions) ([]swarm.Service, error)
ServiceRemove(ctx context.Context, serviceID string) error
}

// DeleteHandler delete a function
func DeleteHandler(c *client.Client) http.HandlerFunc {
func DeleteHandler(c ServiceDeleter) http.HandlerFunc {

return func(w http.ResponseWriter, r *http.Request) {

req := requests.DeleteFunctionRequest{}
req := deleteFunctionRequest{}
defer r.Body.Close()
reqData, _ := ioutil.ReadAll(r.Body)
unmarshalErr := json.Unmarshal(reqData, &req)
Expand Down
111 changes: 111 additions & 0 deletions handlers/delete_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package handlers

import (
"context"
"errors"
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"

"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/swarm"
)

type fakeServiceDeleter struct {
services []swarm.Service
listErr error
removeErr error
}

func (s fakeServiceDeleter) ServiceList(ctx context.Context, options types.ServiceListOptions) ([]swarm.Service, error) {
return s.services, s.listErr
}

func (s fakeServiceDeleter) ServiceRemove(ctx context.Context, serviceID string) error {
return s.removeErr
}

func Test_DeleteHandler(t *testing.T) {

cases := []struct {
name string
funcName string
services []swarm.Service
listErr error
removeErr error
expectedCode int
}{
{
name: "parsing error returns StatusBadRequest",
expectedCode: http.StatusBadRequest,
},
{
name: "listing error returns StatusNotFound",
funcName: "test-func",
listErr: errors.New("failed to list functions"),
expectedCode: http.StatusNotFound,
},
{
name: "remove error returns StatusInternalServerError",
funcName: "test-func",
services: []swarm.Service{
{
ID: "test-func-id",
Spec: swarm.ServiceSpec{
Annotations: swarm.Annotations{Name: "test-func"},
TaskTemplate: swarm.TaskSpec{
ContainerSpec: &swarm.ContainerSpec{
Labels: map[string]string{"function": "true"},
},
},
},
},
},
removeErr: errors.New("failed to delete function"),
expectedCode: http.StatusInternalServerError,
},
{
name: "returns Accepted when no errors",
funcName: "test-func",
services: []swarm.Service{
{
ID: "test-func-id",
Spec: swarm.ServiceSpec{
Annotations: swarm.Annotations{Name: "test-func"},
TaskTemplate: swarm.TaskSpec{
ContainerSpec: &swarm.ContainerSpec{
Labels: map[string]string{"function": "true"},
},
},
},
},
},
expectedCode: http.StatusAccepted,
},
}

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
client := fakeServiceDeleter{
listErr: tc.listErr,
removeErr: tc.removeErr,
services: tc.services,
}
handler := DeleteHandler(client)

payload := fmt.Sprintf(`{"functionName": %q}`, tc.funcName)

w := httptest.NewRecorder()
r := httptest.NewRequest("POST", "/", strings.NewReader(payload))
handler(w, r)

if w.Code != tc.expectedCode {
t.Fatalf("expected status code %d, got %d", tc.expectedCode, w.Code)
}

})
}

}
20 changes: 10 additions & 10 deletions handlers/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import (
"encoding/json"
"net/http"

"github.com/openfaas/faas-provider/types"
typesv1 "github.com/openfaas/faas-provider/types"
)

const (
//SwarmIdentifier identifier string for swarm provider
SwarmIdentifier = "swarm"
//SwarmProvider provider string for swarm provider
SwarmProvider = "faas-swarm"
//OrchestrationIdentifier identifier string for swarm provider
OrchestrationIdentifier = "swarm"
//ProviderName provider string for swarm provider
ProviderName = "faas-swarm"
)

//MakeInfoHandler creates handler for /system/info endpoint
Expand All @@ -21,16 +21,16 @@ func MakeInfoHandler(version, sha string) http.HandlerFunc {
defer r.Body.Close()
}

infoRequest := types.InfoRequest{
Orchestration: SwarmIdentifier,
Provider: SwarmProvider,
Version: types.ProviderVersion{
infoResponse := typesv1.InfoResponse{
Orchestration: OrchestrationIdentifier,
Provider: ProviderName,
Version: typesv1.ProviderVersion{
Release: version,
SHA: sha,
},
}

jsonOut, marshalErr := json.Marshal(infoRequest)
jsonOut, marshalErr := json.Marshal(infoResponse)
if marshalErr != nil {
w.WriteHeader(http.StatusInternalServerError)
return
Expand Down
39 changes: 39 additions & 0 deletions handlers/info_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package handlers

import (
"encoding/json"
"github.com/openfaas/faas-provider/types"
"net/http/httptest"
"testing"
)

func Test_InfoHandler(t *testing.T) {
sha := "4b825dc642cb6eb9a060e54bf8d69288fbee4904"
version := "0.0.1"
handler := MakeInfoHandler(version, sha)
w := httptest.NewRecorder()
r := httptest.NewRequest("GET", "/", nil)
handler(w, r)

resp := types.InfoResponse{}
err := json.Unmarshal(w.Body.Bytes(), &resp)
if err != nil {
t.Fatalf("unexpected error unmarshalling the response")
}

if resp.Provider != ProviderName {
t.Fatalf("expected provider %q, got %q", ProviderName, resp.Provider)
}

if resp.Orchestration != OrchestrationIdentifier {
t.Fatalf("expected orchestration %q, got %q", OrchestrationIdentifier, resp.Orchestration)
}

if resp.Version.SHA != sha {
t.Fatalf("expected orchestration %q, got %q", sha, resp.Version.SHA)
}

if resp.Version.Release != version {
t.Fatalf("expected release %q, got %q", version, resp.Version.Release)
}
}
53 changes: 47 additions & 6 deletions handlers/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ import (

"github.com/docker/cli/opts"
"github.com/docker/docker/api/types/filters"
typesv1 "github.com/openfaas/faas-provider/types"

"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/swarm"
"github.com/docker/docker/client"

"github.com/openfaas/faas/gateway/requests"
)

var (
Expand Down Expand Up @@ -130,10 +129,10 @@ func getSecrets(c client.SecretAPIClient, _ []byte) (responseStatus int, respons
)
}

results := []requests.Secret{}
results := []typesv1.Secret{}

for _, s := range secrets {
results = append(results, requests.Secret{Name: s.Spec.Name, Value: string(s.Spec.Data)})
results = append(results, typesv1.Secret{Name: s.Spec.Name, Value: string(s.Spec.Data)})
}

resultsJSON, marshalErr := json.Marshal(results)
Expand All @@ -148,7 +147,7 @@ func getSecrets(c client.SecretAPIClient, _ []byte) (responseStatus int, respons
}

func createNewSecret(c client.SecretAPIClient, body []byte) (responseStatus int, responseBody []byte, err error) {
var secret requests.Secret
var secret typesv1.Secret

unmarshalErr := json.Unmarshal(body, &secret)
if unmarshalErr != nil {
Expand Down Expand Up @@ -177,8 +176,50 @@ func createNewSecret(c client.SecretAPIClient, body []byte) (responseStatus int,
return http.StatusCreated, nil, nil
}

func updateSecret(c client.SecretAPIClient, body []byte) (responseStatus int, responseBody []byte, err error) {
var secret typesv1.Secret

unmarshalErr := json.Unmarshal(body, &secret)
if unmarshalErr != nil {
return http.StatusBadRequest, nil, fmt.Errorf(
"error unmarshaling secret in secretPutHandler: %s",
unmarshalErr,
)
}

foundSecret, status, getSecretErr := getSecretWithName(c, secret.Name)
if getSecretErr != nil {
return status, nil, fmt.Errorf(
"cannot get secret with name: %s. Error: %s",
secret.Name,
getSecretErr.Error(),
)
}

updateSecretErr := c.SecretUpdate(context.Background(), foundSecret.ID, foundSecret.Version, swarm.SecretSpec{
Annotations: swarm.Annotations{
Name: secret.Name,
Labels: map[string]string{
ownerLabel: ownerLabelValue,
},
},
Data: []byte(secret.Value),
})

if updateSecretErr != nil {
return http.StatusInternalServerError, nil, fmt.Errorf(
"couldn't update secret (name: %s, ID: %s) because of error: %s",
secret.Name,
foundSecret.ID,
updateSecretErr.Error(),
)
}

return http.StatusOK, nil, nil
}

func deleteSecret(c client.SecretAPIClient, body []byte) (responseStatus int, responseBody []byte, err error) {
var secret requests.Secret
var secret typesv1.Secret

unmarshalErr := json.Unmarshal(body, &secret)
if unmarshalErr != nil {
Expand Down
4 changes: 2 additions & 2 deletions handlers/secrets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/swarm"
"github.com/openfaas/faas/gateway/requests"
typesv1 "github.com/openfaas/faas-provider/types"
)

func genFakeSecret(name string, data string, includeOwnerLabel bool) swarm.Secret {
Expand Down Expand Up @@ -185,7 +185,7 @@ func Test_SecretsHandler(t *testing.T) {

decoder := json.NewDecoder(resp.Body)

secretList := []requests.Secret{}
secretList := []typesv1.Secret{}
err := decoder.Decode(&secretList)
if err != nil {
t.Error(err)
Expand Down
Loading

0 comments on commit bb8b883

Please sign in to comment.