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/02/02 18:45:52 UTC

[GitHub] pritidesai closed pull request #699: WIP: adding API Gateway functionality

pritidesai closed pull request #699: WIP: adding API Gateway functionality
URL: https://github.com/apache/incubator-openwhisk-wskdeploy/pull/699
 
 
   

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/deployers/manifestreader.go b/deployers/manifestreader.go
index a8df3851..88737125 100644
--- a/deployers/manifestreader.go
+++ b/deployers/manifestreader.go
@@ -373,12 +373,11 @@ func (reader *ManifestReader) SetRules(rules []*whisk.Rule) error {
 
 func (reader *ManifestReader) SetApis(ar []*whisk.ApiCreateRequest) error {
 	dep := reader.serviceDeployer
-	var apis []*whisk.ApiCreateRequest = make([]*whisk.ApiCreateRequest, 0)
 
 	dep.mt.Lock()
 	defer dep.mt.Unlock()
 
-	for _, api := range apis {
+	for _, api := range ar {
 		existApi, exist := dep.Deployment.Apis[api.ApiDoc.ApiName]
 		if exist {
 			existApi.ApiDoc.ApiName = api.ApiDoc.ApiName
diff --git a/parsers/manifest_parser.go b/parsers/manifest_parser.go
index 37d318e6..f1cd0e2c 100644
--- a/parsers/manifest_parser.go
+++ b/parsers/manifest_parser.go
@@ -883,12 +883,10 @@ func (dm *YAMLParser) ComposeApiRecordsFromAllPackages(manifest *YAML) ([]*whisk
 
 func (dm *YAMLParser) ComposeApiRecords(pkg Package) ([]*whisk.ApiCreateRequest, error) {
 	var acq []*whisk.ApiCreateRequest = make([]*whisk.ApiCreateRequest, 0)
-	apis := pkg.GetApis()
 
-	for _, api := range apis {
-		acr := new(whisk.ApiCreateRequest)
-		acr.ApiDoc = api
-		acq = append(acq, acr)
+	for _, api := range pkg.GetApis() {
+		wskapi := api.ComposeWskApi()
+		acq = append(acq, wskapi)
 	}
 	return acq, nil
 }
diff --git a/parsers/manifest_parser_test.go b/parsers/manifest_parser_test.go
index dde8866a..869b3bb4 100644
--- a/parsers/manifest_parser_test.go
+++ b/parsers/manifest_parser_test.go
@@ -1267,23 +1267,18 @@ func TestComposeRules(t *testing.T) {
 }
 
 func TestComposeApiRecords(t *testing.T) {
-    data := `package:
-  name: helloworld
-  apis:
-    book-club:
-      club:
-        books:
-           putBooks: put
-           deleteBooks: delete
-        members:
-           listMembers: get
-    book-club2:
-      club2:
-        books2:
-           getBooks2: get
-           postBooks2: post
-        members2:
-           listMembers2: get`
+    data := `packages:
+  helloworld:
+    apis:
+      api1:
+        apidoc:
+          apiName: book-club
+          gatewayBasePath: club
+          gatewayPath: books
+          gatewayMethod: GET
+          action:
+             name: greeting
+             backendMethod: GET`
     tmpfile, err := _createTmpfile(data, "manifest_parser_test_")
     if err != nil {
         assert.Fail(t, "Failed to create temp file")
@@ -1299,41 +1294,16 @@ func TestComposeApiRecords(t *testing.T) {
     if err != nil {
         assert.Fail(t, "Failed to compose api records")
     }
-    assert.Equal(t, 6, len(apiList), "Failed to get api records")
+    assert.Equal(t, 1, len(apiList), "Failed to get api records")
     for _, apiRecord := range apiList {
         apiDoc := apiRecord.ApiDoc
         action := apiDoc.Action
         switch action.Name {
-        case "putBooks":
-            assert.Equal(t, "book-club", apiDoc.ApiName, "Failed to set api name")
-            assert.Equal(t, "club", apiDoc.GatewayBasePath, "Failed to set api base path")
-            assert.Equal(t, "books", apiDoc.GatewayRelPath, "Failed to set api rel path")
-            assert.Equal(t, "put", action.BackendMethod, "Failed to set api backend method")
-        case "deleteBooks":
+        case "greeting":
             assert.Equal(t, "book-club", apiDoc.ApiName, "Failed to set api name")
             assert.Equal(t, "club", apiDoc.GatewayBasePath, "Failed to set api base path")
             assert.Equal(t, "books", apiDoc.GatewayRelPath, "Failed to set api rel path")
-            assert.Equal(t, "delete", action.BackendMethod, "Failed to set api backend method")
-        case "listMembers":
-            assert.Equal(t, "book-club", apiDoc.ApiName, "Failed to set api name")
-            assert.Equal(t, "club", apiDoc.GatewayBasePath, "Failed to set api base path")
-            assert.Equal(t, "members", apiDoc.GatewayRelPath, "Failed to set api rel path")
-            assert.Equal(t, "get", action.BackendMethod, "Failed to set api backend method")
-        case "getBooks2":
-            assert.Equal(t, "book-club2", apiDoc.ApiName, "Failed to set api name")
-            assert.Equal(t, "club2", apiDoc.GatewayBasePath, "Failed to set api base path")
-            assert.Equal(t, "books2", apiDoc.GatewayRelPath, "Failed to set api rel path")
-            assert.Equal(t, "get", action.BackendMethod, "Failed to set api backend method")
-        case "postBooks2":
-            assert.Equal(t, "book-club2", apiDoc.ApiName, "Failed to set api name")
-            assert.Equal(t, "club2", apiDoc.GatewayBasePath, "Failed to set api base path")
-            assert.Equal(t, "books2", apiDoc.GatewayRelPath, "Failed to set api rel path")
-            assert.Equal(t, "post", action.BackendMethod, "Failed to set api backend method")
-        case "listMembers2":
-            assert.Equal(t, "book-club2", apiDoc.ApiName, "Failed to set api name")
-            assert.Equal(t, "club2", apiDoc.GatewayBasePath, "Failed to set api base path")
-            assert.Equal(t, "members2", apiDoc.GatewayRelPath, "Failed to set api rel path")
-            assert.Equal(t, "get", action.BackendMethod, "Failed to set api backend method")
+            assert.Equal(t, "GET", action.BackendMethod, "Failed to set api backend method")
         default:
             assert.Fail(t, "Failed to get api action name")
         }
diff --git a/parsers/yamlparser.go b/parsers/yamlparser.go
index 6bcee666..8202150a 100644
--- a/parsers/yamlparser.go
+++ b/parsers/yamlparser.go
@@ -182,6 +182,57 @@ type Repository struct {
 	Credential  string `yaml:"credential,omitempty"`
 }
 
+type ApiCreateRequest struct {
+	ApiDoc          *Api      `yaml:"apidoc,omitempty"`
+}
+
+type Api struct {
+	Namespace       string    `yaml:"namespace,omitempty"`
+	ApiName         string    `yaml:"apiName,omitempty"`
+	GatewayBasePath string    `yaml:"gatewayBasePath,omitempty"`
+	GatewayRelPath  string    `yaml:"gatewayPath,omitempty"`
+	GatewayMethod   string    `yaml:"gatewayMethod,omitempty"`
+	Id              string    `yaml:"id,omitempty"`
+	GatewayFullPath string    `yaml:"gatewayFullPath,omitempty"`
+	Swagger         string    `yaml:"swagger,omitempty"`
+	Action          *ApiAction `yaml:"action,omitempty"`
+	PathParameters   []ApiParameter `yaml:"pathParameters,omitempty"`
+}
+
+type ApiParameter struct {
+	Name              string      `yaml:"name"`
+	In                string      `yaml:"in"`
+	Description       string      `yaml:"description,omitempty"`
+	Required          bool        `yaml:"required,omitempty"`
+	Type              string      `yaml:"type,omitempty"`
+	Format            string      `yaml:"format,omitempty"`
+	AllowEmptyValue   bool        `yaml:"allowEmptyValue,omitempty"`
+	Items             map[string]interface{}    `yaml:"items,omitempty"`
+	CollectionFormat  string      `yaml:"collectionFormat,omitempty"`
+	Default           interface{} `yaml:"default,omitempty"`
+	Maximum           int         `yaml:"maximum,omitempty"`
+	ExclusiveMaximum  bool        `yaml:"exclusiveMaximum,omitempty"`
+	Minimum           int         `yaml:"minimum,omitempty"`
+	ExclusiveMinimum  bool        `yaml:"exclusiveMinimum,omitempty"`
+	MaxLength         int         `yaml:"maxLength,omitempty"`
+	MinLength         int         `yaml:"minLength,omitempty"`
+	Pattern           string      `yaml:"pattern,omitempty"`
+	MaxItems          int         `yaml:"maxItems,omitempty"`
+	MinItems          int         `yaml:"minItems,omitempty"`
+	UniqueItems       bool        `yaml:"uniqueItems,omitempty"`
+	MultipleOf        int         `yaml:"multipleOf,omitempty"`
+	Enum              interface{} `yaml:"enum,omitempty"`
+	Ref               string      `yaml:"$ref,omitempty"`
+}
+
+type ApiAction struct {
+	Name            string    `yaml:"name,omitempty"`
+	Namespace       string    `yaml:"namespace,omitempty"`
+	BackendMethod   string    `yaml:"backendMethod,omitempty"`
+	BackendUrl      string    `yaml:"backendUrl,omitempty"`
+	Auth            string    `yaml:"authkey,omitempty"`
+}
+
 type Package struct {
 	//mapping to wsk.SentPackageNoPublish.Name
 	Packagename string `yaml:"name"` //used in manifest.yaml
@@ -202,7 +253,7 @@ type Package struct {
 	Sequences   map[string]Sequence    `yaml:"sequences"`
 	Annotations map[string]interface{} `yaml:"annotations,omitempty"`
 	//Parameters  map[string]interface{} `yaml: parameters` // used in manifest.yaml
-	Apis map[string]map[string]map[string]map[string]string `yaml:"apis"` //used in manifest.yaml
+	Apis 	map[string]ApiCreateRequest `yaml:"apis"` //used in manifest.yaml
 }
 
 type Project struct {
@@ -295,6 +346,22 @@ func (pkg *Package) ComposeWskPackage() *whisk.Package {
 	return wskpag
 }
 
+func (api *ApiCreateRequest) ComposeWskApi() *whisk.ApiCreateRequest {
+	wskapi := new(whisk.ApiCreateRequest)
+	apidoc := new(whisk.Api)
+	apidoc.Namespace = api.ApiDoc.Namespace
+	apidoc.ApiName = api.ApiDoc.ApiName
+	apidoc.GatewayBasePath = api.ApiDoc.GatewayBasePath
+	apidoc.GatewayRelPath = api.ApiDoc.GatewayRelPath
+	apidoc.GatewayMethod = api.ApiDoc.GatewayMethod
+	wskapi.ApiDoc = apidoc
+	action := new(whisk.ApiAction)
+	action.Name = api.ApiDoc.Action.Name
+	action.BackendMethod = api.ApiDoc.Action.BackendMethod
+	wskapi.ApiDoc.Action = action
+	return wskapi
+}
+
 func (pkg *Package) GetActionList() []Action {
 	var s1 []Action = make([]Action, 0)
 	for action_name, action := range pkg.Actions {
@@ -333,27 +400,10 @@ func (pkg *Package) GetFeedList() []Feed {
 }
 
 // This is for parse the manifest yaml file.
-func (pkg *Package) GetApis() []*whisk.Api {
-	var apis = make([]*whisk.Api, 0)
-	for k, v := range pkg.Apis {
-		var apiName string = k
-		for k, v := range v {
-			var gatewayBasePath string = k
-			for k, v := range v {
-				var gatewayRelPath string = k
-				for k, v := range v {
-					api := &whisk.Api{}
-					api.ApiName = apiName
-					api.GatewayBasePath = gatewayBasePath
-					api.GatewayRelPath = gatewayRelPath
-					action := &whisk.ApiAction{}
-					action.Name = k
-					action.BackendMethod = v
-					api.Action = action
-					apis = append(apis, api)
-				}
-			}
-		}
+func (pkg *Package) GetApis() []ApiCreateRequest {
+	var apis []ApiCreateRequest = make([]ApiCreateRequest, 0)
+	for _, api := range pkg.Apis {
+		apis = append(apis, api)
 	}
 	return apis
 }
diff --git a/parsers/yamlparser_test.go b/parsers/yamlparser_test.go
index ad206f3b..40dfc9cd 100644
--- a/parsers/yamlparser_test.go
+++ b/parsers/yamlparser_test.go
@@ -133,5 +133,5 @@ func TestGetApisList(t *testing.T) {
 	manifest, _ := mm.ParseManifest(manifestfile_val_tar)
 	pkg := manifest.Packages["manifest2"]
 	apis := pkg.GetApis()
-	assert.Equal(t, 5, len(apis), "Get api list failed.")
+	assert.Equal(t, 1, len(apis), "Get api list failed.")
 }
diff --git a/tests/src/integration/apigateway/manifest.yml b/tests/src/integration/apigateway/manifest.yml
index 56286e54..c0794b05 100644
--- a/tests/src/integration/apigateway/manifest.yml
+++ b/tests/src/integration/apigateway/manifest.yml
@@ -1,34 +1,25 @@
 packages:
-  api-gateway-test:
-      version: 1.0
-      license: Apache-2.0
-      actions:
-          greeting:
-            version: 1.0
-            function: src/greeting.js
-            runtime: nodejs:6
-            inputs:
-              name: string
-              place: string
-            outputs:
-              payload: string
-      apis: # new top-level key for defining groups of named APIs
-        book-club: #api name
-          club: # shared base path
-            books:   #path
-               getBooks: get #action name:verb
-               postBooks: post
-               putBooks: put
-               deleteBooks: delete
-            members: #path
-               listMembers: get #action name:verb
-        book-club2: #api name, added for multi api definition test
-              club2: # shared base path
-                books2:   #path
-                   getBooks2: get #action name:verb
-                   postBooks2: post
-                   putBooks2: put
-                   deleteBooks2: delete
-                members2: #path
-                   listMembers2: get #action name:verb
-    
+    api-gateway-test:
+        version: 1.0
+        license: Apache-2.0
+        actions:
+            greeting:
+                version: 1.0
+                function: src/greeting.js
+                runtime: nodejs:6
+                inputs:
+                    name: string
+                    place: string
+                outputs:
+                    payload: string
+        apis: # new top-level key for defining groups of named APIs
+            api1:
+                apidoc:
+                    apiName: book-club
+                    gatewayBasePath: club # shared base path
+                    gatewayPath: books   #path
+                    gatewayMethod: GET
+                    action:
+                       name: greeting
+                       backendMethod: GET #action name:verb
+


 

----------------------------------------------------------------
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