rocketmq.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package aliyun
  15. import (
  16. "fmt"
  17. "yunion.io/x/jsonutils"
  18. "yunion.io/x/pkg/errors"
  19. )
  20. const (
  21. ROCKYMQ_API_VERSION = "2022-08-01"
  22. )
  23. type SRocketmqInstance struct {
  24. InstanceId string
  25. InstanceName string
  26. }
  27. func (self *SRegion) rocketmqRequest(apiName string, params map[string]string, body interface{}) (jsonutils.JSONObject, error) {
  28. client, err := self.getSdkClient()
  29. if err != nil {
  30. return nil, err
  31. }
  32. params = self.client.SetResourceGropuId(params)
  33. return doRequest(client, fmt.Sprintf("rocketmq.%s.aliyuncs.com", self.RegionId), ROCKYMQ_API_VERSION, apiName, params, body, self.client.debug)
  34. }
  35. func (region *SRegion) GetRocketmqInstances() ([]SRocketmqInstance, error) {
  36. params := map[string]string{
  37. "PathPattern": "/instances",
  38. "pageSize": "200",
  39. }
  40. pageNumber := 1
  41. ret := []SRocketmqInstance{}
  42. for {
  43. params["pageNumber"] = fmt.Sprintf("%d", pageNumber)
  44. resp, err := region.rocketmqRequest("ListInstances", params, nil)
  45. if err != nil {
  46. return nil, errors.Wrapf(err, "ListInstances")
  47. }
  48. part := struct {
  49. Data struct {
  50. TotalCount int
  51. List []SRocketmqInstance
  52. }
  53. }{}
  54. err = resp.Unmarshal(&part)
  55. if err != nil {
  56. return nil, errors.Wrapf(err, "resp.Unmarshal")
  57. }
  58. ret = append(ret, part.Data.List...)
  59. if len(ret) >= part.Data.TotalCount || len(part.Data.List) == 0 {
  60. break
  61. }
  62. pageNumber++
  63. }
  64. return ret, nil
  65. }