yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/huawei/dbinstance_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 huawei
    16  
    17  import (
    18  	"fmt"
    19  
    20  	"yunion.io/x/jsonutils"
    21  
    22  	api "yunion.io/x/cloudmux/pkg/apis/compute"
    23  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    24  	"yunion.io/x/cloudmux/pkg/multicloud"
    25  )
    26  
    27  type SDBInstanceAccount struct {
    28  	multicloud.SDBInstanceAccountBase
    29  	HuaweiTags
    30  	instance *SDBInstance
    31  	Name     string
    32  }
    33  
    34  func (account *SDBInstanceAccount) GetName() string {
    35  	return account.Name
    36  }
    37  
    38  func (account *SDBInstanceAccount) Delete() error {
    39  	return account.instance.region.DeleteDBInstanceAccount(account.instance.Id, account.Name)
    40  }
    41  
    42  func (region *SRegion) DeleteDBInstanceAccount(instanceId string, account string) error {
    43  	return DoDeleteWithSpec(region.ecsClient.DBInstance.DeleteInContextWithSpec, nil, instanceId, fmt.Sprintf("db_user/%s", account), nil, nil)
    44  }
    45  
    46  func (account *SDBInstanceAccount) GetIDBInstanceAccountPrivileges() ([]cloudprovider.ICloudDBInstanceAccountPrivilege, error) {
    47  	privileges, err := account.instance.region.GetDBInstancePrivvileges(account.instance.Id, account.Name)
    48  	if err != nil {
    49  		return nil, err
    50  	}
    51  	iprivileves := []cloudprovider.ICloudDBInstanceAccountPrivilege{}
    52  	for i := 0; i < len(privileges); i++ {
    53  		privileges[i].account = account
    54  		iprivileves = append(iprivileves, &privileges[i])
    55  	}
    56  	return iprivileves, nil
    57  }
    58  
    59  func (region *SRegion) GetDBInstanceAccounts(instanceId string) ([]SDBInstanceAccount, error) {
    60  	params := map[string]string{
    61  		"instance_id": instanceId,
    62  	}
    63  	accounts := []SDBInstanceAccount{}
    64  	err := doListAllWithPage(region.ecsClient.DBInstance.ListAccounts, params, &accounts)
    65  	if err != nil {
    66  		return nil, err
    67  	}
    68  	return accounts, nil
    69  }
    70  
    71  func (region *SRegion) GetDBInstancePrivvileges(instanceId string, username string) ([]SDatabasePrivilege, error) {
    72  	params := map[string]string{
    73  		"instance_id": instanceId,
    74  		"user-name":   username,
    75  	}
    76  	privileges := []SDatabasePrivilege{}
    77  	err := doListAllWithPage(region.ecsClient.DBInstance.ListPrivileges, params, &privileges)
    78  	if err != nil {
    79  		return nil, err
    80  	}
    81  	return privileges, nil
    82  }
    83  
    84  func (account *SDBInstanceAccount) RevokePrivilege(database string) error {
    85  	return account.instance.region.RevokeDBInstancePrivilege(account.instance.Id, account.Name, database)
    86  }
    87  
    88  func (region *SRegion) RevokeDBInstancePrivilege(instanceId string, account, database string) error {
    89  	params := map[string]interface{}{
    90  		"db_name": database,
    91  		"users": []map[string]interface{}{
    92  			map[string]interface{}{
    93  				"name": account,
    94  			},
    95  		},
    96  	}
    97  	return DoDeleteWithSpec(region.ecsClient.DBInstance.DeleteInContextWithSpec, nil, instanceId, "db_privilege", nil, jsonutils.Marshal(params))
    98  }
    99  
   100  func (account *SDBInstanceAccount) GrantPrivilege(database, privilege string) error {
   101  	return account.instance.region.GrantDBInstancePrivilege(account.instance.Id, account.Name, database, privilege)
   102  }
   103  
   104  func (region *SRegion) GrantDBInstancePrivilege(instanceId string, account, database string, privilege string) error {
   105  	readonly := false
   106  	switch privilege {
   107  	case api.DATABASE_PRIVILEGE_R:
   108  		readonly = true
   109  	case api.DATABASE_PRIVILEGE_RW:
   110  	default:
   111  		return fmt.Errorf("Unknown privilege %s", privilege)
   112  	}
   113  	params := map[string]interface{}{
   114  		"db_name": database,
   115  		"users": []map[string]interface{}{
   116  			map[string]interface{}{
   117  				"name":     account,
   118  				"readonly": readonly,
   119  			},
   120  		},
   121  	}
   122  	_, err := region.ecsClient.DBInstance.PerformAction("db_privilege", instanceId, jsonutils.Marshal(params))
   123  	return err
   124  }
   125  
   126  func (account *SDBInstanceAccount) ResetPassword(password string) error {
   127  	return account.instance.region.ResetDBInstanceAccountPassword(account.instance.Id, account.Name, password)
   128  }
   129  
   130  func (region *SRegion) ResetDBInstanceAccountPassword(instanceId, account, password string) error {
   131  	return fmt.Errorf("The API does not exist or has not been published in the environment")
   132  }