yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/qcloud/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 qcloud 16 17 import ( 18 "fmt" 19 "strings" 20 21 "github.com/pkg/errors" 22 23 "yunion.io/x/jsonutils" 24 25 api "yunion.io/x/cloudmux/pkg/apis/compute" 26 "yunion.io/x/cloudmux/pkg/cloudprovider" 27 "yunion.io/x/cloudmux/pkg/multicloud" 28 ) 29 30 type SElasticcacheAccount struct { 31 multicloud.SElasticcacheAccountBase 32 QcloudTags 33 34 cacheDB *SElasticcache 35 36 InstanceID string `json:"InstanceId"` 37 AccountName string `json:"AccountName"` 38 Remark string `json:"Remark"` 39 Privilege string `json:"Privilege"` 40 ReadonlyPolicy []string `json:"ReadonlyPolicy"` 41 Status int `json:"Status"` 42 IsEmulate bool `json:"is_emulate"` 43 } 44 45 func (self *SElasticcacheAccount) GetId() string { 46 return fmt.Sprintf("%s/%s", self.InstanceID, self.AccountName) 47 } 48 49 func (self *SElasticcacheAccount) GetName() string { 50 return self.AccountName 51 } 52 53 func (self *SElasticcacheAccount) IsEmulated() bool { 54 return self.IsEmulate 55 } 56 57 func (self *SElasticcacheAccount) GetGlobalId() string { 58 return self.GetId() 59 } 60 61 func (self *SElasticcacheAccount) GetStatus() string { 62 switch self.Status { 63 case 1: 64 return api.ELASTIC_CACHE_ACCOUNT_STATUS_CREATING 65 case 2: 66 return api.ELASTIC_CACHE_ACCOUNT_STATUS_AVAILABLE 67 case 4: 68 return api.ELASTIC_CACHE_ACCOUNT_STATUS_DELETED 69 default: 70 return api.ELASTIC_CACHE_ACCOUNT_STATUS_UNAVAILABLE 71 } 72 } 73 74 func (self *SElasticcacheAccount) GetAccountType() string { 75 if strings.ToLower(self.AccountName) == "root" { 76 return api.ELASTIC_CACHE_ACCOUNT_TYPE_ADMIN 77 } else { 78 return api.ELASTIC_CACHE_ACCOUNT_TYPE_NORMAL 79 } 80 } 81 82 func (self *SElasticcacheAccount) GetAccountPrivilege() string { 83 switch self.Privilege { 84 case "r": 85 return api.ELASTIC_CACHE_ACCOUNT_PRIVILEGE_READ 86 case "w": 87 return api.ELASTIC_CACHE_ACCOUNT_PRIVILEGE_WRITE 88 case "rw": 89 return api.ELASTIC_CACHE_ACCOUNT_PRIVILEGE_WRITE 90 default: 91 return self.Privilege 92 } 93 } 94 95 func (self *SElasticcacheAccount) Refresh() error { 96 account, err := self.cacheDB.GetICloudElasticcacheAccount(self.GetGlobalId()) 97 if err != nil { 98 return errors.Wrap(err, "GetICloudElasticcacheAccount") 99 } 100 101 err = jsonutils.Update(self, account) 102 if err != nil { 103 return errors.Wrap(err, "Update") 104 } 105 106 return nil 107 } 108 109 // https://cloud.tencent.com/document/product/239/38925 110 func (self *SElasticcacheAccount) Delete() error { 111 params := map[string]string{} 112 params["InstanceId"] = self.cacheDB.GetId() 113 params["AccountName"] = self.AccountName 114 _, err := self.cacheDB.region.redisRequest("DeleteInstanceAccount", params) 115 if err != nil { 116 return errors.Wrap(err, "DeleteInstanceAccount") 117 } 118 119 return nil 120 } 121 122 // https://cloud.tencent.com/document/product/239/20014 123 func (self *SElasticcacheAccount) ResetPassword(input cloudprovider.SCloudElasticCacheAccountResetPasswordInput) error { 124 params := map[string]string{} 125 params["InstanceId"] = self.cacheDB.GetId() 126 if input.NoPasswordAccess != nil && *input.NoPasswordAccess { 127 params["NoAuth"] = "true" 128 } else { 129 if len(input.NewPassword) > 0 { 130 params["Password"] = input.NewPassword 131 } else { 132 return nil 133 } 134 } 135 136 _, err := self.cacheDB.region.redisRequest("ResetPassword", params) 137 if err != nil { 138 return errors.Wrap(err, "ResetPassword") 139 } 140 141 return nil 142 } 143 144 // https://cloud.tencent.com/document/product/239/38923 145 func (self *SElasticcacheAccount) UpdateAccount(input cloudprovider.SCloudElasticCacheAccountUpdateInput) error { 146 params := map[string]string{} 147 params["InstanceId"] = self.cacheDB.GetId() 148 params["AccountName"] = self.AccountName 149 if input.NoPasswordAccess != nil && *input.NoPasswordAccess { 150 params["NoAuth"] = "true" 151 } else { 152 if input.Password != nil && len(*input.Password) > 0 { 153 params["AccountPassword"] = *input.Password 154 } 155 } 156 157 if input.Description != nil && len(*input.Description) > 0 { 158 params["Remark"] = *input.Description 159 } 160 161 if input.AccountPrivilege != nil && len(*input.AccountPrivilege) > 0 { 162 params["Privilege"] = *input.AccountPrivilege 163 params["ReadonlyPolicy.0"] = "master" 164 } 165 166 _, err := self.cacheDB.region.redisRequest("ModifyInstanceAccount", params) 167 if err != nil { 168 return errors.Wrap(err, "ModifyInstanceAccount") 169 } 170 171 return nil 172 }