yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/qcloud/tdsql_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 qcloud
    16  
    17  import (
    18  	"yunion.io/x/pkg/errors"
    19  
    20  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    21  	"yunion.io/x/cloudmux/pkg/multicloud"
    22  )
    23  
    24  type STDSQLAccount struct {
    25  	multicloud.SDBInstanceAccountBase
    26  	QcloudTags
    27  	rds *STDSQL
    28  
    29  	UserName    string
    30  	Host        string
    31  	Description string
    32  	CreateTime  string
    33  	UpdateTime  string
    34  	ReadOnly    int
    35  	DelayThresh int
    36  }
    37  
    38  func (self *STDSQLAccount) GetName() string {
    39  	return self.UserName
    40  }
    41  
    42  func (self *STDSQLAccount) GetHost() string {
    43  	return self.Host
    44  }
    45  
    46  func (self *STDSQLAccount) Delete() error {
    47  	return self.rds.region.DeleteTDSQLAccount(self.rds.InstanceId, self.UserName, self.Host)
    48  }
    49  
    50  func (self *STDSQLAccount) GrantPrivilege(database, privilege string) error {
    51  	return cloudprovider.ErrNotImplemented
    52  }
    53  
    54  func (self *STDSQLAccount) RevokePrivilege(database string) error {
    55  	return cloudprovider.ErrNotImplemented
    56  }
    57  
    58  func (self *STDSQLAccount) GetIDBInstanceAccountPrivileges() ([]cloudprovider.ICloudDBInstanceAccountPrivilege, error) {
    59  	return nil, cloudprovider.ErrNotImplemented
    60  }
    61  
    62  func (self *SRegion) GetTDSQLAccount(id string) ([]STDSQLAccount, error) {
    63  	params := map[string]string{
    64  		"InstanceId": id,
    65  	}
    66  	resp, err := self.dcdbRequest("DescribeAccounts", params)
    67  	if err != nil {
    68  		return nil, errors.Wrapf(err, "DescribeAccounts")
    69  	}
    70  	ret := []STDSQLAccount{}
    71  	err = resp.Unmarshal(&ret, "Users")
    72  	if err != nil {
    73  		return nil, errors.Wrapf(err, "resp.Unmarshal")
    74  	}
    75  	return ret, nil
    76  }
    77  
    78  func (self *STDSQL) GetIDBInstanceAccounts() ([]cloudprovider.ICloudDBInstanceAccount, error) {
    79  	accounts, err := self.region.GetTDSQLAccount(self.InstanceId)
    80  	if err != nil {
    81  		return nil, errors.Wrapf(err, "GetTDSQLAccount")
    82  	}
    83  	ret := []cloudprovider.ICloudDBInstanceAccount{}
    84  	for i := range accounts {
    85  		accounts[i].rds = self
    86  		ret = append(ret, &accounts[i])
    87  	}
    88  	return ret, nil
    89  }
    90  
    91  func (self *SRegion) DeleteTDSQLAccount(id, name, host string) error {
    92  	params := map[string]string{
    93  		"InstanceId": id,
    94  		"UserName":   name,
    95  		"Host":       host,
    96  	}
    97  	_, err := self.dcdbRequest("DeleteAccount", params)
    98  	return errors.Wrapf(err, "DeleteAccount")
    99  }