yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/qcloud/rds_tdsql.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  	"context"
    19  	"fmt"
    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  )
    30  
    31  type STDSQL struct {
    32  	multicloud.SDBInstanceBase
    33  	// multicloud.SBillingBase
    34  	QcloudTags
    35  	region *SRegion
    36  
    37  	AppId             int           `json:"AppId"`
    38  	AutoRenewFlag     int           `json:"AutoRenewFlag"`
    39  	CPU               int           `json:"Cpu"`
    40  	CreateTime        time.Time     `json:"CreateTime"`
    41  	DbEngine          string        `json:"DbEngine"`
    42  	DbVersion         string        `json:"DbVersion"`
    43  	DcnDstNum         int           `json:"DcnDstNum"`
    44  	DcnFlag           int           `json:"DcnFlag"`
    45  	DcnStatus         int           `json:"DcnStatus"`
    46  	ExclusterId       string        `json:"ExclusterId"`
    47  	Id                int           `json:"Id"`
    48  	InstanceId        string        `json:"InstanceId"`
    49  	InstanceName      string        `json:"InstanceName"`
    50  	InstanceType      int           `json:"InstanceType"`
    51  	Ipv6Flag          int           `json:"Ipv6Flag"`
    52  	IsAuditSupported  int           `json:"IsAuditSupported"`
    53  	IsTmp             int           `json:"IsTmp"`
    54  	IsolatedTimestamp string        `json:"IsolatedTimestamp"`
    55  	Locker            int           `json:"Locker"`
    56  	Memory            int           `json:"Memory"`
    57  	NodeCount         int           `json:"NodeCount"`
    58  	Paymode           string        `json:"Paymode"`
    59  	PeriodEndTime     string        `json:"PeriodEndTime"`
    60  	Pid               int           `json:"Pid"`
    61  	ProjectId         int           `json:"ProjectId"`
    62  	Region            string        `json:"Region"`
    63  	ShardCount        int           `json:"ShardCount"`
    64  	ShardDetail       []ShardDetail `json:"ShardDetail"`
    65  	Status            int           `json:"Status"`
    66  	StatusDesc        string        `json:"StatusDesc"`
    67  	Storage           int           `json:"Storage"`
    68  	SubnetId          int           `json:"SubnetId"`
    69  	Uin               string        `json:"Uin"`
    70  	UniqueSubnetId    string        `json:"UniqueSubnetId"`
    71  	UniqueVpcId       string        `json:"UniqueVpcId"`
    72  	UpdateTime        string        `json:"UpdateTime"`
    73  	Vip               string        `json:"Vip"`
    74  	Vipv6             string        `json:"Vipv6"`
    75  	VpcId             int           `json:"VpcId"`
    76  	Vport             int           `json:"Vport"`
    77  	WanDomain         string        `json:"WanDomain"`
    78  	WanPort           int           `json:"WanPort"`
    79  	WanPortIpv6       int           `json:"WanPortIpv6"`
    80  	WanStatus         int           `json:"WanStatus"`
    81  	WanStatusIpv6     int           `json:"WanStatusIpv6"`
    82  	WanVip            string        `json:"WanVip"`
    83  	WanVipv6          string        `json:"WanVipv6"`
    84  	Zone              string        `json:"Zone"`
    85  }
    86  type ShardDetail struct {
    87  	CPU             int    `json:"Cpu"`
    88  	Createtime      string `json:"Createtime"`
    89  	Memory          int    `json:"Memory"`
    90  	NodeCount       int    `json:"NodeCount"`
    91  	Pid             int    `json:"Pid"`
    92  	ShardId         int    `json:"ShardId"`
    93  	ShardInstanceId string `json:"ShardInstanceId"`
    94  	ShardSerialId   string `json:"ShardSerialId"`
    95  	Status          int    `json:"Status"`
    96  	Storage         int    `json:"Storage"`
    97  }
    98  
    99  func (self *STDSQL) GetName() string {
   100  	return self.InstanceName
   101  }
   102  
   103  func (self *STDSQL) GetId() string {
   104  	return self.InstanceId
   105  }
   106  
   107  func (self *STDSQL) GetGlobalId() string {
   108  	return self.InstanceId
   109  }
   110  
   111  // 0 创建中,1 流程处理中, 2 运行中,3 实例未初始化,-1 实例已隔离,-2 实例已删除,4 实例初始化中,5 实例删除中,6 实例重启中,7 数据迁移中
   112  func (self *STDSQL) GetStatus() string {
   113  	switch self.Status {
   114  	case 0, 1, 3, 4:
   115  		return api.DBINSTANCE_DEPLOYING
   116  	case 2:
   117  		return api.DBINSTANCE_RUNNING
   118  	case -1, -2, 5:
   119  		return api.DBINSTANCE_DELETING
   120  	case 6:
   121  		return api.DBINSTANCE_REBOOTING
   122  	case 7:
   123  		return api.DBINSTANCE_MIGRATING
   124  	default:
   125  		return fmt.Sprintf("%d", self.Status)
   126  	}
   127  }
   128  
   129  func (self *STDSQL) GetPort() int {
   130  	return self.Vport
   131  }
   132  
   133  func (self *STDSQL) GetVmemSizeMB() int {
   134  	return self.Memory * 1024
   135  }
   136  
   137  func (self *STDSQL) GetDiskSizeGB() int {
   138  	return self.Storage
   139  }
   140  
   141  func (self *STDSQL) GetVcpuCount() int {
   142  	return self.CPU
   143  }
   144  
   145  func (self *STDSQL) GetCreatedAt() time.Time {
   146  	return self.CreateTime.Add(time.Hour * -8)
   147  }
   148  
   149  func (self *STDSQL) GetBillingType() string {
   150  	return self.Paymode
   151  }
   152  
   153  func (self *STDSQL) GetProjectId() string {
   154  	return fmt.Sprintf("%d", self.ProjectId)
   155  }
   156  
   157  func (self *STDSQL) Refresh() error {
   158  	sql, err := self.region.GetTDSQL(self.InstanceId)
   159  	if err != nil {
   160  		return err
   161  	}
   162  	return jsonutils.Update(self, sql)
   163  }
   164  
   165  func (self *STDSQL) Reboot() error {
   166  	return cloudprovider.ErrNotSupported
   167  }
   168  
   169  func (self *STDSQL) GetMasterInstanceId() string {
   170  	return ""
   171  }
   172  
   173  func (self *STDSQL) GetSecurityGroupIds() ([]string, error) {
   174  	ret := []string{}
   175  	groups, err := self.region.GetTDSQLSecurityGroups(self.InstanceId)
   176  	if err != nil {
   177  		return ret, err
   178  	}
   179  	for i := range groups {
   180  		ret = append(ret, groups[i].SecurityGroupId)
   181  	}
   182  	return ret, nil
   183  }
   184  
   185  func (self *STDSQL) SetSecurityGroups(ids []string) error {
   186  	return cloudprovider.ErrNotImplemented
   187  }
   188  
   189  func (self *STDSQL) GetEngine() string {
   190  	return self.DbEngine
   191  }
   192  
   193  func (self *STDSQL) GetEngineVersion() string {
   194  	return self.DbVersion
   195  }
   196  
   197  func (self *STDSQL) GetInstanceType() string {
   198  	return fmt.Sprintf("%dC%dG", self.GetVcpuCount(), self.GetVmemSizeMB()/1024)
   199  }
   200  
   201  func (self *STDSQL) ChangeConfig(ctx context.Context, opts *cloudprovider.SManagedDBInstanceChangeConfig) error {
   202  	return cloudprovider.ErrNotImplemented
   203  }
   204  
   205  func (self *STDSQL) ClosePublicConnection() error {
   206  	return cloudprovider.ErrNotImplemented
   207  }
   208  
   209  func (self *STDSQL) OpenPublicConnection() error {
   210  	return cloudprovider.ErrNotImplemented
   211  }
   212  
   213  func (self *STDSQL) CreateAccount(opts *cloudprovider.SDBInstanceAccountCreateConfig) error {
   214  	return cloudprovider.ErrNotImplemented
   215  }
   216  
   217  func (self *STDSQL) CreateDatabase(opts *cloudprovider.SDBInstanceDatabaseCreateConfig) error {
   218  	return cloudprovider.ErrNotImplemented
   219  }
   220  
   221  func (self *STDSQL) CreateIBackup(opts *cloudprovider.SDBInstanceBackupCreateConfig) (string, error) {
   222  	return "", cloudprovider.ErrNotImplemented
   223  }
   224  
   225  func (self *STDSQL) GetMaintainTime() string {
   226  	return ""
   227  }
   228  
   229  func (self *STDSQL) GetStorageType() string {
   230  	return api.QCLOUD_DBINSTANCE_STORAGE_TYPE_LOCAL_SSD
   231  }
   232  
   233  func (self *STDSQL) GetIVpcId() string {
   234  	return self.UniqueVpcId
   235  }
   236  
   237  func (self *STDSQL) Delete() error {
   238  	if self.GetBillingType() == billing_api.BILLING_TYPE_PREPAID {
   239  		return self.region.DeletePrepaidTDSQL(self.InstanceId)
   240  	}
   241  	return self.region.DeletePostpaidTDSQL(self.InstanceId)
   242  }
   243  
   244  func (self *STDSQL) GetCategory() string {
   245  	return api.QCLOUD_DBINSTANCE_CATEGORY_TDSQL
   246  }
   247  
   248  func (self *STDSQL) GetConnectionStr() string {
   249  	if len(self.WanDomain) > 0 {
   250  		return fmt.Sprintf("%s:%d", self.WanDomain, self.WanPort)
   251  	}
   252  	return ""
   253  }
   254  
   255  func (self *STDSQL) GetInternalConnectionStr() string {
   256  	if len(self.Vip) > 0 {
   257  		return fmt.Sprintf("%s:%d", self.Vip, self.Vport)
   258  	}
   259  	return ""
   260  }
   261  
   262  func (self *STDSQL) GetZone1Id() string {
   263  	return self.Zone
   264  }
   265  
   266  func (self *STDSQL) GetZone2Id() string {
   267  	return ""
   268  }
   269  
   270  func (self *STDSQL) GetZone3Id() string {
   271  	return ""
   272  }
   273  
   274  func (self *STDSQL) RecoveryFromBackup(conf *cloudprovider.SDBInstanceRecoveryConfig) error {
   275  	return cloudprovider.ErrNotImplemented
   276  }
   277  
   278  func (self *STDSQL) GetDBNetworks() ([]cloudprovider.SDBInstanceNetwork, error) {
   279  	ret := []cloudprovider.SDBInstanceNetwork{}
   280  	if len(self.Vip) > 0 && len(self.UniqueSubnetId) > 0 {
   281  		ret = append(ret, cloudprovider.SDBInstanceNetwork{NetworkId: self.UniqueSubnetId, IP: self.Vip})
   282  	}
   283  	return ret, nil
   284  }
   285  
   286  func (self *SRegion) GetTDSQL(id string) (*STDSQL, error) {
   287  	sqls, _, err := self.GetTDSQLs([]string{id}, 1, 0)
   288  	if err != nil {
   289  		return nil, errors.Wrapf(err, "GetTDSQLs")
   290  	}
   291  	for i := range sqls {
   292  		if sqls[i].InstanceId == id {
   293  			sqls[i].region = self
   294  			return &sqls[i], nil
   295  		}
   296  	}
   297  	return nil, errors.Wrapf(cloudprovider.ErrNotFound, "id: [%s]", id)
   298  }
   299  
   300  func (self *SRegion) GetTDSQLs(ids []string, limit, offset int) ([]STDSQL, int, error) {
   301  	if limit < 1 || limit > 100 {
   302  		limit = 100
   303  	}
   304  	params := map[string]string{
   305  		"Limit":  fmt.Sprintf("%d", limit),
   306  		"Offset": fmt.Sprintf("%d", offset),
   307  	}
   308  	for idx, id := range ids {
   309  		params[fmt.Sprintf("InstanceIds.%d", idx)] = id
   310  	}
   311  	resp, err := self.dcdbRequest("DescribeDCDBInstances", params)
   312  	if err != nil {
   313  		return nil, 0, errors.Wrapf(err, "DescribeDCDBInstances")
   314  	}
   315  	ret := []STDSQL{}
   316  	err = resp.Unmarshal(&ret, "Instances")
   317  	if err != nil {
   318  		return nil, 0, errors.Wrapf(err, "resp.Unmarshal")
   319  	}
   320  	totalCount, _ := resp.Float("TotalCount")
   321  	return ret, int(totalCount), nil
   322  }
   323  
   324  func (self *SRegion) GetITDSQLs() ([]cloudprovider.ICloudDBInstance, error) {
   325  	ret := []cloudprovider.ICloudDBInstance{}
   326  	for {
   327  		part, total, err := self.GetTDSQLs(nil, 100, len(ret))
   328  		if err != nil {
   329  			return nil, errors.Wrapf(err, "GetTDSQLs")
   330  		}
   331  		for i := range part {
   332  			part[i].region = self
   333  			ret = append(ret, &part[i])
   334  		}
   335  		if len(ret) >= total {
   336  			break
   337  		}
   338  	}
   339  	return ret, nil
   340  }
   341  
   342  type STDSQLSecurityGroup struct {
   343  	CreateTime          string `json:"CreateTime"`
   344  	ProjectID           int    `json:"ProjectId"`
   345  	SecurityGroupId     string `json:"SecurityGroupId"`
   346  	SecurityGroupName   string `json:"SecurityGroupName"`
   347  	SecurityGroupRemark string `json:"SecurityGroupRemark"`
   348  }
   349  
   350  func (self *SRegion) GetTDSQLSecurityGroups(id string) ([]STDSQLSecurityGroup, error) {
   351  	params := map[string]string{
   352  		"Product":    "dcdb",
   353  		"InstanceId": id,
   354  	}
   355  	resp, err := self.dcdbRequest("DescribeDBSecurityGroups", params)
   356  	if err != nil {
   357  		return nil, errors.Wrapf(err, "DescribeDBSecurityGroups")
   358  	}
   359  	ret := []STDSQLSecurityGroup{}
   360  	err = resp.Unmarshal(&ret, "Groups")
   361  	if err != nil {
   362  		return nil, errors.Wrapf(err, "resp.Unmarshal")
   363  	}
   364  	return ret, nil
   365  }
   366  
   367  func (self *STDSQL) GetIDBInstanceBackups() ([]cloudprovider.ICloudDBInstanceBackup, error) {
   368  	return []cloudprovider.ICloudDBInstanceBackup{}, nil
   369  }
   370  
   371  func (self *STDSQL) GetIDBInstanceParameters() ([]cloudprovider.ICloudDBInstanceParameter, error) {
   372  	return nil, cloudprovider.ErrNotImplemented
   373  }
   374  
   375  func (self *SRegion) DeletePostpaidTDSQL(id string) error {
   376  	params := map[string]string{
   377  		"InstanceId": id,
   378  	}
   379  	_, err := self.dcdbRequest("DestroyHourDCDBInstance", params)
   380  	return errors.Wrapf(err, "DestroyHourDCDBInstance")
   381  }
   382  
   383  func (self *SRegion) DeletePrepaidTDSQL(id string) error {
   384  	params := map[string]string{
   385  		"InstanceId": id,
   386  	}
   387  	_, err := self.dcdbRequest("DestroyDCDBInstance", params)
   388  	return errors.Wrapf(err, "DestroyDCDBInstance")
   389  }