/
data_source_huaweicloud_rds_pg_accounts.go
228 lines (204 loc) · 5.72 KB
/
data_source_huaweicloud_rds_pg_accounts.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
package rds
import (
"context"
"encoding/json"
"strings"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/go-uuid"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/jmespath/go-jmespath"
"github.com/chnsz/golangsdk/pagination"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/common"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils"
)
// @API RDS GET /v3/{project_id}/instances/{instance_id}/db_user/detail
func DataSourcePgAccounts() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourcePgAccountsRead,
Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"instance_id": {
Type: schema.TypeString,
Required: true,
},
"user_name": {
Type: schema.TypeString,
Optional: true,
},
"users": {
Type: schema.TypeList,
Elem: pgAccountsSchema(),
Computed: true,
},
},
}
}
func pgAccountsSchema() *schema.Resource {
sc := schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Computed: true,
},
"attributes": {
Type: schema.TypeList,
Elem: attributesSchema(),
Computed: true,
},
"memberof": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"description": {
Type: schema.TypeString,
Computed: true,
},
},
}
return &sc
}
func attributesSchema() *schema.Resource {
sc := schema.Resource{
Schema: map[string]*schema.Schema{
"rolsuper": {
Type: schema.TypeBool,
Computed: true,
},
"rolinherit": {
Type: schema.TypeBool,
Computed: true,
},
"rolcreaterole": {
Type: schema.TypeBool,
Computed: true,
},
"rolcreatedb": {
Type: schema.TypeBool,
Computed: true,
},
"rolcanlogin": {
Type: schema.TypeBool,
Computed: true,
},
"rolconnlimit": {
Type: schema.TypeInt,
Computed: true,
},
"rolreplication": {
Type: schema.TypeBool,
Computed: true,
},
"rolbypassrls": {
Type: schema.TypeBool,
Computed: true,
},
},
}
return &sc
}
func dataSourcePgAccountsRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
cfg := meta.(*config.Config)
region := cfg.GetRegion(d)
var mErr *multierror.Error
var (
listPgAccountsHttpUrl = "v3/{project_id}/instances/{instance_id}/db_user/detail?page=1&limit=100"
listPgAccountsProduct = "rds"
)
listPgAccountsClient, err := cfg.NewServiceClient(listPgAccountsProduct, region)
if err != nil {
return diag.Errorf("error creating RDS client: %s", err)
}
listPgAccountsPath := listPgAccountsClient.Endpoint + listPgAccountsHttpUrl
listPgAccountsPath = strings.ReplaceAll(listPgAccountsPath, "{project_id}", listPgAccountsClient.ProjectID)
listPgAccountsPath = strings.ReplaceAll(listPgAccountsPath, "{instance_id}", d.Get("instance_id").(string))
listPgAccountsResp, err := pagination.ListAllItems(
listPgAccountsClient,
"page",
listPgAccountsPath,
&pagination.QueryOpts{MarkerField: ""})
if err != nil {
return common.CheckDeletedDiag(d, err, "error retrieving RDS PostgreSQL accounts")
}
listPgAccountsRespJson, err := json.Marshal(listPgAccountsResp)
if err != nil {
return diag.FromErr(err)
}
var listPgAccountsRespBody interface{}
err = json.Unmarshal(listPgAccountsRespJson, &listPgAccountsRespBody)
if err != nil {
return diag.FromErr(err)
}
dataSourceId, err := uuid.GenerateUUID()
if err != nil {
return diag.Errorf("unable to generate ID: %s", err)
}
d.SetId(dataSourceId)
mErr = multierror.Append(
mErr,
d.Set("region", region),
d.Set("users", flattenPgAccountsBody(filterPgAccounts(d, listPgAccountsRespBody))),
)
return diag.FromErr(mErr.ErrorOrNil())
}
func flattenPgAccountsBody(resp []interface{}) []interface{} {
if resp == nil {
return nil
}
rst := make([]interface{}, 0, len(resp))
for _, v := range resp {
rst = append(rst, map[string]interface{}{
"name": utils.PathSearch("name", v, nil),
"attributes": flattenAttributes(v),
"memberof": utils.PathSearch("memberof", v, nil),
"description": utils.PathSearch("comment", v, nil),
})
}
return rst
}
func filterPgAccounts(d *schema.ResourceData, resp interface{}) []interface{} {
accountsJson := utils.PathSearch("users", resp, make([]interface{}, 0))
accountArray := accountsJson.([]interface{})
if len(accountArray) < 1 {
return nil
}
result := make([]interface{}, 0)
rawUserName, rawUserNameOK := d.GetOk("user_name")
for _, account := range accountArray {
name := utils.PathSearch("name", account, nil)
if rawUserNameOK && rawUserName != name {
continue
}
result = append(result, account)
}
return result
}
func flattenAttributes(resp interface{}) []interface{} {
var rst []interface{}
curJson, err := jmespath.Search("attributes", resp)
if err != nil {
return rst
}
if curJson == nil {
return nil
}
rst = []interface{}{
map[string]interface{}{
"rolsuper": utils.PathSearch("rolsuper", curJson, nil),
"rolinherit": utils.PathSearch("rolinherit", curJson, nil),
"rolcreaterole": utils.PathSearch("rolcreaterole", curJson, nil),
"rolcreatedb": utils.PathSearch("rolcreatedb", curJson, nil),
"rolcanlogin": utils.PathSearch("rolcanlogin", curJson, nil),
"rolconnlimit": utils.PathSearch("rolconnlimit", curJson, nil),
"rolreplication": utils.PathSearch("rolreplication", curJson, nil),
"rolbypassrls": utils.PathSearch("rolbypassrls", curJson, nil),
},
}
return rst
}