You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@apisix.apache.org by kv...@apache.org on 2020/08/13 06:09:24 UTC

[apisix-control-plane] branch master updated: feat: add Rule yaml tranformer (#8)

This is an automated email from the ASF dual-hosted git repository.

kvn pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix-control-plane.git


The following commit(s) were added to refs/heads/master by this push:
     new be7e05e  feat: add Rule yaml tranformer (#8)
be7e05e is described below

commit be7e05e87be1f8313746288ac515a3aac7003c1f
Author: kv <gx...@163.com>
AuthorDate: Thu Aug 13 14:09:19 2020 +0800

    feat: add Rule yaml tranformer (#8)
---
 doc/yaml_struct.md    | 23 +++++++++++++--
 pkg/yml/model.go      | 34 ++++++++++++++++++++--
 pkg/yml/trans.go      | 19 ++++++++++++-
 pkg/yml/trans_test.go | 79 +++++++++++++++++++++++++++++++++++++++++++++------
 4 files changed, 141 insertions(+), 14 deletions(-)

diff --git a/doc/yaml_struct.md b/doc/yaml_struct.md
index ad48339..56527e1 100644
--- a/doc/yaml_struct.md
+++ b/doc/yaml_struct.md
@@ -1,3 +1,22 @@
+<!--
+#
+# 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.
+#
+-->
+
 ## Currently, 4 types are defined, namely
 
 1.Gateway
@@ -41,12 +60,12 @@ gateways:
 - foo-gw
 http:
 - route:
- - destination:
+  - destination:
      port: 28002
      host: foo-server
      subset: foo-v1
      weight: 10
- label:
+  label:
     app: foo
     version: v1
   match:
diff --git a/pkg/yml/model.go b/pkg/yml/model.go
index f8c73ed..e27382f 100644
--- a/pkg/yml/model.go
+++ b/pkg/yml/model.go
@@ -19,7 +19,6 @@ package yml
 
 type YmlModel interface {
 	ToMem() string
-	Type() string
 }
 
 type Gateway struct {
@@ -43,6 +42,35 @@ func (g *Gateway) ToMem() string {
 	return "gateway"
 }
 
-func (g *Gateway) Type() string {
-	return "Gateway"
+type Rule struct {
+	Kind     string   `json:"kind"`
+	Name     string   `json:"name"`
+	Hosts    []string `json:"hosts"`
+	Gateways []string `json:"gateways"`
+	HTTP     []HTTP   `json:"http"`
+}
+type Destination struct {
+	Port   int64  `json:"port"`
+	Host   string `json:"host"`
+	Subset string `json:"subset"`
+	Weight int64  `json:"weight"`
+}
+type Route struct {
+	Destination Destination `json:"destination"`
+}
+type Label map[string]string
+
+type Headers map[string]interface{}
+
+type Match struct {
+	Headers Headers `json:"headers"`
+}
+type HTTP struct {
+	Route []Route `json:"route"`
+	Label Label   `json:"label"`
+	Match []Match `json:"match,omitempty"`
+}
+
+func (r *Rule) ToMem() string {
+	return "Rule"
 }
diff --git a/pkg/yml/trans.go b/pkg/yml/trans.go
index 696cd40..2aa1977 100644
--- a/pkg/yml/trans.go
+++ b/pkg/yml/trans.go
@@ -38,9 +38,16 @@ func Trans(b []byte, y []byte) YmlModel {
 				fmt.Println("trans to Gateway error ", err)
 				return nil
 			} else {
-				fmt.Println(g)
 				return g
 			}
+		case "Rule":
+			// trans to Rule
+			if r, err := ToRule(y); err != nil {
+				fmt.Println("trans to Rule error ", err)
+				return nil
+			} else {
+				return r
+			}
 		default:
 			fmt.Println("nil")
 			return nil
@@ -57,3 +64,13 @@ func ToGateway(y []byte) (*Gateway, error) {
 		return g, nil
 	}
 }
+
+func ToRule(y []byte) (*Rule, error) {
+	var r *Rule
+	if err := yaml.Unmarshal(y, &r); err != nil {
+		fmt.Println(err)
+		return nil, err
+	} else {
+		return r, nil
+	}
+}
diff --git a/pkg/yml/trans_test.go b/pkg/yml/trans_test.go
index d5eca5e..29c085d 100644
--- a/pkg/yml/trans_test.go
+++ b/pkg/yml/trans_test.go
@@ -1,3 +1,20 @@
+/*
+ * 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 yml_test
 
 import (
@@ -10,9 +27,9 @@ import (
 
 var _ = Describe("Trans", func() {
 	Describe("trans to model", func() {
-		var b []byte
-		BeforeEach(func() {
-			b = []byte(`
+		Context("trans", func() {
+			It("trans to gateway no error", func() {
+				b := []byte(`
 kind: Gateway
 name: foo-gw
 servers:
@@ -24,19 +41,65 @@ servers:
    - "a.foo.com"
    - "b.foo.com"
 `)
-			fmt.Println("BeforeEach executed")
-		})
-		Context("trans", func() {
-			It("trans to gateway no error", func() {
 				y, err := yaml.YAMLToJSON(b)
 				fmt.Println(string(y))
 				ym := yml.Trans(y, b)
 				Expect(err).NotTo(HaveOccurred())
-				Expect(ym.Type()).To(Equal("Gateway"))
+				v := typeof(ym)
+				fmt.Println(v)
+				Expect(v).To(Equal("*yml.Gateway"))
 				g, ok := ym.(*yml.Gateway)
 				Expect(ok).To(Equal(true))
 				Expect(len(g.Servers[0].Hosts)).To(Equal(2))
 			})
+
+			It("trans to rule no error", func() {
+				b := []byte(`
+kind: Rule
+name: xxx-rules
+hosts:
+- "a.foo.com"
+gateways:
+- foo-gw
+http:
+- route:
+  - destination:
+     port: 28002
+     host: foo-server
+     subset: foo-v1
+     weight: 10
+  label:
+    app: foo
+    version: v1
+  match:
+  - headers:
+     product_id:
+       exact: v1
+- route:
+  - destination:
+       port: 28002
+       host: foo-server
+       subset: v2
+  label:
+    app: foo
+    version: v2
+`)
+				y, err := yaml.YAMLToJSON(b)
+				fmt.Println(string(y))
+				ym := yml.Trans(y, b)
+				Expect(err).NotTo(HaveOccurred())
+				v := typeof(ym)
+				fmt.Println(v)
+				Expect(v).To(Equal("*yml.Rule"))
+				r, ok := ym.(*yml.Rule)
+				Expect(ok).To(Equal(true))
+				Expect(r.Kind).To(Equal("Rule"))
+				Expect(r.Kind).To(Equal("Rule"))
+			})
 		})
 	})
 })
+
+func typeof(v interface{}) string {
+	return fmt.Sprintf("%T", v)
+}