|
| 1 | +/* |
| 2 | +Use this data source to query detailed information of cynosdb accounts |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +data "tencentcloud_cynosdb_accounts" "accounts" { |
| 8 | + cluster_id = "cynosdbmysql-bws8h88b" |
| 9 | + account_names = ["root"] |
| 10 | +} |
| 11 | +``` |
| 12 | +*/ |
| 13 | +package tencentcloud |
| 14 | + |
| 15 | +import ( |
| 16 | + "context" |
| 17 | + |
| 18 | + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" |
| 19 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 20 | + cynosdb "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cynosdb/v20190107" |
| 21 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 22 | +) |
| 23 | + |
| 24 | +func dataSourceTencentCloudCynosdbAccounts() *schema.Resource { |
| 25 | + return &schema.Resource{ |
| 26 | + Read: dataSourceTencentCloudCynosdbAccountsRead, |
| 27 | + Schema: map[string]*schema.Schema{ |
| 28 | + "cluster_id": { |
| 29 | + Required: true, |
| 30 | + Type: schema.TypeString, |
| 31 | + Description: "The ID of cluster.", |
| 32 | + }, |
| 33 | + |
| 34 | + "account_names": { |
| 35 | + Optional: true, |
| 36 | + Type: schema.TypeSet, |
| 37 | + Elem: &schema.Schema{ |
| 38 | + Type: schema.TypeString, |
| 39 | + }, |
| 40 | + Description: "List of accounts to be filtered.", |
| 41 | + }, |
| 42 | + |
| 43 | + "hosts": { |
| 44 | + Optional: true, |
| 45 | + Type: schema.TypeSet, |
| 46 | + Elem: &schema.Schema{ |
| 47 | + Type: schema.TypeString, |
| 48 | + }, |
| 49 | + Description: "List of hosts to be filtered.", |
| 50 | + }, |
| 51 | + |
| 52 | + "account_set": { |
| 53 | + Type: schema.TypeList, |
| 54 | + Computed: true, |
| 55 | + Description: "Database account list.""Note: This field may return null, indicating that no valid value can be obtained.", |
| 56 | + Elem: &schema.Resource{ |
| 57 | + Schema: map[string]*schema.Schema{ |
| 58 | + "account_name": { |
| 59 | + Type: schema.TypeString, |
| 60 | + Computed: true, |
| 61 | + Description: "Account name of database.", |
| 62 | + }, |
| 63 | + "description": { |
| 64 | + Type: schema.TypeString, |
| 65 | + Computed: true, |
| 66 | + Description: "The account description of database.", |
| 67 | + }, |
| 68 | + "create_time": { |
| 69 | + Type: schema.TypeString, |
| 70 | + Computed: true, |
| 71 | + Description: "Create time.", |
| 72 | + }, |
| 73 | + "update_time": { |
| 74 | + Type: schema.TypeString, |
| 75 | + Computed: true, |
| 76 | + Description: "Update time.", |
| 77 | + }, |
| 78 | + "host": { |
| 79 | + Type: schema.TypeString, |
| 80 | + Computed: true, |
| 81 | + Description: "Host.", |
| 82 | + }, |
| 83 | + "max_user_connections": { |
| 84 | + Type: schema.TypeInt, |
| 85 | + Computed: true, |
| 86 | + Description: "Maximum number of user connections.", |
| 87 | + }, |
| 88 | + }, |
| 89 | + }, |
| 90 | + }, |
| 91 | + |
| 92 | + "result_output_file": { |
| 93 | + Type: schema.TypeString, |
| 94 | + Optional: true, |
| 95 | + Description: "Used to save results.", |
| 96 | + }, |
| 97 | + }, |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +func dataSourceTencentCloudCynosdbAccountsRead(d *schema.ResourceData, meta interface{}) error { |
| 102 | + defer logElapsed("data_source.tencentcloud_cynosdb_accounts.read")() |
| 103 | + defer inconsistentCheck(d, meta)() |
| 104 | + |
| 105 | + logId := getLogId(contextNil) |
| 106 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 107 | + |
| 108 | + var clusterId string |
| 109 | + |
| 110 | + paramMap := make(map[string]interface{}) |
| 111 | + if v, ok := d.GetOk("cluster_id"); ok { |
| 112 | + clusterId = v.(string) |
| 113 | + } |
| 114 | + |
| 115 | + if v, ok := d.GetOk("account_names"); ok { |
| 116 | + accountNamesSet := v.(*schema.Set).List() |
| 117 | + paramMap["account_names"] = accountNamesSet |
| 118 | + } |
| 119 | + |
| 120 | + if v, ok := d.GetOk("hosts"); ok { |
| 121 | + hostsSet := v.(*schema.Set).List() |
| 122 | + paramMap["Hosts"] = helper.InterfacesStringsPoint(hostsSet) |
| 123 | + } |
| 124 | + |
| 125 | + service := CynosdbService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 126 | + |
| 127 | + var accountSet []*cynosdb.Account |
| 128 | + |
| 129 | + err := resource.Retry(readRetryTimeout, func() *resource.RetryError { |
| 130 | + response, e := service.DescribeCynosdbAccountsByFilter(ctx, clusterId, paramMap) |
| 131 | + if e != nil { |
| 132 | + return retryError(e) |
| 133 | + } |
| 134 | + accountSet = response.AccountSet |
| 135 | + return nil |
| 136 | + }) |
| 137 | + if err != nil { |
| 138 | + return err |
| 139 | + } |
| 140 | + |
| 141 | + ids := make([]string, 0, len(accountSet)) |
| 142 | + tmpList := make([]map[string]interface{}, 0, len(accountSet)) |
| 143 | + for _, item := range accountSet { |
| 144 | + ids = append(ids, *item.AccountName) |
| 145 | + account := make(map[string]interface{}) |
| 146 | + account["account_name"] = item.AccountName |
| 147 | + account["description"] = item.Description |
| 148 | + account["create_time"] = item.CreateTime |
| 149 | + account["update_time"] = item.UpdateTime |
| 150 | + account["host"] = item.Host |
| 151 | + account["max_user_connections"] = item.MaxUserConnections |
| 152 | + |
| 153 | + tmpList = append(tmpList, account) |
| 154 | + } |
| 155 | + d.SetId(helper.DataResourceIdsHash(ids)) |
| 156 | + d.Set("account_set", tmpList) |
| 157 | + output, ok := d.GetOk("result_output_file") |
| 158 | + if ok && output.(string) != "" { |
| 159 | + if e := writeToFile(output.(string), tmpList); e != nil { |
| 160 | + return e |
| 161 | + } |
| 162 | + } |
| 163 | + return nil |
| 164 | +} |
0 commit comments