ots.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 apsara
  15. import (
  16. "fmt"
  17. "time"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/pkg/errors"
  20. api "yunion.io/x/cloudmux/pkg/apis/compute"
  21. "yunion.io/x/cloudmux/pkg/cloudprovider"
  22. "yunion.io/x/cloudmux/pkg/multicloud"
  23. )
  24. func (self *SRegion) otsRequest(apiName string, params map[string]string) (jsonutils.JSONObject, error) {
  25. client, err := self.getSdkClient()
  26. if err != nil {
  27. return nil, err
  28. }
  29. domain := self.client.getDomain(APSARA_PRODUCT_OTS)
  30. return self.productRequest(client, APSARA_PRODUCT_OTS, domain, APSARA_OTS_API_VERSION, apiName, params, self.client.debug)
  31. }
  32. type STablestore struct {
  33. multicloud.SResourceBase
  34. ApsaraTags
  35. region *SRegion
  36. InstanceName string
  37. Timestamp time.Time
  38. DepartmentInfo
  39. }
  40. func (self *STablestore) GetGlobalId() string {
  41. return self.InstanceName
  42. }
  43. func (self *STablestore) GetName() string {
  44. return self.InstanceName
  45. }
  46. func (self *STablestore) GetId() string {
  47. return self.InstanceName
  48. }
  49. func (self *STablestore) GetStatus() string {
  50. return api.TABLESTORE_STATUS_RUNNING
  51. }
  52. func (self *SRegion) GetTablestoreInstances(pageSize, pageNumber int) ([]STablestore, int, error) {
  53. params := map[string]string{
  54. "RegionId": self.RegionId,
  55. "PageSize": fmt.Sprintf("%d", pageSize),
  56. "PageNumber": fmt.Sprintf("%d", pageNumber),
  57. }
  58. resp, err := self.otsRequest("ListInstance", params)
  59. if err != nil {
  60. return nil, 0, errors.Wrapf(err, "ListInstance")
  61. }
  62. total, _ := resp.Int("TotalCount")
  63. ret := []STablestore{}
  64. return ret, int(total), resp.Unmarshal(&ret, "InstanceInfos", "InstanceInfo")
  65. }
  66. func (self *SRegion) GetICloudTablestores() ([]cloudprovider.ICloudTablestore, error) {
  67. ots, pageNumber := []STablestore{}, 1
  68. for {
  69. part, total, err := self.GetTablestoreInstances(50, pageNumber)
  70. if err != nil {
  71. return nil, err
  72. }
  73. ots = append(ots, part...)
  74. if len(ots) >= total {
  75. break
  76. }
  77. pageNumber++
  78. }
  79. ret := []cloudprovider.ICloudTablestore{}
  80. for i := range ots {
  81. ots[i].region = self
  82. ret = append(ret, &ots[i])
  83. }
  84. return ret, nil
  85. }