|
| 1 | +/* |
| 2 | +Use this data source to query detailed information of dts migrate_db_instances |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +data "tencentcloud_dts_migrate_db_instances" "migrate_db_instances" { |
| 8 | + database_type = "mysql" |
| 9 | + migrate_role = "src" |
| 10 | + instance_id = "cdb-ffulb2sg" |
| 11 | + instance_name = "cdb_test" |
| 12 | + limit = 10 |
| 13 | + offset = 10 |
| 14 | + account_mode = "self" |
| 15 | + tmp_secret_id = "AKIDvBDyVmna9TadcS4YzfBZmkU5TbX12345" |
| 16 | + tmp_secret_key = "ZswjGWWHm24qMeiX6QUJsELDpC12345" |
| 17 | + tmp_token = "JOqqCPVuWdNZvlVDLxxx" |
| 18 | + } |
| 19 | +``` |
| 20 | +*/ |
| 21 | +package tencentcloud |
| 22 | + |
| 23 | +import ( |
| 24 | + "context" |
| 25 | + |
| 26 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 27 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 28 | + dts "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dts/v20211206" |
| 29 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 30 | +) |
| 31 | + |
| 32 | +func dataSourceTencentCloudDtsMigrateDbInstances() *schema.Resource { |
| 33 | + return &schema.Resource{ |
| 34 | + Read: dataSourceTencentCloudDtsMigrateDbInstancesRead, |
| 35 | + Schema: map[string]*schema.Schema{ |
| 36 | + "database_type": { |
| 37 | + Required: true, |
| 38 | + Type: schema.TypeString, |
| 39 | + Description: "Database type.", |
| 40 | + }, |
| 41 | + |
| 42 | + "migrate_role": { |
| 43 | + Optional: true, |
| 44 | + Type: schema.TypeString, |
| 45 | + Description: "Whether the instance is the migration source or destination,src(for source), dst(for destination).", |
| 46 | + }, |
| 47 | + |
| 48 | + "instance_id": { |
| 49 | + Optional: true, |
| 50 | + Type: schema.TypeString, |
| 51 | + Description: "Database instance id.", |
| 52 | + }, |
| 53 | + |
| 54 | + "instance_name": { |
| 55 | + Optional: true, |
| 56 | + Type: schema.TypeString, |
| 57 | + Description: "Database instance name.", |
| 58 | + }, |
| 59 | + |
| 60 | + "limit": { |
| 61 | + Optional: true, |
| 62 | + Type: schema.TypeInt, |
| 63 | + Description: "Limit.", |
| 64 | + }, |
| 65 | + |
| 66 | + "offset": { |
| 67 | + Optional: true, |
| 68 | + Type: schema.TypeInt, |
| 69 | + Description: "Offset.", |
| 70 | + }, |
| 71 | + |
| 72 | + "account_mode": { |
| 73 | + Optional: true, |
| 74 | + Type: schema.TypeString, |
| 75 | + Description: "The owning account of the resource is null or self(resources in the self account), other(resources in the other account).", |
| 76 | + }, |
| 77 | + |
| 78 | + "tmp_secret_id": { |
| 79 | + Optional: true, |
| 80 | + Type: schema.TypeString, |
| 81 | + Description: "temporary secret id, used across account.", |
| 82 | + }, |
| 83 | + |
| 84 | + "tmp_secret_key": { |
| 85 | + Optional: true, |
| 86 | + Type: schema.TypeString, |
| 87 | + Description: "temporary secret key, used across account.", |
| 88 | + }, |
| 89 | + |
| 90 | + "tmp_token": { |
| 91 | + Optional: true, |
| 92 | + Type: schema.TypeString, |
| 93 | + Description: "temporary token, used across account.", |
| 94 | + }, |
| 95 | + |
| 96 | + "instances": { |
| 97 | + Computed: true, |
| 98 | + Type: schema.TypeList, |
| 99 | + Description: "Instance list.", |
| 100 | + Elem: &schema.Resource{ |
| 101 | + Schema: map[string]*schema.Schema{ |
| 102 | + "instance_id": { |
| 103 | + Type: schema.TypeString, |
| 104 | + Computed: true, |
| 105 | + Description: "Instance Id.", |
| 106 | + }, |
| 107 | + "instance_name": { |
| 108 | + Type: schema.TypeString, |
| 109 | + Computed: true, |
| 110 | + Description: "Database instance name.", |
| 111 | + }, |
| 112 | + "vip": { |
| 113 | + Type: schema.TypeString, |
| 114 | + Computed: true, |
| 115 | + Description: "Instance vip.", |
| 116 | + }, |
| 117 | + "vport": { |
| 118 | + Type: schema.TypeInt, |
| 119 | + Computed: true, |
| 120 | + Description: "Instance port.", |
| 121 | + }, |
| 122 | + "usable": { |
| 123 | + Type: schema.TypeInt, |
| 124 | + Computed: true, |
| 125 | + Description: "Can used in migration, 1-yes, 0-no.", |
| 126 | + }, |
| 127 | + "hint": { |
| 128 | + Type: schema.TypeString, |
| 129 | + Computed: true, |
| 130 | + Description: "The reason of can't used in migration.", |
| 131 | + }, |
| 132 | + }, |
| 133 | + }, |
| 134 | + }, |
| 135 | + |
| 136 | + "request_id": { |
| 137 | + Computed: true, |
| 138 | + Type: schema.TypeString, |
| 139 | + Description: "Unique request id, provide this when encounter a problem.", |
| 140 | + }, |
| 141 | + |
| 142 | + "result_output_file": { |
| 143 | + Type: schema.TypeString, |
| 144 | + Optional: true, |
| 145 | + Description: "Used to save results.", |
| 146 | + }, |
| 147 | + }, |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +func dataSourceTencentCloudDtsMigrateDbInstancesRead(d *schema.ResourceData, meta interface{}) error { |
| 152 | + defer logElapsed("data_source.tencentcloud_dts_migrate_db_instances.read")() |
| 153 | + defer inconsistentCheck(d, meta)() |
| 154 | + |
| 155 | + logId := getLogId(contextNil) |
| 156 | + |
| 157 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 158 | + |
| 159 | + paramMap := make(map[string]interface{}) |
| 160 | + if v, ok := d.GetOk("database_type"); ok { |
| 161 | + paramMap["DatabaseType"] = helper.String(v.(string)) |
| 162 | + } |
| 163 | + |
| 164 | + if v, ok := d.GetOk("migrate_role"); ok { |
| 165 | + paramMap["MigrateRole"] = helper.String(v.(string)) |
| 166 | + } |
| 167 | + |
| 168 | + if v, ok := d.GetOk("instance_id"); ok { |
| 169 | + paramMap["InstanceId"] = helper.String(v.(string)) |
| 170 | + } |
| 171 | + |
| 172 | + if v, ok := d.GetOk("instance_name"); ok { |
| 173 | + paramMap["InstanceName"] = helper.String(v.(string)) |
| 174 | + } |
| 175 | + |
| 176 | + if v, ok := d.GetOk("account_mode"); ok { |
| 177 | + paramMap["AccountMode"] = helper.String(v.(string)) |
| 178 | + } |
| 179 | + |
| 180 | + if v, ok := d.GetOk("tmp_secret_id"); ok { |
| 181 | + paramMap["TmpSecretId"] = helper.String(v.(string)) |
| 182 | + } |
| 183 | + |
| 184 | + if v, ok := d.GetOk("tmp_secret_key"); ok { |
| 185 | + paramMap["TmpSecretKey"] = helper.String(v.(string)) |
| 186 | + } |
| 187 | + |
| 188 | + if v, ok := d.GetOk("tmp_token"); ok { |
| 189 | + paramMap["TmpToken"] = helper.String(v.(string)) |
| 190 | + } |
| 191 | + |
| 192 | + service := DtsService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 193 | + |
| 194 | + var instances []*dts.MigrateDBItem |
| 195 | + |
| 196 | + err := resource.Retry(readRetryTimeout, func() *resource.RetryError { |
| 197 | + result, e := service.DescribeDtsMigrateDbInstancesByFilter(ctx, paramMap) |
| 198 | + if e != nil { |
| 199 | + return retryError(e) |
| 200 | + } |
| 201 | + instances = result |
| 202 | + return nil |
| 203 | + }) |
| 204 | + if err != nil { |
| 205 | + return err |
| 206 | + } |
| 207 | + |
| 208 | + ids := make([]string, 0, len(instances)) |
| 209 | + tmpList := make([]map[string]interface{}, 0, len(instances)) |
| 210 | + |
| 211 | + if instances != nil { |
| 212 | + for _, migrateDBItem := range instances { |
| 213 | + migrateDBItemMap := map[string]interface{}{} |
| 214 | + |
| 215 | + if migrateDBItem.InstanceId != nil { |
| 216 | + migrateDBItemMap["instance_id"] = migrateDBItem.InstanceId |
| 217 | + } |
| 218 | + |
| 219 | + if migrateDBItem.InstanceName != nil { |
| 220 | + migrateDBItemMap["instance_name"] = migrateDBItem.InstanceName |
| 221 | + } |
| 222 | + |
| 223 | + if migrateDBItem.Vip != nil { |
| 224 | + migrateDBItemMap["vip"] = migrateDBItem.Vip |
| 225 | + } |
| 226 | + |
| 227 | + if migrateDBItem.Vport != nil { |
| 228 | + migrateDBItemMap["vport"] = migrateDBItem.Vport |
| 229 | + } |
| 230 | + |
| 231 | + if migrateDBItem.Usable != nil { |
| 232 | + migrateDBItemMap["usable"] = migrateDBItem.Usable |
| 233 | + } |
| 234 | + |
| 235 | + if migrateDBItem.Hint != nil { |
| 236 | + migrateDBItemMap["hint"] = migrateDBItem.Hint |
| 237 | + } |
| 238 | + |
| 239 | + ids = append(ids, *migrateDBItem.InstanceId) |
| 240 | + tmpList = append(tmpList, migrateDBItemMap) |
| 241 | + } |
| 242 | + |
| 243 | + _ = d.Set("instances", tmpList) |
| 244 | + } |
| 245 | + |
| 246 | + d.SetId(helper.DataResourceIdsHash(ids)) |
| 247 | + output, ok := d.GetOk("result_output_file") |
| 248 | + if ok && output.(string) != "" { |
| 249 | + if e := writeToFile(output.(string), tmpList); e != nil { |
| 250 | + return e |
| 251 | + } |
| 252 | + } |
| 253 | + return nil |
| 254 | +} |
0 commit comments