From 76d4ea7b042a94e3b9298af2d38be9474092f314 Mon Sep 17 00:00:00 2001 From: Kaustubh Phatak Date: Thu, 2 Feb 2017 15:14:00 -0800 Subject: [PATCH] function code download using HTTP URL (#100) * function code download using HTTP URL * Addressing review comments * Updating help-text for function --- fission/function.go | 41 ++++++++++++++++++++++++++++++++++++----- fission/main.go | 2 +- 2 files changed, 37 insertions(+), 6 deletions(-) diff --git a/fission/function.go b/fission/function.go index 0530164f..1f6c0e56 100644 --- a/fission/function.go +++ b/fission/function.go @@ -17,10 +17,13 @@ limitations under the License. package main import ( + "errors" "fmt" "io/ioutil" + "net/http" "os" "os/exec" + "strings" "text/tabwriter" "github.com/satori/go.uuid" @@ -29,6 +32,36 @@ import ( "github.com/fission/fission" ) +func fnFetchCode(filePath string) []byte { + var code []byte + var err error + + if strings.HasPrefix(filePath, "http://") || strings.HasPrefix(filePath, "https://") { + var resp *http.Response + resp, err = http.Get(filePath) + if err != nil { + checkErr(err, fmt.Sprintf("download function")) + } + + defer resp.Body.Close() + if resp.StatusCode != http.StatusOK { + + err = errors.New(fmt.Sprintf("%v - HTTP response returned non 200 status", + resp.StatusCode)) + checkErr(err, fmt.Sprintf("download function")) + } + + code, err = ioutil.ReadAll(resp.Body) + if err != nil { + checkErr(err, fmt.Sprintf("download function body %v", filePath)) + } + } else { + code, err = ioutil.ReadFile(filePath) + checkErr(err, fmt.Sprintf("read %v", filePath)) + } + return code +} + func fnCreate(c *cli.Context) error { client := getClient(c.GlobalString("server")) @@ -47,8 +80,7 @@ func fnCreate(c *cli.Context) error { fatal("Need --code argument.") } - code, err := ioutil.ReadFile(fileName) - checkErr(err, fmt.Sprintf("read %v", fileName)) + code := fnFetchCode(fileName) function := &fission.Function{ Metadata: fission.Metadata{Name: fnName}, @@ -56,7 +88,7 @@ func fnCreate(c *cli.Context) error { Code: string(code), } - _, err = client.FunctionCreate(function) + _, err := client.FunctionCreate(function) checkErr(err, "create function") fmt.Printf("function '%v' created\n", fnName) @@ -138,8 +170,7 @@ func fnUpdate(c *cli.Context) error { fileName := c.String("code") if len(fileName) > 0 { - code, err := ioutil.ReadFile(fileName) - checkErr(err, fmt.Sprintf("read %v", fileName)) + code := fnFetchCode(fileName) function.Code = string(code) } diff --git a/fission/main.go b/fission/main.go index f02986ed..c1f0d292 100644 --- a/fission/main.go +++ b/fission/main.go @@ -38,7 +38,7 @@ func main() { // functions fnNameFlag := cli.StringFlag{Name: "name", Usage: "function name"} fnEnvNameFlag := cli.StringFlag{Name: "env", Usage: "environment name for function"} - fnCodeFlag := cli.StringFlag{Name: "code", Usage: "file containing source code, or - for stdin"} + fnCodeFlag := cli.StringFlag{Name: "code", Usage: "local path or URL for source code"} fnUidFlag := cli.StringFlag{Name: "uid", Usage: "function uid, optional (use latest if unspecified)"} fnSubcommands := []cli.Command{ {Name: "create", Usage: "Create new function (and optionally, an HTTP route to it)", Flags: []cli.Flag{fnNameFlag, fnEnvNameFlag, fnCodeFlag, htUrlFlag, htMethodFlag}, Action: fnCreate},