This repository has been archived by the owner on Nov 16, 2020. It is now read-only.
/
create_function.go
148 lines (136 loc) · 4.79 KB
/
create_function.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
///////////////////////////////////////////////////////////////////////
// Copyright (c) 2017 VMware, Inc. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
///////////////////////////////////////////////////////////////////////
package cmd
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"path"
"github.com/go-openapi/spec"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"golang.org/x/net/context"
"github.com/vmware/dispatch/pkg/api/v1"
"github.com/vmware/dispatch/pkg/client"
"github.com/vmware/dispatch/pkg/dispatchcli/i18n"
"github.com/vmware/dispatch/pkg/utils"
)
var (
createFunctionLong = i18n.T(`Create dispatch function.`)
// TODO: add examples
createFunctionExample = i18n.T(``)
depsImage = ""
handler = ""
schemaInFile = ""
schemaOutFile = ""
fnSecrets []string
fnServices []string
timeout int64
)
// NewCmdCreateFunction creates command responsible for dispatch function creation.
func NewCmdCreateFunction(out io.Writer, errOut io.Writer) *cobra.Command {
cmd := &cobra.Command{
Use: "function NAME PATH --image=IMAGE --handler=HANDLER [--schema-in=FILE] [--schema-out=FILE]",
Short: i18n.T("Create function"),
Long: createFunctionLong,
Example: createFunctionExample,
Args: cobra.ExactArgs(2),
Run: func(cmd *cobra.Command, args []string) {
c := functionManagerClient()
err := createFunction(out, errOut, cmd, args, c)
CheckErr(err)
},
}
cmd.Flags().StringVar(&depsImage, "image", "", "REQUIRED: image to build function on")
cmd.Flags().StringVar(&handler, "handler", "", "REQUIRED: fully-qualified function impl name (e.g. Java class or Python def)")
cmd.Flags().StringVarP(&cmdFlagApplication, "application", "a", "", "associate with an application")
cmd.Flags().StringVar(&schemaInFile, "schema-in", "", "path to file with input validation schema")
cmd.Flags().StringVar(&schemaOutFile, "schema-out", "", "path to file with output validation schema")
cmd.Flags().StringArrayVar(&fnSecrets, "secret", []string{}, "Function secrets, can be specified multiple times or a comma-delimited string")
cmd.Flags().StringArrayVar(&fnServices, "service", []string{}, "Service instances this function uses, can be specified multiple times or a comma-delimited string")
cmd.Flags().Int64Var(&timeout, "timeout", 0, "A timeout to limit function execution time (in milliseconds). Default: 0 (no timeout)")
cmd.MarkFlagRequired("image")
return cmd
}
// CallCreateFunction makes the API call to create a function
func CallCreateFunction(c client.FunctionsClient) ModelAction {
return func(f interface{}) error {
function := f.(*v1.Function)
// How will we verify that the user has permissions for the org?
created, err := c.CreateFunction(context.TODO(), "", function)
if err != nil {
return err
}
*function = *created
return nil
}
}
func createFunction(out, errOut io.Writer, cmd *cobra.Command, args []string, c client.FunctionsClient) error {
sourcePath := args[1]
isDir, err := utils.IsDir(sourcePath)
if err != nil {
return err
}
if isDir && handler == "" {
return fmt.Errorf("error creating function %s: handler is required, source path %s is a directory", args[0], sourcePath)
}
codeFileContent, err := utils.TarGzBytes(sourcePath)
if err != nil {
return errors.Wrapf(err, "error reading %s", sourcePath)
}
function := &v1.Function{
Image: &depsImage,
Name: &args[0],
Source: codeFileContent,
Handler: handler,
Secrets: fnSecrets,
Services: fnServices,
Timeout: timeout,
Tags: []*v1.Tag{},
}
if cmdFlagApplication != "" {
function.Tags = append(function.Tags, &v1.Tag{
Key: "Application",
Value: cmdFlagApplication,
})
}
var schemaIn, schemaOut *spec.Schema
if schemaInFile != "" {
fullPath := path.Join(workDir, schemaInFile)
schemaInContent, err := ioutil.ReadFile(fullPath)
if err != nil {
return errors.Wrapf(err, "error when reading content of %s", fullPath)
}
schemaIn = new(spec.Schema)
if err := json.Unmarshal(schemaInContent, schemaIn); err != nil {
return errors.Wrapf(err, "error when parsing JSON from %s", fullPath)
}
}
if schemaOutFile != "" {
fullPath := path.Join(workDir, schemaOutFile)
schemaOutContent, err := ioutil.ReadFile(fullPath)
if err != nil {
return errors.Wrapf(err, "error when reading content of %s", fullPath)
}
schemaOut = new(spec.Schema)
if err := json.Unmarshal(schemaOutContent, schemaOut); err != nil {
return errors.Wrapf(err, "error when parsing JSON from %s", fullPath)
}
}
function.Schema = &v1.Schema{
In: schemaIn,
Out: schemaOut,
}
err = CallCreateFunction(c)(function)
if err != nil {
return err
}
if w, err := formatOutput(out, false, function); w {
return err
}
fmt.Fprintf(out, "Created function: %s\n", *function.Name)
return nil
}