|
| 1 | +/* |
| 2 | +Provides a resource to create a redis backup_download_restriction |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +resource "tencentcloud_redis_backup_download_restriction" "backup_download_restriction" { |
| 8 | + limit_type = "Customize" |
| 9 | + vpc_comparison_symbol = "In" |
| 10 | + ip_comparison_symbol = "In" |
| 11 | + limit_vpc { |
| 12 | + region = "ap-guangzhou" |
| 13 | + vpc_list = [var.vpc_id] |
| 14 | + } |
| 15 | + limit_ip = ["10.1.1.12", "10.1.1.13"] |
| 16 | +} |
| 17 | +``` |
| 18 | +
|
| 19 | +Import |
| 20 | +
|
| 21 | +redis backup_download_restriction can be imported using the id, e.g. |
| 22 | +
|
| 23 | +``` |
| 24 | +terraform import tencentcloud_redis_backup_download_restriction.backup_download_restriction backup_download_restriction_id |
| 25 | +``` |
| 26 | +*/ |
| 27 | +package tencentcloud |
| 28 | + |
| 29 | +import ( |
| 30 | + "context" |
| 31 | + "log" |
| 32 | + |
| 33 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 34 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 35 | + redis "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/redis/v20180412" |
| 36 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 37 | +) |
| 38 | + |
| 39 | +func resourceTencentCloudRedisBackupDownloadRestriction() *schema.Resource { |
| 40 | + return &schema.Resource{ |
| 41 | + Create: resourceTencentCloudRedisBackupDownloadRestrictionCreate, |
| 42 | + Read: resourceTencentCloudRedisBackupDownloadRestrictionRead, |
| 43 | + Update: resourceTencentCloudRedisBackupDownloadRestrictionUpdate, |
| 44 | + Delete: resourceTencentCloudRedisBackupDownloadRestrictionDelete, |
| 45 | + Importer: &schema.ResourceImporter{ |
| 46 | + State: schema.ImportStatePassthrough, |
| 47 | + }, |
| 48 | + Schema: map[string]*schema.Schema{ |
| 49 | + "limit_type": { |
| 50 | + Required: true, |
| 51 | + Type: schema.TypeString, |
| 52 | + Description: "Types of network restrictions for downloading backup files:- NoLimit: There is no limit, and backup files can be downloaded from both Tencent Cloud and internal and external networks.- LimitOnlyIntranet: Only intranet addresses automatically assigned by Tencent Cloud can download backup files.- Customize: refers to a user-defined private network downloadable backup file.", |
| 53 | + }, |
| 54 | + |
| 55 | + "vpc_comparison_symbol": { |
| 56 | + Optional: true, |
| 57 | + Type: schema.TypeString, |
| 58 | + Description: "This parameter only supports entering In, which means that the custom LimitVpc can download the backup file.", |
| 59 | + }, |
| 60 | + |
| 61 | + "ip_comparison_symbol": { |
| 62 | + Optional: true, |
| 63 | + Type: schema.TypeString, |
| 64 | + Description: "Identifies whether the customized LimitIP address can download the backup file.- In: Custom IP addresses are available for download.- NotIn: Custom IPs are not available for download.", |
| 65 | + }, |
| 66 | + |
| 67 | + "limit_vpc": { |
| 68 | + Optional: true, |
| 69 | + Type: schema.TypeList, |
| 70 | + Description: "A custom VPC ID for a downloadable backup file.If the parameter LimitType is **Customize**, you need to configure this parameter.", |
| 71 | + Elem: &schema.Resource{ |
| 72 | + Schema: map[string]*schema.Schema{ |
| 73 | + "region": { |
| 74 | + Type: schema.TypeString, |
| 75 | + Required: true, |
| 76 | + Description: "Customize the region of the VPC to which the backup file is downloaded.", |
| 77 | + }, |
| 78 | + "vpc_list": { |
| 79 | + Type: schema.TypeSet, |
| 80 | + Elem: &schema.Schema{ |
| 81 | + Type: schema.TypeString, |
| 82 | + }, |
| 83 | + Required: true, |
| 84 | + Description: "Customize the list of VPCs to download backup files.", |
| 85 | + }, |
| 86 | + }, |
| 87 | + }, |
| 88 | + }, |
| 89 | + |
| 90 | + "limit_ip": { |
| 91 | + Optional: true, |
| 92 | + Type: schema.TypeSet, |
| 93 | + Elem: &schema.Schema{ |
| 94 | + Type: schema.TypeString, |
| 95 | + }, |
| 96 | + Description: "A custom VPC IP address for downloadable backup files.If the parameter LimitType is **Customize**, you need to configure this parameter.", |
| 97 | + }, |
| 98 | + }, |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +func resourceTencentCloudRedisBackupDownloadRestrictionCreate(d *schema.ResourceData, meta interface{}) error { |
| 103 | + defer logElapsed("resource.tencentcloud_redis_backup_download_restriction.create")() |
| 104 | + defer inconsistentCheck(d, meta)() |
| 105 | + |
| 106 | + region := meta.(*TencentCloudClient).apiV3Conn.Region |
| 107 | + |
| 108 | + d.SetId(region) |
| 109 | + |
| 110 | + return resourceTencentCloudRedisBackupDownloadRestrictionUpdate(d, meta) |
| 111 | +} |
| 112 | + |
| 113 | +func resourceTencentCloudRedisBackupDownloadRestrictionRead(d *schema.ResourceData, meta interface{}) error { |
| 114 | + defer logElapsed("resource.tencentcloud_redis_backup_download_restriction.read")() |
| 115 | + defer inconsistentCheck(d, meta)() |
| 116 | + |
| 117 | + logId := getLogId(contextNil) |
| 118 | + |
| 119 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 120 | + |
| 121 | + service := RedisService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 122 | + backupDownloadRestriction, err := service.DescribeRedisBackupDownloadRestrictionById(ctx) |
| 123 | + if err != nil { |
| 124 | + return err |
| 125 | + } |
| 126 | + |
| 127 | + if backupDownloadRestriction == nil { |
| 128 | + d.SetId("") |
| 129 | + log.Printf("[WARN]%s resource `RedisBackupDownloadRestriction` [%s] not found, please check if it has been deleted.\n", logId, d.Id()) |
| 130 | + return nil |
| 131 | + } |
| 132 | + |
| 133 | + if backupDownloadRestriction.LimitType != nil { |
| 134 | + _ = d.Set("limit_type", backupDownloadRestriction.LimitType) |
| 135 | + } |
| 136 | + |
| 137 | + if backupDownloadRestriction.VpcComparisonSymbol != nil { |
| 138 | + _ = d.Set("vpc_comparison_symbol", backupDownloadRestriction.VpcComparisonSymbol) |
| 139 | + } |
| 140 | + |
| 141 | + if backupDownloadRestriction.IpComparisonSymbol != nil { |
| 142 | + _ = d.Set("ip_comparison_symbol", backupDownloadRestriction.IpComparisonSymbol) |
| 143 | + } |
| 144 | + |
| 145 | + if backupDownloadRestriction.LimitVpc != nil { |
| 146 | + limitVpcList := []interface{}{} |
| 147 | + for _, limitVpc := range backupDownloadRestriction.LimitVpc { |
| 148 | + limitVpcMap := map[string]interface{}{} |
| 149 | + |
| 150 | + if limitVpc.Region != nil { |
| 151 | + limitVpcMap["region"] = limitVpc.Region |
| 152 | + } |
| 153 | + |
| 154 | + if limitVpc.VpcList != nil { |
| 155 | + limitVpcMap["vpc_list"] = limitVpc.VpcList |
| 156 | + } |
| 157 | + |
| 158 | + limitVpcList = append(limitVpcList, limitVpcMap) |
| 159 | + } |
| 160 | + |
| 161 | + _ = d.Set("limit_vpc", limitVpcList) |
| 162 | + |
| 163 | + } |
| 164 | + |
| 165 | + if backupDownloadRestriction.LimitIp != nil { |
| 166 | + _ = d.Set("limit_ip", backupDownloadRestriction.LimitIp) |
| 167 | + } |
| 168 | + |
| 169 | + return nil |
| 170 | +} |
| 171 | + |
| 172 | +func resourceTencentCloudRedisBackupDownloadRestrictionUpdate(d *schema.ResourceData, meta interface{}) error { |
| 173 | + defer logElapsed("resource.tencentcloud_redis_backup_download_restriction.update")() |
| 174 | + defer inconsistentCheck(d, meta)() |
| 175 | + |
| 176 | + logId := getLogId(contextNil) |
| 177 | + |
| 178 | + request := redis.NewModifyBackupDownloadRestrictionRequest() |
| 179 | + |
| 180 | + if v, ok := d.GetOk("limit_type"); ok { |
| 181 | + request.LimitType = helper.String(v.(string)) |
| 182 | + } |
| 183 | + |
| 184 | + if v, ok := d.GetOk("vpc_comparison_symbol"); ok { |
| 185 | + request.VpcComparisonSymbol = helper.String(v.(string)) |
| 186 | + } |
| 187 | + |
| 188 | + if v, ok := d.GetOk("ip_comparison_symbol"); ok { |
| 189 | + request.IpComparisonSymbol = helper.String(v.(string)) |
| 190 | + } |
| 191 | + |
| 192 | + if v, ok := d.GetOk("limit_vpc"); ok { |
| 193 | + for _, item := range v.([]interface{}) { |
| 194 | + dMap := item.(map[string]interface{}) |
| 195 | + backupLimitVpcItem := redis.BackupLimitVpcItem{} |
| 196 | + if v, ok := dMap["region"]; ok { |
| 197 | + backupLimitVpcItem.Region = helper.String(v.(string)) |
| 198 | + } |
| 199 | + if v, ok := dMap["vpc_list"]; ok { |
| 200 | + vpcListSet := v.(*schema.Set).List() |
| 201 | + for i := range vpcListSet { |
| 202 | + vpcList := vpcListSet[i].(string) |
| 203 | + backupLimitVpcItem.VpcList = append(backupLimitVpcItem.VpcList, &vpcList) |
| 204 | + } |
| 205 | + } |
| 206 | + request.LimitVpc = append(request.LimitVpc, &backupLimitVpcItem) |
| 207 | + } |
| 208 | + } |
| 209 | + |
| 210 | + if d.HasChange("limit_ip") { |
| 211 | + if v, ok := d.GetOk("limit_ip"); ok { |
| 212 | + limitIpSet := v.(*schema.Set).List() |
| 213 | + for i := range limitIpSet { |
| 214 | + limitIp := limitIpSet[i].(string) |
| 215 | + request.LimitIp = append(request.LimitIp, &limitIp) |
| 216 | + } |
| 217 | + } |
| 218 | + } |
| 219 | + |
| 220 | + err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 221 | + result, e := meta.(*TencentCloudClient).apiV3Conn.UseRedisClient().ModifyBackupDownloadRestriction(request) |
| 222 | + if e != nil { |
| 223 | + return retryError(e) |
| 224 | + } else { |
| 225 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 226 | + } |
| 227 | + return nil |
| 228 | + }) |
| 229 | + if err != nil { |
| 230 | + log.Printf("[CRITAL]%s update redis backupDownloadRestriction failed, reason:%+v", logId, err) |
| 231 | + return err |
| 232 | + } |
| 233 | + |
| 234 | + return resourceTencentCloudRedisBackupDownloadRestrictionRead(d, meta) |
| 235 | +} |
| 236 | + |
| 237 | +func resourceTencentCloudRedisBackupDownloadRestrictionDelete(d *schema.ResourceData, meta interface{}) error { |
| 238 | + defer logElapsed("resource.tencentcloud_redis_backup_download_restriction.delete")() |
| 239 | + defer inconsistentCheck(d, meta)() |
| 240 | + |
| 241 | + return nil |
| 242 | +} |
0 commit comments