-
Notifications
You must be signed in to change notification settings - Fork 141
/
Copy pathdata_source_tc_dcdb_accounts.go
156 lines (142 loc) · 5.26 KB
/
data_source_tc_dcdb_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
package dcdb
import (
"context"
"log"
tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
dcdb "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dcdb/v20180411"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
)
func DataSourceTencentCloudDcdbAccounts() *schema.Resource {
return &schema.Resource{
Read: dataSourceTencentCloudDcdbAccountsRead,
Schema: map[string]*schema.Schema{
"instance_id": {
Type: schema.TypeString,
Required: true,
Description: "instance id.",
},
"list": {
Type: schema.TypeList,
Computed: true,
Description: "Cloud database account information.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"user_name": {
Type: schema.TypeString,
Computed: true,
Description: "User Name.",
},
"host": {
Type: schema.TypeString,
Computed: true,
Description: "From which host the user can log in (corresponding to the host field of MySQL users, UserName + Host uniquely identifies a user, in the form of IP, the IP segment ends with %; supports filling in %; if it is empty, it defaults to %).",
},
"description": {
Type: schema.TypeString,
Computed: true,
Description: "User remarks info.",
},
"create_time": {
Type: schema.TypeString,
Computed: true,
Description: "Creation time.",
},
"update_time": {
Type: schema.TypeString,
Computed: true,
Description: "Last update time.",
},
"read_only": {
Type: schema.TypeInt,
Computed: true,
Description: "Read-only flag, 0: No, 1: The SQL request of this account is preferentially executed on the standby machine, and the host is selected for execution when the standby machine is unavailable. 2: The standby machine is preferentially selected for execution, and the operation fails when the standby machine is unavailable.",
},
"delay_thresh": {
Type: schema.TypeInt,
Computed: true,
Description: "If the standby machine delay exceeds the setting value of this parameter, the system will consider that the standby machine is faulty and recommend that the parameter value be greater than 10. This parameter takes effect when ReadOnly selects 1 and 2.",
},
"slave_const": {
Type: schema.TypeInt,
Computed: true,
Description: "For read-only accounts, set the policy whether to fix the standby machine, 0: not fix the standby machine, that is, the standby machine will not disconnect from the client if it does not meet the conditions, the Proxy selects other available standby machines, 1: the standby machine will be disconnected if the conditions are not met, Make sure a connection is secured to the standby machine.",
},
},
},
},
"result_output_file": {
Type: schema.TypeString,
Optional: true,
Description: "Used to save results.",
},
},
}
}
func dataSourceTencentCloudDcdbAccountsRead(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("data_source.tencentcloud_dcdb_accounts.read")()
defer tccommon.InconsistentCheck(d, meta)()
logId := tccommon.GetLogId(tccommon.ContextNil)
ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
paramMap := make(map[string]interface{})
if v, ok := d.GetOk("instance_id"); ok {
paramMap["instance_id"] = helper.String(v.(string))
}
dcdbService := DcdbService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
var dbAccountList []*dcdb.DBAccount
err := resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError {
results, e := dcdbService.DescribeDcdbAccountsByFilter(ctx, paramMap)
if e != nil {
return tccommon.RetryError(e)
}
dbAccountList = results
return nil
})
if err != nil {
log.Printf("[CRITAL]%s read Dcdb list failed, reason:%+v", logId, err)
return err
}
retList := []interface{}{}
if dbAccountList != nil {
ids := make([]string, 0, len(dbAccountList))
for _, dbA := range dbAccountList {
listMap := map[string]interface{}{}
if dbA.UserName != nil {
listMap["user_name"] = dbA.UserName
}
if dbA.Host != nil {
listMap["host"] = dbA.Host
}
if dbA.Description != nil {
listMap["description"] = dbA.Description
}
if dbA.CreateTime != nil {
listMap["create_time"] = dbA.CreateTime
}
if dbA.UpdateTime != nil {
listMap["update_time"] = dbA.UpdateTime
}
if dbA.ReadOnly != nil {
listMap["read_only"] = dbA.ReadOnly
}
if dbA.DelayThresh != nil {
listMap["delay_thresh"] = dbA.DelayThresh
}
if dbA.SlaveConst != nil {
listMap["slave_const"] = dbA.SlaveConst
}
ids = append(ids, *dbA.UserName+tccommon.FILED_SP+*dbA.Host)
retList = append(retList, listMap)
}
d.SetId(helper.DataResourceIdsHash(ids))
_ = d.Set("list", retList)
}
output, ok := d.GetOk("result_output_file")
if ok && output.(string) != "" {
if e := tccommon.WriteToFile(output.(string), retList); e != nil {
return e
}
}
return nil
}