yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/aliyun/tablestore.go (about)

     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  
    15  package aliyun
    16  
    17  import (
    18  	"fmt"
    19  	"time"
    20  
    21  	"yunion.io/x/jsonutils"
    22  	"yunion.io/x/pkg/errors"
    23  
    24  	api "yunion.io/x/cloudmux/pkg/apis/compute"
    25  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    26  	"yunion.io/x/cloudmux/pkg/multicloud"
    27  )
    28  
    29  func (self *SAliyunClient) otsRequest(apiName string, params map[string]string) (jsonutils.JSONObject, error) {
    30  	cli, err := self.getDefaultClient()
    31  	if err != nil {
    32  		return nil, err
    33  	}
    34  	regionId, _ := params["RegionId"]
    35  	params = self.SetResourceGropuId(params)
    36  	return jsonRequest(cli, fmt.Sprintf("ots.%s.aliyuncs.com", regionId), ALIYUN_OTS_API_VERSION, apiName, params, self.debug)
    37  }
    38  
    39  func (self *SRegion) otsRequest(apiName string, params map[string]string) (jsonutils.JSONObject, error) {
    40  	params["RegionId"] = self.RegionId
    41  	return self.client.otsRequest(apiName, params)
    42  }
    43  
    44  type STablestore struct {
    45  	multicloud.SResourceBase
    46  	AliyunTags
    47  	region *SRegion
    48  
    49  	InstanceName string
    50  	Timestamp    time.Time
    51  }
    52  
    53  func (self *STablestore) GetGlobalId() string {
    54  	return self.InstanceName
    55  }
    56  
    57  func (self *STablestore) GetName() string {
    58  	return self.InstanceName
    59  }
    60  
    61  func (self *STablestore) GetId() string {
    62  	return self.InstanceName
    63  }
    64  
    65  func (self *STablestore) GetStatus() string {
    66  	return api.TABLESTORE_STATUS_RUNNING
    67  }
    68  
    69  func (self *STablestore) GetProjectId() string {
    70  	return ""
    71  }
    72  
    73  func (self *SRegion) GetTablestoreInstances(pageSize, pageNumber int) ([]STablestore, int, error) {
    74  	params := map[string]string{
    75  		"RegionId":   self.RegionId,
    76  		"PageSize":   fmt.Sprintf("%d", pageSize),
    77  		"PageNumber": fmt.Sprintf("%d", pageNumber),
    78  	}
    79  	resp, err := self.otsRequest("ListInstance", params)
    80  	if err != nil {
    81  		return nil, 0, errors.Wrapf(err, "ListInstance")
    82  	}
    83  	total, _ := resp.Int("TotalCount")
    84  	ret := []STablestore{}
    85  	return ret, int(total), resp.Unmarshal(&ret, "InstanceInfos", "InstanceInfo")
    86  }
    87  
    88  func (self *SRegion) GetICloudTablestores() ([]cloudprovider.ICloudTablestore, error) {
    89  	ots, pageNumber := []STablestore{}, 1
    90  	for {
    91  		part, total, err := self.GetTablestoreInstances(50, pageNumber)
    92  		if err != nil {
    93  			return nil, err
    94  		}
    95  		ots = append(ots, part...)
    96  		if len(ots) >= total {
    97  			break
    98  		}
    99  		pageNumber++
   100  	}
   101  	ret := []cloudprovider.ICloudTablestore{}
   102  	for i := range ots {
   103  		ots[i].region = self
   104  		ret = append(ret, &ots[i])
   105  	}
   106  	return ret, nil
   107  }