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