yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/jdcloud/rds.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 jdcloud
    16  
    17  import (
    18  	"fmt"
    19  	"strconv"
    20  	"strings"
    21  	"time"
    22  
    23  	commodels "github.com/jdcloud-api/jdcloud-sdk-go/services/common/models"
    24  	"github.com/jdcloud-api/jdcloud-sdk-go/services/rds/apis"
    25  	"github.com/jdcloud-api/jdcloud-sdk-go/services/rds/client"
    26  	"github.com/jdcloud-api/jdcloud-sdk-go/services/rds/models"
    27  
    28  	"yunion.io/x/jsonutils"
    29  	"yunion.io/x/pkg/errors"
    30  
    31  	api "yunion.io/x/cloudmux/pkg/apis/compute"
    32  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    33  	"yunion.io/x/cloudmux/pkg/multicloud"
    34  )
    35  
    36  type SDBInstance struct {
    37  	multicloud.SDBInstanceBase
    38  	JdcloudTags
    39  	region *SRegion
    40  
    41  	models.DBInstance
    42  }
    43  
    44  func (self *SDBInstance) GetName() string {
    45  	return self.InstanceName
    46  }
    47  
    48  func (self *SDBInstance) GetInstanceType() string {
    49  	return self.InstanceClass
    50  }
    51  
    52  func (self *SDBInstance) GetCategory() string {
    53  	return self.InstanceType
    54  }
    55  
    56  func (self *SDBInstance) GetDiskSizeGB() int {
    57  	return self.InstanceStorageGB
    58  }
    59  
    60  func (self *SDBInstance) GetEngine() string {
    61  	return self.Engine
    62  }
    63  
    64  func (self *SDBInstance) GetEngineVersion() string {
    65  	return self.EngineVersion
    66  }
    67  
    68  func (self *SDBInstance) GetGlobalId() string {
    69  	return self.InstanceId
    70  }
    71  
    72  func (self *SDBInstance) GetId() string {
    73  	return self.InstanceId
    74  }
    75  
    76  func (self *SDBInstance) GetIVpcId() string {
    77  	return self.VpcId
    78  }
    79  
    80  func (self *SDBInstance) Refresh() error {
    81  	rds, err := self.region.GetDBInstance(self.InstanceId)
    82  	if err != nil {
    83  		return err
    84  	}
    85  	return jsonutils.Update(self, rds)
    86  }
    87  
    88  func (self *SDBInstance) GetMaintainTime() string {
    89  	return ""
    90  }
    91  
    92  func (self *SDBInstance) GetMasterInstanceId() string {
    93  	return self.SourceInstanceId
    94  }
    95  
    96  func (self *SDBInstance) GetPort() int {
    97  	if len(self.InstancePort) == 0 {
    98  		self.Refresh()
    99  	}
   100  	port, _ := strconv.Atoi(self.InstancePort)
   101  	return int(port)
   102  }
   103  
   104  func (self *SDBInstance) GetCreatedAt() time.Time {
   105  	return parseTime(self.CreateTime)
   106  }
   107  
   108  func (self *SDBInstance) GetExpiredAt() time.Time {
   109  	return expireAt(&self.Charge)
   110  }
   111  
   112  func (self *SDBInstance) GetStatus() string {
   113  	switch self.InstanceStatus {
   114  	case "BUILDING", "DDLING", "PARAMETERGROUP_MODIFYING", "AUDIT_OPENING", "AUDIT_CLOSING", "SECURITY_OPENING", "SECURITY_CLOSING", "SSL_OPENING", "SSL_CLOSING":
   115  		return api.DBINSTANCE_DEPLOYING
   116  	case "RUNNING":
   117  		return api.DBINSTANCE_RUNNING
   118  	case "DELETING":
   119  		return api.DBINSTANCE_DELETING
   120  	case "FAILOVER", "AZ_MIGRATING":
   121  		return api.DBINSTANCE_MIGRATING
   122  	case "RESTORING", "DB_RESTORING":
   123  		return api.DBINSTANCE_RESTORING
   124  	case "MODIFYING":
   125  		return api.DBINSTANCE_CHANGE_CONFIG
   126  	case "BUILD_READONLY":
   127  		return api.DBINSTANCE_DEPLOYING
   128  	case "REBOOTING":
   129  		return api.DBINSTANCE_REBOOTING
   130  	case "MAINTENANCE":
   131  		return api.DBINSTANCE_MAINTENANCE
   132  	default:
   133  		return self.InstanceStatus
   134  	}
   135  }
   136  
   137  func (self *SDBInstance) GetStorageType() string {
   138  	return self.InstanceStorageType
   139  }
   140  
   141  func (self *SDBInstance) GetVcpuCount() int {
   142  	return self.InstanceCPU
   143  }
   144  
   145  func (self *SDBInstance) GetVmemSizeMB() int {
   146  	return self.InstanceMemoryMB
   147  }
   148  
   149  func (self *SDBInstance) GetZone1Id() string {
   150  	if len(self.AzId) > 0 {
   151  		return self.AzId[0]
   152  	}
   153  	return ""
   154  }
   155  
   156  func (self *SDBInstance) GetZone2Id() string {
   157  	if len(self.AzId) > 1 {
   158  		return self.AzId[1]
   159  	}
   160  	return ""
   161  }
   162  
   163  func (self *SDBInstance) GetZone3Id() string {
   164  	if len(self.AzId) > 2 {
   165  		return self.AzId[2]
   166  	}
   167  	return ""
   168  }
   169  
   170  func (self *SDBInstance) GetConnectionStr() string {
   171  	return self.PublicDomainName
   172  }
   173  
   174  func (self *SDBInstance) GetInternalConnectionStr() string {
   175  	return self.InternalDomainName
   176  }
   177  
   178  func (self *SDBInstance) Delete() error {
   179  	return self.region.DeleteDBInstance(self.InstanceId)
   180  }
   181  
   182  func (self *SRegion) GetIDBInstances() ([]cloudprovider.ICloudDBInstance, error) {
   183  	rds := []SDBInstance{}
   184  	n := 1
   185  	for {
   186  		part, total, err := self.GetDBInstances(n, 100)
   187  		if err != nil {
   188  			return nil, errors.Wrapf(err, "GetDBInstances")
   189  		}
   190  		rds = append(rds, part...)
   191  		if len(rds) >= total {
   192  			break
   193  		}
   194  		n++
   195  	}
   196  	ret := []cloudprovider.ICloudDBInstance{}
   197  	for i := range rds {
   198  		ret = append(ret, &rds[i])
   199  	}
   200  	return ret, nil
   201  }
   202  
   203  func (self *SRegion) GetIDBInstanceById(id string) (cloudprovider.ICloudDBInstance, error) {
   204  	rds, err := self.GetDBInstance(id)
   205  	if err != nil {
   206  		return nil, errors.Wrapf(err, "GetDBInstance(%s)", id)
   207  	}
   208  	return rds, nil
   209  }
   210  
   211  func (self *SRegion) GetDBInstance(id string) (*SDBInstance, error) {
   212  	req := apis.NewDescribeInstanceAttributesRequest(self.ID, id)
   213  	client := client.NewRdsClient(self.getCredential())
   214  	client.Logger = Logger{debug: self.client.debug}
   215  	resp, err := client.DescribeInstanceAttributes(req)
   216  	if err != nil {
   217  		return nil, errors.Wrapf(err, "DescribeInstanceAttributes")
   218  	}
   219  	if resp.Error.Code == 404 || strings.Contains(resp.Error.Status, "NotFound") {
   220  		return nil, errors.Wrapf(cloudprovider.ErrNotFound, jsonutils.Marshal(resp.Error).String())
   221  	} else if resp.Error.Code != 0 {
   222  		return nil, errors.Error(jsonutils.Marshal(resp.Error).String())
   223  	}
   224  	ret := SDBInstance{
   225  		region: self,
   226  	}
   227  	jsonutils.Update(&ret.DBInstance, resp.Result.DbInstanceAttributes)
   228  	return &ret, nil
   229  }
   230  
   231  func (self *SRegion) DeleteDBInstance(id string) error {
   232  	req := apis.NewDeleteInstanceRequest(self.ID, id)
   233  	client := client.NewRdsClient(self.getCredential())
   234  	client.Logger = Logger{}
   235  	resp, err := client.DeleteInstance(req)
   236  	if err != nil {
   237  		return err
   238  	}
   239  	if resp.Error.Code == 404 {
   240  		return nil
   241  	}
   242  	return nil
   243  }
   244  
   245  func (self *SRegion) GetDBInstances(pageNumber int, pageSize int) ([]SDBInstance, int, error) {
   246  	req := apis.NewDescribeInstancesRequestWithAllParams(self.ID, &pageNumber, &pageSize, []commodels.Filter{}, []commodels.TagFilter{})
   247  	client := client.NewRdsClient(self.getCredential())
   248  	client.Logger = Logger{debug: self.client.debug}
   249  	resp, err := client.DescribeInstances(req)
   250  	if err != nil {
   251  		return nil, 0, errors.Wrapf(err, "DescribeInstances")
   252  	}
   253  	if resp.Error.Code >= 400 {
   254  		err = fmt.Errorf(resp.Error.Message)
   255  		return nil, 0, err
   256  	}
   257  	total := resp.Result.TotalCount
   258  	ret := []SDBInstance{}
   259  	for i := range resp.Result.DbInstances {
   260  		ret = append(ret, SDBInstance{
   261  			region:     self,
   262  			DBInstance: resp.Result.DbInstances[i],
   263  		})
   264  	}
   265  	return ret, total, nil
   266  }