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