yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/huawei/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 huawei
    16  
    17  import (
    18  	"fmt"
    19  
    20  	"yunion.io/x/jsonutils"
    21  	"yunion.io/x/pkg/errors"
    22  
    23  	api "yunion.io/x/cloudmux/pkg/apis/compute"
    24  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    25  	"yunion.io/x/cloudmux/pkg/multicloud"
    26  )
    27  
    28  type SElasticcacheAccount struct {
    29  	multicloud.SElasticcacheAccountBase
    30  	HuaweiTags
    31  
    32  	cacheDB *SElasticcache
    33  }
    34  
    35  func (self *SElasticcacheAccount) GetId() string {
    36  	return fmt.Sprintf("%s/root", self.cacheDB.InstanceID)
    37  }
    38  
    39  func (self *SElasticcacheAccount) GetName() string {
    40  	if len(self.cacheDB.AccessUser) > 0 {
    41  		return self.cacheDB.AccessUser
    42  	}
    43  
    44  	return "root"
    45  }
    46  
    47  func (self *SElasticcacheAccount) GetGlobalId() string {
    48  	return self.GetId()
    49  }
    50  
    51  func (self *SElasticcacheAccount) GetStatus() string {
    52  	return api.ELASTIC_CACHE_ACCOUNT_STATUS_AVAILABLE
    53  }
    54  
    55  func (self *SElasticcacheAccount) GetAccountType() string {
    56  	return api.ELASTIC_CACHE_ACCOUNT_TYPE_ADMIN
    57  }
    58  
    59  func (self *SElasticcacheAccount) GetAccountPrivilege() string {
    60  	return api.ELASTIC_CACHE_ACCOUNT_PRIVILEGE_WRITE
    61  }
    62  
    63  // https://support.huaweicloud.com/api-dcs/dcs-zh-api-180423031.html
    64  // 未找到关闭密码的开放api, 不支持开启/关闭密码访问
    65  // https://console.huaweicloud.com/dcs/rest/v2/41f6bfe48d7f4455b7754f7c1b11ae34/instances/26db46e2-c7d8-4b5e-bd36-b5278d2fe17c/password/reset
    66  // new_password: "26db46e2!"
    67  // no_password_access: false
    68  func (self *SElasticcacheAccount) ResetPassword(input cloudprovider.SCloudElasticCacheAccountResetPasswordInput) error {
    69  	if input.OldPassword == nil {
    70  		return fmt.Errorf("elasticcacheAccount.ResetPassword.input OldPassword should not be empty")
    71  	}
    72  
    73  	type ResetPasswordResult struct {
    74  		Result  string `json:"result"`
    75  		Message string `json:"message"`
    76  	}
    77  
    78  	result := ResetPasswordResult{}
    79  	params := jsonutils.NewDict()
    80  	params.Set("old_password", jsonutils.NewString(*input.OldPassword))
    81  	params.Set("new_password", jsonutils.NewString(input.NewPassword))
    82  	err := DoUpdateWithSpec2(self.cacheDB.region.ecsClient.Elasticcache.UpdateInContextWithSpec, self.cacheDB.GetId(), "password", params, &result)
    83  	if err != nil {
    84  		return errors.Wrap(err, "elasticcacheAccount.ResetPassword")
    85  	}
    86  
    87  	if result.Result != "success" {
    88  		return errors.Wrap(fmt.Errorf(result.Message), "elasticcacheAccount.ResetPassword")
    89  	}
    90  
    91  	return nil
    92  }
    93  
    94  func (self *SElasticcacheAccount) UpdateAccount(input cloudprovider.SCloudElasticCacheAccountUpdateInput) error {
    95  	if input.Password != nil {
    96  		inputPassword := cloudprovider.SCloudElasticCacheAccountResetPasswordInput{}
    97  		inputPassword.NewPassword = *input.Password
    98  		inputPassword.OldPassword = input.OldPassword
    99  		inputPassword.NoPasswordAccess = input.NoPasswordAccess
   100  		return self.ResetPassword(inputPassword)
   101  	}
   102  
   103  	return nil
   104  }
   105  
   106  func (self *SElasticcacheAccount) Delete() error {
   107  	return cloudprovider.ErrNotSupported
   108  }