Merge pull request #9 from platform9/function-raw-code-api

Base64 encode the code in json objects.
This commit is contained in:
Soam Vasani
2016-09-22 01:11:44 -07:00
committed by GitHub
3 changed files with 60 additions and 5 deletions
+4
View File
@@ -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)
+30
View File
@@ -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,35 @@ 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) {
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
+26 -5
View File
@@ -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)