|
| 1 | +/* |
| 2 | +Provides a resource to create a mysql backup_download_restriction |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +resource "tencentcloud_mysql_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 = ["vpc-4owdpnwr"] |
| 14 | + } |
| 15 | + limit_ip = ["127.0.0.1"] |
| 16 | +} |
| 17 | +``` |
| 18 | +
|
| 19 | +Import |
| 20 | +
|
| 21 | +mysql backup_download_restriction can be imported using the "BackupDownloadRestriction", as follows. |
| 22 | +
|
| 23 | +``` |
| 24 | +terraform import tencentcloud_mysql_backup_download_restriction.backup_download_restriction BackupDownloadRestriction |
| 25 | +``` |
| 26 | +*/ |
| 27 | +package tencentcloud |
| 28 | + |
| 29 | +import ( |
| 30 | + "context" |
| 31 | + "log" |
| 32 | + "strings" |
| 33 | + |
| 34 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 35 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 36 | + mysql "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cdb/v20170320" |
| 37 | + sdkErrors "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors" |
| 38 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 39 | +) |
| 40 | + |
| 41 | +func resourceTencentCloudMysqlBackupDownloadRestriction() *schema.Resource { |
| 42 | + return &schema.Resource{ |
| 43 | + Create: resourceTencentCloudMysqlBackupDownloadRestrictionCreate, |
| 44 | + Read: resourceTencentCloudMysqlBackupDownloadRestrictionRead, |
| 45 | + Update: resourceTencentCloudMysqlBackupDownloadRestrictionUpdate, |
| 46 | + Delete: resourceTencentCloudMysqlBackupDownloadRestrictionDelete, |
| 47 | + Importer: &schema.ResourceImporter{ |
| 48 | + State: schema.ImportStatePassthrough, |
| 49 | + }, |
| 50 | + Schema: map[string]*schema.Schema{ |
| 51 | + "limit_type": { |
| 52 | + Required: true, |
| 53 | + Type: schema.TypeString, |
| 54 | + Description: "NoLimit No limit, both internal and external networks can be downloaded; LimitOnlyIntranet Only intranet can be downloaded; Customize user-defined vpc:ip can be downloaded. LimitVpc and LimitIp can be set only when the value is Customize.", |
| 55 | + }, |
| 56 | + |
| 57 | + "vpc_comparison_symbol": { |
| 58 | + Optional: true, |
| 59 | + Type: schema.TypeString, |
| 60 | + Description: "This parameter only supports In, which means that the vpc specified by LimitVpc can be downloaded. The default is In.", |
| 61 | + }, |
| 62 | + |
| 63 | + "ip_comparison_symbol": { |
| 64 | + Optional: true, |
| 65 | + Type: schema.TypeString, |
| 66 | + Description: "In: The specified ip can be downloaded; NotIn: The specified ip cannot be downloaded. The default is In.", |
| 67 | + }, |
| 68 | + |
| 69 | + "limit_vpc": { |
| 70 | + Optional: true, |
| 71 | + Type: schema.TypeList, |
| 72 | + Description: "vpc settings to limit downloads.", |
| 73 | + Elem: &schema.Resource{ |
| 74 | + Schema: map[string]*schema.Schema{ |
| 75 | + "region": { |
| 76 | + Type: schema.TypeString, |
| 77 | + Required: true, |
| 78 | + Description: "Restrict downloads from regions. Currently only the current region is supported.", |
| 79 | + }, |
| 80 | + "vpc_list": { |
| 81 | + Type: schema.TypeSet, |
| 82 | + Elem: &schema.Schema{ |
| 83 | + Type: schema.TypeString, |
| 84 | + }, |
| 85 | + Required: true, |
| 86 | + Description: "List of vpcs to limit downloads.", |
| 87 | + }, |
| 88 | + }, |
| 89 | + }, |
| 90 | + }, |
| 91 | + |
| 92 | + "limit_ip": { |
| 93 | + Optional: true, |
| 94 | + Type: schema.TypeSet, |
| 95 | + Elem: &schema.Schema{ |
| 96 | + Type: schema.TypeString, |
| 97 | + }, |
| 98 | + Description: "ip settings to limit downloads.", |
| 99 | + }, |
| 100 | + }, |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +func resourceTencentCloudMysqlBackupDownloadRestrictionCreate(d *schema.ResourceData, meta interface{}) error { |
| 105 | + defer logElapsed("resource.tencentcloud_mysql_backup_download_restriction.create")() |
| 106 | + defer inconsistentCheck(d, meta)() |
| 107 | + |
| 108 | + d.SetId("BackupDownloadRestriction") |
| 109 | + |
| 110 | + return resourceTencentCloudMysqlBackupDownloadRestrictionUpdate(d, meta) |
| 111 | +} |
| 112 | + |
| 113 | +func resourceTencentCloudMysqlBackupDownloadRestrictionRead(d *schema.ResourceData, meta interface{}) error { |
| 114 | + defer logElapsed("resource.tencentcloud_mysql_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 := MysqlService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 122 | + |
| 123 | + backupDownloadRestriction, err := service.DescribeMysqlBackupDownloadRestrictionById(ctx) |
| 124 | + if err != nil { |
| 125 | + return err |
| 126 | + } |
| 127 | + |
| 128 | + if backupDownloadRestriction == nil { |
| 129 | + d.SetId("") |
| 130 | + log.Printf("[WARN]%s resource `MysqlBackupDownloadRestriction` [%s] not found, please check if it has been deleted.\n", logId, d.Id()) |
| 131 | + return nil |
| 132 | + } |
| 133 | + |
| 134 | + if backupDownloadRestriction.LimitType != nil { |
| 135 | + _ = d.Set("limit_type", backupDownloadRestriction.LimitType) |
| 136 | + } |
| 137 | + |
| 138 | + if backupDownloadRestriction.VpcComparisonSymbol != nil { |
| 139 | + _ = d.Set("vpc_comparison_symbol", backupDownloadRestriction.VpcComparisonSymbol) |
| 140 | + } |
| 141 | + |
| 142 | + if backupDownloadRestriction.IpComparisonSymbol != nil { |
| 143 | + _ = d.Set("ip_comparison_symbol", backupDownloadRestriction.IpComparisonSymbol) |
| 144 | + } |
| 145 | + |
| 146 | + if backupDownloadRestriction.LimitVpc != nil { |
| 147 | + limitVpcList := []interface{}{} |
| 148 | + for _, limitVpc := range backupDownloadRestriction.LimitVpc { |
| 149 | + limitVpcMap := map[string]interface{}{} |
| 150 | + |
| 151 | + if limitVpc.Region != nil { |
| 152 | + limitVpcMap["region"] = limitVpc.Region |
| 153 | + } |
| 154 | + |
| 155 | + if limitVpc.VpcList != nil { |
| 156 | + limitVpcMap["vpc_list"] = limitVpc.VpcList |
| 157 | + } |
| 158 | + |
| 159 | + limitVpcList = append(limitVpcList, limitVpcMap) |
| 160 | + } |
| 161 | + |
| 162 | + _ = d.Set("limit_vpc", limitVpcList) |
| 163 | + |
| 164 | + } |
| 165 | + |
| 166 | + if backupDownloadRestriction.LimitIp != nil { |
| 167 | + _ = d.Set("limit_ip", backupDownloadRestriction.LimitIp) |
| 168 | + } |
| 169 | + |
| 170 | + return nil |
| 171 | +} |
| 172 | + |
| 173 | +func resourceTencentCloudMysqlBackupDownloadRestrictionUpdate(d *schema.ResourceData, meta interface{}) error { |
| 174 | + defer logElapsed("resource.tencentcloud_mysql_backup_download_restriction.update")() |
| 175 | + defer inconsistentCheck(d, meta)() |
| 176 | + |
| 177 | + logId := getLogId(contextNil) |
| 178 | + |
| 179 | + request := mysql.NewModifyBackupDownloadRestrictionRequest() |
| 180 | + |
| 181 | + if v, ok := d.GetOk("limit_type"); ok { |
| 182 | + request.LimitType = helper.String(v.(string)) |
| 183 | + } |
| 184 | + |
| 185 | + if d.HasChange("vpc_comparison_symbol") { |
| 186 | + if v, ok := d.GetOk("vpc_comparison_symbol"); ok { |
| 187 | + request.VpcComparisonSymbol = helper.String(v.(string)) |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + if d.HasChange("ip_comparison_symbol") { |
| 192 | + if v, ok := d.GetOk("ip_comparison_symbol"); ok { |
| 193 | + request.IpComparisonSymbol = helper.String(v.(string)) |
| 194 | + } |
| 195 | + } |
| 196 | + |
| 197 | + if d.HasChange("limit_vpc") { |
| 198 | + if v, ok := d.GetOk("limit_vpc"); ok { |
| 199 | + for _, item := range v.([]interface{}) { |
| 200 | + dMap := item.(map[string]interface{}) |
| 201 | + limitVpcItem := mysql.BackupLimitVpcItem{} |
| 202 | + if v, ok := dMap["region"]; ok { |
| 203 | + limitVpcItem.Region = helper.String(v.(string)) |
| 204 | + } |
| 205 | + if v, ok := dMap["vpc_list"]; ok { |
| 206 | + vpcListSet := v.(*schema.Set).List() |
| 207 | + limitVpcItem.VpcList = helper.InterfacesStringsPoint(vpcListSet) |
| 208 | + } |
| 209 | + request.LimitVpc = append(request.LimitVpc, &limitVpcItem) |
| 210 | + } |
| 211 | + } |
| 212 | + } |
| 213 | + |
| 214 | + if d.HasChange("limit_ip") { |
| 215 | + if v, ok := d.GetOk("limit_ip"); ok { |
| 216 | + limitIpSet := v.(*schema.Set).List() |
| 217 | + request.LimitIp = helper.InterfacesStringsPoint(limitIpSet) |
| 218 | + } |
| 219 | + } |
| 220 | + |
| 221 | + err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 222 | + result, e := meta.(*TencentCloudClient).apiV3Conn.UseMysqlClient().ModifyBackupDownloadRestriction(request) |
| 223 | + if e != nil { |
| 224 | + if sdkerr, ok := e.(*sdkErrors.TencentCloudSDKError); ok { |
| 225 | + if strings.Contains(sdkerr.Code, "FailedOperation") { |
| 226 | + return resource.NonRetryableError(e) |
| 227 | + } |
| 228 | + } |
| 229 | + return retryError(e) |
| 230 | + } else { |
| 231 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 232 | + } |
| 233 | + return nil |
| 234 | + }) |
| 235 | + if err != nil { |
| 236 | + log.Printf("[CRITAL]%s update mysql backupDownloadRestriction failed, reason:%+v", logId, err) |
| 237 | + return err |
| 238 | + } |
| 239 | + |
| 240 | + return resourceTencentCloudMysqlBackupDownloadRestrictionRead(d, meta) |
| 241 | +} |
| 242 | + |
| 243 | +func resourceTencentCloudMysqlBackupDownloadRestrictionDelete(d *schema.ResourceData, meta interface{}) error { |
| 244 | + defer logElapsed("resource.tencentcloud_mysql_backup_download_restriction.delete")() |
| 245 | + defer inconsistentCheck(d, meta)() |
| 246 | + |
| 247 | + return nil |
| 248 | +} |
0 commit comments