-
Notifications
You must be signed in to change notification settings - Fork 44
/
action.go
316 lines (273 loc) · 12.1 KB
/
action.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package whisk
import (
"errors"
"fmt"
"github.com/apache/openwhisk-client-go/wski18n"
"net/http"
"net/url"
"strings"
)
type ActionService struct {
client ClientInterface
}
type Action struct {
Namespace string `json:"namespace,omitempty"`
Name string `json:"name,omitempty"`
Version string `json:"version,omitempty"`
Exec *Exec `json:"exec,omitempty"`
Annotations KeyValueArr `json:"annotations,omitempty"`
DelAnnotations []string `json:"delAnnotations,omitempty"`
Parameters KeyValueArr `json:"parameters,omitempty"`
Limits *Limits `json:"limits,omitempty"`
Error string `json:"error,omitempty"`
Code int `json:"code,omitempty"`
Publish *bool `json:"publish,omitempty"`
Updated int64 `json:"updated,omitempty"`
}
type Exec struct {
Kind string `json:"kind,omitempty"`
Code *string `json:"code,omitempty"`
Image string `json:"image,omitempty"`
Init string `json:"init,omitempty"`
Main string `json:"main,omitempty"`
Components []string `json:"components,omitempty"` // List of fully qualified actions
Binary *bool `json:"binary,omitempty"`
}
type ActionListOptions struct {
Limit int `url:"limit"`
Skip int `url:"skip"`
Docs bool `url:"docs,omitempty"`
}
// Compare(sortable) compares action to sortable for the purpose of sorting.
// REQUIRED: sortable must also be of type Action.
// ***Method of type Sortable***
func (action Action) Compare(sortable Sortable) bool {
// Sorts alphabetically by NAMESPACE -> PACKAGE_NAME -> ACTION_NAME, with
// actions under default package at the top.
var actionString string
var compareString string
actionToCompare := sortable.(Action)
actionString = strings.ToLower(fmt.Sprintf("%s%s", action.Namespace, action.Name))
compareString = strings.ToLower(fmt.Sprintf("%s%s", actionToCompare.Namespace,
actionToCompare.Name))
if strings.Contains(action.Namespace, "/") && !strings.Contains(actionToCompare.Namespace, "/") {
return false
} else if !strings.Contains(action.Namespace, "/") && strings.Contains(actionToCompare.Namespace, "/") {
return true
} else if strings.Contains(action.Namespace, "/") && strings.Contains(actionToCompare.Namespace, "/") {
return actionString < compareString
} else {
return action.Name < actionToCompare.Name
}
}
// ToHeaderString() returns the header for a list of actions
func (action Action) ToHeaderString() string {
return fmt.Sprintf("%s\n", "actions")
}
// ToSummaryRowString() returns a compound string of required parameters for printing
// from CLI command `wsk action list`.
// ***Method of type Sortable***
func (action Action) ToSummaryRowString() string {
var kind string
publishState := wski18n.T("private")
for i := range action.Annotations {
if action.Annotations[i].Key == "exec" {
kind = action.Annotations[i].Value.(string)
break
}
}
return fmt.Sprintf("%-70s %s %s\n", fmt.Sprintf("/%s/%s", action.Namespace, action.Name), publishState, kind)
}
/*
Determines if an action is a web action by examining the action's annotations. A value of true is returned if the
action's annotations contains a "web-export" key and its associated value is a boolean value of "true". Otherwise, false
is returned.
*/
func (action Action) WebAction() (webExportValue bool) {
webExport := action.Annotations.GetValue("web-export")
webExportValue, _ = webExport.(bool)
Debug(DbgInfo, "Web export value is '%t'\n", webExportValue)
return webExportValue
}
/*
Returns the URL of an action as a string. A valid API host, path and version must be passed. A package that contains the
action must be passed as well. An empty string must be passed if the action is not packaged.
*/
func (action Action) ActionURL(apiHost string, apiPath string, apiVersion string, pkg string) (string, error) {
baseURL, err := GetURLBase(apiHost, apiPath)
if err != nil {
Debug(DbgError, "GetURLBase(%s, %s) failed: %s\n", apiHost, apiPath, err)
return "", err
}
webActionPath := "%s/%s/web/%s/%s/%s"
actionPath := "%s/%s/namespaces/%s/actions/%s"
packagedActionPath := actionPath + "/%s"
namespace := strings.Split(action.Namespace, "/")[0]
namespace = strings.Replace(url.QueryEscape(namespace), "+", "%20", -1)
name := strings.Replace(url.QueryEscape(action.Name), "+", "%20", -1)
pkg = strings.Replace(url.QueryEscape(pkg), "+", "%20", -1)
var actionURL string
if action.WebAction() {
if len(pkg) == 0 {
pkg = "default"
}
actionURL = fmt.Sprintf(webActionPath, baseURL, apiVersion, namespace, pkg, name)
Debug(DbgInfo, "Web action URL: %s\n", actionURL)
} else {
if len(pkg) == 0 {
actionURL = fmt.Sprintf(actionPath, baseURL, apiVersion, namespace, name)
Debug(DbgInfo, "Packaged action URL: %s\n", actionURL)
} else {
actionURL = fmt.Sprintf(packagedActionPath, baseURL, apiVersion, namespace, pkg, name)
Debug(DbgInfo, "Action URL: %s\n", actionURL)
}
}
return actionURL, nil
}
////////////////////
// Action Methods //
////////////////////
func (s *ActionService) List(packageName string, options *ActionListOptions) ([]Action, *http.Response, error) {
var route string
var actions []Action
if len(packageName) > 0 {
// Encode resource name as a path (with no query params) before inserting it into the URI
// This way any '?' chars in the name won't be treated as the beginning of the query params
packageName = (&url.URL{Path: packageName}).String()
route = fmt.Sprintf("actions/%s/", packageName)
} else {
route = fmt.Sprintf("actions")
}
routeUrl, err := addRouteOptions(route, options)
if err != nil {
Debug(DbgError, "addRouteOptions(%s, %#v) error: '%s'\n", route, options, err)
errMsg := wski18n.T("Unable to add route options '{{.options}}'",
map[string]interface{}{"options": options})
whiskErr := MakeWskErrorFromWskError(errors.New(errMsg), err, EXIT_CODE_ERR_GENERAL, DISPLAY_MSG,
NO_DISPLAY_USAGE)
return nil, nil, whiskErr
}
Debug(DbgError, "Action list route with options: %s\n", route)
req, err := s.client.NewRequestUrl("GET", routeUrl, nil, IncludeNamespaceInUrl, AppendOpenWhiskPathPrefix, EncodeBodyAsJson, AuthRequired)
if err != nil {
Debug(DbgError, "http.NewRequestUrl(GET, %s, nil, IncludeNamespaceInUrl, AppendOpenWhiskPathPrefix, EncodeBodyAsJson, AuthRequired) error: '%s'\n", routeUrl, err)
errMsg := wski18n.T("Unable to create HTTP request for GET '{{.route}}': {{.err}}",
map[string]interface{}{"route": routeUrl, "err": err})
whiskErr := MakeWskErrorFromWskError(errors.New(errMsg), err, EXIT_CODE_ERR_NETWORK, DISPLAY_MSG,
NO_DISPLAY_USAGE)
return nil, nil, whiskErr
}
resp, err := s.client.Do(req, &actions, ExitWithSuccessOnTimeout)
if err != nil {
Debug(DbgError, "s.client.Do() error - HTTP req %s; error '%s'\n", req.URL.String(), err)
return nil, resp, err
}
return actions, resp, err
}
func (s *ActionService) Insert(action *Action, overwrite bool) (*Action, *http.Response, error) {
// Encode resource name as a path (with no query params) before inserting it into the URI
// This way any '?' chars in the name won't be treated as the beginning of the query params
actionName := (&url.URL{Path: action.Name}).String()
route := fmt.Sprintf("actions/%s?overwrite=%t", actionName, overwrite)
Debug(DbgInfo, "Action insert route: %s\n", route)
req, err := s.client.NewRequest("PUT", route, action, IncludeNamespaceInUrl)
if err != nil {
Debug(DbgError, "http.NewRequest(PUT, %s, %#v) error: '%s'\n", route, action, err)
errMsg := wski18n.T("Unable to create HTTP request for PUT '{{.route}}': {{.err}}",
map[string]interface{}{"route": route, "err": err})
whiskErr := MakeWskErrorFromWskError(errors.New(errMsg), err, EXIT_CODE_ERR_NETWORK, DISPLAY_MSG,
NO_DISPLAY_USAGE)
return nil, nil, whiskErr
}
a := new(Action)
resp, err := s.client.Do(req, &a, ExitWithSuccessOnTimeout)
if err != nil {
Debug(DbgError, "s.client.Do() error - HTTP req %s; error '%s'\n", req.URL.String(), err)
return nil, resp, err
}
return a, resp, nil
}
func (s *ActionService) Get(actionName string, fetchCode bool) (*Action, *http.Response, error) {
// Encode resource name as a path (with no query params) before inserting it into the URI
// This way any '?' chars in the name won't be treated as the beginning of the query params
actionName = (&url.URL{Path: actionName}).String()
route := fmt.Sprintf("actions/%s?code=%t", actionName, fetchCode)
req, err := s.client.NewRequest("GET", route, nil, IncludeNamespaceInUrl)
if err != nil {
Debug(DbgError, "http.NewRequest(GET, %s, nil) error: '%s'\n", route, err)
errMsg := wski18n.T("Unable to create HTTP request for GET '{{.route}}': {{.err}}",
map[string]interface{}{"route": route, "err": err})
whiskErr := MakeWskErrorFromWskError(errors.New(errMsg), err, EXIT_CODE_ERR_NETWORK, DISPLAY_MSG,
NO_DISPLAY_USAGE)
return nil, nil, whiskErr
}
a := new(Action)
resp, err := s.client.Do(req, &a, ExitWithSuccessOnTimeout)
if err != nil {
Debug(DbgError, "s.client.Do() error - HTTP req %s; error '%s'\n", req.URL.String(), err)
return nil, resp, err
}
return a, resp, nil
}
func (s *ActionService) Delete(actionName string) (*http.Response, error) {
// Encode resource name as a path (with no query params) before inserting it into the URI
// This way any '?' chars in the name won't be treated as the beginning of the query params
actionName = (&url.URL{Path: actionName}).String()
route := fmt.Sprintf("actions/%s", actionName)
Debug(DbgInfo, "HTTP route: %s\n", route)
req, err := s.client.NewRequest("DELETE", route, nil, IncludeNamespaceInUrl)
if err != nil {
Debug(DbgError, "http.NewRequest(DELETE, %s, nil) error: '%s'\n", route, err)
errMsg := wski18n.T("Unable to create HTTP request for DELETE '{{.route}}': {{.err}}",
map[string]interface{}{"route": route, "err": err})
whiskErr := MakeWskErrorFromWskError(errors.New(errMsg), err, EXIT_CODE_ERR_NETWORK, DISPLAY_MSG,
NO_DISPLAY_USAGE)
return nil, whiskErr
}
a := new(Action)
resp, err := s.client.Do(req, a, ExitWithSuccessOnTimeout)
if err != nil {
Debug(DbgError, "s.client.Do() error - HTTP req %s; error '%s'\n", req.URL.String(), err)
return resp, err
}
return resp, nil
}
func (s *ActionService) Invoke(actionName string, payload interface{}, blocking bool, result bool) (map[string]interface{}, *http.Response, error) {
var res map[string]interface{}
// Encode resource name as a path (with no query params) before inserting it into the URI
// This way any '?' chars in the name won't be treated as the beginning of the query params
actionName = (&url.URL{Path: actionName}).String()
route := fmt.Sprintf("actions/%s?blocking=%t&result=%t", actionName, blocking, result)
Debug(DbgInfo, "HTTP route: %s\n", route)
req, err := s.client.NewRequest("POST", route, payload, IncludeNamespaceInUrl)
if err != nil {
Debug(DbgError, "http.NewRequest(POST, %s, %#v) error: '%s'\n", route, payload, err)
errMsg := wski18n.T("Unable to create HTTP request for POST '{{.route}}': {{.err}}",
map[string]interface{}{"route": route, "err": err})
whiskErr := MakeWskErrorFromWskError(errors.New(errMsg), err, EXIT_CODE_ERR_NETWORK, DISPLAY_MSG,
NO_DISPLAY_USAGE)
return nil, nil, whiskErr
}
resp, err := s.client.Do(req, &res, blocking)
if err != nil {
Debug(DbgError, "s.client.Do() error - HTTP req %s; error '%s'\n", req.URL.String(), err)
return res, resp, err
}
return res, resp, nil
}