This repository has been archived by the owner on Oct 2, 2020. It is now read-only.
forked from baidubce/terraform-provider-baiducloud
/
resource_baiducloud_rds_account.go
193 lines (160 loc) · 4.93 KB
/
resource_baiducloud_rds_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
/*
Use this resource to get information about a RDS Account.
Example Usage
```hcl
resource "baiducloud_rds_account" "default" {
}
```
Import
RDS Account can be imported, e.g.
```hcl
$ terraform import baiducloud_rds_account.default id
```
*/
package baiducloud
import (
"fmt"
"strings"
"github.com/baidubce/bce-sdk-go/bce"
"github.com/baidubce/bce-sdk-go/services/rds"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
"github.com/terraform-providers/terraform-provider-baiducloud/baiducloud/connectivity"
)
func resourceBaiduCloudRdsAccount() *schema.Resource {
return &schema.Resource{
Create: resourceBaiduCloudRdsAccountCreate,
Read: resourceBaiduCloudRdsAccountRead,
Delete: resourceBaiduCloudRdsAccountDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"instance_id": {
Type: schema.TypeString,
Description: "ID of the rds instance.",
Required: true,
ForceNew: true,
},
"account_name": {
Type: schema.TypeString,
Description: "Account name.",
Required: true,
ForceNew: true,
},
"account_type": {
Type: schema.TypeString,
Description: "Type of the Account, Available values are Common、Super. The default is Common",
Optional: true,
ForceNew: true,
Computed: true,
ValidateFunc: validation.StringInSlice([]string{"Common", "Super"}, false),
},
"password": {
Type: schema.TypeString,
Description: "Operation password.",
Required: true,
ForceNew: true,
Sensitive: true,
},
"desc": {
Type: schema.TypeString,
Description: "description.",
Optional: true,
Computed: true,
ForceNew: true,
},
"status": {
Type: schema.TypeString,
Description: "Status of the Account.",
Computed: true,
},
},
}
}
func resourceBaiduCloudRdsAccountCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.BaiduClient)
args := &rds.CreateAccountArgs{
ClientToken: buildClientToken(),
AccountName: d.Get("account_name").(string),
Password: d.Get("password").(string),
}
instanceID := d.Get("instance_id").(string)
if accountType, ok := d.GetOk("account_type"); ok {
args.AccountType = accountType.(string)
}
if desc, ok := d.GetOk("desc"); ok {
args.Desc = desc.(string)
}
action := "Create RDS Account " + args.AccountName
addDebug(action, args)
err := resource.Retry(d.Timeout(schema.TimeoutCreate), func() *resource.RetryError {
_, err := client.WithRdsClient(func(rdsClient *rds.Client) (interface{}, error) {
return instanceID, rdsClient.CreateAccount(instanceID, args)
})
if err != nil {
if IsExceptedErrors(err, []string{bce.EINTERNAL_ERROR}) {
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
}
return nil
})
if err != nil {
return WrapErrorf(err, DefaultErrorMsg, "baiducloud_rds_account", action, BCESDKGoERROR)
}
d.SetId(fmt.Sprintf("%s%s%s", instanceID, COLON_SEPARATED, args.AccountName))
return resourceBaiduCloudRdsAccountRead(d, meta)
}
func resourceBaiduCloudRdsAccountRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.BaiduClient)
items := strings.Split(d.Id(), COLON_SEPARATED)
instanceID := items[0]
accountName := items[1]
action := "Query RDS Account " + accountName
raw, err := client.WithRdsClient(func(rdsClient *rds.Client) (interface{}, error) {
return rdsClient.GetAccount(instanceID, accountName)
})
addDebug(action, raw)
if err != nil {
if NotFoundError(err) {
d.SetId("")
return nil
}
return WrapErrorf(err, DefaultErrorMsg, "baiducloud_rds_account", action, BCESDKGoERROR)
}
result, _ := raw.(*rds.Account)
d.Set("account_name", result.AccountName)
d.Set("account_type", result.AccountType)
d.Set("status", result.Status)
d.Set("desc", result.Desc)
return nil
}
func resourceBaiduCloudRdsAccountDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.BaiduClient)
items := strings.Split(d.Id(), COLON_SEPARATED)
instanceID := items[0]
accountName := items[1]
action := "Delete RDS Account " + accountName
err := resource.Retry(d.Timeout(schema.TimeoutDelete), func() *resource.RetryError {
raw, err := client.WithRdsClient(func(rdsClient *rds.Client) (interface{}, error) {
return accountName, rdsClient.DeleteAccount(instanceID, accountName)
})
if err != nil {
if IsExceptedErrors(err, []string{bce.EINTERNAL_ERROR}) {
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
}
addDebug(action, raw)
return nil
})
if err != nil {
if IsExceptedErrors(err, []string{bce.EINTERNAL_ERROR}) {
return nil
}
return WrapErrorf(err, DefaultErrorMsg, "baiducloud_rds_account", action, BCESDKGoERROR)
}
return nil
}