github.com/polarismesh/polaris@v1.17.8/test/integrate/http/namespace.go (about)

     1  /**
     2   * Tencent is pleased to support the open source community by making Polaris available.
     3   *
     4   * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
     5   *
     6   * Licensed under the BSD 3-Clause License (the "License");
     7   * you may not use this file except in compliance with the License.
     8   * You may obtain a copy of the License at
     9   *
    10   * https://opensource.org/licenses/BSD-3-Clause
    11   *
    12   * Unless required by applicable law or agreed to in writing, software distributed
    13   * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
    14   * CONDITIONS OF ANY KIND, either express or implied. See the License for the
    15   * specific language governing permissions and limitations under the License.
    16   */
    17  
    18  package http
    19  
    20  import (
    21  	"bytes"
    22  	"errors"
    23  	"fmt"
    24  	"io"
    25  
    26  	"github.com/golang/protobuf/jsonpb"
    27  	apimodel "github.com/polarismesh/specification/source/go/api/v1/model"
    28  	apiservice "github.com/polarismesh/specification/source/go/api/v1/service_manage"
    29  
    30  	api "github.com/polarismesh/polaris/common/api/v1"
    31  )
    32  
    33  // JSONFromNamespaces 将命名空间数组转换为JSON
    34  func JSONFromNamespaces(namespaces []*apimodel.Namespace) (*bytes.Buffer, error) {
    35  	m := jsonpb.Marshaler{Indent: " "}
    36  
    37  	buffer := bytes.NewBuffer([]byte{})
    38  
    39  	buffer.Write([]byte("["))
    40  	for index, namespace := range namespaces {
    41  		if index > 0 {
    42  			buffer.Write([]byte(",\n"))
    43  		}
    44  		err := m.Marshal(buffer, namespace)
    45  		if err != nil {
    46  			return nil, err
    47  		}
    48  	}
    49  
    50  	buffer.Write([]byte("]"))
    51  
    52  	return buffer, nil
    53  }
    54  
    55  // CreateNamespaces 创建命名空间
    56  func (c *Client) CreateNamespaces(namespaces []*apimodel.Namespace) (*apiservice.BatchWriteResponse, error) {
    57  	fmt.Printf("\ncreate namespaces\n")
    58  
    59  	url := fmt.Sprintf("http://%v/naming/%v/namespaces", c.Address, c.Version)
    60  
    61  	body, err := JSONFromNamespaces(namespaces)
    62  	if err != nil {
    63  		fmt.Printf("%v\n", err)
    64  		return nil, err
    65  	}
    66  
    67  	response, err := c.SendRequest("POST", url, body)
    68  	if err != nil {
    69  		fmt.Printf("%v\n", err)
    70  		return nil, err
    71  	}
    72  
    73  	ret, err := GetBatchWriteResponse(response)
    74  	if err != nil {
    75  		fmt.Printf("%v\n", err)
    76  		return ret, err
    77  	}
    78  
    79  	return checkCreateNamespacesResponse(ret, namespaces)
    80  }
    81  
    82  // DeleteNamespaces 删除命名空间
    83  func (c *Client) DeleteNamespaces(namespaces []*apimodel.Namespace) error {
    84  	fmt.Printf("\ndelete namespaces\n")
    85  
    86  	url := fmt.Sprintf("http://%v/naming/%v/namespaces/delete", c.Address, c.Version)
    87  
    88  	body, err := JSONFromNamespaces(namespaces)
    89  	if err != nil {
    90  		fmt.Printf("%v\n", err)
    91  		return err
    92  	}
    93  
    94  	response, err := c.SendRequest("POST", url, body)
    95  	if err != nil {
    96  		fmt.Printf("%v\n", err)
    97  		return err
    98  	}
    99  
   100  	_, err = GetBatchWriteResponse(response)
   101  	if err != nil {
   102  		if err == io.EOF {
   103  			return nil
   104  		}
   105  
   106  		fmt.Printf("%v\n", err)
   107  		return err
   108  	}
   109  
   110  	return nil
   111  }
   112  
   113  // DeleteNamespaces 删除命名空间
   114  func (c *Client) DeleteNamespacesGetResp(namespaces []*apimodel.Namespace) (*apiservice.BatchWriteResponse, error) {
   115  	fmt.Printf("\ndelete namespaces\n")
   116  
   117  	url := fmt.Sprintf("http://%v/naming/%v/namespaces/delete", c.Address, c.Version)
   118  
   119  	body, err := JSONFromNamespaces(namespaces)
   120  	if err != nil {
   121  		fmt.Printf("%v\n", err)
   122  		return nil, err
   123  	}
   124  
   125  	response, err := c.SendRequest("POST", url, body)
   126  	if err != nil {
   127  		fmt.Printf("%v\n", err)
   128  		return nil, err
   129  	}
   130  
   131  	resp, err := GetBatchWriteResponse(response)
   132  	if err != nil {
   133  		fmt.Printf("%v\n", err)
   134  		return resp, err
   135  	}
   136  
   137  	return resp, nil
   138  }
   139  
   140  // UpdateNamesapces 更新命名空间
   141  func (c *Client) UpdateNamesapces(namespaces []*apimodel.Namespace) error {
   142  	fmt.Printf("\nupdate namespaces\n")
   143  
   144  	url := fmt.Sprintf("http://%v/naming/%v/namespaces", c.Address, c.Version)
   145  
   146  	body, err := JSONFromNamespaces(namespaces)
   147  	if err != nil {
   148  		fmt.Printf("%v\n", err)
   149  		return err
   150  	}
   151  
   152  	response, err := c.SendRequest("PUT", url, body)
   153  	if err != nil {
   154  		fmt.Printf("%v\n", err)
   155  		return err
   156  	}
   157  
   158  	_, err = GetBatchWriteResponse(response)
   159  	if err != nil {
   160  		if err == io.EOF {
   161  			return nil
   162  		}
   163  
   164  		fmt.Printf("%v\n", err)
   165  		return err
   166  	}
   167  
   168  	return nil
   169  }
   170  
   171  // GetNamespaces 查询命名空间
   172  func (c *Client) GetNamespaces(namespaces []*apimodel.Namespace) ([]*apimodel.Namespace, error) {
   173  	fmt.Printf("\nget namespaces\n")
   174  
   175  	url := fmt.Sprintf("http://%v/naming/%v/namespaces", c.Address, c.Version)
   176  
   177  	params := map[string][]interface{}{
   178  		"name": {namespaces[0].GetName().GetValue(), namespaces[1].GetName().GetValue()},
   179  	}
   180  
   181  	url = c.CompleteURL(url, params)
   182  	response, err := c.SendRequest("GET", url, nil)
   183  	if err != nil {
   184  		return nil, err
   185  	}
   186  
   187  	ret, err := GetBatchQueryResponse(response)
   188  	if err != nil {
   189  		fmt.Printf("%v\n", err)
   190  		return nil, err
   191  	}
   192  
   193  	if ret.GetCode() == nil || ret.GetCode().GetValue() != api.ExecuteSuccess {
   194  		return nil, errors.New("invalid batch code")
   195  	}
   196  
   197  	namespacesSize := len(namespaces)
   198  
   199  	if ret.GetAmount() == nil || ret.GetAmount().GetValue() != uint32(namespacesSize) {
   200  		return nil, errors.New("invalid batch amount")
   201  	}
   202  
   203  	if ret.GetSize() == nil || ret.GetSize().GetValue() != uint32(namespacesSize) {
   204  		return nil, errors.New("invalid batch size")
   205  	}
   206  
   207  	collection := make(map[string]*apimodel.Namespace)
   208  	for _, namespace := range namespaces {
   209  		collection[namespace.GetName().GetValue()] = namespace
   210  	}
   211  
   212  	items := ret.GetNamespaces()
   213  	if items == nil || len(items) != namespacesSize {
   214  		return nil, errors.New("invalid batch namespaces")
   215  	}
   216  
   217  	for _, item := range items {
   218  		if correctItem, ok := collection[item.GetName().GetValue()]; ok {
   219  			if result := compareNamespace(correctItem, item); !result {
   220  				return nil, errors.New("invalid namespace")
   221  			}
   222  		} else {
   223  			return nil, errors.New("invalid namespace")
   224  		}
   225  	}
   226  	return items, nil
   227  }
   228  
   229  /**
   230   * @brief 检查创建命名空间的回复
   231   */
   232  func checkCreateNamespacesResponse(ret *apiservice.BatchWriteResponse, namespaces []*apimodel.Namespace) (
   233  	*apiservice.BatchWriteResponse, error) {
   234  	if ret.GetCode() == nil || ret.GetCode().GetValue() != api.ExecuteSuccess {
   235  		return nil, errors.New("invalid batch code")
   236  	}
   237  
   238  	namespacesSize := len(namespaces)
   239  	if ret.GetSize() == nil || ret.GetSize().GetValue() != uint32(namespacesSize) {
   240  		return nil, errors.New("invalid batch size")
   241  	}
   242  
   243  	items := ret.GetResponses()
   244  	if items == nil || len(items) != namespacesSize {
   245  		return nil, errors.New("invalid batch response")
   246  	}
   247  
   248  	for index, item := range items {
   249  		if item.GetCode() == nil || item.GetCode().GetValue() != api.ExecuteSuccess {
   250  			return nil, errors.New("invalid code")
   251  		}
   252  
   253  		namespace := item.GetNamespace()
   254  		if namespace == nil {
   255  			return nil, errors.New("empty namespace")
   256  		}
   257  
   258  		name := namespaces[index].GetName().GetValue()
   259  		if namespace.GetName() == nil || namespace.GetName().GetValue() != name {
   260  			return nil, errors.New("invalid namespace name")
   261  		}
   262  
   263  		if namespace.GetToken() == nil || namespace.GetToken().GetValue() == "" {
   264  			return nil, errors.New("invalid namespace token")
   265  		}
   266  	}
   267  	return ret, nil
   268  }
   269  
   270  /**
   271   * @brief 比较namespace是否相等
   272   */
   273  func compareNamespace(correctItem *apimodel.Namespace, item *apimodel.Namespace) bool {
   274  	correctName := correctItem.GetName().GetValue()
   275  	correctComment := correctItem.GetComment().GetValue()
   276  
   277  	name := item.GetName().GetValue()
   278  	comment := item.GetComment().GetValue()
   279  
   280  	if correctName == name && correctComment == comment {
   281  		return true
   282  	}
   283  	return false
   284  }