/
resource_alicloud_adb_account.go
252 lines (216 loc) · 7.59 KB
/
resource_alicloud_adb_account.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
package alicloud
import (
"fmt"
"strings"
"time"
"github.com/aliyun/alibaba-cloud-sdk-go/services/adb"
"github.com/aliyun/terraform-provider-alicloud/alicloud/connectivity"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
)
func resourceAlicloudAdbAccount() *schema.Resource {
return &schema.Resource{
Create: resourceAlicloudAdbAccountCreate,
Read: resourceAlicloudAdbAccountRead,
Update: resourceAlicloudAdbAccountUpdate,
Delete: resourceAlicloudAdbAccountDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"db_cluster_id": {
Type: schema.TypeString,
ForceNew: true,
Required: true,
},
"account_name": {
Type: schema.TypeString,
ForceNew: true,
Required: true,
},
"account_password": {
Type: schema.TypeString,
Optional: true,
Sensitive: true,
},
"kms_encrypted_password": {
Type: schema.TypeString,
Optional: true,
DiffSuppressFunc: kmsDiffSuppressFunc,
},
"kms_encryption_context": {
Type: schema.TypeMap,
Optional: true,
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
return d.Get("kms_encrypted_password").(string) == ""
},
Elem: schema.TypeString,
},
"account_type": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{string("Super")}, false),
Default: "Super",
ForceNew: true,
Removed: "Field 'account_type' has been removed from provider version 1.81.0.",
},
"account_description": {
Type: schema.TypeString,
Optional: true,
},
},
}
}
func resourceAlicloudAdbAccountCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
adbService := AdbService{client}
request := adb.CreateCreateAccountRequest()
request.RegionId = client.RegionId
request.DBClusterId = d.Get("db_cluster_id").(string)
request.AccountName = d.Get("account_name").(string)
password := d.Get("account_password").(string)
kmsPassword := d.Get("kms_encrypted_password").(string)
if password == "" && kmsPassword == "" {
return WrapError(Error("One of the 'password' and 'kms_encrypted_password' should be set."))
}
if password != "" {
request.AccountPassword = password
} else {
kmsService := KmsService{client}
decryptResp, err := kmsService.Decrypt(kmsPassword, d.Get("kms_encryption_context").(map[string]interface{}))
if err != nil {
return WrapError(err)
}
request.AccountPassword = decryptResp
}
// Description will not be set when account type is normal and it is a API bug
if v, ok := d.GetOk("account_description"); ok && v.(string) != "" {
request.AccountDescription = v.(string)
}
err := resource.Retry(5*time.Minute, func() *resource.RetryError {
raw, err := client.WithAdbClient(func(adbClient *adb.Client) (interface{}, error) {
return adbClient.CreateAccount(request)
})
if err != nil {
if IsExpectedErrors(err, OperationDeniedDBStatus) {
time.Sleep(5 * time.Second)
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
}
addDebug(request.GetActionName(), raw, request.RpcRequest, request)
return nil
})
if err != nil {
return WrapErrorf(err, DefaultErrorMsg, "alicloud_adb_account", request.GetActionName(), AlibabaCloudSdkGoERROR)
}
d.SetId(fmt.Sprintf("%s%s%s", request.DBClusterId, COLON_SEPARATED, request.AccountName))
if err := adbService.WaitForAdbAccount(d.Id(), Available, DefaultTimeoutMedium); err != nil {
return WrapError(err)
}
return resourceAlicloudAdbAccountRead(d, meta)
}
func resourceAlicloudAdbAccountRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
adbService := AdbService{client}
object, err := adbService.DescribeAdbAccount(d.Id())
if err != nil {
if NotFoundError(err) {
d.SetId("")
return nil
}
return WrapError(err)
}
parts, err := ParseResourceId(d.Id(), 2)
if err != nil {
return WrapError(err)
}
d.Set("db_cluster_id", parts[0])
d.Set("account_name", object.AccountName)
d.Set("account_description", object.AccountDescription)
return nil
}
func resourceAlicloudAdbAccountUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
adbService := AdbService{client}
d.Partial(true)
parts := strings.Split(d.Id(), COLON_SEPARATED)
instanceId := parts[0]
accountName := parts[1]
if d.HasChange("account_description") {
if err := adbService.WaitForAdbAccount(d.Id(), Available, DefaultTimeoutMedium); err != nil {
return WrapError(err)
}
request := adb.CreateModifyAccountDescriptionRequest()
request.RegionId = client.RegionId
request.DBClusterId = instanceId
request.AccountName = accountName
request.AccountDescription = d.Get("account_description").(string)
raw, err := client.WithAdbClient(func(adbClient *adb.Client) (interface{}, error) {
return adbClient.ModifyAccountDescription(request)
})
if err != nil {
return WrapErrorf(err, DefaultErrorMsg, d.Id(), request.GetActionName(), AlibabaCloudSdkGoERROR)
}
addDebug(request.GetActionName(), raw, request.RpcRequest, request)
d.SetPartial("account_description")
}
if d.HasChange("account_password") || d.HasChange("kms_encrypted_password") {
if err := adbService.WaitForAdbAccount(d.Id(), Available, DefaultTimeoutMedium); err != nil {
return WrapError(err)
}
request := adb.CreateResetAccountPasswordRequest()
request.RegionId = client.RegionId
request.DBClusterId = instanceId
request.AccountName = accountName
password := d.Get("account_password").(string)
kmsPassword := d.Get("kms_encrypted_password").(string)
if password == "" && kmsPassword == "" {
return WrapError(Error("One of the 'password' and 'kms_encrypted_password' should be set."))
}
if password != "" {
request.AccountPassword = password
} else {
kmsService := KmsService{meta.(*connectivity.AliyunClient)}
decryptResp, err := kmsService.Decrypt(kmsPassword, d.Get("kms_encryption_context").(map[string]interface{}))
if err != nil {
return WrapError(err)
}
request.AccountPassword = decryptResp
}
raw, err := client.WithAdbClient(func(adbClient *adb.Client) (interface{}, error) {
return adbClient.ResetAccountPassword(request)
})
if err != nil {
return WrapErrorf(err, DefaultErrorMsg, d.Id(), request.GetActionName(), AlibabaCloudSdkGoERROR)
}
addDebug(request.GetActionName(), raw, request.RpcRequest, request)
d.SetPartial("account_password")
}
d.Partial(false)
return resourceAlicloudAdbAccountRead(d, meta)
}
func resourceAlicloudAdbAccountDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
adbService := AdbService{client}
parts, err := ParseResourceId(d.Id(), 2)
if err != nil {
return WrapError(err)
}
request := adb.CreateDeleteAccountRequest()
request.RegionId = client.RegionId
request.DBClusterId = parts[0]
request.AccountName = parts[1]
raw, err := client.WithAdbClient(func(adbClient *adb.Client) (interface{}, error) {
return adbClient.DeleteAccount(request)
})
if err != nil {
if IsExpectedErrors(err, []string{"InvalidAccountName.NotFound"}) {
return nil
}
return WrapErrorf(err, DefaultErrorMsg, d.Id(), request.GetActionName(), AlibabaCloudSdkGoERROR)
}
addDebug(request.GetActionName(), raw, request.RpcRequest, request)
return adbService.WaitForAdbAccount(d.Id(), Deleted, DefaultTimeoutMedium)
}