|
| 1 | +/* |
| 2 | +Use this data source to query detailed information of mariadb accounts |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +data "tencentcloud_mariadb_accounts" "accounts" { |
| 8 | + instance_id = "tdsql-4pzs5b67" |
| 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 | + mariadb "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/mariadb/v20170312" |
| 21 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 22 | +) |
| 23 | + |
| 24 | +func dataSourceTencentCloudMariadbAccounts() *schema.Resource { |
| 25 | + return &schema.Resource{ |
| 26 | + Read: dataSourceTencentCloudMariadbAccountsRead, |
| 27 | + Schema: map[string]*schema.Schema{ |
| 28 | + "instance_id": { |
| 29 | + Type: schema.TypeString, |
| 30 | + Required: true, |
| 31 | + Description: "instance id.", |
| 32 | + }, |
| 33 | + |
| 34 | + "list": { |
| 35 | + Type: schema.TypeList, |
| 36 | + Computed: true, |
| 37 | + Description: "account list.", |
| 38 | + Elem: &schema.Resource{ |
| 39 | + Schema: map[string]*schema.Schema{ |
| 40 | + "user_name": { |
| 41 | + Type: schema.TypeString, |
| 42 | + Computed: true, |
| 43 | + Description: "username.", |
| 44 | + }, |
| 45 | + "host": { |
| 46 | + Type: schema.TypeString, |
| 47 | + Computed: true, |
| 48 | + Description: "The host from which the user can log in (corresponding to the host field of MySQL users, UserName + Host uniquely identifies a user, in the form of IP, and the IP segment ends with %; supports filling in %; if it is empty, it defaults to %).", |
| 49 | + }, |
| 50 | + "description": { |
| 51 | + Type: schema.TypeString, |
| 52 | + Computed: true, |
| 53 | + Description: "User remarks.", |
| 54 | + }, |
| 55 | + "create_time": { |
| 56 | + Type: schema.TypeString, |
| 57 | + Computed: true, |
| 58 | + Description: "creation time.", |
| 59 | + }, |
| 60 | + "update_time": { |
| 61 | + Type: schema.TypeString, |
| 62 | + Computed: true, |
| 63 | + Description: "Update time.", |
| 64 | + }, |
| 65 | + "read_only": { |
| 66 | + Type: schema.TypeInt, |
| 67 | + Computed: true, |
| 68 | + Description: "Read-only flag, `0`: No, `1`: The SQL request of this account is preferentially executed on the standby machine, and the host machine 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.", |
| 69 | + }, |
| 70 | + "delay_thresh": { |
| 71 | + Type: schema.TypeInt, |
| 72 | + Computed: true, |
| 73 | + Description: "This field is meaningful for read-only accounts, indicating that the standby machine with the active-standby delay less than this value is selected.", |
| 74 | + }, |
| 75 | + "slave_const": { |
| 76 | + Type: schema.TypeInt, |
| 77 | + Computed: true, |
| 78 | + Description: "For read-only accounts, set whether the policy is to fix the standby machine, `0`: The standby machine is not fixed, that is, the standby machine does not meet the conditions and will not disconnect from the client, and the Proxy selects other available standby machines, `1`: The standby machine does not meet the conditions Disconnect, make sure one connection secures the standby.", |
| 79 | + }, |
| 80 | + }, |
| 81 | + }, |
| 82 | + }, |
| 83 | + |
| 84 | + "result_output_file": { |
| 85 | + Type: schema.TypeString, |
| 86 | + Optional: true, |
| 87 | + Description: "Used to save results.", |
| 88 | + }, |
| 89 | + }, |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +func dataSourceTencentCloudMariadbAccountsRead(d *schema.ResourceData, meta interface{}) error { |
| 94 | + defer logElapsed("data_source.tencentcloud_mariadb_accounts.read")() |
| 95 | + defer inconsistentCheck(d, meta)() |
| 96 | + |
| 97 | + logId := getLogId(contextNil) |
| 98 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 99 | + var instanceId string |
| 100 | + |
| 101 | + paramMap := make(map[string]interface{}) |
| 102 | + if v, ok := d.GetOk("instance_id"); ok { |
| 103 | + instanceId = v.(string) |
| 104 | + paramMap["instance_id"] = helper.String(v.(string)) |
| 105 | + } |
| 106 | + |
| 107 | + mariadbService := MariadbService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 108 | + |
| 109 | + var users []*mariadb.DBAccount |
| 110 | + err := resource.Retry(readRetryTimeout, func() *resource.RetryError { |
| 111 | + results, e := mariadbService.DescribeMariadbAccountsByFilter(ctx, paramMap) |
| 112 | + if e != nil { |
| 113 | + return retryError(e) |
| 114 | + } |
| 115 | + users = results |
| 116 | + return nil |
| 117 | + }) |
| 118 | + if err != nil { |
| 119 | + log.Printf("[CRITAL]%s read Mariadb users failed, reason:%+v", logId, err) |
| 120 | + return err |
| 121 | + } |
| 122 | + |
| 123 | + userList := []interface{}{} |
| 124 | + if users != nil { |
| 125 | + for _, user := range users { |
| 126 | + userMap := map[string]interface{}{} |
| 127 | + if user.UserName != nil { |
| 128 | + userMap["user_name"] = user.UserName |
| 129 | + } |
| 130 | + if user.Host != nil { |
| 131 | + userMap["host"] = user.Host |
| 132 | + } |
| 133 | + if user.Description != nil { |
| 134 | + userMap["description"] = user.Description |
| 135 | + } |
| 136 | + if user.CreateTime != nil { |
| 137 | + userMap["create_time"] = user.CreateTime |
| 138 | + } |
| 139 | + if user.UpdateTime != nil { |
| 140 | + userMap["update_time"] = user.UpdateTime |
| 141 | + } |
| 142 | + if user.ReadOnly != nil { |
| 143 | + userMap["read_only"] = user.ReadOnly |
| 144 | + } |
| 145 | + if user.DelayThresh != nil { |
| 146 | + userMap["delay_thresh"] = user.DelayThresh |
| 147 | + } |
| 148 | + if user.SlaveConst != nil { |
| 149 | + userMap["slave_const"] = user.SlaveConst |
| 150 | + } |
| 151 | + |
| 152 | + userList = append(userList, userMap) |
| 153 | + } |
| 154 | + err = d.Set("list", userList) |
| 155 | + if err != nil { |
| 156 | + log.Printf("[CRITAL]%s provider set instances list fail, reason:%s\n ", logId, err.Error()) |
| 157 | + return err |
| 158 | + } |
| 159 | + } |
| 160 | + d.SetId(instanceId) |
| 161 | + |
| 162 | + output, ok := d.GetOk("result_output_file") |
| 163 | + if ok && output.(string) != "" { |
| 164 | + if e := writeToFile(output.(string), userList); e != nil { |
| 165 | + return e |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + return nil |
| 170 | +} |
0 commit comments