|
| 1 | +/* |
| 2 | +Use this data source to query detailed information of clickhouse backup job detail |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +data "tencentcloud_clickhouse_backup_job_detail" "backup_job_detail" { |
| 8 | + instance_id = "cdwch-xxxxxx" |
| 9 | + back_up_job_id = 1234 |
| 10 | +} |
| 11 | +``` |
| 12 | +*/ |
| 13 | +package tencentcloud |
| 14 | + |
| 15 | +import ( |
| 16 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 17 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 18 | + clickhouse "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cdwch/v20200915" |
| 19 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 20 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/ratelimit" |
| 21 | +) |
| 22 | + |
| 23 | +func dataSourceTencentCloudClickhouseBackupJobDetail() *schema.Resource { |
| 24 | + return &schema.Resource{ |
| 25 | + Read: dataSourceTencentCloudClickhouseBackupJobDetailRead, |
| 26 | + Schema: map[string]*schema.Schema{ |
| 27 | + "instance_id": { |
| 28 | + Required: true, |
| 29 | + Type: schema.TypeString, |
| 30 | + Description: "Instance id.", |
| 31 | + }, |
| 32 | + |
| 33 | + "back_up_job_id": { |
| 34 | + Required: true, |
| 35 | + Type: schema.TypeInt, |
| 36 | + Description: "Back up job id.", |
| 37 | + }, |
| 38 | + |
| 39 | + "table_contents": { |
| 40 | + Computed: true, |
| 41 | + Type: schema.TypeList, |
| 42 | + Description: "Back up tables.", |
| 43 | + Elem: &schema.Resource{ |
| 44 | + Schema: map[string]*schema.Schema{ |
| 45 | + "database": { |
| 46 | + Type: schema.TypeString, |
| 47 | + Computed: true, |
| 48 | + Description: "Database.", |
| 49 | + }, |
| 50 | + "table": { |
| 51 | + Type: schema.TypeString, |
| 52 | + Computed: true, |
| 53 | + Description: "Table.", |
| 54 | + }, |
| 55 | + "total_bytes": { |
| 56 | + Type: schema.TypeInt, |
| 57 | + Computed: true, |
| 58 | + Description: "Total bytes.", |
| 59 | + }, |
| 60 | + "v_cluster": { |
| 61 | + Type: schema.TypeString, |
| 62 | + Computed: true, |
| 63 | + Description: "Virtual cluster.", |
| 64 | + }, |
| 65 | + "ips": { |
| 66 | + Type: schema.TypeString, |
| 67 | + Computed: true, |
| 68 | + Description: "Ips.", |
| 69 | + }, |
| 70 | + "zoo_path": { |
| 71 | + Type: schema.TypeString, |
| 72 | + Computed: true, |
| 73 | + Description: "ZK path.", |
| 74 | + }, |
| 75 | + "rip": { |
| 76 | + Type: schema.TypeString, |
| 77 | + Computed: true, |
| 78 | + Description: "Ip address of cvm.", |
| 79 | + }, |
| 80 | + }, |
| 81 | + }, |
| 82 | + }, |
| 83 | + |
| 84 | + "result_output_file": { |
| 85 | + Type: schema.TypeString, |
| 86 | + Optional: true, |
| 87 | + Description: "Used to save results.", |
| 88 | + }, |
| 89 | + }, |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +func dataSourceTencentCloudClickhouseBackupJobDetailRead(d *schema.ResourceData, meta interface{}) error { |
| 94 | + defer logElapsed("data_source.tencentcloud_clickhouse_backup_job_detail.read")() |
| 95 | + defer inconsistentCheck(d, meta)() |
| 96 | + |
| 97 | + var ( |
| 98 | + request = clickhouse.NewDescribeBackUpJobDetailRequest() |
| 99 | + instanceId string |
| 100 | + backUpJobId int |
| 101 | + ) |
| 102 | + if v, ok := d.GetOk("instance_id"); ok { |
| 103 | + instanceId = v.(string) |
| 104 | + request.InstanceId = helper.String(instanceId) |
| 105 | + } |
| 106 | + |
| 107 | + if v, _ := d.GetOk("back_up_job_id"); v != nil { |
| 108 | + backUpJobId = v.(int) |
| 109 | + request.BackUpJobId = helper.IntInt64(backUpJobId) |
| 110 | + } |
| 111 | + |
| 112 | + var tableContents []*clickhouse.BackupTableContent |
| 113 | + |
| 114 | + err := resource.Retry(readRetryTimeout, func() *resource.RetryError { |
| 115 | + ratelimit.Check(request.GetAction()) |
| 116 | + response, e := meta.(*TencentCloudClient).apiV3Conn.UseCdwchClient().DescribeBackUpJobDetail(request) |
| 117 | + if e != nil { |
| 118 | + return retryError(e) |
| 119 | + } |
| 120 | + tableContents = response.Response.TableContents |
| 121 | + return nil |
| 122 | + }) |
| 123 | + if err != nil { |
| 124 | + return err |
| 125 | + } |
| 126 | + |
| 127 | + tmpList := make([]map[string]interface{}, 0, len(tableContents)) |
| 128 | + |
| 129 | + if tableContents != nil { |
| 130 | + for _, backupTableContent := range tableContents { |
| 131 | + backupTableContentMap := map[string]interface{}{} |
| 132 | + |
| 133 | + if backupTableContent.Database != nil { |
| 134 | + backupTableContentMap["database"] = backupTableContent.Database |
| 135 | + } |
| 136 | + |
| 137 | + if backupTableContent.Table != nil { |
| 138 | + backupTableContentMap["table"] = backupTableContent.Table |
| 139 | + } |
| 140 | + |
| 141 | + if backupTableContent.TotalBytes != nil { |
| 142 | + backupTableContentMap["total_bytes"] = backupTableContent.TotalBytes |
| 143 | + } |
| 144 | + |
| 145 | + if backupTableContent.VCluster != nil { |
| 146 | + backupTableContentMap["v_cluster"] = backupTableContent.VCluster |
| 147 | + } |
| 148 | + |
| 149 | + if backupTableContent.Ips != nil { |
| 150 | + backupTableContentMap["ips"] = backupTableContent.Ips |
| 151 | + } |
| 152 | + |
| 153 | + if backupTableContent.ZooPath != nil { |
| 154 | + backupTableContentMap["zoo_path"] = backupTableContent.ZooPath |
| 155 | + } |
| 156 | + |
| 157 | + if backupTableContent.Rip != nil { |
| 158 | + backupTableContentMap["rip"] = backupTableContent.Rip |
| 159 | + } |
| 160 | + |
| 161 | + tmpList = append(tmpList, backupTableContentMap) |
| 162 | + } |
| 163 | + |
| 164 | + _ = d.Set("table_contents", tmpList) |
| 165 | + } |
| 166 | + |
| 167 | + d.SetId(instanceId + helper.IntToStr(backUpJobId)) |
| 168 | + output, ok := d.GetOk("result_output_file") |
| 169 | + if ok && output.(string) != "" { |
| 170 | + if e := writeToFile(output.(string), tmpList); e != nil { |
| 171 | + return e |
| 172 | + } |
| 173 | + } |
| 174 | + return nil |
| 175 | +} |
0 commit comments