|
| 1 | +/* |
| 2 | +Use this data source to query detailed information of postgresql backup_download_urls |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +data "tencentcloud_postgresql_log_backups" "log_backups" { |
| 8 | + min_finish_time = "%s" |
| 9 | + max_finish_time = "%s" |
| 10 | + filters { |
| 11 | + name = "db-instance-id" |
| 12 | + values = [local.pgsql_id] |
| 13 | + } |
| 14 | + order_by = "StartTime" |
| 15 | + order_by_type = "desc" |
| 16 | +
|
| 17 | + } |
| 18 | +
|
| 19 | +data "tencentcloud_postgresql_backup_download_urls" "backup_download_urls" { |
| 20 | + db_instance_id = local.pgsql_id |
| 21 | + backup_type = "LogBackup" |
| 22 | + backup_id = data.tencentcloud_postgresql_log_backups.log_backups.log_backup_set.0.id |
| 23 | + url_expire_time = 12 |
| 24 | + backup_download_restriction { |
| 25 | + restriction_type = "NONE" |
| 26 | + vpc_restriction_effect = "ALLOW" |
| 27 | + vpc_id_set = [local.vpc_id] |
| 28 | + ip_restriction_effect = "ALLOW" |
| 29 | + ip_set = ["0.0.0.0"] |
| 30 | + } |
| 31 | +} |
| 32 | +``` |
| 33 | +*/ |
| 34 | +package tencentcloud |
| 35 | + |
| 36 | +import ( |
| 37 | + "context" |
| 38 | + |
| 39 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 40 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 41 | + postgresql "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/postgres/v20170312" |
| 42 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 43 | +) |
| 44 | + |
| 45 | +func dataSourceTencentCloudPostgresqlBackupDownloadUrls() *schema.Resource { |
| 46 | + return &schema.Resource{ |
| 47 | + Read: dataSourceTencentCloudPostgresqlBackupDownloadUrlsRead, |
| 48 | + Schema: map[string]*schema.Schema{ |
| 49 | + "db_instance_id": { |
| 50 | + Required: true, |
| 51 | + Type: schema.TypeString, |
| 52 | + Description: "Instance ID.", |
| 53 | + }, |
| 54 | + |
| 55 | + "backup_type": { |
| 56 | + Required: true, |
| 57 | + Type: schema.TypeString, |
| 58 | + Description: "Backup type. Valid values: `LogBackup`, `BaseBackup`.", |
| 59 | + }, |
| 60 | + |
| 61 | + "backup_id": { |
| 62 | + Required: true, |
| 63 | + Type: schema.TypeString, |
| 64 | + Description: "Unique backup ID.", |
| 65 | + }, |
| 66 | + |
| 67 | + "url_expire_time": { |
| 68 | + Optional: true, |
| 69 | + Type: schema.TypeInt, |
| 70 | + Description: "Validity period of a URL, which is 12 hours by default.", |
| 71 | + }, |
| 72 | + |
| 73 | + "backup_download_restriction": { |
| 74 | + Optional: true, |
| 75 | + Type: schema.TypeList, |
| 76 | + MaxItems: 1, |
| 77 | + Description: "Backup download restriction.", |
| 78 | + Elem: &schema.Resource{ |
| 79 | + Schema: map[string]*schema.Schema{ |
| 80 | + "restriction_type": { |
| 81 | + Type: schema.TypeString, |
| 82 | + Optional: true, |
| 83 | + Description: "Type of the network restrictions for downloading backup files. Valid values: `NONE` (backups can be downloaded over both private and public networks), `INTRANET` (backups can only be downloaded over the private network), `CUSTOMIZE` (backups can be downloaded over specified VPCs or at specified IPs).", |
| 84 | + }, |
| 85 | + "vpc_restriction_effect": { |
| 86 | + Type: schema.TypeString, |
| 87 | + Optional: true, |
| 88 | + Description: "Whether VPC is allowed. Valid values: `ALLOW` (allow), `DENY` (deny).", |
| 89 | + }, |
| 90 | + "vpc_id_set": { |
| 91 | + Type: schema.TypeSet, |
| 92 | + Elem: &schema.Schema{ |
| 93 | + Type: schema.TypeString, |
| 94 | + }, |
| 95 | + Optional: true, |
| 96 | + Description: "Whether it is allowed to download the VPC ID list of the backup files.", |
| 97 | + }, |
| 98 | + "ip_restriction_effect": { |
| 99 | + Type: schema.TypeString, |
| 100 | + Optional: true, |
| 101 | + Description: "Whether IP is allowed. Valid values: `ALLOW` (allow), `DENY` (deny).", |
| 102 | + }, |
| 103 | + "ip_set": { |
| 104 | + Type: schema.TypeSet, |
| 105 | + Elem: &schema.Schema{ |
| 106 | + Type: schema.TypeString, |
| 107 | + }, |
| 108 | + Optional: true, |
| 109 | + Description: "Whether it is allowed to download IP list of the backup files.", |
| 110 | + }, |
| 111 | + }, |
| 112 | + }, |
| 113 | + }, |
| 114 | + |
| 115 | + "backup_download_url": { |
| 116 | + Computed: true, |
| 117 | + Type: schema.TypeString, |
| 118 | + Description: "Backup download URL.", |
| 119 | + }, |
| 120 | + |
| 121 | + "result_output_file": { |
| 122 | + Type: schema.TypeString, |
| 123 | + Optional: true, |
| 124 | + Description: "Used to save results.", |
| 125 | + }, |
| 126 | + }, |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +func dataSourceTencentCloudPostgresqlBackupDownloadUrlsRead(d *schema.ResourceData, meta interface{}) error { |
| 131 | + defer logElapsed("data_source.tencentcloud_postgresql_backup_download_urls.read")() |
| 132 | + defer inconsistentCheck(d, meta)() |
| 133 | + |
| 134 | + logId := getLogId(contextNil) |
| 135 | + var id string |
| 136 | + |
| 137 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 138 | + |
| 139 | + paramMap := make(map[string]interface{}) |
| 140 | + if v, ok := d.GetOk("db_instance_id"); ok { |
| 141 | + paramMap["DBInstanceId"] = helper.String(v.(string)) |
| 142 | + id = v.(string) |
| 143 | + } |
| 144 | + |
| 145 | + if v, ok := d.GetOk("backup_type"); ok { |
| 146 | + paramMap["BackupType"] = helper.String(v.(string)) |
| 147 | + } |
| 148 | + |
| 149 | + if v, ok := d.GetOk("backup_id"); ok { |
| 150 | + paramMap["BackupId"] = helper.String(v.(string)) |
| 151 | + } |
| 152 | + |
| 153 | + if v, _ := d.GetOk("url_expire_time"); v != nil { |
| 154 | + paramMap["URLExpireTime"] = helper.IntUint64(v.(int)) |
| 155 | + } |
| 156 | + |
| 157 | + if dMap, ok := helper.InterfacesHeadMap(d, "backup_download_restriction"); ok { |
| 158 | + backupDownloadRestriction := postgresql.BackupDownloadRestriction{} |
| 159 | + if v, ok := dMap["restriction_type"]; ok { |
| 160 | + backupDownloadRestriction.RestrictionType = helper.String(v.(string)) |
| 161 | + } |
| 162 | + if v, ok := dMap["vpc_restriction_effect"]; ok { |
| 163 | + backupDownloadRestriction.VpcRestrictionEffect = helper.String(v.(string)) |
| 164 | + } |
| 165 | + if v, ok := dMap["vpc_id_set"]; ok { |
| 166 | + vpcIdSetSet := v.(*schema.Set).List() |
| 167 | + backupDownloadRestriction.VpcIdSet = helper.InterfacesStringsPoint(vpcIdSetSet) |
| 168 | + } |
| 169 | + if v, ok := dMap["ip_restriction_effect"]; ok { |
| 170 | + backupDownloadRestriction.IpRestrictionEffect = helper.String(v.(string)) |
| 171 | + } |
| 172 | + if v, ok := dMap["ip_set"]; ok { |
| 173 | + ipSetSet := v.(*schema.Set).List() |
| 174 | + backupDownloadRestriction.IpSet = helper.InterfacesStringsPoint(ipSetSet) |
| 175 | + } |
| 176 | + paramMap["BackupDownloadRestriction"] = &backupDownloadRestriction |
| 177 | + } |
| 178 | + |
| 179 | + service := PostgresqlService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 180 | + |
| 181 | + var backupDownloadURL *string |
| 182 | + err := resource.Retry(readRetryTimeout, func() *resource.RetryError { |
| 183 | + result, e := service.DescribePostgresqlBackupDownloadUrlsByFilter(ctx, paramMap) |
| 184 | + if e != nil { |
| 185 | + return retryError(e) |
| 186 | + } |
| 187 | + backupDownloadURL = result |
| 188 | + return nil |
| 189 | + }) |
| 190 | + if err != nil { |
| 191 | + return err |
| 192 | + } |
| 193 | + |
| 194 | + if backupDownloadURL != nil { |
| 195 | + _ = d.Set("backup_download_url", backupDownloadURL) |
| 196 | + } |
| 197 | + |
| 198 | + d.SetId(helper.DataResourceIdHash(id)) |
| 199 | + output, ok := d.GetOk("result_output_file") |
| 200 | + if ok && output.(string) != "" { |
| 201 | + if e := writeToFile(output.(string), backupDownloadURL); e != nil { |
| 202 | + return e |
| 203 | + } |
| 204 | + } |
| 205 | + return nil |
| 206 | +} |
0 commit comments