forked from aliyun/terraform-provider-alicloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_alicloud_db_account_privilege.go
188 lines (166 loc) · 5.1 KB
/
resource_alicloud_db_account_privilege.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
package alicloud
import (
"fmt"
"strings"
"time"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/terraform-providers/terraform-provider-alicloud/alicloud/connectivity"
)
func resourceAlicloudDBAccountPrivilege() *schema.Resource {
return &schema.Resource{
Create: resourceAlicloudDBAccountPrivilegeCreate,
Read: resourceAlicloudDBAccountPrivilegeRead,
Update: resourceAlicloudDBAccountPrivilegeUpdate,
Delete: resourceAlicloudDBAccountPrivilegeDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"instance_id": {
Type: schema.TypeString,
ForceNew: true,
Required: true,
},
"account_name": {
Type: schema.TypeString,
ForceNew: true,
Required: true,
},
"privilege": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validateAllowedStringValue([]string{string(ReadOnly), string(ReadWrite)}),
Default: ReadOnly,
ForceNew: true,
},
"db_names": {
Type: schema.TypeSet,
Required: true,
Elem: &schema.Schema{Type: schema.TypeString},
MinItems: 1,
},
},
}
}
func resourceAlicloudDBAccountPrivilegeCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
rsdService := RdsService{client}
instanceId := d.Get("instance_id").(string)
account := d.Get("account_name").(string)
privilege := d.Get("privilege").(string)
dbList := d.Get("db_names").(*schema.Set).List()
// wait instance running before granting
if err := rsdService.WaitForDBInstance(instanceId, Running, DefaultLongTimeout); err != nil {
return WrapError(err)
}
d.SetId(fmt.Sprintf("%s%s%s%s%s", instanceId, COLON_SEPARATED, account, COLON_SEPARATED, privilege))
if len(dbList) > 0 {
for _, db := range dbList {
if err := resource.Retry(10*time.Minute, func() *resource.RetryError {
if err := rsdService.GrantAccountPrivilege(d.Id(), db.(string)); err != nil {
if IsExceptedErrors(err, OperationDeniedDBStatus) {
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
}
return nil
}); err != nil {
return WrapError(err)
}
}
}
return resourceAlicloudDBAccountPrivilegeRead(d, meta)
}
func resourceAlicloudDBAccountPrivilegeRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
rsdService := RdsService{client}
parts, err := ParseResourceId(d.Id(), 3)
if err != nil {
return WrapError(err)
}
object, err := rsdService.DescribeDBAccountPrivilege(d.Id())
if err != nil {
if rsdService.NotFoundDBInstance(err) {
d.SetId("")
return nil
}
return WrapError(err)
}
d.Set("instance_id", object.DBInstanceId)
d.Set("account_name", object.AccountName)
d.Set("privilege", parts[2])
var names []string
for _, pri := range object.DatabasePrivileges.DatabasePrivilege {
if pri.AccountPrivilege == parts[2] {
names = append(names, pri.DBName)
}
}
d.Set("db_names", names)
return nil
}
func resourceAlicloudDBAccountPrivilegeUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
rdsService := RdsService{client}
d.Partial(true)
if d.HasChange("db_names") {
parts := strings.Split(d.Id(), COLON_SEPARATED)
o, n := d.GetChange("db_names")
os := o.(*schema.Set)
ns := n.(*schema.Set)
remove := os.Difference(ns).List()
add := ns.Difference(os).List()
if len(remove) > 0 {
// wait instance running before revoking
if err := rdsService.WaitForDBInstance(parts[0], Running, DefaultTimeoutMedium); err != nil {
return WrapError(err)
}
for _, db := range remove {
if err := rdsService.RevokeAccountPrivilege(d.Id(), db.(string)); err != nil {
return WrapError(err)
}
}
}
if len(add) > 0 {
// wait instance running before granting
if err := rdsService.WaitForDBInstance(parts[0], Running, DefaultTimeoutMedium); err != nil {
return WrapError(err)
}
for _, db := range add {
if err := rdsService.GrantAccountPrivilege(d.Id(), db.(string)); err != nil {
return WrapError(err)
}
}
}
d.SetPartial("db_names")
}
d.Partial(false)
return resourceAlicloudDBAccountPrivilegeRead(d, meta)
}
func resourceAlicloudDBAccountPrivilegeDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
rdsService := RdsService{client}
parts, err := ParseResourceId(d.Id(), 3)
if err != nil {
return WrapError(err)
}
object, err := rdsService.DescribeDBAccountPrivilege(d.Id())
if err != nil {
if rdsService.NotFoundDBInstance(err) {
return nil
}
return WrapError(err)
}
var dbName string
if len(object.DatabasePrivileges.DatabasePrivilege) > 0 {
for _, pri := range object.DatabasePrivileges.DatabasePrivilege {
if pri.AccountPrivilege == parts[2] {
dbName = pri.DBName
if err := rdsService.RevokeAccountPrivilege(d.Id(), pri.DBName); err != nil {
return WrapError(err)
}
}
}
}
return rdsService.WaitForAccountPrivilege(d.Id(), dbName, Deleted, DefaultTimeoutMedium)
}