|
| 1 | +/* |
| 2 | +Use this data source to query detailed information of CBS storages in parallel. |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +data "tencentcloud_cbs_storages_set" "storages" { |
| 8 | + availability_zone = "ap-guangzhou-3" |
| 9 | +} |
| 10 | +``` |
| 11 | +
|
| 12 | +*/ |
| 13 | +package tencentcloud |
| 14 | + |
| 15 | +import ( |
| 16 | + "context" |
| 17 | + "fmt" |
| 18 | + "log" |
| 19 | + |
| 20 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 21 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 22 | +) |
| 23 | + |
| 24 | +func dataSourceTencentCloudCbsStoragesSet() *schema.Resource { |
| 25 | + return &schema.Resource{ |
| 26 | + Read: dataSourceTencentCloudCbsStoragesSetRead, |
| 27 | + |
| 28 | + Schema: map[string]*schema.Schema{ |
| 29 | + "storage_id": { |
| 30 | + Type: schema.TypeString, |
| 31 | + Optional: true, |
| 32 | + Description: "ID of the CBS to be queried.", |
| 33 | + }, |
| 34 | + "storage_name": { |
| 35 | + Type: schema.TypeString, |
| 36 | + Optional: true, |
| 37 | + Description: "Name of the CBS to be queried.", |
| 38 | + }, |
| 39 | + "availability_zone": { |
| 40 | + Type: schema.TypeString, |
| 41 | + Optional: true, |
| 42 | + Description: "The available zone that the CBS instance locates at.", |
| 43 | + }, |
| 44 | + "project_id": { |
| 45 | + Type: schema.TypeInt, |
| 46 | + Optional: true, |
| 47 | + Description: "ID of the project with which the CBS is associated.", |
| 48 | + }, |
| 49 | + "storage_type": { |
| 50 | + Type: schema.TypeString, |
| 51 | + Optional: true, |
| 52 | + ValidateFunc: validateAllowedStringValue(CBS_STORAGE_TYPE), |
| 53 | + Description: "Filter by cloud disk media type (`CLOUD_BASIC`: HDD cloud disk | `CLOUD_PREMIUM`: Premium Cloud Storage | `CLOUD_SSD`: SSD cloud disk).", |
| 54 | + }, |
| 55 | + "storage_usage": { |
| 56 | + Type: schema.TypeString, |
| 57 | + Optional: true, |
| 58 | + Description: "Filter by cloud disk type (`SYSTEM_DISK`: system disk | `DATA_DISK`: data disk).", |
| 59 | + }, |
| 60 | + "charge_type": { |
| 61 | + Type: schema.TypeList, |
| 62 | + Optional: true, |
| 63 | + Description: "List filter by disk charge type (`POSTPAID_BY_HOUR` | `PREPAID`).", |
| 64 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 65 | + }, |
| 66 | + "portable": { |
| 67 | + Type: schema.TypeBool, |
| 68 | + Optional: true, |
| 69 | + Description: "Filter by whether the disk is portable (Boolean `true` or `false`).", |
| 70 | + }, |
| 71 | + "storage_state": { |
| 72 | + Type: schema.TypeList, |
| 73 | + Optional: true, |
| 74 | + Description: "List filter by disk state (`UNATTACHED` | `ATTACHING` | `ATTACHED` | `DETACHING` | `EXPANDING` | `ROLLBACKING` | `TORECYCLE`).", |
| 75 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 76 | + }, |
| 77 | + "instance_ips": { |
| 78 | + Type: schema.TypeList, |
| 79 | + Optional: true, |
| 80 | + Description: "List filter by attached instance public or private IPs.", |
| 81 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 82 | + }, |
| 83 | + "instance_name": { |
| 84 | + Type: schema.TypeList, |
| 85 | + Optional: true, |
| 86 | + Description: "List filter by attached instance name.", |
| 87 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 88 | + }, |
| 89 | + "tag_keys": { |
| 90 | + Type: schema.TypeList, |
| 91 | + Optional: true, |
| 92 | + Description: "List filter by tag keys.", |
| 93 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 94 | + }, |
| 95 | + "tag_values": { |
| 96 | + Type: schema.TypeList, |
| 97 | + Optional: true, |
| 98 | + Description: "List filter by tag values.", |
| 99 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 100 | + }, |
| 101 | + "result_output_file": { |
| 102 | + Type: schema.TypeString, |
| 103 | + Optional: true, |
| 104 | + Description: "Used to save results.", |
| 105 | + }, |
| 106 | + "storage_list": { |
| 107 | + Type: schema.TypeList, |
| 108 | + Computed: true, |
| 109 | + Description: "A list of storage. Each element contains the following attributes:", |
| 110 | + Elem: &schema.Resource{ |
| 111 | + Schema: map[string]*schema.Schema{ |
| 112 | + "storage_id": { |
| 113 | + Type: schema.TypeString, |
| 114 | + Computed: true, |
| 115 | + Description: "ID of CBS.", |
| 116 | + }, |
| 117 | + "storage_name": { |
| 118 | + Type: schema.TypeString, |
| 119 | + Computed: true, |
| 120 | + Description: "Name of CBS.", |
| 121 | + }, |
| 122 | + "storage_type": { |
| 123 | + Type: schema.TypeString, |
| 124 | + Computed: true, |
| 125 | + Description: "Types of storage medium.", |
| 126 | + }, |
| 127 | + "storage_usage": { |
| 128 | + Type: schema.TypeString, |
| 129 | + Computed: true, |
| 130 | + Description: "Types of CBS.", |
| 131 | + }, |
| 132 | + "availability_zone": { |
| 133 | + Type: schema.TypeString, |
| 134 | + Computed: true, |
| 135 | + Description: "The zone of CBS.", |
| 136 | + }, |
| 137 | + "project_id": { |
| 138 | + Type: schema.TypeInt, |
| 139 | + Computed: true, |
| 140 | + Description: "ID of the project.", |
| 141 | + }, |
| 142 | + "storage_size": { |
| 143 | + Type: schema.TypeInt, |
| 144 | + Computed: true, |
| 145 | + Description: "Volume of CBS.", |
| 146 | + }, |
| 147 | + "attached": { |
| 148 | + Type: schema.TypeBool, |
| 149 | + Computed: true, |
| 150 | + Description: "Indicates whether the CBS is mounted the CVM.", |
| 151 | + }, |
| 152 | + "instance_id": { |
| 153 | + Type: schema.TypeString, |
| 154 | + Computed: true, |
| 155 | + Description: "ID of the CVM instance that be mounted by this CBS.", |
| 156 | + }, |
| 157 | + "encrypt": { |
| 158 | + Type: schema.TypeBool, |
| 159 | + Computed: true, |
| 160 | + Description: "Indicates whether CBS is encrypted.", |
| 161 | + }, |
| 162 | + "create_time": { |
| 163 | + Type: schema.TypeString, |
| 164 | + Computed: true, |
| 165 | + Description: "Creation time of CBS.", |
| 166 | + }, |
| 167 | + "status": { |
| 168 | + Type: schema.TypeString, |
| 169 | + Computed: true, |
| 170 | + Description: "Status of CBS.", |
| 171 | + }, |
| 172 | + "tags": { |
| 173 | + Type: schema.TypeMap, |
| 174 | + Computed: true, |
| 175 | + Description: "The available tags within this CBS.", |
| 176 | + }, |
| 177 | + "prepaid_renew_flag": { |
| 178 | + Type: schema.TypeString, |
| 179 | + Computed: true, |
| 180 | + Description: "The way that CBS instance will be renew automatically or not when it reach the end of the prepaid tenancy.", |
| 181 | + }, |
| 182 | + "charge_type": { |
| 183 | + Type: schema.TypeString, |
| 184 | + Computed: true, |
| 185 | + Description: "Pay type of the CBS instance.", |
| 186 | + }, |
| 187 | + "throughput_performance": { |
| 188 | + Type: schema.TypeInt, |
| 189 | + Computed: true, |
| 190 | + Description: "Add extra performance to the data disk. Only works when disk type is `CLOUD_TSSD` or `CLOUD_HSSD`.", |
| 191 | + }, |
| 192 | + }, |
| 193 | + }, |
| 194 | + }, |
| 195 | + }, |
| 196 | + } |
| 197 | +} |
| 198 | + |
| 199 | +func dataSourceTencentCloudCbsStoragesSetRead(d *schema.ResourceData, meta interface{}) error { |
| 200 | + defer logElapsed("data_source.tencentcloud_cbs_storages.read")() |
| 201 | + |
| 202 | + logId := getLogId(contextNil) |
| 203 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 204 | + |
| 205 | + params := make(map[string]interface{}) |
| 206 | + if v, ok := d.GetOk("storage_id"); ok { |
| 207 | + params["disk-id"] = v.(string) |
| 208 | + } |
| 209 | + if v, ok := d.GetOk("storage_name"); ok { |
| 210 | + params["disk-name"] = v.(string) |
| 211 | + } |
| 212 | + if v, ok := d.GetOk("availability_zone"); ok { |
| 213 | + params["zone"] = v.(string) |
| 214 | + } |
| 215 | + if v, ok := d.GetOkExists("project_id"); ok { |
| 216 | + params["project-id"] = fmt.Sprintf("%d", v.(int)) |
| 217 | + } |
| 218 | + if v, ok := d.GetOk("storage_type"); ok { |
| 219 | + params["disk-type"] = v.(string) |
| 220 | + } |
| 221 | + if v, ok := d.GetOk("storage_usage"); ok { |
| 222 | + params["disk-usage"] = v.(string) |
| 223 | + } |
| 224 | + |
| 225 | + if v, ok := d.GetOk("charge_type"); ok { |
| 226 | + params["disk-charge-type"] = helper.InterfacesStringsPoint(v.([]interface{})) |
| 227 | + } |
| 228 | + |
| 229 | + if v, ok := d.GetOk("portable"); ok { |
| 230 | + if v.(bool) { |
| 231 | + params["portable"] = "TRUE" |
| 232 | + } else { |
| 233 | + params["portable"] = "FALSE" |
| 234 | + } |
| 235 | + } |
| 236 | + |
| 237 | + if v, ok := d.GetOk("storage_state"); ok { |
| 238 | + params["disk-state"] = helper.InterfacesStringsPoint(v.([]interface{})) |
| 239 | + } |
| 240 | + if v, ok := d.GetOk("instance_ips"); ok { |
| 241 | + params["instance-ip-address"] = helper.InterfacesStringsPoint(v.([]interface{})) |
| 242 | + } |
| 243 | + if v, ok := d.GetOk("instance_name"); ok { |
| 244 | + params["instance-name"] = helper.InterfacesStringsPoint(v.([]interface{})) |
| 245 | + } |
| 246 | + if v, ok := d.GetOk("tag_keys"); ok { |
| 247 | + params["tag-key"] = helper.InterfacesStringsPoint(v.([]interface{})) |
| 248 | + } |
| 249 | + if v, ok := d.GetOk("tag_values"); ok { |
| 250 | + params["tag-value"] = helper.InterfacesStringsPoint(v.([]interface{})) |
| 251 | + } |
| 252 | + |
| 253 | + cbsService := CbsService{ |
| 254 | + client: meta.(*TencentCloudClient).apiV3Conn, |
| 255 | + } |
| 256 | + |
| 257 | + storages, e := cbsService.DescribeDisksInParallelByFilter(ctx, params) |
| 258 | + if e != nil { |
| 259 | + return e |
| 260 | + } |
| 261 | + ids := make([]string, 0, len(storages)) |
| 262 | + storageList := make([]map[string]interface{}, 0, len(storages)) |
| 263 | + for _, storage := range storages { |
| 264 | + mapping := map[string]interface{}{ |
| 265 | + "storage_id": storage.DiskId, |
| 266 | + "storage_name": storage.DiskName, |
| 267 | + "storage_usage": storage.DiskUsage, |
| 268 | + "storage_type": storage.DiskType, |
| 269 | + "availability_zone": storage.Placement.Zone, |
| 270 | + "project_id": storage.Placement.ProjectId, |
| 271 | + "storage_size": storage.DiskSize, |
| 272 | + "attached": storage.Attached, |
| 273 | + "instance_id": storage.InstanceId, |
| 274 | + "encrypt": storage.Encrypt, |
| 275 | + "create_time": storage.CreateTime, |
| 276 | + "status": storage.DiskState, |
| 277 | + "prepaid_renew_flag": storage.RenewFlag, |
| 278 | + "charge_type": storage.DiskChargeType, |
| 279 | + "throughput_performance": storage.ThroughputPerformance, |
| 280 | + } |
| 281 | + if storage.Tags != nil { |
| 282 | + tags := make(map[string]interface{}, len(storage.Tags)) |
| 283 | + for _, t := range storage.Tags { |
| 284 | + tags[*t.Key] = *t.Value |
| 285 | + } |
| 286 | + mapping["tags"] = tags |
| 287 | + } |
| 288 | + storageList = append(storageList, mapping) |
| 289 | + ids = append(ids, *storage.DiskId) |
| 290 | + } |
| 291 | + |
| 292 | + d.SetId(helper.DataResourceIdsHash(ids)) |
| 293 | + if e = d.Set("storage_list", storageList); e != nil { |
| 294 | + log.Printf("[CRITAL]%s provider set storage list fail, reason:%s\n ", logId, e.Error()) |
| 295 | + return e |
| 296 | + } |
| 297 | + |
| 298 | + output, ok := d.GetOk("result_output_file") |
| 299 | + if ok && output.(string) != "" { |
| 300 | + if e := writeToFile(output.(string), storageList); e != nil { |
| 301 | + return e |
| 302 | + } |
| 303 | + } |
| 304 | + |
| 305 | + return nil |
| 306 | + |
| 307 | +} |
0 commit comments