yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/aliyun/elasticcache_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 aliyun
    16  
    17  import (
    18  	"fmt"
    19  	"strings"
    20  
    21  	"yunion.io/x/jsonutils"
    22  	"yunion.io/x/pkg/errors"
    23  
    24  	api "yunion.io/x/cloudmux/pkg/apis/compute"
    25  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    26  	"yunion.io/x/cloudmux/pkg/multicloud"
    27  )
    28  
    29  // https://help.aliyun.com/document_detail/95802.html?spm=a2c4g.11186623.6.746.1d4b302ayCuzXB
    30  type SElasticcacheAccount struct {
    31  	multicloud.SElasticcacheAccountBase
    32  	AliyunTags
    33  
    34  	cacheDB *SElasticcache
    35  
    36  	AccountStatus      string             `json:"AccountStatus"`
    37  	DatabasePrivileges DatabasePrivileges `json:"DatabasePrivileges"`
    38  	InstanceID         string             `json:"InstanceId"`
    39  	AccountName        string             `json:"AccountName"`
    40  	PrivExceeded       string             `json:"PrivExceeded"`
    41  	AccountType        string             `json:"AccountType"`
    42  }
    43  
    44  type DatabasePrivileges struct {
    45  	DatabasePrivilege []DatabasePrivilege `json:"DatabasePrivilege"`
    46  }
    47  
    48  type DatabasePrivilege struct {
    49  	AccountPrivilege string `json:"AccountPrivilege"`
    50  }
    51  
    52  func (self *SElasticcacheAccount) GetId() string {
    53  	return fmt.Sprintf("%s/%s", self.InstanceID, self.AccountName)
    54  }
    55  
    56  func (self *SElasticcacheAccount) GetName() string {
    57  	return self.AccountName
    58  }
    59  
    60  func (self *SElasticcacheAccount) GetGlobalId() string {
    61  	return self.GetId()
    62  }
    63  
    64  func (self *SElasticcacheAccount) GetStatus() string {
    65  	switch strings.ToLower(self.AccountStatus) {
    66  	case "unavailable":
    67  		return api.ELASTIC_CACHE_ACCOUNT_STATUS_UNAVAILABLE
    68  	case "available":
    69  		return api.ELASTIC_CACHE_ACCOUNT_STATUS_AVAILABLE
    70  	default:
    71  		return self.AccountStatus
    72  	}
    73  }
    74  
    75  func (self *SElasticcacheAccount) Refresh() error {
    76  	iaccount, err := self.cacheDB.GetICloudElasticcacheAccountByName(self.GetName())
    77  	if err != nil {
    78  		return err
    79  	}
    80  
    81  	err = jsonutils.Update(self, iaccount.(*SElasticcacheAccount))
    82  	if err != nil {
    83  		return err
    84  	}
    85  
    86  	return nil
    87  }
    88  
    89  func (self *SElasticcacheAccount) GetAccountType() string {
    90  	if self.AccountName == self.cacheDB.InstanceID {
    91  		return api.ELASTIC_CACHE_ACCOUNT_TYPE_ADMIN
    92  	}
    93  
    94  	switch self.AccountType {
    95  	case "Normal":
    96  		return api.ELASTIC_CACHE_ACCOUNT_TYPE_NORMAL
    97  	case "Super":
    98  		return api.ELASTIC_CACHE_ACCOUNT_TYPE_ADMIN
    99  	default:
   100  		return self.AccountType
   101  	}
   102  }
   103  
   104  func (self *SElasticcacheAccount) GetAccountPrivilege() string {
   105  	if len(self.DatabasePrivileges.DatabasePrivilege) == 0 {
   106  		return ""
   107  	}
   108  
   109  	privilege := self.DatabasePrivileges.DatabasePrivilege[0].AccountPrivilege
   110  	switch privilege {
   111  	case "RoleReadOnly":
   112  		return api.ELASTIC_CACHE_ACCOUNT_PRIVILEGE_READ
   113  	case "RoleReadWrite":
   114  		return api.ELASTIC_CACHE_ACCOUNT_PRIVILEGE_WRITE
   115  	case "RoleRepl":
   116  		return api.ELASTIC_CACHE_ACCOUNT_PRIVILEGE_REPL
   117  	default:
   118  		return privilege
   119  	}
   120  }
   121  
   122  // https://help.aliyun.com/document_detail/95941.html?spm=a2c4g.11186623.6.746.523555d8D8Whxq
   123  // https://help.aliyun.com/document_detail/98531.html?spm=5176.11065259.1996646101.searchclickresult.4df474c38Sc2SO
   124  func (self *SElasticcacheAccount) ResetPassword(input cloudprovider.SCloudElasticCacheAccountResetPasswordInput) error {
   125  	params := make(map[string]string)
   126  	params["InstanceId"] = self.cacheDB.GetId()
   127  	params["AccountName"] = self.GetName()
   128  	params["AccountPassword"] = input.NewPassword
   129  
   130  	err := DoAction(self.cacheDB.region.kvsRequest, "ResetAccountPassword", params, nil, nil)
   131  	if err != nil {
   132  		return errors.Wrap(err, "elasticcacheAccount.ResetPassword")
   133  	}
   134  
   135  	if input.NoPasswordAccess != nil {
   136  		return cloudprovider.ErrNotSupported
   137  	}
   138  
   139  	return nil
   140  }
   141  
   142  func (self *SElasticcacheAccount) UpdateAccount(input cloudprovider.SCloudElasticCacheAccountUpdateInput) error {
   143  	if input.Password != nil {
   144  		inputPassword := cloudprovider.SCloudElasticCacheAccountResetPasswordInput{}
   145  		inputPassword.NewPassword = *input.Password
   146  		inputPassword.NoPasswordAccess = input.NoPasswordAccess
   147  		err := self.ResetPassword(inputPassword)
   148  		if err != nil {
   149  			return err
   150  		}
   151  	}
   152  
   153  	if input.Description != nil {
   154  		err := self.ModifyAccountDescription(*input.Description)
   155  		if err != nil {
   156  			return err
   157  		}
   158  	}
   159  
   160  	if input.AccountPrivilege != nil {
   161  		err := self.GrantAccountPrivilege(*input.AccountPrivilege)
   162  		if err != nil {
   163  			return err
   164  		}
   165  	}
   166  
   167  	return nil
   168  }
   169  
   170  // https://help.aliyun.com/document_detail/96020.html?spm=a2c4g.11186623.6.747.5c6f1c717weYEX
   171  func (self *SElasticcacheAccount) ModifyAccountDescription(desc string) error {
   172  	params := make(map[string]string)
   173  	params["InstanceId"] = self.cacheDB.GetId()
   174  	params["AccountName"] = self.GetName()
   175  	params["AccountDescription"] = desc
   176  
   177  	err := DoAction(self.cacheDB.region.kvsRequest, "ModifyAccountDescription", params, nil, nil)
   178  	if err != nil {
   179  		return errors.Wrap(err, "elasticcacheAccount.ModifyAccountDescription")
   180  	}
   181  
   182  	return nil
   183  }
   184  
   185  // https://help.aliyun.com/document_detail/95897.html?spm=a2c4g.11186623.6.745.28576cf9IqM44R
   186  func (self *SElasticcacheAccount) GrantAccountPrivilege(privilege string) error {
   187  	params := make(map[string]string)
   188  	params["InstanceId"] = self.cacheDB.GetId()
   189  	params["AccountName"] = self.GetName()
   190  	params["AccountPrivilege"] = privilege
   191  
   192  	err := DoAction(self.cacheDB.region.kvsRequest, "GrantAccountPrivilege", params, nil, nil)
   193  	if err != nil {
   194  		return errors.Wrap(err, "elasticcacheAccount.GrantAccountPrivilege")
   195  	}
   196  
   197  	return nil
   198  }
   199  
   200  // https://help.aliyun.com/document_detail/95988.html?spm=a2c4g.11186623.6.743.cb291c71D1Hlmu
   201  func (self *SElasticcacheAccount) Delete() error {
   202  	params := make(map[string]string)
   203  	params["InstanceId"] = self.cacheDB.GetId()
   204  	params["AccountName"] = self.GetName()
   205  
   206  	err := DoAction(self.cacheDB.region.kvsRequest, "DeleteAccount", params, nil, nil)
   207  	if err != nil {
   208  		return errors.Wrap(err, "elasticcacheAccount.Delete")
   209  	}
   210  
   211  	return nil
   212  }