Adds support for api export (#918)
* add export api support
* Rebasing and bug fixing
* gofmt
* temporary disable export test
* wip
* wip
* enabling TestExportApi test
diff --git a/cmd/export.go b/cmd/export.go
index 5135dba..ba42f72 100644
--- a/cmd/export.go
+++ b/cmd/export.go
@@ -152,9 +152,9 @@
// TODO: throw if there more than single package managed by project
// currently will be a mess because triggers and rules managed under packages
// instead of the top level (similar to OW model)
- // if len(maniyaml.Packages) > 1 {
- // return errors.New("currently can't work with more than one package managed by one project")
- // }
+ // if len(maniyaml.Packages) > 1 {
+ // return errors.New("currently can't work with more than one package managed by one project")
+ // }
// perform the similar check on the list of actions from this package
// get a list of actions in your namespace
@@ -274,6 +274,75 @@
}
+ // List API request query parameters
+ apiListReqOptions := new(whisk.ApiListRequestOptions)
+ apiListReqOptions.SpaceGuid = strings.Split(client.Config.AuthToken, ":")[0]
+ apiListReqOptions.AccessToken = client.Config.ApigwAccessToken
+
+ // Get list of APIs from OW
+ retApiList, _, err := client.Apis.List(apiListReqOptions)
+ if err != nil {
+ return err
+ }
+
+ // iterate over the list of APIs to determine whether any of them part of the managed project
+ retApiArray := (*whisk.RetApiArray)(retApiList)
+ for _, api := range retApiArray.Apis {
+
+ apiName := api.ApiValue.Swagger.Info.Title
+ apiBasePath := strings.TrimPrefix(api.ApiValue.Swagger.BasePath, "/")
+
+ // run over api paths looking for one pointing to an action belonging to the given project
+ for path := range api.ApiValue.Swagger.Paths {
+ for op, opv := range api.ApiValue.Swagger.Paths[path].MakeOperationMap() {
+ if len(opv.XOpenWhisk.Package) > 0 {
+ pkgName := opv.XOpenWhisk.Package
+
+ if pkg, ok := maniyaml.Packages[pkgName]; ok {
+ if pkg.Namespace == opv.XOpenWhisk.Namespace {
+
+ // now adding the api to the maniyaml
+ if pkg.Apis == nil {
+ pkg.Apis = make(map[string]map[string]map[string]map[string]parsers.APIMethodResponse)
+ }
+
+ path = strings.TrimPrefix(path, "/")
+
+ apiMethodResponse := *new(parsers.APIMethodResponse)
+ splitApiUrl := strings.Split(opv.XOpenWhisk.ApiUrl, ".")
+ responseType := splitApiUrl[len(splitApiUrl)-1]
+
+ apiMethodResponse.Method = op
+ apiMethodResponse.Response = responseType
+
+ if pkgApi, ok := pkg.Apis[apiName]; ok {
+ if pkgApiBasePath, ok := pkgApi[apiBasePath]; ok {
+ if _, ok := pkgApiBasePath[path]; ok {
+ pkg.Apis[apiName][apiBasePath][path][opv.XOpenWhisk.ActionName] = apiMethodResponse
+ } else {
+ pkg.Apis[apiName][apiBasePath][path] = map[string]parsers.APIMethodResponse{}
+ pkg.Apis[apiName][apiBasePath][path][opv.XOpenWhisk.ActionName] = apiMethodResponse
+ }
+ } else {
+ pkg.Apis[apiName][apiBasePath] = map[string]map[string]parsers.APIMethodResponse{}
+ pkg.Apis[apiName][apiBasePath][path] = map[string]parsers.APIMethodResponse{}
+ pkg.Apis[apiName][apiBasePath][path][opv.XOpenWhisk.ActionName] = apiMethodResponse
+ }
+ } else {
+ pkg.Apis[apiName] = map[string]map[string]map[string]parsers.APIMethodResponse{}
+ pkg.Apis[apiName][apiBasePath] = map[string]map[string]parsers.APIMethodResponse{}
+ pkg.Apis[apiName][apiBasePath][path] = map[string]parsers.APIMethodResponse{}
+ pkg.Apis[apiName][apiBasePath][path][opv.XOpenWhisk.ActionName] = apiMethodResponse
+ }
+
+ maniyaml.Packages[pkgName] = pkg
+ }
+ }
+ }
+ }
+ }
+ }
+
// adding dependencies to the first package
for pkgName := range maniyaml.Packages {
for bPkg, binding := range bindings {
diff --git a/tests/src/integration/export/export_test.go b/tests/src/integration/export/export_test.go
index 6436819..7bc2ccc 100644
--- a/tests/src/integration/export/export_test.go
+++ b/tests/src/integration/export/export_test.go
@@ -1,4 +1,4 @@
-// +build skip_integration
+// +build integration
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
@@ -44,6 +44,8 @@
wskdeploy := common.NewWskdeploy()
+ defer os.RemoveAll(targetManifestFolder)
+
_, err := wskdeploy.ManagedDeploymentOnlyManifest(manifestLib1Path)
assert.Equal(t, nil, err, "Failed to deploy the lib1 manifest file.")
@@ -81,6 +83,8 @@
targetManifestFolder := os.Getenv("GOPATH") + EXPORT_TEST_PATH + "tmp-" + strconv.Itoa(rand.Intn(1000)) + "/"
targetManifestHelloWorldPath := targetManifestFolder + "manifest-" + projectName + ".yaml"
+ defer os.RemoveAll(targetManifestFolder)
+
wskdeploy := common.NewWskdeploy()
_, err := wskdeploy.ManagedDeploymentManifestAndProject(manifestHelloWorldPath, projectName)
@@ -114,6 +118,9 @@
manifest2PackPath := os.Getenv("GOPATH") + EXPORT_TEST_PATH + "manifest_2pack.yaml"
targetManifestFolder := os.Getenv("GOPATH") + EXPORT_TEST_PATH + "tmp-" + strconv.Itoa(rand.Intn(1000)) + "/"
target2PackManifestPath := targetManifestFolder + "exported2packmanifest.yaml"
+
+ defer os.RemoveAll(targetManifestFolder)
+
projectName := "2pack"
wskdeploy := common.NewWskdeploy()
@@ -135,3 +142,46 @@
_, err = wskdeploy.UndeployManifestPathOnly(manifest2PackPath)
assert.Equal(t, nil, err, "Failed to undeploy")
}
+
+func TestExportApi(t *testing.T) {
+ projectName := "ApiExp"
+ wskdeploy := common.NewWskdeploy()
+
+ defer os.RemoveAll(targetManifestFolder)
+
+ _, err := wskdeploy.ManagedDeploymentManifestAndProject(manifestApiExpPath, projectName)
+ assert.Equal(t, nil, err, "Failed to deploy the ApiExp manifest file.")
+
+ _, err = wskdeploy.ExportProject(projectName, targetApiExpManifestPath)
+ assert.Equal(t, nil, err, "Failed to export project.")
+
+ _, err = os.Stat(manifestApiExpPath)
+ assert.Equal(t, nil, err, "Missing exported manifest file")
+
+ _, err = os.Stat(targetManifestFolder + "api-gateway-test/greeting.js")
+ assert.Equal(t, nil, err, "Missing exported api-gateway-test/greeting.js")
+
+ _, err = wskdeploy.UndeployManifestPathOnly(manifestApiExpPath)
+ assert.Equal(t, nil, err, "Failed to undeploy")
+
+ _, err = wskdeploy.ManagedDeploymentOnlyManifest(targetApiExpManifestPath)
+ assert.Equal(t, nil, err, "Failed to redeploy the exported manifest file.")
+
+ _, err = wskdeploy.UndeployManifestPathOnly(targetApiExpManifestPath)
+ assert.Equal(t, nil, err, "Failed to undeploy the exported manifest file")
+}
+
+var (
+ manifestLib1Path = os.Getenv("GOPATH") + "/src/github.com/apache/incubator-openwhisk-wskdeploy/tests/src/integration/export/manifest_lib1.yaml"
+ manifestLib2Path = os.Getenv("GOPATH") + "/src/github.com/apache/incubator-openwhisk-wskdeploy/tests/src/integration/export/manifest_lib2.yaml"
+ manifestExtPath = os.Getenv("GOPATH") + "/src/github.com/apache/incubator-openwhisk-wskdeploy/tests/src/integration/export/manifest_ext.yaml"
+
+ targetManifestFolder = os.Getenv("GOPATH") + "/src/github.com/apache/incubator-openwhisk-wskdeploy/tests/src/integration/export/tmp/"
+ targetManifestPath = targetManifestFolder + "manifest.yaml"
+
+ manifest2PackPath = os.Getenv("GOPATH") + "/src/github.com/apache/incubator-openwhisk-wskdeploy/tests/src/integration/export/manifest_2pack.yaml"
+ target2PackManifestPath = targetManifestFolder + "exported2packmanifest.yaml"
+
+ manifestApiExpPath = os.Getenv("GOPATH") + "/src/github.com/apache/incubator-openwhisk-wskdeploy/tests/src/integration/export/manifest_apiexp.yaml"
+ targetApiExpManifestPath = targetManifestFolder + "exportedapimanifest.yaml"
+)
diff --git a/tests/src/integration/export/manifest_apiexp.yaml b/tests/src/integration/export/manifest_apiexp.yaml
new file mode 100644
index 0000000..9efff09
--- /dev/null
+++ b/tests/src/integration/export/manifest_apiexp.yaml
@@ -0,0 +1,21 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more contributor
+# license agreements; and to You under the Apache License, Version 2.0.
+
+packages:
+ api-gateway-test:
+ version: 1.0
+ license: Apache-2.0
+ actions:
+ greeting:
+ web-export: true
+ version: 1.0
+ function: src/greeting.js
+ runtime: nodejs:6
+ # new top-level key for defining groups of named APIs
+ apis:
+ hello-world:
+ hello:
+ world:
+ greeting:
+ method: GET
+ response: http