|
| 1 | +package oceanus |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 9 | + oceanus "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/oceanus/v20190422" |
| 10 | + tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common" |
| 11 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 12 | +) |
| 13 | + |
| 14 | +func DataSourceTencentCloudOceanusMetaTable() *schema.Resource { |
| 15 | + return &schema.Resource{ |
| 16 | + Read: dataSourceTencentCloudOceanusMetaTableRead, |
| 17 | + Schema: map[string]*schema.Schema{ |
| 18 | + "work_space_id": { |
| 19 | + Required: true, |
| 20 | + Type: schema.TypeString, |
| 21 | + Description: "Unique identifier of the space.", |
| 22 | + }, |
| 23 | + "catalog": { |
| 24 | + Required: true, |
| 25 | + Type: schema.TypeString, |
| 26 | + Description: "Catalog name.", |
| 27 | + }, |
| 28 | + "database": { |
| 29 | + Required: true, |
| 30 | + Type: schema.TypeString, |
| 31 | + Description: "Database name.", |
| 32 | + }, |
| 33 | + "table": { |
| 34 | + Required: true, |
| 35 | + Type: schema.TypeString, |
| 36 | + Description: "Table name.", |
| 37 | + }, |
| 38 | + // computed |
| 39 | + "serial_id": { |
| 40 | + Computed: true, |
| 41 | + Type: schema.TypeString, |
| 42 | + Description: "Unique identifier of the metadata table.", |
| 43 | + }, |
| 44 | + "ddl": { |
| 45 | + Computed: true, |
| 46 | + Type: schema.TypeString, |
| 47 | + Description: "Table creation statement, encoded in Base64.For example,Q1JFQVRFIFRBQkxFIGRhdGFnZW5fc291cmNlX3RhYmxlICggCiAgICBpZCBJTlQsIAogICAgbmFtZSBTVFJJTkcgCikgV0lUSCAoCidjb25uZWN0b3InPSdkYXRhZ2VuJywKJ3Jvd3MtcGVyLXNlY29uZCcgPSAnMScKKTs=.", |
| 48 | + }, |
| 49 | + "create_time": { |
| 50 | + Computed: true, |
| 51 | + Type: schema.TypeString, |
| 52 | + Description: "Scene time.", |
| 53 | + }, |
| 54 | + "result_output_file": { |
| 55 | + Type: schema.TypeString, |
| 56 | + Optional: true, |
| 57 | + Description: "Used to save results.", |
| 58 | + }, |
| 59 | + }, |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +func dataSourceTencentCloudOceanusMetaTableRead(d *schema.ResourceData, meta interface{}) error { |
| 64 | + defer tccommon.LogElapsed("data_source.tencentcloud_oceanus_meta_table.read")() |
| 65 | + defer tccommon.InconsistentCheck(d, meta)() |
| 66 | + |
| 67 | + var ( |
| 68 | + logId = tccommon.GetLogId(tccommon.ContextNil) |
| 69 | + ctx = context.WithValue(context.TODO(), tccommon.LogIdKey, logId) |
| 70 | + service = OceanusService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()} |
| 71 | + MetaTable *oceanus.GetMetaTableResponseParams |
| 72 | + workSpaceId string |
| 73 | + ) |
| 74 | + |
| 75 | + paramMap := make(map[string]interface{}) |
| 76 | + if v, ok := d.GetOk("work_space_id"); ok { |
| 77 | + paramMap["WorkSpaceId"] = helper.String(v.(string)) |
| 78 | + workSpaceId = v.(string) |
| 79 | + } |
| 80 | + |
| 81 | + if v, ok := d.GetOk("catalog"); ok { |
| 82 | + paramMap["Catalog"] = helper.String(v.(string)) |
| 83 | + } |
| 84 | + |
| 85 | + if v, ok := d.GetOk("database"); ok { |
| 86 | + paramMap["Database"] = helper.String(v.(string)) |
| 87 | + } |
| 88 | + |
| 89 | + if v, ok := d.GetOk("table"); ok { |
| 90 | + paramMap["Table"] = helper.String(v.(string)) |
| 91 | + } |
| 92 | + |
| 93 | + err := resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError { |
| 94 | + result, e := service.DescribeOceanusMetaTableByFilter(ctx, paramMap) |
| 95 | + if e != nil { |
| 96 | + return tccommon.RetryError(e) |
| 97 | + } |
| 98 | + |
| 99 | + if result == nil { |
| 100 | + e = fmt.Errorf("MetaTable not exists") |
| 101 | + return resource.NonRetryableError(e) |
| 102 | + } |
| 103 | + |
| 104 | + MetaTable = result |
| 105 | + return nil |
| 106 | + }) |
| 107 | + |
| 108 | + if err != nil { |
| 109 | + return err |
| 110 | + } |
| 111 | + |
| 112 | + _ = d.Set("work_space_id", workSpaceId) |
| 113 | + |
| 114 | + if MetaTable.Catalog != nil { |
| 115 | + _ = d.Set("catalog", MetaTable.Catalog) |
| 116 | + } |
| 117 | + |
| 118 | + if MetaTable.Database != nil { |
| 119 | + _ = d.Set("database", MetaTable.Database) |
| 120 | + } |
| 121 | + |
| 122 | + if MetaTable.Table != nil { |
| 123 | + _ = d.Set("table", MetaTable.Table) |
| 124 | + } |
| 125 | + |
| 126 | + if MetaTable.SerialId != nil { |
| 127 | + _ = d.Set("serial_id", MetaTable.SerialId) |
| 128 | + } |
| 129 | + |
| 130 | + if MetaTable.DDL != nil { |
| 131 | + _ = d.Set("ddl", MetaTable.DDL) |
| 132 | + } |
| 133 | + |
| 134 | + if MetaTable.CreateTime != nil { |
| 135 | + _ = d.Set("create_time", MetaTable.CreateTime) |
| 136 | + } |
| 137 | + |
| 138 | + d.SetId(workSpaceId) |
| 139 | + output, ok := d.GetOk("result_output_file") |
| 140 | + if ok && output.(string) != "" { |
| 141 | + if e := tccommon.WriteToFile(output.(string), d); e != nil { |
| 142 | + return e |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + return nil |
| 147 | +} |
0 commit comments