hbase.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. HBASE_API_VERSION = "2019-01-01"
  22. )
  23. type SHbase struct {
  24. ClusterId string
  25. ClusterName string
  26. }
  27. func (region *SRegion) fetchHbaseEndpoints() error {
  28. if len(region.client.hbaseEndpoint) > 0 {
  29. return nil
  30. }
  31. client, err := region.getSdkClient()
  32. if err != nil {
  33. return err
  34. }
  35. endpoint := "hbase.aliyuncs.com"
  36. resp, err := jsonRequest(client, endpoint, HBASE_API_VERSION, "DescribeRegions", map[string]string{}, region.client.debug)
  37. if err != nil {
  38. return err
  39. }
  40. ret := []SRegion{}
  41. err = resp.Unmarshal(&ret, "Regions", "Region")
  42. if err != nil {
  43. return err
  44. }
  45. hbaseEndpoints := map[string]string{}
  46. for _, r := range ret {
  47. hbaseEndpoints[r.RegionId] = r.RegionEndpoint
  48. }
  49. region.client.hbaseEndpoint = hbaseEndpoints
  50. return nil
  51. }
  52. func (region *SRegion) hbaseRequest(apiName string, params map[string]string) (jsonutils.JSONObject, error) {
  53. err := region.fetchHbaseEndpoints()
  54. if err != nil {
  55. return nil, errors.Wrapf(err, "fetchHbaseEndpoints")
  56. }
  57. client, err := region.getSdkClient()
  58. if err != nil {
  59. return nil, err
  60. }
  61. endpoint := "hbase.aliyuncs.com"
  62. if ep, ok := region.client.hbaseEndpoint[region.RegionId]; ok {
  63. endpoint = ep
  64. }
  65. params = region.client.SetResourceGropuId(params)
  66. return jsonRequest(client, endpoint, HBASE_API_VERSION, apiName, params, region.client.debug)
  67. }
  68. func (self *SRegion) GetHbaseInstances() ([]SHbase, error) {
  69. ret := []SHbase{}
  70. pageNumber := 1
  71. params := map[string]string{
  72. "RegionId": self.RegionId,
  73. "PageSize": "100",
  74. }
  75. for {
  76. params["PageNumber"] = fmt.Sprintf("%d", pageNumber)
  77. resp, err := self.hbaseRequest("DescribeInstances", params)
  78. if err != nil {
  79. return nil, err
  80. }
  81. part := struct {
  82. TotalCount int
  83. Instances struct {
  84. Instance []SHbase
  85. }
  86. }{}
  87. err = resp.Unmarshal(&part)
  88. if err != nil {
  89. return nil, err
  90. }
  91. ret = append(ret, part.Instances.Instance...)
  92. if len(ret) >= part.TotalCount || len(part.Instances.Instance) == 0 {
  93. break
  94. }
  95. pageNumber++
  96. }
  97. return ret, nil
  98. }