|
| 1 | +/* |
| 2 | +Use this data source to query CDH instances. |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +data "tencentcloud_cdh_instances" "list" { |
| 8 | + availability_zone = "ap-guangzhou-3" |
| 9 | + host_id = "host-d6s7i5q4" |
| 10 | + host_name = "test" |
| 11 | + host_state = "RUNNING" |
| 12 | + project_id = 1154137 |
| 13 | +} |
| 14 | +``` |
| 15 | +*/ |
| 16 | +package tencentcloud |
| 17 | + |
| 18 | +import ( |
| 19 | + "context" |
| 20 | + "log" |
| 21 | + "strconv" |
| 22 | + |
| 23 | + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" |
| 24 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 25 | + cvm "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm/v20170312" |
| 26 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 27 | +) |
| 28 | + |
| 29 | +func dataSourceTencentCloudCdhInstances() *schema.Resource { |
| 30 | + return &schema.Resource{ |
| 31 | + Read: dataSourceTencentCloudCdhInstancesRead, |
| 32 | + |
| 33 | + Schema: map[string]*schema.Schema{ |
| 34 | + "host_id": { |
| 35 | + Type: schema.TypeString, |
| 36 | + Optional: true, |
| 37 | + Description: "ID of the CDH instances to be queried.", |
| 38 | + }, |
| 39 | + "host_name": { |
| 40 | + Type: schema.TypeString, |
| 41 | + Optional: true, |
| 42 | + Description: "Name of the CDH instances to be queried.", |
| 43 | + }, |
| 44 | + "host_state": { |
| 45 | + Type: schema.TypeString, |
| 46 | + Optional: true, |
| 47 | + Description: "State of the CDH instances to be queried. Valid values: `PENDING`, `LAUNCH_FAILURE`, `RUNNING`, `EXPIRED`.", |
| 48 | + }, |
| 49 | + "availability_zone": { |
| 50 | + Type: schema.TypeString, |
| 51 | + Optional: true, |
| 52 | + Description: "The available zone that the CDH instance locates at.", |
| 53 | + }, |
| 54 | + "project_id": { |
| 55 | + Type: schema.TypeInt, |
| 56 | + Optional: true, |
| 57 | + Description: "The project CDH belongs to.", |
| 58 | + }, |
| 59 | + "result_output_file": { |
| 60 | + Type: schema.TypeString, |
| 61 | + Optional: true, |
| 62 | + Description: "Used to save results.", |
| 63 | + }, |
| 64 | + |
| 65 | + // computed |
| 66 | + "cdh_instance_list": { |
| 67 | + Type: schema.TypeList, |
| 68 | + Computed: true, |
| 69 | + Description: "An information list of cdh instance. Each element contains the following attributes:", |
| 70 | + Elem: &schema.Resource{ |
| 71 | + Schema: map[string]*schema.Schema{ |
| 72 | + "host_id": { |
| 73 | + Type: schema.TypeString, |
| 74 | + Computed: true, |
| 75 | + Description: "ID of the CDH instance.", |
| 76 | + }, |
| 77 | + "host_name": { |
| 78 | + Type: schema.TypeString, |
| 79 | + Computed: true, |
| 80 | + Description: "Name of the CDH instance.", |
| 81 | + }, |
| 82 | + "host_type": { |
| 83 | + Type: schema.TypeString, |
| 84 | + Computed: true, |
| 85 | + Description: "Type of the CDH instance.", |
| 86 | + }, |
| 87 | + "host_state": { |
| 88 | + Type: schema.TypeString, |
| 89 | + Computed: true, |
| 90 | + Description: "State of the CDH instance.", |
| 91 | + }, |
| 92 | + "charge_type": { |
| 93 | + Type: schema.TypeString, |
| 94 | + Computed: true, |
| 95 | + Description: "The charge type of the CDH instance.", |
| 96 | + }, |
| 97 | + "availability_zone": { |
| 98 | + Type: schema.TypeString, |
| 99 | + Computed: true, |
| 100 | + Description: "The available zone that the CDH instance locates at.", |
| 101 | + }, |
| 102 | + "project_id": { |
| 103 | + Type: schema.TypeInt, |
| 104 | + Computed: true, |
| 105 | + Description: "The project CDH belongs to.", |
| 106 | + }, |
| 107 | + "prepaid_renew_flag": { |
| 108 | + Type: schema.TypeString, |
| 109 | + Computed: true, |
| 110 | + Description: "Auto renewal flag.", |
| 111 | + }, |
| 112 | + "create_time": { |
| 113 | + Type: schema.TypeString, |
| 114 | + Computed: true, |
| 115 | + Description: "Creation time of the CDH instance.", |
| 116 | + }, |
| 117 | + "expired_time": { |
| 118 | + Type: schema.TypeString, |
| 119 | + Computed: true, |
| 120 | + Description: "Expired time of the CDH instance.", |
| 121 | + }, |
| 122 | + "cvm_instance_ids": { |
| 123 | + Type: schema.TypeList, |
| 124 | + Computed: true, |
| 125 | + Description: "Id of CVM instances that have been created on the CDH instance.", |
| 126 | + Elem: &schema.Schema{ |
| 127 | + Type: schema.TypeString, |
| 128 | + }, |
| 129 | + }, |
| 130 | + "cage_id": { |
| 131 | + Type: schema.TypeString, |
| 132 | + Computed: true, |
| 133 | + Description: "Cage ID of the CDH instance. This parameter is only valid for CDH instances in the cages of finance availability zones.", |
| 134 | + }, |
| 135 | + "host_resource": { |
| 136 | + Type: schema.TypeList, |
| 137 | + Computed: true, |
| 138 | + Description: "An information list of host resource. Each element contains the following attributes:", |
| 139 | + Elem: &schema.Resource{ |
| 140 | + Schema: map[string]*schema.Schema{ |
| 141 | + "cpu_total_num": { |
| 142 | + Type: schema.TypeInt, |
| 143 | + Computed: true, |
| 144 | + Description: "The number of total CPU cores of the instance.", |
| 145 | + }, |
| 146 | + "cpu_available_num": { |
| 147 | + Type: schema.TypeInt, |
| 148 | + Computed: true, |
| 149 | + Description: "The number of available CPU cores of the instance.", |
| 150 | + }, |
| 151 | + "memory_total_size": { |
| 152 | + Type: schema.TypeFloat, |
| 153 | + Computed: true, |
| 154 | + Description: "Instance memory total capacity, unit in GB.", |
| 155 | + }, |
| 156 | + "memory_available_size": { |
| 157 | + Type: schema.TypeFloat, |
| 158 | + Computed: true, |
| 159 | + Description: "Instance memory available capacity, unit in GB.", |
| 160 | + }, |
| 161 | + "disk_total_size": { |
| 162 | + Type: schema.TypeInt, |
| 163 | + Computed: true, |
| 164 | + Description: "Instance disk total capacity, unit in GB.", |
| 165 | + }, |
| 166 | + "disk_available_size": { |
| 167 | + Type: schema.TypeInt, |
| 168 | + Computed: true, |
| 169 | + Description: "Instance disk available capacity, unit in GB.", |
| 170 | + }, |
| 171 | + "disk_type": { |
| 172 | + Type: schema.TypeString, |
| 173 | + Computed: true, |
| 174 | + Description: "Type of the disk.", |
| 175 | + }, |
| 176 | + }, |
| 177 | + }, |
| 178 | + }, |
| 179 | + }, |
| 180 | + }, |
| 181 | + }, |
| 182 | + }, |
| 183 | + } |
| 184 | +} |
| 185 | + |
| 186 | +func dataSourceTencentCloudCdhInstancesRead(d *schema.ResourceData, meta interface{}) error { |
| 187 | + defer logElapsed("data_source.tencentcloud_cdh_instances.read")() |
| 188 | + logId := getLogId(contextNil) |
| 189 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 190 | + cdhService := CdhService{ |
| 191 | + client: meta.(*TencentCloudClient).apiV3Conn, |
| 192 | + } |
| 193 | + |
| 194 | + filter := make(map[string]string) |
| 195 | + if v, ok := d.GetOk("host_id"); ok { |
| 196 | + filter["host-id"] = v.(string) |
| 197 | + } |
| 198 | + if v, ok := d.GetOk("host_name"); ok { |
| 199 | + filter["host-name"] = v.(string) |
| 200 | + } |
| 201 | + if v, ok := d.GetOk("host_state"); ok { |
| 202 | + filter["host-state"] = v.(string) |
| 203 | + } |
| 204 | + if v, ok := d.GetOk("availability_zone"); ok { |
| 205 | + filter["zone"] = v.(string) |
| 206 | + } |
| 207 | + if v, ok := d.GetOk("project_id"); ok { |
| 208 | + filter["project-id"] = strconv.FormatInt(int64(v.(int)), 10) |
| 209 | + } |
| 210 | + |
| 211 | + var instances []*cvm.HostItem |
| 212 | + var errRet error |
| 213 | + err := resource.Retry(readRetryTimeout, func() *resource.RetryError { |
| 214 | + instances, errRet = cdhService.DescribeCdhInstanceByFilter(ctx, filter) |
| 215 | + if errRet != nil { |
| 216 | + return retryError(errRet) |
| 217 | + } |
| 218 | + return nil |
| 219 | + }) |
| 220 | + if err != nil { |
| 221 | + return err |
| 222 | + } |
| 223 | + |
| 224 | + instanceList := make([]map[string]interface{}, 0, len(instances)) |
| 225 | + ids := make([]string, 0, len(instances)) |
| 226 | + for _, instance := range instances { |
| 227 | + mapping := map[string]interface{}{ |
| 228 | + "host_id": instance.HostId, |
| 229 | + "host_name": instance.HostName, |
| 230 | + "host_type": instance.HostType, |
| 231 | + "host_state": instance.HostState, |
| 232 | + "charge_type": instance.HostChargeType, |
| 233 | + "availability_zone": instance.Placement.Zone, |
| 234 | + "project_id": instance.Placement.ProjectId, |
| 235 | + "prepaid_renew_flag": instance.RenewFlag, |
| 236 | + "create_time": instance.CreatedTime, |
| 237 | + "expired_time": instance.ExpiredTime, |
| 238 | + "cvm_instance_ids": helper.StringsInterfaces(instance.InstanceIds), |
| 239 | + "cage_id": instance.CageId, |
| 240 | + } |
| 241 | + hostResource := map[string]interface{}{ |
| 242 | + "cpu_total_num": instance.HostResource.CpuTotal, |
| 243 | + "cpu_available_num": instance.HostResource.CpuAvailable, |
| 244 | + "memory_total_size": instance.HostResource.MemTotal, |
| 245 | + "memory_available_size": instance.HostResource.MemAvailable, |
| 246 | + "disk_total_size": instance.HostResource.DiskTotal, |
| 247 | + "disk_available_size": instance.HostResource.DiskAvailable, |
| 248 | + "disk_type": instance.HostResource.DiskType, |
| 249 | + } |
| 250 | + mapping["host_resource"] = []map[string]interface{}{hostResource} |
| 251 | + instanceList = append(instanceList, mapping) |
| 252 | + ids = append(ids, *instance.HostId) |
| 253 | + } |
| 254 | + |
| 255 | + d.SetId(helper.DataResourceIdsHash(ids)) |
| 256 | + err = d.Set("cdh_instance_list", instanceList) |
| 257 | + if err != nil { |
| 258 | + log.Printf("[CRITAL]%s provider set cdh instance list fail, reason:%s\n ", logId, err.Error()) |
| 259 | + return err |
| 260 | + } |
| 261 | + |
| 262 | + output, ok := d.GetOk("result_output_file") |
| 263 | + if ok && output.(string) != "" { |
| 264 | + if err := writeToFile(output.(string), instanceList); err != nil { |
| 265 | + return err |
| 266 | + } |
| 267 | + } |
| 268 | + return nil |
| 269 | +} |
0 commit comments