|
| 1 | +/* |
| 2 | +Use this data source to query detailed information of dbbrain db_space_status |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +data "tencentcloud_dbbrain_db_space_status" "db_space_status" { |
| 8 | + instance_id = "%s" |
| 9 | + range_days = 7 |
| 10 | + product = "mysql" |
| 11 | +} |
| 12 | +``` |
| 13 | +*/ |
| 14 | +package tencentcloud |
| 15 | + |
| 16 | +import ( |
| 17 | + "context" |
| 18 | + |
| 19 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 20 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 21 | + dbbrain "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dbbrain/v20210527" |
| 22 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 23 | +) |
| 24 | + |
| 25 | +func dataSourceTencentCloudDbbrainDbSpaceStatus() *schema.Resource { |
| 26 | + return &schema.Resource{ |
| 27 | + Read: dataSourceTencentCloudDbbrainDbSpaceStatusRead, |
| 28 | + Schema: map[string]*schema.Schema{ |
| 29 | + "instance_id": { |
| 30 | + Required: true, |
| 31 | + Type: schema.TypeString, |
| 32 | + Description: "instance id.", |
| 33 | + }, |
| 34 | + |
| 35 | + "range_days": { |
| 36 | + Optional: true, |
| 37 | + Type: schema.TypeInt, |
| 38 | + Description: "The number of days in the time period, the deadline is the current day, and the default is 7 days.", |
| 39 | + }, |
| 40 | + |
| 41 | + "product": { |
| 42 | + Optional: true, |
| 43 | + Type: schema.TypeString, |
| 44 | + Description: "Service product type, supported values include: mysql - cloud database MySQL, cynosdb - cloud database CynosDB for MySQL, the default is mysql.", |
| 45 | + }, |
| 46 | + |
| 47 | + "growth": { |
| 48 | + Computed: true, |
| 49 | + Type: schema.TypeInt, |
| 50 | + Description: "Disk growth (MB).", |
| 51 | + }, |
| 52 | + |
| 53 | + "remain": { |
| 54 | + Computed: true, |
| 55 | + Type: schema.TypeInt, |
| 56 | + Description: "Disk remaining (MB).", |
| 57 | + }, |
| 58 | + |
| 59 | + "total": { |
| 60 | + Computed: true, |
| 61 | + Type: schema.TypeInt, |
| 62 | + Description: "Total disk size (MB).", |
| 63 | + }, |
| 64 | + |
| 65 | + "available_days": { |
| 66 | + Computed: true, |
| 67 | + Type: schema.TypeInt, |
| 68 | + Description: "Estimated number of days available.", |
| 69 | + }, |
| 70 | + |
| 71 | + "result_output_file": { |
| 72 | + Type: schema.TypeString, |
| 73 | + Optional: true, |
| 74 | + Description: "Used to save results.", |
| 75 | + }, |
| 76 | + }, |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +func dataSourceTencentCloudDbbrainDbSpaceStatusRead(d *schema.ResourceData, meta interface{}) error { |
| 81 | + defer logElapsed("data_source.tencentcloud_dbbrain_db_space_status.read")() |
| 82 | + defer inconsistentCheck(d, meta)() |
| 83 | + |
| 84 | + logId := getLogId(contextNil) |
| 85 | + |
| 86 | + var instanceId string |
| 87 | + |
| 88 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 89 | + |
| 90 | + paramMap := make(map[string]interface{}) |
| 91 | + if v, ok := d.GetOk("instance_id"); ok { |
| 92 | + paramMap["InstanceId"] = helper.String(v.(string)) |
| 93 | + instanceId = v.(string) |
| 94 | + } |
| 95 | + |
| 96 | + if v, _ := d.GetOk("range_days"); v != nil { |
| 97 | + paramMap["RangeDays"] = helper.IntInt64(v.(int)) |
| 98 | + } |
| 99 | + |
| 100 | + if v, ok := d.GetOk("product"); ok { |
| 101 | + paramMap["Product"] = helper.String(v.(string)) |
| 102 | + } |
| 103 | + |
| 104 | + service := DbbrainService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 105 | + |
| 106 | + var rows *dbbrain.DescribeDBSpaceStatusResponseParams |
| 107 | + |
| 108 | + err := resource.Retry(readRetryTimeout, func() *resource.RetryError { |
| 109 | + result, e := service.DescribeDbbrainDbSpaceStatusByFilter(ctx, paramMap) |
| 110 | + if e != nil { |
| 111 | + return retryError(e) |
| 112 | + } |
| 113 | + rows = result |
| 114 | + return nil |
| 115 | + }) |
| 116 | + if err != nil { |
| 117 | + return err |
| 118 | + } |
| 119 | + |
| 120 | + tmpList := []map[string]interface{}{} |
| 121 | + |
| 122 | + if rows != nil { |
| 123 | + |
| 124 | + if rows.Growth != nil { |
| 125 | + _ = d.Set("growth", rows.Growth) |
| 126 | + } |
| 127 | + |
| 128 | + if rows.Remain != nil { |
| 129 | + _ = d.Set("remain", rows.Remain) |
| 130 | + } |
| 131 | + |
| 132 | + if rows.Total != nil { |
| 133 | + _ = d.Set("total", rows.Total) |
| 134 | + } |
| 135 | + |
| 136 | + if rows.AvailableDays != nil { |
| 137 | + _ = d.Set("available_days", rows.AvailableDays) |
| 138 | + } |
| 139 | + tmpList = append(tmpList, map[string]interface{}{ |
| 140 | + "growth": rows.Growth, |
| 141 | + "remain": rows.Remain, |
| 142 | + "total": rows.Total, |
| 143 | + "available_days": rows.AvailableDays, |
| 144 | + }) |
| 145 | + |
| 146 | + } |
| 147 | + |
| 148 | + d.SetId(helper.DataResourceIdHash(instanceId)) |
| 149 | + output, ok := d.GetOk("result_output_file") |
| 150 | + if ok && output.(string) != "" { |
| 151 | + if e := writeToFile(output.(string), tmpList); e != nil { |
| 152 | + return e |
| 153 | + } |
| 154 | + } |
| 155 | + return nil |
| 156 | +} |
0 commit comments