|
| 1 | +/* |
| 2 | +Use this data source to query detailed information of cynosdb proxy_node |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +data "tencentcloud_cynosdb_proxy_node" "proxy_node" { |
| 8 | + order_by = "CREATETIME" |
| 9 | + order_by_type = "DESC" |
| 10 | + filters { |
| 11 | + names = "ClusterId" |
| 12 | + values = "cynosdbmysql-cgd2gpwr" |
| 13 | + exact_match = false |
| 14 | + name = "ClusterId" |
| 15 | + } |
| 16 | +} |
| 17 | +``` |
| 18 | +*/ |
| 19 | +package tencentcloud |
| 20 | + |
| 21 | +import ( |
| 22 | + "context" |
| 23 | + |
| 24 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 25 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 26 | + cynosdb "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cynosdb/v20190107" |
| 27 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 28 | +) |
| 29 | + |
| 30 | +func dataSourceTencentCloudCynosdbProxyNode() *schema.Resource { |
| 31 | + return &schema.Resource{ |
| 32 | + Read: dataSourceTencentCloudCynosdbProxyNodeRead, |
| 33 | + Schema: map[string]*schema.Schema{ |
| 34 | + "order_by": { |
| 35 | + Optional: true, |
| 36 | + Type: schema.TypeString, |
| 37 | + Description: "Sort field, value range:CREATETIME: creation time; PRIODENDTIME: expiration time.", |
| 38 | + }, |
| 39 | + "order_by_type": { |
| 40 | + Optional: true, |
| 41 | + Type: schema.TypeString, |
| 42 | + Description: "Sort type, value range:ASC: ascending sort; DESC: descending sort.", |
| 43 | + }, |
| 44 | + "filters": { |
| 45 | + Optional: true, |
| 46 | + Type: schema.TypeList, |
| 47 | + Description: "Search criteria, if there are multiple filters, the relationship between the filters is a logical AND relationship.", |
| 48 | + Elem: &schema.Resource{ |
| 49 | + Schema: map[string]*schema.Schema{ |
| 50 | + "names": { |
| 51 | + Type: schema.TypeSet, |
| 52 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 53 | + Required: true, |
| 54 | + Description: "Search String.", |
| 55 | + }, |
| 56 | + "values": { |
| 57 | + Type: schema.TypeSet, |
| 58 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 59 | + Required: true, |
| 60 | + Description: "Search String.", |
| 61 | + }, |
| 62 | + "exact_match": { |
| 63 | + Type: schema.TypeBool, |
| 64 | + Optional: true, |
| 65 | + Description: "Exact match or not.", |
| 66 | + }, |
| 67 | + "name": { |
| 68 | + Type: schema.TypeString, |
| 69 | + Optional: true, |
| 70 | + Description: "Search Fields. Supported: Status, ProxyNodeId, ClusterId.", |
| 71 | + }, |
| 72 | + "operator": { |
| 73 | + Type: schema.TypeString, |
| 74 | + Optional: true, |
| 75 | + Description: "Operator.", |
| 76 | + }, |
| 77 | + }, |
| 78 | + }, |
| 79 | + }, |
| 80 | + "proxy_node_infos": { |
| 81 | + Computed: true, |
| 82 | + Type: schema.TypeList, |
| 83 | + Description: "Database Agent Node List.", |
| 84 | + Elem: &schema.Resource{ |
| 85 | + Schema: map[string]*schema.Schema{ |
| 86 | + "proxy_node_id": { |
| 87 | + Type: schema.TypeString, |
| 88 | + Computed: true, |
| 89 | + Description: "Database Agent Node ID.", |
| 90 | + }, |
| 91 | + "proxy_node_connections": { |
| 92 | + Type: schema.TypeInt, |
| 93 | + Computed: true, |
| 94 | + Description: "The current number of connections of the node. The DescribeProxyNodes interface does not return a value for this field.", |
| 95 | + }, |
| 96 | + "cpu": { |
| 97 | + Type: schema.TypeInt, |
| 98 | + Computed: true, |
| 99 | + Description: "Database Agent Node CPU.", |
| 100 | + }, |
| 101 | + "mem": { |
| 102 | + Type: schema.TypeInt, |
| 103 | + Computed: true, |
| 104 | + Description: "Database Agent Node Memory.", |
| 105 | + }, |
| 106 | + "status": { |
| 107 | + Type: schema.TypeString, |
| 108 | + Computed: true, |
| 109 | + Description: "Database Agent Node Status.", |
| 110 | + }, |
| 111 | + "proxy_group_id": { |
| 112 | + Type: schema.TypeString, |
| 113 | + Computed: true, |
| 114 | + Description: "Database Agent Group ID.", |
| 115 | + }, |
| 116 | + "cluster_id": { |
| 117 | + Type: schema.TypeString, |
| 118 | + Computed: true, |
| 119 | + Description: "Cluster ID.", |
| 120 | + }, |
| 121 | + "app_id": { |
| 122 | + Type: schema.TypeInt, |
| 123 | + Computed: true, |
| 124 | + Description: "User AppID.", |
| 125 | + }, |
| 126 | + "region": { |
| 127 | + Type: schema.TypeString, |
| 128 | + Computed: true, |
| 129 | + Description: "region.", |
| 130 | + }, |
| 131 | + "zone": { |
| 132 | + Type: schema.TypeString, |
| 133 | + Computed: true, |
| 134 | + Description: "Availability Zone.", |
| 135 | + }, |
| 136 | + }, |
| 137 | + }, |
| 138 | + }, |
| 139 | + "result_output_file": { |
| 140 | + Type: schema.TypeString, |
| 141 | + Optional: true, |
| 142 | + Description: "Used to save results.", |
| 143 | + }, |
| 144 | + }, |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +func dataSourceTencentCloudCynosdbProxyNodeRead(d *schema.ResourceData, meta interface{}) error { |
| 149 | + defer logElapsed("data_source.tencentcloud_cynosdb_proxy_node.read")() |
| 150 | + defer inconsistentCheck(d, meta)() |
| 151 | + |
| 152 | + var ( |
| 153 | + logId = getLogId(contextNil) |
| 154 | + ctx = context.WithValue(context.TODO(), logIdKey, logId) |
| 155 | + service = CynosdbService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 156 | + proxyNodeInfos []*cynosdb.ProxyNodeInfo |
| 157 | + ) |
| 158 | + |
| 159 | + paramMap := make(map[string]interface{}) |
| 160 | + if v, ok := d.GetOk("order_by"); ok { |
| 161 | + paramMap["OrderBy"] = helper.String(v.(string)) |
| 162 | + } |
| 163 | + |
| 164 | + if v, ok := d.GetOk("order_by_type"); ok { |
| 165 | + paramMap["OrderByType"] = helper.String(v.(string)) |
| 166 | + } |
| 167 | + |
| 168 | + if v, ok := d.GetOk("filters"); ok { |
| 169 | + filtersSet := v.([]interface{}) |
| 170 | + tmpSet := make([]*cynosdb.QueryFilter, 0, len(filtersSet)) |
| 171 | + |
| 172 | + for _, item := range filtersSet { |
| 173 | + queryFilter := cynosdb.QueryFilter{} |
| 174 | + queryFilterMap := item.(map[string]interface{}) |
| 175 | + |
| 176 | + if v, ok := queryFilterMap["names"]; ok { |
| 177 | + namesSet := v.(*schema.Set).List() |
| 178 | + queryFilter.Names = helper.InterfacesStringsPoint(namesSet) |
| 179 | + } |
| 180 | + if v, ok := queryFilterMap["values"]; ok { |
| 181 | + valuesSet := v.(*schema.Set).List() |
| 182 | + queryFilter.Values = helper.InterfacesStringsPoint(valuesSet) |
| 183 | + } |
| 184 | + if v, ok := queryFilterMap["exact_match"]; ok { |
| 185 | + queryFilter.ExactMatch = helper.Bool(v.(bool)) |
| 186 | + } |
| 187 | + if v, ok := queryFilterMap["name"]; ok { |
| 188 | + queryFilter.Name = helper.String(v.(string)) |
| 189 | + } |
| 190 | + if v, ok := queryFilterMap["operator"]; ok { |
| 191 | + queryFilter.Operator = helper.String(v.(string)) |
| 192 | + } |
| 193 | + tmpSet = append(tmpSet, &queryFilter) |
| 194 | + } |
| 195 | + paramMap["filters"] = tmpSet |
| 196 | + } |
| 197 | + |
| 198 | + err := resource.Retry(readRetryTimeout, func() *resource.RetryError { |
| 199 | + result, e := service.DescribeCynosdbProxyNodeByFilter(ctx, paramMap) |
| 200 | + if e != nil { |
| 201 | + return retryError(e) |
| 202 | + } |
| 203 | + |
| 204 | + proxyNodeInfos = result |
| 205 | + return nil |
| 206 | + }) |
| 207 | + |
| 208 | + if err != nil { |
| 209 | + return err |
| 210 | + } |
| 211 | + |
| 212 | + ids := make([]string, 0, len(proxyNodeInfos)) |
| 213 | + tmpList := make([]map[string]interface{}, 0, len(proxyNodeInfos)) |
| 214 | + |
| 215 | + if proxyNodeInfos != nil { |
| 216 | + for _, proxyNodeInfo := range proxyNodeInfos { |
| 217 | + proxyNodeInfoMap := map[string]interface{}{} |
| 218 | + |
| 219 | + if proxyNodeInfo.ProxyNodeId != nil { |
| 220 | + proxyNodeInfoMap["proxy_node_id"] = proxyNodeInfo.ProxyNodeId |
| 221 | + } |
| 222 | + |
| 223 | + if proxyNodeInfo.ProxyNodeConnections != nil { |
| 224 | + proxyNodeInfoMap["proxy_node_connections"] = proxyNodeInfo.ProxyNodeConnections |
| 225 | + } |
| 226 | + |
| 227 | + if proxyNodeInfo.Cpu != nil { |
| 228 | + proxyNodeInfoMap["cpu"] = proxyNodeInfo.Cpu |
| 229 | + } |
| 230 | + |
| 231 | + if proxyNodeInfo.Mem != nil { |
| 232 | + proxyNodeInfoMap["mem"] = proxyNodeInfo.Mem |
| 233 | + } |
| 234 | + |
| 235 | + if proxyNodeInfo.Status != nil { |
| 236 | + proxyNodeInfoMap["status"] = proxyNodeInfo.Status |
| 237 | + } |
| 238 | + |
| 239 | + if proxyNodeInfo.ProxyGroupId != nil { |
| 240 | + proxyNodeInfoMap["proxy_group_id"] = proxyNodeInfo.ProxyGroupId |
| 241 | + } |
| 242 | + |
| 243 | + if proxyNodeInfo.ClusterId != nil { |
| 244 | + proxyNodeInfoMap["cluster_id"] = proxyNodeInfo.ClusterId |
| 245 | + } |
| 246 | + |
| 247 | + if proxyNodeInfo.AppId != nil { |
| 248 | + proxyNodeInfoMap["app_id"] = proxyNodeInfo.AppId |
| 249 | + } |
| 250 | + |
| 251 | + if proxyNodeInfo.Region != nil { |
| 252 | + proxyNodeInfoMap["region"] = proxyNodeInfo.Region |
| 253 | + } |
| 254 | + |
| 255 | + if proxyNodeInfo.Zone != nil { |
| 256 | + proxyNodeInfoMap["zone"] = proxyNodeInfo.Zone |
| 257 | + } |
| 258 | + |
| 259 | + ids = append(ids, *proxyNodeInfo.ProxyNodeId) |
| 260 | + tmpList = append(tmpList, proxyNodeInfoMap) |
| 261 | + } |
| 262 | + |
| 263 | + _ = d.Set("proxy_node_infos", tmpList) |
| 264 | + } |
| 265 | + |
| 266 | + d.SetId(helper.DataResourceIdsHash(ids)) |
| 267 | + output, ok := d.GetOk("result_output_file") |
| 268 | + if ok && output.(string) != "" { |
| 269 | + if e := writeToFile(output.(string), tmpList); e != nil { |
| 270 | + return e |
| 271 | + } |
| 272 | + } |
| 273 | + |
| 274 | + return nil |
| 275 | +} |
0 commit comments