Skip to content

Commit 755d4a4

Browse files
Pavel Kravchenkopritidesai
Pavel Kravchenko
authored andcommitted
Adds support for api export (#918)
* add export api support * Rebasing and bug fixing * gofmt * temporary disable export test * wip * wip * enabling TestExportApi test
1 parent 7620ef7 commit 755d4a4

File tree

3 files changed

+144
-4
lines changed

3 files changed

+144
-4
lines changed

cmd/export.go

+72-3
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,9 @@ func exportProject(projectName string, targetManifest string) error {
152152
// TODO: throw if there more than single package managed by project
153153
// currently will be a mess because triggers and rules managed under packages
154154
// instead of the top level (similar to OW model)
155-
// if len(maniyaml.Packages) > 1 {
156-
// return errors.New("currently can't work with more than one package managed by one project")
157-
// }
155+
// if len(maniyaml.Packages) > 1 {
156+
// return errors.New("currently can't work with more than one package managed by one project")
157+
// }
158158

159159
// perform the similar check on the list of actions from this package
160160
// get a list of actions in your namespace
@@ -274,6 +274,75 @@ func exportProject(projectName string, targetManifest string) error {
274274

275275
}
276276

277+
// List API request query parameters
278+
apiListReqOptions := new(whisk.ApiListRequestOptions)
279+
apiListReqOptions.SpaceGuid = strings.Split(client.Config.AuthToken, ":")[0]
280+
apiListReqOptions.AccessToken = client.Config.ApigwAccessToken
281+
282+
// Get list of APIs from OW
283+
retApiList, _, err := client.Apis.List(apiListReqOptions)
284+
if err != nil {
285+
return err
286+
}
287+
288+
// iterate over the list of APIs to determine whether any of them part of the managed project
289+
retApiArray := (*whisk.RetApiArray)(retApiList)
290+
for _, api := range retApiArray.Apis {
291+
292+
apiName := api.ApiValue.Swagger.Info.Title
293+
apiBasePath := strings.TrimPrefix(api.ApiValue.Swagger.BasePath, "/")
294+
295+
// run over api paths looking for one pointing to an action belonging to the given project
296+
for path := range api.ApiValue.Swagger.Paths {
297+
for op, opv := range api.ApiValue.Swagger.Paths[path].MakeOperationMap() {
298+
if len(opv.XOpenWhisk.Package) > 0 {
299+
pkgName := opv.XOpenWhisk.Package
300+
301+
if pkg, ok := maniyaml.Packages[pkgName]; ok {
302+
if pkg.Namespace == opv.XOpenWhisk.Namespace {
303+
304+
// now adding the api to the maniyaml
305+
if pkg.Apis == nil {
306+
pkg.Apis = make(map[string]map[string]map[string]map[string]parsers.APIMethodResponse)
307+
}
308+
309+
path = strings.TrimPrefix(path, "/")
310+
311+
apiMethodResponse := *new(parsers.APIMethodResponse)
312+
splitApiUrl := strings.Split(opv.XOpenWhisk.ApiUrl, ".")
313+
responseType := splitApiUrl[len(splitApiUrl)-1]
314+
315+
apiMethodResponse.Method = op
316+
apiMethodResponse.Response = responseType
317+
318+
if pkgApi, ok := pkg.Apis[apiName]; ok {
319+
if pkgApiBasePath, ok := pkgApi[apiBasePath]; ok {
320+
if _, ok := pkgApiBasePath[path]; ok {
321+
pkg.Apis[apiName][apiBasePath][path][opv.XOpenWhisk.ActionName] = apiMethodResponse
322+
} else {
323+
pkg.Apis[apiName][apiBasePath][path] = map[string]parsers.APIMethodResponse{}
324+
pkg.Apis[apiName][apiBasePath][path][opv.XOpenWhisk.ActionName] = apiMethodResponse
325+
}
326+
} else {
327+
pkg.Apis[apiName][apiBasePath] = map[string]map[string]parsers.APIMethodResponse{}
328+
pkg.Apis[apiName][apiBasePath][path] = map[string]parsers.APIMethodResponse{}
329+
pkg.Apis[apiName][apiBasePath][path][opv.XOpenWhisk.ActionName] = apiMethodResponse
330+
}
331+
} else {
332+
pkg.Apis[apiName] = map[string]map[string]map[string]parsers.APIMethodResponse{}
333+
pkg.Apis[apiName][apiBasePath] = map[string]map[string]parsers.APIMethodResponse{}
334+
pkg.Apis[apiName][apiBasePath][path] = map[string]parsers.APIMethodResponse{}
335+
pkg.Apis[apiName][apiBasePath][path][opv.XOpenWhisk.ActionName] = apiMethodResponse
336+
}
337+
338+
maniyaml.Packages[pkgName] = pkg
339+
}
340+
}
341+
}
342+
}
343+
}
344+
}
345+
277346
// adding dependencies to the first package
278347
for pkgName := range maniyaml.Packages {
279348
for bPkg, binding := range bindings {

tests/src/integration/export/export_test.go

+51-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// +build skip_integration
1+
// +build integration
22

33
/*
44
* Licensed to the Apache Software Foundation (ASF) under one or more
@@ -44,6 +44,8 @@ func TestExport(t *testing.T) {
4444

4545
wskdeploy := common.NewWskdeploy()
4646

47+
defer os.RemoveAll(targetManifestFolder)
48+
4749
_, err := wskdeploy.ManagedDeploymentOnlyManifest(manifestLib1Path)
4850
assert.Equal(t, nil, err, "Failed to deploy the lib1 manifest file.")
4951

@@ -81,6 +83,8 @@ func SkipTestExportHelloWorld(t *testing.T) {
8183
targetManifestFolder := os.Getenv("GOPATH") + EXPORT_TEST_PATH + "tmp-" + strconv.Itoa(rand.Intn(1000)) + "/"
8284
targetManifestHelloWorldPath := targetManifestFolder + "manifest-" + projectName + ".yaml"
8385

86+
defer os.RemoveAll(targetManifestFolder)
87+
8488
wskdeploy := common.NewWskdeploy()
8589

8690
_, err := wskdeploy.ManagedDeploymentManifestAndProject(manifestHelloWorldPath, projectName)
@@ -114,6 +118,9 @@ func TestExport2Pack(t *testing.T) {
114118
manifest2PackPath := os.Getenv("GOPATH") + EXPORT_TEST_PATH + "manifest_2pack.yaml"
115119
targetManifestFolder := os.Getenv("GOPATH") + EXPORT_TEST_PATH + "tmp-" + strconv.Itoa(rand.Intn(1000)) + "/"
116120
target2PackManifestPath := targetManifestFolder + "exported2packmanifest.yaml"
121+
122+
defer os.RemoveAll(targetManifestFolder)
123+
117124
projectName := "2pack"
118125
wskdeploy := common.NewWskdeploy()
119126

@@ -135,3 +142,46 @@ func TestExport2Pack(t *testing.T) {
135142
_, err = wskdeploy.UndeployManifestPathOnly(manifest2PackPath)
136143
assert.Equal(t, nil, err, "Failed to undeploy")
137144
}
145+
146+
func TestExportApi(t *testing.T) {
147+
projectName := "ApiExp"
148+
wskdeploy := common.NewWskdeploy()
149+
150+
defer os.RemoveAll(targetManifestFolder)
151+
152+
_, err := wskdeploy.ManagedDeploymentManifestAndProject(manifestApiExpPath, projectName)
153+
assert.Equal(t, nil, err, "Failed to deploy the ApiExp manifest file.")
154+
155+
_, err = wskdeploy.ExportProject(projectName, targetApiExpManifestPath)
156+
assert.Equal(t, nil, err, "Failed to export project.")
157+
158+
_, err = os.Stat(manifestApiExpPath)
159+
assert.Equal(t, nil, err, "Missing exported manifest file")
160+
161+
_, err = os.Stat(targetManifestFolder + "api-gateway-test/greeting.js")
162+
assert.Equal(t, nil, err, "Missing exported api-gateway-test/greeting.js")
163+
164+
_, err = wskdeploy.UndeployManifestPathOnly(manifestApiExpPath)
165+
assert.Equal(t, nil, err, "Failed to undeploy")
166+
167+
_, err = wskdeploy.ManagedDeploymentOnlyManifest(targetApiExpManifestPath)
168+
assert.Equal(t, nil, err, "Failed to redeploy the exported manifest file.")
169+
170+
_, err = wskdeploy.UndeployManifestPathOnly(targetApiExpManifestPath)
171+
assert.Equal(t, nil, err, "Failed to undeploy the exported manifest file")
172+
}
173+
174+
var (
175+
manifestLib1Path = os.Getenv("GOPATH") + "/src/github.com/apache/incubator-openwhisk-wskdeploy/tests/src/integration/export/manifest_lib1.yaml"
176+
manifestLib2Path = os.Getenv("GOPATH") + "/src/github.com/apache/incubator-openwhisk-wskdeploy/tests/src/integration/export/manifest_lib2.yaml"
177+
manifestExtPath = os.Getenv("GOPATH") + "/src/github.com/apache/incubator-openwhisk-wskdeploy/tests/src/integration/export/manifest_ext.yaml"
178+
179+
targetManifestFolder = os.Getenv("GOPATH") + "/src/github.com/apache/incubator-openwhisk-wskdeploy/tests/src/integration/export/tmp/"
180+
targetManifestPath = targetManifestFolder + "manifest.yaml"
181+
182+
manifest2PackPath = os.Getenv("GOPATH") + "/src/github.com/apache/incubator-openwhisk-wskdeploy/tests/src/integration/export/manifest_2pack.yaml"
183+
target2PackManifestPath = targetManifestFolder + "exported2packmanifest.yaml"
184+
185+
manifestApiExpPath = os.Getenv("GOPATH") + "/src/github.com/apache/incubator-openwhisk-wskdeploy/tests/src/integration/export/manifest_apiexp.yaml"
186+
targetApiExpManifestPath = targetManifestFolder + "exportedapimanifest.yaml"
187+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one or more contributor
2+
# license agreements; and to You under the Apache License, Version 2.0.
3+
4+
packages:
5+
api-gateway-test:
6+
version: 1.0
7+
license: Apache-2.0
8+
actions:
9+
greeting:
10+
web-export: true
11+
version: 1.0
12+
function: src/greeting.js
13+
runtime: nodejs:6
14+
# new top-level key for defining groups of named APIs
15+
apis:
16+
hello-world:
17+
hello:
18+
world:
19+
greeting:
20+
method: GET
21+
response: http

0 commit comments

Comments
 (0)