/
resource_apsarastack_gpdb_account.go
191 lines (179 loc) · 6.29 KB
/
resource_apsarastack_gpdb_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
package alibabacloudstack
import (
"fmt"
"log"
"regexp"
"time"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
util "github.com/alibabacloud-go/tea-utils/service"
"github.com/aliyun/terraform-provider-alibabacloudstack/alibabacloudstack/connectivity"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func resourceAlibabacloudStackGpdbAccount() *schema.Resource {
return &schema.Resource{
Create: resourceAlibabacloudStackGpdbAccountCreate,
Read: resourceAlibabacloudStackGpdbAccountRead,
Update: resourceAlibabacloudStackGpdbAccountUpdate,
Delete: resourceAlibabacloudStackGpdbAccountDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(5 * time.Minute),
},
Schema: map[string]*schema.Schema{
"account_description": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: validation.StringMatch(regexp.MustCompile(`^[A-Za-z][\w\\_]{2,255}$`), "The description of the account. The description must be 2 to 256 characters in length and can contain letters, digits, underscores (_)."),
},
"account_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringMatch(regexp.MustCompile(`^[a-z][a-z0-9_]{1,14}[a-z0-9]$`), "The name of the account. The name must be 2 to 16 characters in length and can contain lower letters, digits, underscores (_)."),
},
"account_password": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringLenBetween(8, 32),
},
"db_instance_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"status": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func resourceAlibabacloudStackGpdbAccountCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AlibabacloudStackClient)
var response map[string]interface{}
action := "CreateAccount"
request := make(map[string]interface{})
conn, err := client.NewGpdbClient()
if err != nil {
return WrapError(err)
}
if v, ok := d.GetOk("account_description"); ok {
request["AccountDescription"] = v
}
request["Product"] = "gpdb"
request["OrganizationId"] = client.Department
request["RegionId"] = client.RegionId
request["AccountName"] = d.Get("account_name")
request["DBInstanceId"] = d.Get("db_instance_id")
request["AccountPassword"] = d.Get("account_password")
wait := incrementalWait(3*time.Second, 3*time.Second)
err = resource.Retry(d.Timeout(schema.TimeoutCreate), func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2016-05-03"), StringPointer("AK"), nil, request, &util.RuntimeOptions{})
if err != nil {
if NeedRetry(err) {
wait()
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
}
return nil
})
addDebug(action, response, request)
if err != nil {
return WrapErrorf(err, DefaultErrorMsg, "alibabacloudstack_gpdb_account", action, AlibabacloudStackSdkGoERROR)
}
d.SetId(fmt.Sprint(request["DBInstanceId"], ":", request["AccountName"]))
gpdbService := GpdbService{client}
stateConf := BuildStateConf([]string{}, []string{"1"}, d.Timeout(schema.TimeoutCreate), 60*time.Second, gpdbService.GpdbAccountStateRefreshFunc(d.Id(), []string{}))
if _, err := stateConf.WaitForState(); err != nil {
return WrapErrorf(err, IdMsg, d.Id())
}
return resourceAlibabacloudStackGpdbAccountRead(d, meta)
}
func resourceAlibabacloudStackGpdbAccountRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AlibabacloudStackClient)
gpdbService := GpdbService{client}
object, err := gpdbService.DescribeGpdbAccount(d.Id())
if err != nil {
if NotFoundError(err) {
log.Printf("[DEBUG] Resource alibabacloudstack_gpdb_account gpdbService.DescribeGpdbAccount Failed!!! %s", err)
d.SetId("")
return nil
}
return WrapError(err)
}
parts, err := ParseResourceId(d.Id(), 2)
if err != nil {
return WrapError(err)
}
d.Set("account_name", parts[1])
d.Set("db_instance_id", parts[0])
d.Set("account_description", object["AccountDescription"])
d.Set("status", convertGpdbAccountStatusResponse(object["AccountStatus"]))
return nil
}
func resourceAlibabacloudStackGpdbAccountUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AlibabacloudStackClient)
var response map[string]interface{}
parts, err := ParseResourceId(d.Id(), 2)
if err != nil {
return WrapError(err)
}
request := map[string]interface{}{
"AccountName": parts[1],
"DBInstanceId": parts[0],
}
update := false
if d.HasChange("account_password") {
update = true
if v, ok := d.GetOk("account_password"); ok {
request["AccountPassword"] = v
}
}
if update {
action := "ResetAccountPassword"
request["Product"] = "gpdb"
request["OrganizationId"] = client.Department
request["RegionId"] = client.RegionId
conn, err := client.NewGpdbClient()
if err != nil {
return WrapError(err)
}
wait := incrementalWait(3*time.Second, 3*time.Second)
err = resource.Retry(d.Timeout(schema.TimeoutUpdate), func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2016-05-03"), StringPointer("AK"), nil, request, &util.RuntimeOptions{})
if err != nil {
if NeedRetry(err) {
wait()
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
}
return nil
})
addDebug(action, response, request)
if err != nil {
return WrapErrorf(err, DefaultErrorMsg, d.Id(), action, AlibabacloudStackSdkGoERROR)
}
}
return resourceAlibabacloudStackGpdbAccountRead(d, meta)
}
func resourceAlibabacloudStackGpdbAccountDelete(d *schema.ResourceData, meta interface{}) error {
log.Printf("[WARN] Cannot destroy resourcealibabacloudstackGpdbAccount. Terraform will remove this resource from the state file, however resources may remain.")
return nil
}
func convertGpdbAccountStatusResponse(source interface{}) interface{} {
switch source {
case "Creating":
return "0"
case "Active":
return "1"
case "Deleting":
return "3"
}
return source
}