yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/qcloud/memcached.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 qcloud
    16  
    17  import (
    18  	"fmt"
    19  	"strconv"
    20  	"time"
    21  
    22  	"yunion.io/x/jsonutils"
    23  	"yunion.io/x/pkg/errors"
    24  
    25  	billing_api "yunion.io/x/cloudmux/pkg/apis/billing"
    26  	api "yunion.io/x/cloudmux/pkg/apis/compute"
    27  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    28  	"yunion.io/x/cloudmux/pkg/multicloud"
    29  	"yunion.io/x/onecloud/pkg/util/billing"
    30  )
    31  
    32  type SMemcached struct {
    33  	multicloud.SElasticcacheBase
    34  	QcloudTags
    35  	region *SRegion
    36  
    37  	InstanceId        string    `json:"InstanceId"`
    38  	InstanceName      string    `json:"InstanceName"`
    39  	AppId             int       `json:"AppId"`
    40  	ProjectId         int       `json:"ProjectId"`
    41  	InstanceDesc      string    `json:"InstanceDesc"`
    42  	Vip               string    `json:"Vip"`
    43  	Vport             int       `json:"Vport"`
    44  	Status            int       `json:"Status"`
    45  	AutoRenewFlag     int       `json:"AutoRenewFlag"`
    46  	VpcId             int       `json:"VpcId"`
    47  	SubnetId          int       `json:"SubnetId"`
    48  	PayMode           int       `json:"PayMode"`
    49  	ZoneId            int       `json:"ZoneId"`
    50  	Expire            int       `json:"Expire"`
    51  	RegionId          int       `json:"RegionId"`
    52  	AddTimeStamp      time.Time `json:"AddTimeStamp"`
    53  	ModtimeStamp      time.Time `json:"ModTimeStamp"`
    54  	IsolateTimesSamp  time.Time `json:"IsolateTimeStamp"`
    55  	UniqVpcId         string    `json:"UniqVpcId"`
    56  	UniqSubnetId      string    `json:"UniqSubnetId"`
    57  	DeadlineTimeStamp string    `json:"DeadlineTimeStamp"`
    58  	SetId             int       `json:"SetId"`
    59  	CmemId            int       `json:"CmemId"`
    60  }
    61  
    62  func (self *SMemcached) GetId() string {
    63  	return self.InstanceId
    64  }
    65  
    66  func (self *SMemcached) GetName() string {
    67  	return self.InstanceName
    68  }
    69  
    70  func (self *SMemcached) GetGlobalId() string {
    71  	return self.GetId()
    72  }
    73  
    74  func (self *SMemcached) GetStatus() string {
    75  	switch self.Status {
    76  	case 0:
    77  		return api.ELASTIC_CACHE_STATUS_DEPLOYING
    78  	case 1:
    79  		return api.ELASTIC_CACHE_STATUS_RUNNING
    80  	case 2:
    81  		return api.ELASTIC_CACHE_STATUS_CREATE_FAILED
    82  	case 4, 5, 6, 7:
    83  		return api.ELASTIC_CACHE_STATUS_RELEASING
    84  	default:
    85  		return fmt.Sprintf("%d", self.Status)
    86  	}
    87  }
    88  
    89  func (self *SMemcached) GetProjectId() string {
    90  	return strconv.Itoa(self.ProjectId)
    91  }
    92  
    93  func (self *SMemcached) GetBillingType() string {
    94  	// 计费模式:0-按量计费,1-包年包月
    95  	if self.PayMode == 1 {
    96  		return billing_api.BILLING_TYPE_PREPAID
    97  	} else {
    98  		return billing_api.BILLING_TYPE_POSTPAID
    99  	}
   100  }
   101  
   102  func (self *SMemcached) GetCreatedAt() time.Time {
   103  	return self.AddTimeStamp
   104  }
   105  
   106  func (self *SMemcached) GetExpiredAt() time.Time {
   107  	return time.Time{}
   108  }
   109  
   110  func (self *SMemcached) SetAutoRenew(bc billing.SBillingCycle) error {
   111  	return cloudprovider.ErrNotSupported
   112  }
   113  
   114  func (self *SMemcached) IsAutoRenew() bool {
   115  	return self.AutoRenewFlag == 1
   116  }
   117  
   118  func (self *SMemcached) GetInstanceType() string {
   119  	return ""
   120  }
   121  
   122  func (self *SMemcached) GetCapacityMB() int {
   123  	return 0
   124  }
   125  
   126  func (self *SMemcached) GetArchType() string {
   127  	return api.ELASTIC_CACHE_NODE_TYPE_SINGLE
   128  }
   129  
   130  func (self *SMemcached) GetNodeType() string {
   131  	return api.ELASTIC_CACHE_ARCH_TYPE_SINGLE
   132  }
   133  
   134  func (self *SMemcached) GetEngine() string {
   135  	return api.ELASTIC_CACHE_ENGINE_MEMCACHED
   136  }
   137  
   138  func (self *SMemcached) GetEngineVersion() string {
   139  	return "latest"
   140  }
   141  
   142  func (self *SMemcached) GetVpcId() string {
   143  	return self.UniqVpcId
   144  }
   145  
   146  func (self *SMemcached) GetZoneId() string {
   147  	return fmt.Sprintf("%s/%s-%d", self.region.GetGlobalId(), self.region.Region, self.ZoneId%10)
   148  }
   149  
   150  func (self *SMemcached) GetNetworkType() string {
   151  	if len(self.UniqSubnetId) > 0 {
   152  		return api.LB_NETWORK_TYPE_VPC
   153  	}
   154  	return api.LB_NETWORK_TYPE_CLASSIC
   155  }
   156  
   157  func (self *SMemcached) GetNetworkId() string {
   158  	return self.UniqSubnetId
   159  }
   160  
   161  func (self *SMemcached) GetPrivateDNS() string {
   162  	return ""
   163  }
   164  
   165  func (self *SMemcached) GetPrivateIpAddr() string {
   166  	return self.Vip
   167  }
   168  
   169  func (self *SMemcached) GetPrivateConnectPort() int {
   170  	return self.Vport
   171  }
   172  
   173  func (self *SMemcached) GetPublicDNS() string {
   174  	return ""
   175  }
   176  
   177  func (self *SMemcached) GetPublicIpAddr() string {
   178  	return ""
   179  }
   180  
   181  func (self *SMemcached) GetPublicConnectPort() int {
   182  	return 0
   183  }
   184  
   185  func (self *SMemcached) GetMaintainStartTime() string {
   186  	return ""
   187  }
   188  
   189  func (self *SMemcached) GetMaintainEndTime() string {
   190  	return ""
   191  }
   192  
   193  func (self *SMemcached) GetAuthMode() string {
   194  	return "off"
   195  }
   196  
   197  func (self *SMemcached) GetSecurityGroupIds() ([]string, error) {
   198  	return []string{}, nil
   199  }
   200  
   201  func (self *SMemcached) GetICloudElasticcacheAccounts() ([]cloudprovider.ICloudElasticcacheAccount, error) {
   202  	return []cloudprovider.ICloudElasticcacheAccount{}, nil
   203  }
   204  
   205  func (self *SMemcached) GetICloudElasticcacheAcls() ([]cloudprovider.ICloudElasticcacheAcl, error) {
   206  	return []cloudprovider.ICloudElasticcacheAcl{}, nil
   207  }
   208  
   209  func (self *SMemcached) GetICloudElasticcacheBackups() ([]cloudprovider.ICloudElasticcacheBackup, error) {
   210  	return []cloudprovider.ICloudElasticcacheBackup{}, nil
   211  }
   212  
   213  func (self *SMemcached) GetICloudElasticcacheParameters() ([]cloudprovider.ICloudElasticcacheParameter, error) {
   214  	return []cloudprovider.ICloudElasticcacheParameter{}, nil
   215  }
   216  
   217  func (self *SMemcached) GetICloudElasticcacheAccount(accountId string) (cloudprovider.ICloudElasticcacheAccount, error) {
   218  	return nil, cloudprovider.ErrNotFound
   219  }
   220  
   221  func (self *SMemcached) GetICloudElasticcacheAcl(aclId string) (cloudprovider.ICloudElasticcacheAcl, error) {
   222  	return nil, cloudprovider.ErrNotFound
   223  }
   224  
   225  func (self *SMemcached) GetICloudElasticcacheBackup(backupId string) (cloudprovider.ICloudElasticcacheBackup, error) {
   226  	return nil, cloudprovider.ErrNotFound
   227  }
   228  
   229  func (self *SMemcached) Restart() error {
   230  	return errors.Wrap(cloudprovider.ErrNotSupported, "Restart")
   231  }
   232  
   233  func (self *SMemcached) Delete() error {
   234  	return cloudprovider.ErrNotSupported
   235  }
   236  
   237  func (self *SMemcached) CleanupInstance() error {
   238  	return cloudprovider.ErrNotSupported
   239  }
   240  
   241  func (self *SMemcached) ChangeInstanceSpec(spec string) error {
   242  	return cloudprovider.ErrNotSupported
   243  }
   244  
   245  func (self *SMemcached) SetMaintainTime(maintainStartTime, maintainEndTime string) error {
   246  	return cloudprovider.ErrNotSupported
   247  }
   248  
   249  func (self *SMemcached) AllocatePublicConnection(port int) (string, error) {
   250  	return "", cloudprovider.ErrNotSupported
   251  }
   252  
   253  func (self *SMemcached) ReleasePublicConnection() error {
   254  	return errors.Wrap(cloudprovider.ErrNotSupported, "ReleasePublicConnection")
   255  }
   256  
   257  func (self *SMemcached) CreateAccount(account cloudprovider.SCloudElasticCacheAccountInput) (cloudprovider.ICloudElasticcacheAccount, error) {
   258  	return nil, cloudprovider.ErrNotSupported
   259  }
   260  
   261  func (self *SMemcached) CreateAcl(aclName, securityIps string) (cloudprovider.ICloudElasticcacheAcl, error) {
   262  	return nil, errors.Wrap(cloudprovider.ErrNotSupported, "CreateAcl")
   263  }
   264  
   265  func (self *SMemcached) CreateBackup(desc string) (cloudprovider.ICloudElasticcacheBackup, error) {
   266  	return nil, cloudprovider.ErrNotSupported
   267  }
   268  
   269  func (self *SMemcached) FlushInstance(input cloudprovider.SCloudElasticCacheFlushInstanceInput) error {
   270  	return cloudprovider.ErrNotSupported
   271  }
   272  
   273  func (self *SMemcached) UpdateAuthMode(noPasswordAccess bool, password string) error {
   274  	return cloudprovider.ErrNotSupported
   275  }
   276  
   277  func (self *SMemcached) UpdateInstanceParameters(config jsonutils.JSONObject) error {
   278  	return cloudprovider.ErrNotSupported
   279  }
   280  
   281  func (self *SMemcached) UpdateBackupPolicy(config cloudprovider.SCloudElasticCacheBackupPolicyUpdateInput) error {
   282  	return cloudprovider.ErrNotSupported
   283  }
   284  
   285  func (self *SMemcached) UpdateSecurityGroups(secgroupIds []string) error {
   286  	return cloudprovider.ErrNotSupported
   287  }
   288  
   289  func (self *SMemcached) Renew(bc billing.SBillingCycle) error {
   290  	return cloudprovider.ErrNotSupported
   291  }
   292  
   293  func (self *SRegion) GetMemcaches(ids []string, limit, offset int) ([]SMemcached, int, error) {
   294  	params := map[string]string{}
   295  	if limit <= 0 {
   296  		limit = 100
   297  	}
   298  	params["Limit"] = fmt.Sprintf("%d", limit)
   299  	params["Offset"] = fmt.Sprintf("%d", offset)
   300  	for i, id := range ids {
   301  		params[fmt.Sprintf("InstanceIds.%d", i)] = id
   302  	}
   303  	resp, err := self.memcachedRequest("DescribeInstances", params)
   304  	if err != nil {
   305  		return nil, 0, errors.Wrapf(err, "DescribeInstances")
   306  	}
   307  	ret := []SMemcached{}
   308  	err = resp.Unmarshal(&ret, "InstanceList")
   309  	if err != nil {
   310  		return nil, 0, errors.Wrapf(err, "resp.Unmarshal InstanceList")
   311  	}
   312  	total, _ := resp.Float("TotalCount")
   313  	return ret, int(total), nil
   314  }