yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/google/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 google
    16  
    17  import (
    18  	"fmt"
    19  	"net/url"
    20  
    21  	"yunion.io/x/jsonutils"
    22  	"yunion.io/x/pkg/errors"
    23  
    24  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    25  	"yunion.io/x/cloudmux/pkg/multicloud"
    26  )
    27  
    28  type SSqlserverUserDetails struct {
    29  	ServerRoles []string
    30  }
    31  
    32  type SDBInstanceAccount struct {
    33  	multicloud.SDBInstanceAccountBase
    34  	GoogleTags
    35  	rds *SDBInstance
    36  
    37  	Kind                 string
    38  	Etag                 string
    39  	Name                 string
    40  	Host                 string
    41  	Instance             string
    42  	SelfLink             string
    43  	Project              string
    44  	SqlserverUserDetails SSqlserverUserDetails
    45  }
    46  
    47  func (region *SRegion) GetDBInstanceAccounts(instance string) ([]SDBInstanceAccount, error) {
    48  	accounts := []SDBInstanceAccount{}
    49  	params := map[string]string{}
    50  	resource := fmt.Sprintf("instances/%s/users", instance)
    51  	err := region.RdsListAll(resource, params, &accounts)
    52  	if err != nil {
    53  		return nil, errors.Wrap(err, "RdsListAll")
    54  	}
    55  	return accounts, nil
    56  }
    57  
    58  func (region *SRegion) GetDBInstanceAccount(id string) (*SDBInstanceAccount, error) {
    59  	account := &SDBInstanceAccount{}
    60  	err := region.rdsGet(id, account)
    61  	if err != nil {
    62  		return nil, errors.Wrap(err, "rdsGet")
    63  	}
    64  	return account, nil
    65  }
    66  
    67  func (region *SRegion) DeleteDBInstanceAccount(instanceName, user, host string) error {
    68  	resource := fmt.Sprintf("projects/%s/instances/%s/users?name=%s&host=%s", region.GetProjectId(), instanceName, url.PathEscape(user), url.PathEscape(host))
    69  	return region.rdsDelete(resource)
    70  }
    71  
    72  func (account *SDBInstanceAccount) Delete() error {
    73  	return account.rds.region.DeleteDBInstanceAccount(account.rds.Name, account.Name, account.Host)
    74  }
    75  
    76  func (account *SDBInstanceAccount) GetHost() string {
    77  	return account.Host
    78  }
    79  
    80  func (account *SDBInstanceAccount) GetName() string {
    81  	return account.Name
    82  }
    83  
    84  func (account *SDBInstanceAccount) Refresh() error {
    85  	_account, err := account.rds.region.GetDBInstanceAccount(account.SelfLink)
    86  	if err != nil {
    87  		return errors.Wrap(err, "GetDBInstanceAccount")
    88  	}
    89  	return jsonutils.Update(account, _account)
    90  }
    91  
    92  func (account *SDBInstanceAccount) GetIDBInstanceAccountPrivileges() ([]cloudprovider.ICloudDBInstanceAccountPrivilege, error) {
    93  	return []cloudprovider.ICloudDBInstanceAccountPrivilege{}, nil
    94  }
    95  
    96  func (account *SDBInstanceAccount) ResetPassword(password string) error {
    97  	params := map[string]string{
    98  		"host": account.Host,
    99  		"name": account.Name,
   100  	}
   101  	resource := fmt.Sprintf("instances/%s/users", account.rds.Name)
   102  	body := map[string]string{
   103  		"password": password,
   104  	}
   105  	return account.rds.region.rdsUpdate(resource, params, jsonutils.Marshal(body))
   106  }
   107  
   108  func (account *SDBInstanceAccount) GrantPrivilege(database, privilege string) error {
   109  	return cloudprovider.ErrNotSupported
   110  }
   111  
   112  func (account *SDBInstanceAccount) RevokePrivilege(database string) error {
   113  	return cloudprovider.ErrNotSupported
   114  }
   115  
   116  func (region *SRegion) CreateDBInstanceAccount(instanceId string, name, password, host string) error {
   117  	body := map[string]interface{}{
   118  		"name":     name,
   119  		"password": password,
   120  	}
   121  	if len(host) > 0 {
   122  		body["host"] = host
   123  	}
   124  	return region.rdsDo(instanceId, "users", nil, jsonutils.Marshal(body))
   125  }