yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/jdcloud/rds_account.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  
    20  	"github.com/jdcloud-api/jdcloud-sdk-go/services/rds/apis"
    21  	"github.com/jdcloud-api/jdcloud-sdk-go/services/rds/client"
    22  	"github.com/jdcloud-api/jdcloud-sdk-go/services/rds/models"
    23  
    24  	"yunion.io/x/pkg/errors"
    25  
    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 SDBInstanceAccount struct {
    32  	multicloud.SDBInstanceAccountBase
    33  	JdcloudTags
    34  
    35  	rds *SDBInstance
    36  	models.Account
    37  }
    38  
    39  func (self *SDBInstanceAccount) GetGlobalId() string {
    40  	return self.AccountName
    41  }
    42  
    43  func (self *SDBInstanceAccount) GetId() string {
    44  	return self.AccountName
    45  }
    46  
    47  func (self *SDBInstanceAccount) GetName() string {
    48  	return self.AccountName
    49  }
    50  
    51  func (self *SDBInstanceAccount) GetStatus() string {
    52  	switch self.AccountStatus {
    53  	case "BUILDING":
    54  		return api.DBINSTANCE_USER_CREATING
    55  	case "RUNNING":
    56  		return api.DBINSTANCE_USER_AVAILABLE
    57  	case "DELETING":
    58  		return api.DBINSTANCE_USER_DELETING
    59  	default:
    60  		return api.DBINSTANCE_USER_AVAILABLE
    61  	}
    62  }
    63  
    64  func (self *SDBInstanceAccount) GetIDBInstanceAccountPrivileges() ([]cloudprovider.ICloudDBInstanceAccountPrivilege, error) {
    65  	ret := []cloudprovider.ICloudDBInstanceAccountPrivilege{}
    66  	for i := range self.AccountPrivileges {
    67  		ret = append(ret, &SDBInstanceAccountPrivilege{
    68  			Account:          self.AccountName,
    69  			AccountPrivilege: self.AccountPrivileges[i],
    70  		})
    71  	}
    72  	return ret, nil
    73  }
    74  
    75  func (self *SRegion) GetDBInstanceAccounts(id string, pageNumber, pageSize int) ([]SDBInstanceAccount, int, error) {
    76  	req := apis.NewDescribeAccountsRequestWithAllParams(self.ID, id, &pageNumber, &pageSize)
    77  	client := client.NewRdsClient(self.getCredential())
    78  	client.Logger = Logger{debug: self.client.debug}
    79  	resp, err := client.DescribeAccounts(req)
    80  	if err != nil {
    81  		return nil, 0, errors.Wrapf(err, "DescribeAccounts")
    82  	}
    83  	if resp.Error.Code >= 400 {
    84  		err = fmt.Errorf(resp.Error.Message)
    85  		return nil, 0, err
    86  	}
    87  	total := resp.Result.TotalCount
    88  	ret := []SDBInstanceAccount{}
    89  	for i := range resp.Result.Accounts {
    90  		ret = append(ret, SDBInstanceAccount{
    91  			Account: resp.Result.Accounts[i],
    92  		})
    93  	}
    94  	return ret, total, nil
    95  }
    96  
    97  func (self *SDBInstance) GetIDBInstanceAccounts() ([]cloudprovider.ICloudDBInstanceAccount, error) {
    98  	accounts := []SDBInstanceAccount{}
    99  	n := 1
   100  	for {
   101  		part, total, err := self.region.GetDBInstanceAccounts(self.InstanceId, n, 100)
   102  		if err != nil {
   103  			return nil, errors.Wrapf(err, "GetDBInstanceAccounts")
   104  		}
   105  		accounts = append(accounts, part...)
   106  		if len(accounts) >= total {
   107  			break
   108  		}
   109  		n++
   110  	}
   111  	ret := []cloudprovider.ICloudDBInstanceAccount{}
   112  	for i := range accounts {
   113  		accounts[i].rds = self
   114  		ret = append(ret, &accounts[i])
   115  	}
   116  	return ret, nil
   117  }