|
| 1 | +package wedata |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 7 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 8 | + wedatav20250806 "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/wedata/v20250806" |
| 9 | + |
| 10 | + tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common" |
| 11 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 12 | +) |
| 13 | + |
| 14 | +func DataSourceTencentCloudWedataGetTable() *schema.Resource { |
| 15 | + return &schema.Resource{ |
| 16 | + Read: dataSourceTencentCloudWedataGetTableRead, |
| 17 | + Schema: map[string]*schema.Schema{ |
| 18 | + "table_guid": { |
| 19 | + Type: schema.TypeString, |
| 20 | + Required: true, |
| 21 | + Description: "Table GUID.", |
| 22 | + }, |
| 23 | + |
| 24 | + "data": { |
| 25 | + Type: schema.TypeList, |
| 26 | + Computed: true, |
| 27 | + Description: "Data table details.", |
| 28 | + Elem: &schema.Resource{ |
| 29 | + Schema: map[string]*schema.Schema{ |
| 30 | + "guid": { |
| 31 | + Type: schema.TypeString, |
| 32 | + Computed: true, |
| 33 | + Description: "Data table GUID.", |
| 34 | + }, |
| 35 | + "name": { |
| 36 | + Type: schema.TypeString, |
| 37 | + Computed: true, |
| 38 | + Description: "Data table name.", |
| 39 | + }, |
| 40 | + "description": { |
| 41 | + Type: schema.TypeString, |
| 42 | + Computed: true, |
| 43 | + Description: "Data table description.", |
| 44 | + }, |
| 45 | + "database_name": { |
| 46 | + Type: schema.TypeString, |
| 47 | + Computed: true, |
| 48 | + Description: "Database name.", |
| 49 | + }, |
| 50 | + "schema_name": { |
| 51 | + Type: schema.TypeString, |
| 52 | + Computed: true, |
| 53 | + Description: "Database schema name.", |
| 54 | + }, |
| 55 | + "table_type": { |
| 56 | + Type: schema.TypeString, |
| 57 | + Computed: true, |
| 58 | + Description: "Table type.", |
| 59 | + }, |
| 60 | + "create_time": { |
| 61 | + Type: schema.TypeString, |
| 62 | + Computed: true, |
| 63 | + Description: "Creation time.", |
| 64 | + }, |
| 65 | + "update_time": { |
| 66 | + Type: schema.TypeString, |
| 67 | + Computed: true, |
| 68 | + Description: "Update time.", |
| 69 | + }, |
| 70 | + "technical_metadata": { |
| 71 | + Type: schema.TypeList, |
| 72 | + Computed: true, |
| 73 | + Description: "Technical metadata of the table.", |
| 74 | + Elem: &schema.Resource{ |
| 75 | + Schema: map[string]*schema.Schema{ |
| 76 | + "owner": { |
| 77 | + Type: schema.TypeString, |
| 78 | + Computed: true, |
| 79 | + Description: "Responsible person.", |
| 80 | + }, |
| 81 | + "location": { |
| 82 | + Type: schema.TypeString, |
| 83 | + Computed: true, |
| 84 | + Description: "Data table location.", |
| 85 | + }, |
| 86 | + "storage_size": { |
| 87 | + Type: schema.TypeInt, |
| 88 | + Computed: true, |
| 89 | + Description: "Storage size.", |
| 90 | + }, |
| 91 | + }, |
| 92 | + }, |
| 93 | + }, |
| 94 | + "business_metadata": { |
| 95 | + Type: schema.TypeList, |
| 96 | + Computed: true, |
| 97 | + Description: "Business metadata of the table.", |
| 98 | + Elem: &schema.Resource{ |
| 99 | + Schema: map[string]*schema.Schema{ |
| 100 | + "tag_names": { |
| 101 | + Type: schema.TypeSet, |
| 102 | + Computed: true, |
| 103 | + Description: "Tag names.", |
| 104 | + Elem: &schema.Schema{ |
| 105 | + Type: schema.TypeString, |
| 106 | + }, |
| 107 | + }, |
| 108 | + }, |
| 109 | + }, |
| 110 | + }, |
| 111 | + }, |
| 112 | + }, |
| 113 | + }, |
| 114 | + |
| 115 | + "result_output_file": { |
| 116 | + Type: schema.TypeString, |
| 117 | + Optional: true, |
| 118 | + Description: "Used to save results.", |
| 119 | + }, |
| 120 | + }, |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +func dataSourceTencentCloudWedataGetTableRead(d *schema.ResourceData, meta interface{}) error { |
| 125 | + defer tccommon.LogElapsed("data_source.tencentcloud_wedata_get_table.read")() |
| 126 | + defer tccommon.InconsistentCheck(d, meta)() |
| 127 | + |
| 128 | + var ( |
| 129 | + logId = tccommon.GetLogId(nil) |
| 130 | + ctx = tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) |
| 131 | + service = WedataService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()} |
| 132 | + tableGuid string |
| 133 | + ) |
| 134 | + |
| 135 | + paramMap := make(map[string]interface{}) |
| 136 | + if v, ok := d.GetOk("table_guid"); ok { |
| 137 | + paramMap["TableGuid"] = helper.String(v.(string)) |
| 138 | + tableGuid = v.(string) |
| 139 | + } |
| 140 | + |
| 141 | + var respData *wedatav20250806.TableInfo |
| 142 | + reqErr := resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError { |
| 143 | + result, e := service.DescribeWedataGetTableByFilter(ctx, paramMap) |
| 144 | + if e != nil { |
| 145 | + return tccommon.RetryError(e) |
| 146 | + } |
| 147 | + |
| 148 | + respData = result |
| 149 | + return nil |
| 150 | + }) |
| 151 | + if reqErr != nil { |
| 152 | + return reqErr |
| 153 | + } |
| 154 | + |
| 155 | + tmpList := make([]map[string]interface{}, 0) |
| 156 | + if respData != nil { |
| 157 | + dataMap := map[string]interface{}{} |
| 158 | + if respData.Guid != nil { |
| 159 | + dataMap["guid"] = respData.Guid |
| 160 | + } |
| 161 | + |
| 162 | + if respData.Name != nil { |
| 163 | + dataMap["name"] = respData.Name |
| 164 | + } |
| 165 | + |
| 166 | + if respData.Description != nil { |
| 167 | + dataMap["description"] = respData.Description |
| 168 | + } |
| 169 | + |
| 170 | + if respData.DatabaseName != nil { |
| 171 | + dataMap["database_name"] = respData.DatabaseName |
| 172 | + } |
| 173 | + |
| 174 | + if respData.SchemaName != nil { |
| 175 | + dataMap["schema_name"] = respData.SchemaName |
| 176 | + } |
| 177 | + |
| 178 | + if respData.TableType != nil { |
| 179 | + dataMap["table_type"] = respData.TableType |
| 180 | + } |
| 181 | + |
| 182 | + if respData.CreateTime != nil { |
| 183 | + dataMap["create_time"] = respData.CreateTime |
| 184 | + } |
| 185 | + |
| 186 | + if respData.UpdateTime != nil { |
| 187 | + dataMap["update_time"] = respData.UpdateTime |
| 188 | + } |
| 189 | + |
| 190 | + technicalMetadataMap := map[string]interface{}{} |
| 191 | + if respData.TechnicalMetadata != nil { |
| 192 | + if respData.TechnicalMetadata.Owner != nil { |
| 193 | + technicalMetadataMap["owner"] = respData.TechnicalMetadata.Owner |
| 194 | + } |
| 195 | + |
| 196 | + if respData.TechnicalMetadata.Location != nil { |
| 197 | + technicalMetadataMap["location"] = respData.TechnicalMetadata.Location |
| 198 | + } |
| 199 | + |
| 200 | + if respData.TechnicalMetadata.StorageSize != nil { |
| 201 | + technicalMetadataMap["storage_size"] = respData.TechnicalMetadata.StorageSize |
| 202 | + } |
| 203 | + |
| 204 | + dataMap["technical_metadata"] = []interface{}{technicalMetadataMap} |
| 205 | + } |
| 206 | + |
| 207 | + businessMetadataMap := map[string]interface{}{} |
| 208 | + if respData.BusinessMetadata != nil { |
| 209 | + if respData.BusinessMetadata.TagNames != nil { |
| 210 | + businessMetadataMap["tag_names"] = respData.BusinessMetadata.TagNames |
| 211 | + } |
| 212 | + |
| 213 | + dataMap["business_metadata"] = []interface{}{businessMetadataMap} |
| 214 | + } |
| 215 | + |
| 216 | + tmpList = append(tmpList, dataMap) |
| 217 | + } |
| 218 | + |
| 219 | + _ = d.Set("data", tmpList) |
| 220 | + |
| 221 | + d.SetId(tableGuid) |
| 222 | + output, ok := d.GetOk("result_output_file") |
| 223 | + if ok && output.(string) != "" { |
| 224 | + if e := tccommon.WriteToFile(output.(string), tmpList); e != nil { |
| 225 | + return e |
| 226 | + } |
| 227 | + } |
| 228 | + |
| 229 | + return nil |
| 230 | +} |
0 commit comments