forked from aliyun/terraform-provider-alicloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_alicloud_db_account.go
193 lines (167 loc) · 5.75 KB
/
resource_alicloud_db_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
package alicloud
import (
"fmt"
"strings"
"time"
"github.com/aliyun/alibaba-cloud-sdk-go/services/rds"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/terraform-providers/terraform-provider-alicloud/alicloud/connectivity"
)
func resourceAlicloudDBAccount() *schema.Resource {
return &schema.Resource{
Create: resourceAlicloudDBAccountCreate,
Read: resourceAlicloudDBAccountRead,
Update: resourceAlicloudDBAccountUpdate,
Delete: resourceAlicloudDBAccountDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"instance_id": {
Type: schema.TypeString,
ForceNew: true,
Required: true,
},
"name": {
Type: schema.TypeString,
ForceNew: true,
Required: true,
},
"password": {
Type: schema.TypeString,
Required: true,
Sensitive: true,
},
"type": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validateAllowedStringValue([]string{string(DBAccountNormal), string(DBAccountSuper)}),
ForceNew: true,
Computed: true,
},
"description": {
Type: schema.TypeString,
Optional: true,
},
},
}
}
func resourceAlicloudDBAccountCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
rdsService := RdsService{client}
request := rds.CreateCreateAccountRequest()
request.DBInstanceId = d.Get("instance_id").(string)
request.AccountName = d.Get("name").(string)
request.AccountPassword = d.Get("password").(string)
request.AccountType = d.Get("type").(string)
// Description will not be set when account type is normal and it is a API bug
if v, ok := d.GetOk("description"); ok && v.(string) != "" {
request.AccountDescription = v.(string)
}
// wait instance running before modifying
if err := rdsService.WaitForDBInstance(request.DBInstanceId, Running, DefaultTimeoutMedium); err != nil {
return WrapError(err)
}
err := resource.Retry(5*time.Minute, func() *resource.RetryError {
raw, err := client.WithRdsClient(func(rdsClient *rds.Client) (interface{}, error) {
return rdsClient.CreateAccount(request)
})
if err != nil {
if IsExceptedErrors(err, OperationDeniedDBStatus) {
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
}
addDebug(request.GetActionName(), raw)
return nil
})
if err != nil {
return WrapErrorf(err, DefaultErrorMsg, "alicloud_db_account", request.GetActionName(), AlibabaCloudSdkGoERROR)
}
d.SetId(fmt.Sprintf("%s%s%s", request.DBInstanceId, COLON_SEPARATED, request.AccountName))
if err := rdsService.WaitForAccount(d.Id(), Available, DefaultTimeoutMedium); err != nil {
return WrapError(err)
}
return resourceAlicloudDBAccountRead(d, meta)
}
func resourceAlicloudDBAccountRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
rdsService := RdsService{client}
object, err := rdsService.DescribeDBAccount(d.Id())
if err != nil {
if rdsService.NotFoundDBInstance(err) {
d.SetId("")
return nil
}
return WrapError(err)
}
d.Set("instance_id", object.DBInstanceId)
d.Set("name", object.AccountName)
d.Set("type", object.AccountType)
d.Set("description", object.AccountDescription)
return nil
}
func resourceAlicloudDBAccountUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
rdsService := RdsService{client}
d.Partial(true)
parts := strings.Split(d.Id(), COLON_SEPARATED)
instanceId := parts[0]
accountName := parts[1]
if d.HasChange("description") {
if err := rdsService.WaitForAccount(d.Id(), Available, DefaultTimeoutMedium); err != nil {
return WrapError(err)
}
request := rds.CreateModifyAccountDescriptionRequest()
request.DBInstanceId = instanceId
request.AccountName = accountName
request.AccountDescription = d.Get("description").(string)
raw, err := client.WithRdsClient(func(rdsClient *rds.Client) (interface{}, error) {
return rdsClient.ModifyAccountDescription(request)
})
if err != nil {
return WrapErrorf(err, DefaultErrorMsg, d.Id(), request.GetActionName(), AlibabaCloudSdkGoERROR)
}
addDebug(request.GetActionName(), raw)
d.SetPartial("description")
}
if d.HasChange("password") {
if err := rdsService.WaitForAccount(d.Id(), Available, DefaultTimeoutMedium); err != nil {
return WrapError(err)
}
request := rds.CreateResetAccountPasswordRequest()
request.DBInstanceId = instanceId
request.AccountName = accountName
request.AccountPassword = d.Get("password").(string)
raw, err := client.WithRdsClient(func(rdsClient *rds.Client) (interface{}, error) {
return rdsClient.ResetAccountPassword(request)
})
if err != nil {
return WrapErrorf(err, DefaultErrorMsg, d.Id(), request.GetActionName(), AlibabaCloudSdkGoERROR)
}
addDebug(request.GetActionName(), raw)
d.SetPartial("password")
}
d.Partial(false)
return resourceAlicloudDBAccountRead(d, meta)
}
func resourceAlicloudDBAccountDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
rdsService := RdsService{client}
parts, err := ParseResourceId(d.Id(), 2)
if err != nil {
return WrapError(err)
}
request := rds.CreateDeleteAccountRequest()
request.DBInstanceId = parts[0]
request.AccountName = parts[1]
raw, err := client.WithRdsClient(func(rdsClient *rds.Client) (interface{}, error) {
return rdsClient.DeleteAccount(request)
})
if err != nil && !IsExceptedError(err, InvalidAccountNameNotFound) {
return WrapErrorf(err, DefaultErrorMsg, d.Id(), request.GetActionName(), AlibabaCloudSdkGoERROR)
}
addDebug(request.GetActionName(), raw)
return rdsService.WaitForAccount(d.Id(), Deleted, DefaultTimeoutMedium)
}