|
| 1 | +/* |
| 2 | +Use this data source to query detailed information of dcdb database_objects |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +data "tencentcloud_dcdb_database_objects" "database_objects" { |
| 8 | + instance_id = "dcdbt-ow7t8lmc" |
| 9 | + db_name = <nil> |
| 10 | + } |
| 11 | +``` |
| 12 | +*/ |
| 13 | +package tencentcloud |
| 14 | + |
| 15 | +import ( |
| 16 | + "context" |
| 17 | + "strings" |
| 18 | + |
| 19 | + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" |
| 20 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 21 | + dcdb "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dcdb/v20180411" |
| 22 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 23 | +) |
| 24 | + |
| 25 | +func dataSourceTencentCloudDcdbDatabaseObjects() *schema.Resource { |
| 26 | + return &schema.Resource{ |
| 27 | + Read: dataSourceTencentCloudDcdbDatabaseObjectsRead, |
| 28 | + Schema: map[string]*schema.Schema{ |
| 29 | + "instance_id": { |
| 30 | + Required: true, |
| 31 | + Type: schema.TypeString, |
| 32 | + Description: "The ID of instance.", |
| 33 | + }, |
| 34 | + |
| 35 | + "db_name": { |
| 36 | + Required: true, |
| 37 | + Type: schema.TypeString, |
| 38 | + Description: "Database name, obtained through the DescribeDatabases api.", |
| 39 | + }, |
| 40 | + |
| 41 | + "tables": { |
| 42 | + Computed: true, |
| 43 | + Type: schema.TypeList, |
| 44 | + Description: "Table list.", |
| 45 | + Elem: &schema.Resource{ |
| 46 | + Schema: map[string]*schema.Schema{ |
| 47 | + "table": { |
| 48 | + Type: schema.TypeString, |
| 49 | + Computed: true, |
| 50 | + Description: "The name of table.", |
| 51 | + }, |
| 52 | + }, |
| 53 | + }, |
| 54 | + }, |
| 55 | + |
| 56 | + "views": { |
| 57 | + Computed: true, |
| 58 | + Type: schema.TypeList, |
| 59 | + Description: "View list.", |
| 60 | + Elem: &schema.Resource{ |
| 61 | + Schema: map[string]*schema.Schema{ |
| 62 | + "view": { |
| 63 | + Type: schema.TypeString, |
| 64 | + Computed: true, |
| 65 | + Description: "The name of view.", |
| 66 | + }, |
| 67 | + }, |
| 68 | + }, |
| 69 | + }, |
| 70 | + |
| 71 | + "procs": { |
| 72 | + Computed: true, |
| 73 | + Type: schema.TypeList, |
| 74 | + Description: "Procedure list.", |
| 75 | + Elem: &schema.Resource{ |
| 76 | + Schema: map[string]*schema.Schema{ |
| 77 | + "proc": { |
| 78 | + Type: schema.TypeString, |
| 79 | + Computed: true, |
| 80 | + Description: "The name of procedure.", |
| 81 | + }, |
| 82 | + }, |
| 83 | + }, |
| 84 | + }, |
| 85 | + |
| 86 | + "funcs": { |
| 87 | + Computed: true, |
| 88 | + Type: schema.TypeList, |
| 89 | + Description: "Function list.", |
| 90 | + Elem: &schema.Resource{ |
| 91 | + Schema: map[string]*schema.Schema{ |
| 92 | + "func": { |
| 93 | + Type: schema.TypeString, |
| 94 | + Computed: true, |
| 95 | + Description: "The name of function.", |
| 96 | + }, |
| 97 | + }, |
| 98 | + }, |
| 99 | + }, |
| 100 | + |
| 101 | + "result_output_file": { |
| 102 | + Type: schema.TypeString, |
| 103 | + Optional: true, |
| 104 | + Description: "Used to save results.", |
| 105 | + }, |
| 106 | + }, |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +func dataSourceTencentCloudDcdbDatabaseObjectsRead(d *schema.ResourceData, meta interface{}) error { |
| 111 | + defer logElapsed("data_source.tencentcloud_dcdb_database_objects.read")() |
| 112 | + defer inconsistentCheck(d, meta)() |
| 113 | + |
| 114 | + logId := getLogId(contextNil) |
| 115 | + |
| 116 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 117 | + |
| 118 | + paramMap := make(map[string]interface{}) |
| 119 | + if v, ok := d.GetOk("instance_id"); ok { |
| 120 | + paramMap["instance_id"] = helper.String(v.(string)) |
| 121 | + } |
| 122 | + |
| 123 | + if v, ok := d.GetOk("db_name"); ok { |
| 124 | + paramMap["db_name"] = helper.String(v.(string)) |
| 125 | + } |
| 126 | + |
| 127 | + service := DcdbService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 128 | + var result *dcdb.DescribeDatabaseObjectsResponseParams |
| 129 | + |
| 130 | + err := resource.Retry(readRetryTimeout, func() *resource.RetryError { |
| 131 | + var e error |
| 132 | + result, e = service.DescribeDcdbDBObjectsByFilter(ctx, paramMap) |
| 133 | + if e != nil { |
| 134 | + return retryError(e) |
| 135 | + } |
| 136 | + return nil |
| 137 | + }) |
| 138 | + if err != nil { |
| 139 | + return err |
| 140 | + } |
| 141 | + |
| 142 | + ids := make([]string, 0) |
| 143 | + data := make(map[string]interface{}) |
| 144 | + |
| 145 | + if result != nil { |
| 146 | + tables := result.Tables |
| 147 | + tabList := make([]map[string]interface{}, 0, len(tables)) |
| 148 | + if tables != nil { |
| 149 | + for _, databaseTable := range tables { |
| 150 | + databaseTableMap := map[string]interface{}{} |
| 151 | + |
| 152 | + if databaseTable.Table != nil { |
| 153 | + databaseTableMap["table"] = databaseTable.Table |
| 154 | + } |
| 155 | + tabList = append(tabList, databaseTableMap) |
| 156 | + } |
| 157 | + _ = d.Set("tables", tabList) |
| 158 | + data["tables"] = tabList |
| 159 | + } |
| 160 | + |
| 161 | + views := result.Views |
| 162 | + viewList := make([]map[string]interface{}, 0, len(views)) |
| 163 | + if views != nil { |
| 164 | + for _, databaseView := range views { |
| 165 | + databaseViewMap := map[string]interface{}{} |
| 166 | + |
| 167 | + if databaseView.View != nil { |
| 168 | + databaseViewMap["view"] = databaseView.View |
| 169 | + } |
| 170 | + viewList = append(viewList, databaseViewMap) |
| 171 | + } |
| 172 | + _ = d.Set("views", viewList) |
| 173 | + data["views"] = viewList |
| 174 | + } |
| 175 | + |
| 176 | + procs := result.Procs |
| 177 | + procList := make([]map[string]interface{}, 0, len(procs)) |
| 178 | + if procs != nil { |
| 179 | + for _, databaseProcedure := range procs { |
| 180 | + databaseProcedureMap := map[string]interface{}{} |
| 181 | + |
| 182 | + if databaseProcedure.Proc != nil { |
| 183 | + databaseProcedureMap["proc"] = databaseProcedure.Proc |
| 184 | + } |
| 185 | + procList = append(procList, databaseProcedureMap) |
| 186 | + } |
| 187 | + _ = d.Set("procs", procList) |
| 188 | + data["procs"] = procList |
| 189 | + } |
| 190 | + |
| 191 | + funcs := result.Funcs |
| 192 | + funcList := make([]map[string]interface{}, 0, len(funcs)) |
| 193 | + if funcs != nil { |
| 194 | + for _, databaseFunction := range funcs { |
| 195 | + databaseFunctionMap := map[string]interface{}{} |
| 196 | + |
| 197 | + if databaseFunction.Func != nil { |
| 198 | + databaseFunctionMap["func"] = databaseFunction.Func |
| 199 | + } |
| 200 | + funcList = append(funcList, databaseFunctionMap) |
| 201 | + } |
| 202 | + _ = d.Set("funcs", funcList) |
| 203 | + data["funcs"] = funcList |
| 204 | + } |
| 205 | + } |
| 206 | + |
| 207 | + ids = append(ids, strings.Join([]string{*result.InstanceId, *result.DbName}, FILED_SP)) |
| 208 | + |
| 209 | + d.SetId(helper.DataResourceIdsHash(ids)) |
| 210 | + output, ok := d.GetOk("result_output_file") |
| 211 | + if ok && output.(string) != "" { |
| 212 | + if e := writeToFile(output.(string), data); e != nil { |
| 213 | + return e |
| 214 | + } |
| 215 | + } |
| 216 | + return nil |
| 217 | +} |
0 commit comments