You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@openwhisk.apache.org by GitBox <gi...@apache.org> on 2018/06/21 13:28:32 UTC

[GitHub] kpavel closed pull request #918: Adds support for api export

kpavel closed pull request #918: Adds support for api export
URL: https://github.com/apache/incubator-openwhisk-wskdeploy/pull/918
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/cmd/export.go b/cmd/export.go
index 5135dba6..ba42f727 100644
--- a/cmd/export.go
+++ b/cmd/export.go
@@ -152,9 +152,9 @@ func exportProject(projectName string, targetManifest string) error {
 				// 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 @@ func exportProject(projectName string, targetManifest string) error {
 
 	}
 
+	// 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 64368193..7bc2ccc3 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 @@ func TestExport(t *testing.T) {
 
 	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 @@ func SkipTestExportHelloWorld(t *testing.T) {
 	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 @@ func TestExport2Pack(t *testing.T) {
 	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 @@ func TestExport2Pack(t *testing.T) {
 	_, 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 00000000..9efff098
--- /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


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services