Skip to content

Commit 4d27883

Browse files
committed
Introduces the /update API
1 parent a38a062 commit 4d27883

4 files changed

Lines changed: 32 additions & 13 deletions

File tree

docs/api.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@
3737
> | `503` | `text/plain` | | Creation failed |
3838
3939

40+
### Updating a function
41+
42+
<code>POST</code> <code><b>/update</b></code> (registers a new function or updates one)
43+
44+
See `/create` above. The only difference is that `/update` does not return any
45+
error if a function with the same name already exists.
46+
47+
4048

4149
------------------------------------------------------------------------------------------
4250
### Deleting a function

internal/api/api.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -132,22 +132,26 @@ func PollAsyncResult(c echo.Context) error {
132132
}
133133
}
134134

135-
// CreateFunction handles a function creation request.
136-
func CreateFunction(c echo.Context) error {
135+
// CreateOrUpdateFunction handles a function creation/update request.
136+
func CreateOrUpdateFunction(c echo.Context) error {
137137
var f function.Function
138138
err := json.NewDecoder(c.Request().Body).Decode(&f)
139139
if err != nil && err != io.EOF {
140140
log.Printf("Could not parse request: %v\n", err)
141141
return err
142142
}
143143

144-
_, ok := function.GetFunction(f.Name) // TODO: we would need a system-wide lock here...
145-
if ok {
146-
log.Printf("Dropping request for already existing function '%s'\n", f.Name)
147-
return c.String(http.StatusConflict, "")
148-
}
144+
if c.Path() != "/update" {
145+
_, ok := function.GetFunction(f.Name) // TODO: we would need a system-wide lock here...
146+
if ok {
147+
log.Printf("Dropping request for already existing function '%s'\n", f.Name)
148+
return c.String(http.StatusConflict, "")
149+
}
149150

150-
log.Printf("New request: creation of %s\n", f.Name)
151+
log.Printf("New request: creation of %s\n", f.Name)
152+
} else {
153+
log.Printf("New request: creation/update of %s\n", f.Name)
154+
}
151155

152156
// Check that the selected runtime exists
153157
if f.Runtime != container.CUSTOM_RUNTIME {

internal/api/server.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ func StartAPIServer(e *echo.Echo) {
2424

2525
// Routes
2626
e.POST("/invoke/:fun", InvokeFunction)
27-
e.POST("/create", CreateFunction)
27+
e.POST("/create", CreateOrUpdateFunction)
28+
e.POST("/update", CreateOrUpdateFunction)
2829
e.POST("/delete", DeleteFunction)
2930
e.GET("/function", GetFunctions)
3031
e.GET("/poll/:reqId", PollAsyncResult)

internal/cli/cli.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ var paramsFile string
101101
var asyncInvocation bool
102102
var verbose bool
103103
var returnOutput bool
104+
var update bool
104105

105106
func Init() {
106107
rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "verbose output")
@@ -117,6 +118,7 @@ func Init() {
117118
invokeCmd.Flags().BoolVarP(&returnOutput, "ret_output", "o", false, "Capture function output (if supported by used runtime)")
118119

119120
rootCmd.AddCommand(createCmd)
121+
createCmd.Flags().BoolVarP(&update, "update", "u", false, "Overwrite any function with the same name")
120122
createCmd.Flags().StringVarP(&funcName, "function", "f", "", "name of the function")
121123
createCmd.Flags().StringVarP(&runtime, "runtime", "", "python38", "runtime for the function")
122124
createCmd.Flags().StringVarP(&handler, "handler", "", "", "function handler (runtime specific)")
@@ -324,7 +326,12 @@ func create(cmd *cobra.Command, args []string) {
324326
showHelpAndExit(cmd)
325327
}
326328

327-
url := fmt.Sprintf("http://%s:%d/create", ServerConfig.Host, ServerConfig.Port)
329+
apiName := "create"
330+
if update {
331+
apiName = "update"
332+
}
333+
334+
url := fmt.Sprintf("http://%s:%d/%s", ServerConfig.Host, ServerConfig.Port, apiName)
328335
resp, err := utils.PostJson(url, requestBody)
329336
if err != nil {
330337
// TODO: check returned error code
@@ -471,9 +478,8 @@ func invokeWorkflow(cmd *cobra.Command, args []string) {
471478

472479
// Prepare request // TODO: it's ok to reuse the same type that function invocation uses?
473480
request := client.InvocationRequest{
474-
Params: paramsMap,
475-
QoSClass: api.DecodeServiceClass(qosClass),
476-
// QoSClass: qosClass,
481+
Params: paramsMap,
482+
QoSClass: api.DecodeServiceClass(qosClass),
477483
QoSMaxRespT: qosMaxRespT,
478484
CanDoOffloading: true,
479485
Async: asyncInvocation}

0 commit comments

Comments
 (0)