gxthrj commented on a change in pull request #147:
URL: 
https://github.com/apache/apisix-ingress-controller/pull/147#discussion_r552385084



##########
File path: pkg/apisix/resource_test.go
##########
@@ -21,15 +21,15 @@ import (
        "github.com/stretchr/testify/assert"
 )
 
-func TestServiceUnmarshalJSON(t *testing.T) {
-       var svc Services
+func TestItemUnmarshalJSON(t *testing.T) {

Review comment:
       I think we need  conversion tests for `route/service/upstream/ssl`.

##########
File path: pkg/apisix/resource.go
##########
@@ -0,0 +1,208 @@
+// 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 apisix
+
+import (
+       "encoding/json"
+       "errors"
+       "fmt"
+       "net"
+       "strconv"
+       "strings"
+
+       v1 "github.com/api7/ingress-controller/pkg/types/apisix/v1"
+)
+
+// listResponse is the unified LIST response mapping of APISIX.
+type listResponse struct {
+       Count string `json:"count"`
+       Node  node   `json:"node"`
+}
+
+type createResponse struct {
+       Action string `json:"action"`
+       Item   item   `json:"node"`
+}
+
+type node struct {
+       Key   string `json:"key"`
+       Items items  `json:"nodes"`
+}
+
+type items []item
+
+// items implements json.Unmarshaler interface.
+// lua-cjson doesn't distinguish empty array and table,
+// and by default empty array will be encoded as '{}'.
+// We have to maintain the compatibility.
+func (items *items) UnmarshalJSON(p []byte) error {
+       if p[0] == '{' {
+               if len(p) != 2 {
+                       return errors.New("unexpected non-empty object")
+               }
+               return nil
+       }
+       var data []item
+       if err := json.Unmarshal(p, &data); err != nil {
+               return err
+       }
+       *items = data
+       return nil
+}
+
+type item struct {
+       Key   string          `json:"key"`
+       Value json.RawMessage `json:"value"`
+}
+
+type routeItem struct {
+       UpstreamId *string                `json:"upstream_id"`
+       ServiceId  *string                `json:"service_id"`
+       Host       *string                `json:"host"`
+       URI        *string                `json:"uri"`
+       Desc       *string                `json:"desc"`
+       Methods    []*string              `json:"methods"`
+       Plugins    map[string]interface{} `json:"plugins"`
+}
+
+// route decodes item.Value and converts it to v1.Route.
+func (i *item) route(group string) (*v1.Route, error) {

Review comment:
       The role of Group is used to group APISIX cluster, it will change 
BaseUrl.
   It seems that baseUrl in stub will not be changed.

##########
File path: pkg/apisix/stub.go
##########
@@ -0,0 +1,144 @@
+// 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 apisix
+
+import (
+       "context"
+       "encoding/json"
+       "fmt"
+       "io"
+       "io/ioutil"
+       "net/http"
+
+       "go.uber.org/zap"
+
+       "github.com/api7/ingress-controller/pkg/log"
+)
+
+type stub struct {
+       baseURL  string
+       adminKey string
+       cli      *http.Client
+}
+
+func (s *stub) applyAuth(req *http.Request) {
+       if s.adminKey != "" {
+               req.Header.Set("X-API-Key", s.adminKey)
+       }
+}
+
+func (s *stub) do(req *http.Request) (*http.Response, error) {
+       s.applyAuth(req)
+       return s.cli.Do(req)
+}
+
+func (s *stub) listResource(ctx context.Context, url string) (*listResponse, 
error) {
+       req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
+       if err != nil {
+               return nil, err
+       }
+       resp, err := s.do(req)
+       if err != nil {
+               return nil, err
+       }
+       defer drainBody(resp.Body, url)
+       if resp.StatusCode != http.StatusOK {
+               return nil, fmt.Errorf("unexpected status code %d", 
resp.StatusCode)
+       }
+
+       var list listResponse
+
+       dec := json.NewDecoder(resp.Body)
+       if err := dec.Decode(&list); err != nil {
+               return nil, err
+       }
+       return &list, nil
+}
+
+func (s *stub) createResource(ctx context.Context, url string, body io.Reader) 
(*createResponse, error) {
+       req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, body)
+       if err != nil {
+               return nil, err
+       }
+       resp, err := s.do(req)
+       if err != nil {
+               return nil, err
+       }
+
+       defer drainBody(resp.Body, url)
+
+       if resp.StatusCode != http.StatusCreated && resp.StatusCode != 
http.StatusOK {
+               return nil, fmt.Errorf("unexpected status code %d", 
resp.StatusCode)
+       }
+
+       var cr createResponse
+       dec := json.NewDecoder(resp.Body)
+       if err := dec.Decode(&cr); err != nil {
+               return nil, err
+       }
+       return &cr, nil
+}
+
+func (s *stub) updateResource(ctx context.Context, url string, body io.Reader) 
error {

Review comment:
       Update also need a response. APISIX has returned response body when 
update.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to