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