|
| 1 | +/* |
| 2 | +Use this data source to query detailed information of clb cluster_resources |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +data "tencentcloud_clb_cluster_resources" "cluster_resources" { |
| 8 | + filters { |
| 9 | + name = "idle" |
| 10 | + values = ["True"] |
| 11 | + } |
| 12 | +} |
| 13 | +``` |
| 14 | +*/ |
| 15 | +package tencentcloud |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + |
| 20 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 21 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 22 | + clb "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/clb/v20180317" |
| 23 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 24 | +) |
| 25 | + |
| 26 | +func dataSourceTencentCloudClbClusterResources() *schema.Resource { |
| 27 | + return &schema.Resource{ |
| 28 | + Read: dataSourceTencentCloudClbClusterResourcesRead, |
| 29 | + Schema: map[string]*schema.Schema{ |
| 30 | + "filters": { |
| 31 | + Optional: true, |
| 32 | + Type: schema.TypeList, |
| 33 | + Description: "Filter conditions to query cluster. cluster-id - String - Required: No - (Filter condition) Filter by cluster ID, such as tgw-12345678. vip - String - Required: No - (Filter condition) Filter by loadbalancer vip, such as 192.168.0.1. loadblancer-id - String - Required: No - (Filter condition) Filter by loadblancer ID, such as lbl-12345678. idle - String - Required: No - (Filter condition) Filter by Whether load balancing is idle, such as True, False.", |
| 34 | + Elem: &schema.Resource{ |
| 35 | + Schema: map[string]*schema.Schema{ |
| 36 | + "name": { |
| 37 | + Type: schema.TypeString, |
| 38 | + Required: true, |
| 39 | + Description: "Filter name.", |
| 40 | + }, |
| 41 | + "values": { |
| 42 | + Type: schema.TypeSet, |
| 43 | + Elem: &schema.Schema{ |
| 44 | + Type: schema.TypeString, |
| 45 | + }, |
| 46 | + Required: true, |
| 47 | + Description: "Filter values.", |
| 48 | + }, |
| 49 | + }, |
| 50 | + }, |
| 51 | + }, |
| 52 | + |
| 53 | + "cluster_resource_set": { |
| 54 | + Computed: true, |
| 55 | + Type: schema.TypeList, |
| 56 | + Description: "Cluster resource set.", |
| 57 | + Elem: &schema.Resource{ |
| 58 | + Schema: map[string]*schema.Schema{ |
| 59 | + "cluster_id": { |
| 60 | + Type: schema.TypeString, |
| 61 | + Computed: true, |
| 62 | + Description: "Cluster ID.", |
| 63 | + }, |
| 64 | + "vip": { |
| 65 | + Type: schema.TypeString, |
| 66 | + Computed: true, |
| 67 | + Description: "vip.", |
| 68 | + }, |
| 69 | + "load_balancer_id": { |
| 70 | + Type: schema.TypeString, |
| 71 | + Computed: true, |
| 72 | + Description: "Loadbalance Id.", |
| 73 | + }, |
| 74 | + "idle": { |
| 75 | + Type: schema.TypeString, |
| 76 | + Computed: true, |
| 77 | + Description: "Is it idle.", |
| 78 | + }, |
| 79 | + "cluster_name": { |
| 80 | + Type: schema.TypeString, |
| 81 | + Computed: true, |
| 82 | + Description: "cluster name.", |
| 83 | + }, |
| 84 | + "isp": { |
| 85 | + Type: schema.TypeString, |
| 86 | + Computed: true, |
| 87 | + Description: "Isp.", |
| 88 | + }, |
| 89 | + "clusters_zone": { |
| 90 | + Type: schema.TypeList, |
| 91 | + Computed: true, |
| 92 | + Description: "clusters zone.", |
| 93 | + Elem: &schema.Resource{ |
| 94 | + Schema: map[string]*schema.Schema{ |
| 95 | + "master_zone": { |
| 96 | + Type: schema.TypeSet, |
| 97 | + Elem: &schema.Schema{ |
| 98 | + Type: schema.TypeString, |
| 99 | + }, |
| 100 | + Computed: true, |
| 101 | + Description: "Availability master zone where the cluster is located.", |
| 102 | + }, |
| 103 | + "slave_zone": { |
| 104 | + Type: schema.TypeSet, |
| 105 | + Elem: &schema.Schema{ |
| 106 | + Type: schema.TypeString, |
| 107 | + }, |
| 108 | + Computed: true, |
| 109 | + Description: "Availability slave zone where the cluster is located.", |
| 110 | + }, |
| 111 | + }, |
| 112 | + }, |
| 113 | + }, |
| 114 | + }, |
| 115 | + }, |
| 116 | + }, |
| 117 | + |
| 118 | + "result_output_file": { |
| 119 | + Type: schema.TypeString, |
| 120 | + Optional: true, |
| 121 | + Description: "Used to save results.", |
| 122 | + }, |
| 123 | + }, |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +func dataSourceTencentCloudClbClusterResourcesRead(d *schema.ResourceData, meta interface{}) error { |
| 128 | + defer logElapsed("data_source.tencentcloud_clb_cluster_resources.read")() |
| 129 | + defer inconsistentCheck(d, meta)() |
| 130 | + |
| 131 | + logId := getLogId(contextNil) |
| 132 | + |
| 133 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 134 | + |
| 135 | + paramMap := make(map[string]interface{}) |
| 136 | + if v, ok := d.GetOk("filters"); ok { |
| 137 | + filtersSet := v.([]interface{}) |
| 138 | + tmpSet := make([]*clb.Filter, 0, len(filtersSet)) |
| 139 | + |
| 140 | + for _, item := range filtersSet { |
| 141 | + filter := clb.Filter{} |
| 142 | + filterMap := item.(map[string]interface{}) |
| 143 | + |
| 144 | + if v, ok := filterMap["name"]; ok { |
| 145 | + filter.Name = helper.String(v.(string)) |
| 146 | + } |
| 147 | + if v, ok := filterMap["values"]; ok { |
| 148 | + valuesSet := v.(*schema.Set).List() |
| 149 | + filter.Values = helper.InterfacesStringsPoint(valuesSet) |
| 150 | + } |
| 151 | + tmpSet = append(tmpSet, &filter) |
| 152 | + } |
| 153 | + paramMap["Filters"] = tmpSet |
| 154 | + } |
| 155 | + |
| 156 | + service := ClbService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 157 | + |
| 158 | + var clusterResourceSet []*clb.ClusterResource |
| 159 | + |
| 160 | + err := resource.Retry(readRetryTimeout, func() *resource.RetryError { |
| 161 | + result, e := service.DescribeClbClusterResourcesByFilter(ctx, paramMap) |
| 162 | + if e != nil { |
| 163 | + return retryError(e) |
| 164 | + } |
| 165 | + clusterResourceSet = result |
| 166 | + return nil |
| 167 | + }) |
| 168 | + if err != nil { |
| 169 | + return err |
| 170 | + } |
| 171 | + |
| 172 | + ids := make([]string, 0, len(clusterResourceSet)) |
| 173 | + tmpList := make([]map[string]interface{}, 0, len(clusterResourceSet)) |
| 174 | + |
| 175 | + if clusterResourceSet != nil { |
| 176 | + for _, clusterResource := range clusterResourceSet { |
| 177 | + clusterResourceMap := map[string]interface{}{} |
| 178 | + |
| 179 | + if clusterResource.ClusterId != nil { |
| 180 | + clusterResourceMap["cluster_id"] = clusterResource.ClusterId |
| 181 | + } |
| 182 | + |
| 183 | + if clusterResource.Vip != nil { |
| 184 | + clusterResourceMap["vip"] = clusterResource.Vip |
| 185 | + } |
| 186 | + |
| 187 | + if clusterResource.LoadBalancerId != nil { |
| 188 | + clusterResourceMap["load_balancer_id"] = clusterResource.LoadBalancerId |
| 189 | + } |
| 190 | + |
| 191 | + if clusterResource.Idle != nil { |
| 192 | + clusterResourceMap["idle"] = clusterResource.Idle |
| 193 | + } |
| 194 | + |
| 195 | + if clusterResource.ClusterName != nil { |
| 196 | + clusterResourceMap["cluster_name"] = clusterResource.ClusterName |
| 197 | + } |
| 198 | + |
| 199 | + if clusterResource.Isp != nil { |
| 200 | + clusterResourceMap["isp"] = clusterResource.Isp |
| 201 | + } |
| 202 | + |
| 203 | + if clusterResource.ClustersZone != nil { |
| 204 | + clustersZoneMap := map[string]interface{}{} |
| 205 | + |
| 206 | + if clusterResource.ClustersZone.MasterZone != nil { |
| 207 | + clustersZoneMap["master_zone"] = clusterResource.ClustersZone.MasterZone |
| 208 | + } |
| 209 | + |
| 210 | + if clusterResource.ClustersZone.SlaveZone != nil { |
| 211 | + clustersZoneMap["slave_zone"] = clusterResource.ClustersZone.SlaveZone |
| 212 | + } |
| 213 | + |
| 214 | + clusterResourceMap["clusters_zone"] = []interface{}{clustersZoneMap} |
| 215 | + } |
| 216 | + |
| 217 | + ids = append(ids, *clusterResource.ClusterId) |
| 218 | + tmpList = append(tmpList, clusterResourceMap) |
| 219 | + } |
| 220 | + |
| 221 | + _ = d.Set("cluster_resource_set", tmpList) |
| 222 | + } |
| 223 | + |
| 224 | + d.SetId(helper.DataResourceIdsHash(ids)) |
| 225 | + output, ok := d.GetOk("result_output_file") |
| 226 | + if ok && output.(string) != "" { |
| 227 | + if e := writeToFile(output.(string), tmpList); e != nil { |
| 228 | + return e |
| 229 | + } |
| 230 | + } |
| 231 | + return nil |
| 232 | +} |
0 commit comments