github.com/polarismesh/polaris@v1.17.8/test/integrate/http/service.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  	"reflect"
    26  	"time"
    27  
    28  	"github.com/golang/protobuf/jsonpb"
    29  	apiservice "github.com/polarismesh/specification/source/go/api/v1/service_manage"
    30  
    31  	api "github.com/polarismesh/polaris/common/api/v1"
    32  )
    33  
    34  /**
    35   * @brief 服务数组转JSON
    36   */
    37  func JSONFromServices(services []*apiservice.Service) (*bytes.Buffer, error) {
    38  	m := jsonpb.Marshaler{Indent: " "}
    39  
    40  	buffer := bytes.NewBuffer([]byte{})
    41  
    42  	buffer.Write([]byte("["))
    43  	for index, service := range services {
    44  		if index > 0 {
    45  			buffer.Write([]byte(",\n"))
    46  		}
    47  		err := m.Marshal(buffer, service)
    48  		if err != nil {
    49  			return nil, err
    50  		}
    51  	}
    52  
    53  	buffer.Write([]byte("]"))
    54  	return buffer, nil
    55  }
    56  
    57  /**
    58   * @brief 创建服务
    59   */
    60  func (c *Client) CreateServices(services []*apiservice.Service) (*apiservice.BatchWriteResponse, error) {
    61  	fmt.Printf("\ncreate services\n")
    62  
    63  	url := fmt.Sprintf("http://%v/naming/%v/services", c.Address, c.Version)
    64  
    65  	body, err := JSONFromServices(services)
    66  	if err != nil {
    67  		fmt.Printf("%v\n", err)
    68  		return nil, err
    69  	}
    70  
    71  	response, err := c.SendRequest("POST", url, body)
    72  	if err != nil {
    73  		fmt.Printf("%v\n", err)
    74  		return nil, err
    75  	}
    76  
    77  	ret, err := GetBatchWriteResponse(response)
    78  	if err != nil {
    79  		fmt.Printf("%v\n", err)
    80  		return ret, err
    81  	}
    82  
    83  	return checkCreateServicesResponse(ret, services)
    84  }
    85  
    86  /**
    87   * @brief 删除服务
    88   */
    89  func (c *Client) DeleteServices(services []*apiservice.Service) error {
    90  	fmt.Printf("\ndelete services\n")
    91  
    92  	url := fmt.Sprintf("http://%v/naming/%v/services/delete", c.Address, c.Version)
    93  
    94  	body, err := JSONFromServices(services)
    95  	if err != nil {
    96  		fmt.Printf("%v\n", err)
    97  		return err
    98  	}
    99  
   100  	response, err := c.SendRequest("POST", url, body)
   101  	if err != nil {
   102  		fmt.Printf("%v\n", err)
   103  		return err
   104  	}
   105  
   106  	_, err = GetBatchWriteResponse(response)
   107  	if err != nil {
   108  		if err == io.EOF {
   109  			return nil
   110  		}
   111  
   112  		fmt.Printf("%v\n", err)
   113  		return err
   114  	}
   115  
   116  	return nil
   117  }
   118  
   119  /**
   120   * @brief 更新服务
   121   */
   122  func (c *Client) UpdateServices(services []*apiservice.Service) error {
   123  	fmt.Printf("\nupdate services\n")
   124  
   125  	url := fmt.Sprintf("http://%v/naming/%v/services", c.Address, c.Version)
   126  
   127  	body, err := JSONFromServices(services)
   128  	if err != nil {
   129  		fmt.Printf("%v\n", err)
   130  		return err
   131  	}
   132  
   133  	response, err := c.SendRequest("PUT", url, body)
   134  	if err != nil {
   135  		fmt.Printf("%v\n", err)
   136  		return err
   137  	}
   138  
   139  	_, err = GetBatchWriteResponse(response)
   140  	if err != nil {
   141  		if err == io.EOF {
   142  			return nil
   143  		}
   144  
   145  		fmt.Printf("%v\n", err)
   146  		return err
   147  	}
   148  
   149  	return nil
   150  }
   151  
   152  /**
   153   * @brief 查询服务
   154   */
   155  func (c *Client) GetServices(services []*apiservice.Service) error {
   156  	fmt.Printf("\nget services\n")
   157  
   158  	url := fmt.Sprintf("http://%v/naming/%v/services", c.Address, c.Version)
   159  
   160  	params := map[string][]interface{}{
   161  		"namespace": {services[0].GetNamespace().GetValue()},
   162  	}
   163  	time.Sleep(2 * time.Second)
   164  	url = c.CompleteURL(url, params)
   165  	response, err := c.SendRequest("GET", url, nil)
   166  	if err != nil {
   167  		return err
   168  	}
   169  
   170  	ret, err := GetBatchQueryResponse(response)
   171  	if err != nil {
   172  		fmt.Printf("%v\n", err)
   173  		return err
   174  	}
   175  
   176  	if ret.GetCode() == nil || ret.GetCode().GetValue() != api.ExecuteSuccess {
   177  		return errors.New("invalid batch code")
   178  	}
   179  
   180  	servicesSize := len(services)
   181  
   182  	if ret.GetAmount() == nil || ret.GetAmount().GetValue() != uint32(servicesSize) {
   183  		return fmt.Errorf("invalid batch amount, expect %d, actual is %v", servicesSize, ret.GetAmount())
   184  	}
   185  
   186  	if ret.GetSize() == nil || ret.GetSize().GetValue() != uint32(servicesSize) {
   187  		return errors.New("invalid batch size")
   188  	}
   189  
   190  	collection := make(map[string]*apiservice.Service)
   191  	for _, service := range services {
   192  		collection[service.GetName().GetValue()] = service
   193  	}
   194  
   195  	items := ret.GetServices()
   196  	if items == nil || len(items) != servicesSize {
   197  		return errors.New("invalid batch services")
   198  	}
   199  
   200  	for _, item := range items {
   201  		if correctItem, ok := collection[item.GetName().GetValue()]; ok {
   202  			if result := compareService(correctItem, item); !result {
   203  				return errors.New("invalid service")
   204  			}
   205  		} else {
   206  			return errors.New("invalid service")
   207  		}
   208  	}
   209  	return nil
   210  }
   211  
   212  /**
   213   * @brief 检查创建服务的回复
   214   */
   215  func checkCreateServicesResponse(ret *apiservice.BatchWriteResponse, services []*apiservice.Service) (
   216  	// #lizard forgives
   217  	*apiservice.BatchWriteResponse, error) {
   218  	if ret.GetCode() == nil || ret.GetCode().GetValue() != api.ExecuteSuccess {
   219  		return nil, errors.New("invalid batch code")
   220  	}
   221  
   222  	servicesSize := len(services)
   223  	if ret.GetSize() == nil || ret.GetSize().GetValue() != uint32(servicesSize) {
   224  		return nil, errors.New("invalid batch size")
   225  	}
   226  
   227  	items := ret.GetResponses()
   228  	if items == nil || len(items) != servicesSize {
   229  		return nil, errors.New("invalid batch response")
   230  	}
   231  
   232  	for index, item := range items {
   233  		if item.GetCode() == nil || item.GetCode().GetValue() != api.ExecuteSuccess {
   234  			return nil, errors.New("invalid code")
   235  		}
   236  
   237  		service := item.GetService()
   238  		if service == nil {
   239  			return nil, errors.New("empty service")
   240  		}
   241  
   242  		name := services[index].GetName().GetValue()
   243  		if service.GetName() == nil || service.GetName().GetValue() != name {
   244  			return nil, errors.New("invalid service name")
   245  		}
   246  
   247  		namespace := services[index].GetNamespace().GetValue()
   248  		if service.GetNamespace() == nil || service.GetNamespace().GetValue() != namespace {
   249  			return nil, errors.New("invalid namespace")
   250  		}
   251  
   252  		if service.GetToken() == nil || service.GetToken().GetValue() == "" {
   253  			return nil, errors.New("invalid service token")
   254  		}
   255  	}
   256  	return ret, nil
   257  }
   258  
   259  /**
   260   * @brief 比较service是否相等
   261   */
   262  func compareService(correctItem *apiservice.Service, item *apiservice.Service) bool {
   263  	correctName := correctItem.GetName().GetValue()
   264  	correctNamespace := correctItem.GetNamespace().GetValue()
   265  	correctMeta := correctItem.GetMetadata()
   266  	correctPorts := correctItem.GetPorts().GetValue()
   267  	correctBusiness := correctItem.GetBusiness().GetValue()
   268  	correctDepartment := correctItem.GetDepartment().GetValue()
   269  	correctCmdbMod1 := correctItem.GetCmdbMod1().GetValue()
   270  	correctCmdbMod2 := correctItem.GetCmdbMod2().GetValue()
   271  	correctCmdbMod3 := correctItem.GetCmdbMod3().GetValue()
   272  	correctComment := correctItem.GetComment().GetValue()
   273  
   274  	name := item.GetName().GetValue()
   275  	namespace := item.GetNamespace().GetValue()
   276  	meta := item.GetMetadata()
   277  	ports := item.GetPorts().GetValue()
   278  	business := item.GetBusiness().GetValue()
   279  	department := item.GetDepartment().GetValue()
   280  	cmdbMod1 := item.GetCmdbMod1().GetValue()
   281  	cmdbMod2 := item.GetCmdbMod2().GetValue()
   282  	cmdbMod3 := item.GetCmdbMod3().GetValue()
   283  	comment := item.GetComment().GetValue()
   284  
   285  	if correctName == name && correctNamespace == namespace && reflect.DeepEqual(correctMeta, meta) &&
   286  		correctPorts == ports && correctBusiness == business && correctDepartment == department &&
   287  		correctCmdbMod1 == cmdbMod1 && correctCmdbMod2 == cmdbMod2 && correctCmdbMod3 == cmdbMod3 &&
   288  		correctComment == comment {
   289  		return true
   290  	}
   291  	return false
   292  }