From 79fb54820e1ecbcd9b8b3d9a4506bf600a56c073 Mon Sep 17 00:00:00 2001 From: Soam Vasani Date: Thu, 22 Sep 2016 01:07:34 -0700 Subject: [PATCH 1/2] Base64 encode the code in json objects. This will make the API easier for clients. Also add a ?raw=1 flag to the function GET API, to get just the code of the function. --- controller/api_test.go | 4 ++++ controller/client/client.go | 28 ++++++++++++++++++++++++++++ controller/functionApi.go | 31 ++++++++++++++++++++++++++----- 3 files changed, 58 insertions(+), 5 deletions(-) diff --git a/controller/api_test.go b/controller/api_test.go index d7302804..b85008ed 100644 --- a/controller/api_test.go +++ b/controller/api_test.go @@ -56,6 +56,10 @@ func TestFunctionApi(t *testing.T) { uid1 := m.Uid log.Printf("Created function %v: %v", m.Name, m.Uid) + code, err := g.client.FunctionGetRaw(m) + panicIf(err) + assert(string(code) == testFunc.Code, "code from FunctionGetRaw must match created function") + testFunc.Code = "code2" m, err = g.client.FunctionUpdate(testFunc) panicIf(err) diff --git a/controller/client/client.go b/controller/client/client.go index 413f305c..84853390 100644 --- a/controller/client/client.go +++ b/controller/client/client.go @@ -18,6 +18,7 @@ package client import ( "bytes" + "encoding/base64" "encoding/json" "errors" "fmt" @@ -97,6 +98,10 @@ func (c *Client) handleResponse(resp *http.Response) ([]byte, error) { } func (c *Client) FunctionCreate(f *fission.Function) (*fission.Metadata, error) { + orig := f.Code + f.Code = base64.StdEncoding.EncodeToString([]byte(f.Code)) + defer func() { f.Code = orig }() + reqbody, err := json.Marshal(f) if err != nil { return nil, err @@ -149,10 +154,33 @@ func (c *Client) FunctionGet(m *fission.Metadata) (*fission.Function, error) { return nil, err } + dec, err := base64.StdEncoding.DecodeString(f.Code) + if err != nil { + return nil, err + } + f.Code = string(dec) + return &f, nil } +func (c *Client) FunctionGetRaw(m *fission.Metadata) ([]byte, error) { + relativeUrl := fmt.Sprintf("functions/%v?raw=1", m.Name) + if len(m.Uid) > 0 { + relativeUrl += fmt.Sprintf("&uid=%v", m.Uid) + } + + resp, err := http.Get(c.url(relativeUrl)) + if err != nil { + return nil, err + } + defer resp.Body.Close() + + return c.handleResponse(resp) +} + func (c *Client) FunctionUpdate(f *fission.Function) (*fission.Metadata, error) { + f.Code = base64.StdEncoding.EncodeToString([]byte(f.Code)) + reqbody, err := json.Marshal(f) if err != nil { return nil, err diff --git a/controller/functionApi.go b/controller/functionApi.go index 5319cd35..0ba6e76c 100644 --- a/controller/functionApi.go +++ b/controller/functionApi.go @@ -24,6 +24,7 @@ import ( log "github.com/Sirupsen/logrus" "github.com/gorilla/mux" + "encoding/base64" "github.com/platform9/fission" ) @@ -56,6 +57,13 @@ func (api *API) FunctionApiCreate(w http.ResponseWriter, r *http.Request) { return } + dec, err := base64.StdEncoding.DecodeString(f.Code) + if err != nil { + api.respondWithError(w, err) + return + } + f.Code = string(dec) + uid, err := api.FunctionStore.Create(&f) if err != nil { api.respondWithError(w, err) @@ -78,6 +86,7 @@ func (api *API) FunctionApiGet(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) m.Name = vars["function"] m.Uid = r.FormValue("uid") // empty if uid is absent + raw := r.FormValue("raw") // just the code f, err := api.FunctionStore.Get(&m) if err != nil { @@ -85,12 +94,17 @@ func (api *API) FunctionApiGet(w http.ResponseWriter, r *http.Request) { return } - resp, err := json.Marshal(f) - if err != nil { - api.respondWithError(w, err) - return + var resp []byte + if raw != "" { + resp = []byte(f.Code) + } else { + f.Code = base64.StdEncoding.EncodeToString([]byte(f.Code)) + resp, err = json.Marshal(f) + if err != nil { + api.respondWithError(w, err) + return + } } - api.respondWithSuccess(w, resp) } @@ -116,6 +130,13 @@ func (api *API) FunctionApiUpdate(w http.ResponseWriter, r *http.Request) { return } + dec, err := base64.StdEncoding.DecodeString(f.Code) + if err != nil { + api.respondWithError(w, err) + return + } + f.Code = string(dec) + uid, err := api.FunctionStore.Update(&f) if err != nil { api.respondWithError(w, err) From 9a65c8a6fbba9119aa766ce6aa8a3367e62e15c1 Mon Sep 17 00:00:00 2001 From: Soam Vasani Date: Thu, 22 Sep 2016 01:11:17 -0700 Subject: [PATCH 2/2] Undo changes to client function obj --- controller/client/client.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/controller/client/client.go b/controller/client/client.go index 84853390..cf26213a 100644 --- a/controller/client/client.go +++ b/controller/client/client.go @@ -179,7 +179,9 @@ func (c *Client) FunctionGetRaw(m *fission.Metadata) ([]byte, error) { } func (c *Client) FunctionUpdate(f *fission.Function) (*fission.Metadata, error) { + orig := f.Code f.Code = base64.StdEncoding.EncodeToString([]byte(f.Code)) + defer func() { f.Code = orig }() reqbody, err := json.Marshal(f) if err != nil {