|
| 1 | +/* |
| 2 | +Use this data source to query detailed information of dcdb accounts. |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +data "tencentcloud_dcdb_accounts" "foo" { |
| 8 | + instance_id = tencentcloud_dcdb_account.foo.instance_id |
| 9 | +} |
| 10 | +``` |
| 11 | +*/ |
| 12 | +package tencentcloud |
| 13 | + |
| 14 | +import ( |
| 15 | + "context" |
| 16 | + "log" |
| 17 | + |
| 18 | + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" |
| 19 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 20 | + dcdb "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dcdb/v20180411" |
| 21 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 22 | +) |
| 23 | + |
| 24 | +func dataSourceTencentCloudDcdbAccounts() *schema.Resource { |
| 25 | + return &schema.Resource{ |
| 26 | + Read: dataSourceTencentCloudDcdbAccountsRead, |
| 27 | + Schema: map[string]*schema.Schema{ |
| 28 | + "instance_id": { |
| 29 | + Type: schema.TypeString, |
| 30 | + Required: true, |
| 31 | + Description: "instance id.", |
| 32 | + }, |
| 33 | + "list": { |
| 34 | + Type: schema.TypeList, |
| 35 | + Computed: true, |
| 36 | + Description: "Cloud database account information.", |
| 37 | + Elem: &schema.Resource{ |
| 38 | + Schema: map[string]*schema.Schema{ |
| 39 | + "user_name": { |
| 40 | + Type: schema.TypeString, |
| 41 | + Computed: true, |
| 42 | + Description: "User Name.", |
| 43 | + }, |
| 44 | + "host": { |
| 45 | + Type: schema.TypeString, |
| 46 | + Computed: true, |
| 47 | + 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 %).", |
| 48 | + }, |
| 49 | + "description": { |
| 50 | + Type: schema.TypeString, |
| 51 | + Computed: true, |
| 52 | + Description: "User remarks info.", |
| 53 | + }, |
| 54 | + "create_time": { |
| 55 | + Type: schema.TypeString, |
| 56 | + Computed: true, |
| 57 | + Description: "Creation time.", |
| 58 | + }, |
| 59 | + "update_time": { |
| 60 | + Type: schema.TypeString, |
| 61 | + Computed: true, |
| 62 | + Description: "Last update time.", |
| 63 | + }, |
| 64 | + "read_only": { |
| 65 | + Type: schema.TypeInt, |
| 66 | + Computed: true, |
| 67 | + 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.", |
| 68 | + }, |
| 69 | + "delay_thresh": { |
| 70 | + Type: schema.TypeInt, |
| 71 | + Computed: true, |
| 72 | + 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.", |
| 73 | + }, |
| 74 | + "slave_const": { |
| 75 | + Type: schema.TypeInt, |
| 76 | + Computed: true, |
| 77 | + 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.", |
| 78 | + }, |
| 79 | + }, |
| 80 | + }, |
| 81 | + }, |
| 82 | + "result_output_file": { |
| 83 | + Type: schema.TypeString, |
| 84 | + Optional: true, |
| 85 | + Description: "Used to save results.", |
| 86 | + }, |
| 87 | + }, |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +func dataSourceTencentCloudDcdbAccountsRead(d *schema.ResourceData, meta interface{}) error { |
| 92 | + defer logElapsed("data_source.tencentcloud_dcdb_accounts.read")() |
| 93 | + defer inconsistentCheck(d, meta)() |
| 94 | + |
| 95 | + logId := getLogId(contextNil) |
| 96 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 97 | + |
| 98 | + paramMap := make(map[string]interface{}) |
| 99 | + if v, ok := d.GetOk("instance_id"); ok { |
| 100 | + paramMap["instance_id"] = helper.String(v.(string)) |
| 101 | + } |
| 102 | + |
| 103 | + dcdbService := DcdbService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 104 | + |
| 105 | + var dbAccountList []*dcdb.DBAccount |
| 106 | + err := resource.Retry(readRetryTimeout, func() *resource.RetryError { |
| 107 | + results, e := dcdbService.DescribeDcdbAccountsByFilter(ctx, paramMap) |
| 108 | + if e != nil { |
| 109 | + return retryError(e) |
| 110 | + } |
| 111 | + dbAccountList = results |
| 112 | + return nil |
| 113 | + }) |
| 114 | + if err != nil { |
| 115 | + log.Printf("[CRITAL]%s read Dcdb list failed, reason:%+v", logId, err) |
| 116 | + return err |
| 117 | + } |
| 118 | + |
| 119 | + retList := []interface{}{} |
| 120 | + if dbAccountList != nil { |
| 121 | + ids := make([]string, 0, len(dbAccountList)) |
| 122 | + for _, dbA := range dbAccountList { |
| 123 | + listMap := map[string]interface{}{} |
| 124 | + if dbA.UserName != nil { |
| 125 | + listMap["user_name"] = dbA.UserName |
| 126 | + } |
| 127 | + if dbA.Host != nil { |
| 128 | + listMap["host"] = dbA.Host |
| 129 | + } |
| 130 | + if dbA.Description != nil { |
| 131 | + listMap["description"] = dbA.Description |
| 132 | + } |
| 133 | + if dbA.CreateTime != nil { |
| 134 | + listMap["create_time"] = dbA.CreateTime |
| 135 | + } |
| 136 | + if dbA.UpdateTime != nil { |
| 137 | + listMap["update_time"] = dbA.UpdateTime |
| 138 | + } |
| 139 | + if dbA.ReadOnly != nil { |
| 140 | + listMap["read_only"] = dbA.ReadOnly |
| 141 | + } |
| 142 | + if dbA.DelayThresh != nil { |
| 143 | + listMap["delay_thresh"] = dbA.DelayThresh |
| 144 | + } |
| 145 | + if dbA.SlaveConst != nil { |
| 146 | + listMap["slave_const"] = dbA.SlaveConst |
| 147 | + } |
| 148 | + ids = append(ids, *dbA.UserName+FILED_SP+*dbA.Host) |
| 149 | + retList = append(retList, listMap) |
| 150 | + } |
| 151 | + |
| 152 | + d.SetId(helper.DataResourceIdsHash(ids)) |
| 153 | + _ = d.Set("list", retList) |
| 154 | + } |
| 155 | + |
| 156 | + output, ok := d.GetOk("result_output_file") |
| 157 | + if ok && output.(string) != "" { |
| 158 | + if e := writeToFile(output.(string), retList); e != nil { |
| 159 | + return e |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + return nil |
| 164 | +} |
0 commit comments