Skip to content

Commit 7f60893

Browse files
WeiMengXSWeiMengXS
andauthored
feat: add update delete protect (#2037)
* feat: add update delete protect * feat: fix create * feat: fix create * feat: fix create * feat: change log * feat: GetOkExists * fix: change case name --------- Co-authored-by: WeiMengXS <nickcchen@tencent.com>
1 parent 56cb390 commit 7f60893

File tree

4 files changed

+93
-10
lines changed

4 files changed

+93
-10
lines changed

.changelog/2037.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
resource/tencentcloud_clb_instance: support param `delete_protect`.
3+
```

tencentcloud/resource_tc_clb_instance.go

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,11 @@ func resourceTencentCloudClbInstance() *schema.Resource {
257257
Computed: true,
258258
Description: "Internet charge type, only applicable to open CLB. Valid values are `TRAFFIC_POSTPAID_BY_HOUR`, `BANDWIDTH_POSTPAID_BY_HOUR` and `BANDWIDTH_PACKAGE`.",
259259
},
260+
"delete_protect": {
261+
Type: schema.TypeBool,
262+
Optional: true,
263+
Description: "Whether to enable delete protection.",
264+
},
260265
"bandwidth_package_id": {
261266
Type: schema.TypeString,
262267
Optional: true,
@@ -642,7 +647,33 @@ func resourceTencentCloudClbInstanceCreate(d *schema.ResourceData, meta interfac
642647
return err
643648
}
644649
}
645-
650+
if v, ok := d.GetOkExists("delete_protect"); ok {
651+
isDeleteProect := v.(bool)
652+
if isDeleteProect {
653+
mRequest := clb.NewModifyLoadBalancerAttributesRequest()
654+
mRequest.LoadBalancerId = helper.String(clbId)
655+
mRequest.DeleteProtect = &isDeleteProect
656+
err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {
657+
mResponse, e := meta.(*TencentCloudClient).apiV3Conn.UseClbClient().ModifyLoadBalancerAttributes(mRequest)
658+
if e != nil {
659+
return retryError(e)
660+
} else {
661+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n",
662+
logId, mRequest.GetAction(), mRequest.ToJsonString(), mResponse.ToJsonString())
663+
requestId := *mResponse.Response.RequestId
664+
retryErr := waitForTaskFinish(requestId, meta.(*TencentCloudClient).apiV3Conn.UseClbClient())
665+
if retryErr != nil {
666+
return retryError(errors.WithStack(retryErr))
667+
}
668+
}
669+
return nil
670+
})
671+
if err != nil {
672+
log.Printf("[CRITAL]%s create CLB instance failed, reason:%+v", logId, err)
673+
return err
674+
}
675+
}
676+
}
646677
return resourceTencentCloudClbInstanceRead(d, meta)
647678
}
648679

@@ -739,6 +770,7 @@ func resourceTencentCloudClbInstanceUpdate(d *schema.ResourceData, meta interfac
739770
internet := clb.InternetAccessible{}
740771
changed := false
741772
isLoadBalancerPassToTgt := false
773+
isDeleteProtect := false
742774
snatPro := d.Get("snat_pro").(bool)
743775

744776
if d.HasChange("clb_name") {
@@ -794,6 +826,11 @@ func resourceTencentCloudClbInstanceUpdate(d *schema.ResourceData, meta interfac
794826
changed = true
795827
request.SnatPro = &snatPro
796828
}
829+
if d.HasChange("delete_protect") {
830+
changed = true
831+
isDeleteProtect = d.Get("delete_protect").(bool)
832+
request.DeleteProtect = &isDeleteProtect
833+
}
797834

798835
immutableArgs := []string{"snat_ips", "dynamic_vip", "master_zone_id", "slave_zone_id", "project_id", "vpc_id", "subnet_id", "address_ip_version", "bandwidth_package_id", "snat_pro", "zone_id"}
799836

tencentcloud/resource_tc_clb_instance_test.go

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func testSweepClbInstance(region string) error {
6969
return nil
7070
}
7171

72-
func TestAccTencentCloudClbInstance_basic(t *testing.T) {
72+
func TestAccTencentCloudClbInstanceResource_basic(t *testing.T) {
7373
t.Parallel()
7474

7575
resource.Test(t, resource.TestCase{
@@ -97,7 +97,7 @@ func TestAccTencentCloudClbInstance_basic(t *testing.T) {
9797
})
9898
}
9999

100-
func TestAccTencentCloudClbInstance_open(t *testing.T) {
100+
func TestAccTencentCloudClbInstanceResource_open(t *testing.T) {
101101
t.Parallel()
102102

103103
resource.Test(t, resource.TestCase{
@@ -138,7 +138,7 @@ func TestAccTencentCloudClbInstance_open(t *testing.T) {
138138
})
139139
}
140140

141-
func TestAccTencentCloudClbInstance_snat(t *testing.T) {
141+
func TestAccTencentCloudClbInstanceResource_snat(t *testing.T) {
142142
t.Parallel()
143143

144144
resource.Test(t, resource.TestCase{
@@ -159,7 +159,7 @@ func TestAccTencentCloudClbInstance_snat(t *testing.T) {
159159
})
160160
}
161161

162-
func TestAccTencentCloudClbInstance_internal(t *testing.T) {
162+
func TestAccTencentCloudClbInstanceResource_internal(t *testing.T) {
163163
t.Parallel()
164164

165165
resource.Test(t, resource.TestCase{
@@ -191,17 +191,30 @@ func TestAccTencentCloudClbInstance_internal(t *testing.T) {
191191
resource.TestCheckResourceAttr("tencentcloud_clb_instance.clb_internal", "tags.test", "test"),
192192
),
193193
},
194+
{
195+
Config: testAccClbInstance_updateRecover,
196+
Check: resource.ComposeTestCheckFunc(
197+
testAccCheckClbInstanceExists("tencentcloud_clb_instance.clb_internal"),
198+
resource.TestCheckResourceAttr("tencentcloud_clb_instance.clb_internal", "clb_name", InternalClbNameUpdate),
199+
resource.TestCheckResourceAttr("tencentcloud_clb_instance.clb_internal", "network_type", "INTERNAL"),
200+
resource.TestCheckResourceAttr("tencentcloud_clb_instance.clb_internal", "project_id", "0"),
201+
resource.TestCheckResourceAttrSet("tencentcloud_clb_instance.clb_internal", "vpc_id"),
202+
resource.TestCheckResourceAttrSet("tencentcloud_clb_instance.clb_internal", "subnet_id"),
203+
resource.TestCheckResourceAttrSet("tencentcloud_clb_instance.clb_internal", "delete_protect"),
204+
resource.TestCheckResourceAttr("tencentcloud_clb_instance.clb_internal", "tags.test", "test"),
205+
),
206+
},
194207
{
195208
ResourceName: "tencentcloud_clb_instance.clb_internal",
196209
ImportState: true,
197210
ImportStateVerify: true,
198-
ImportStateVerifyIgnore: []string{"dynamic_vip"},
211+
ImportStateVerifyIgnore: []string{"dynamic_vip", "delete_protect"},
199212
},
200213
},
201214
})
202215
}
203216

204-
func TestAccTencentCloudClbInstance_default_enable(t *testing.T) {
217+
func TestAccTencentCloudClbInstanceResource_default_enable(t *testing.T) {
205218
t.Parallel()
206219

207220
resource.Test(t, resource.TestCase{
@@ -243,7 +256,7 @@ func TestAccTencentCloudClbInstance_default_enable(t *testing.T) {
243256
})
244257
}
245258

246-
func TestAccTencentCloudClbInstance_multiple_instance(t *testing.T) {
259+
func TestAccTencentCloudClbInstanceResource_multiple_instance(t *testing.T) {
247260
t.Parallel()
248261

249262
resource.Test(t, resource.TestCase{
@@ -431,7 +444,7 @@ resource "tencentcloud_vpc" "foo" {
431444
432445
resource "tencentcloud_subnet" "subnet" {
433446
availability_zone = var.availability_zone
434-
name = "guagua-ci-temp-test"
447+
name = "tf-example-subnet-inc"
435448
vpc_id = tencentcloud_vpc.foo.id
436449
cidr_block = "10.0.20.0/28"
437450
is_multicast = false
@@ -443,13 +456,42 @@ resource "tencentcloud_clb_instance" "clb_internal" {
443456
vpc_id = tencentcloud_vpc.foo.id
444457
subnet_id = tencentcloud_subnet.subnet.id
445458
project_id = 0
446-
459+
delete_protect = true
447460
tags = {
448461
test = "test"
449462
}
450463
}
451464
`
465+
const testAccClbInstance_updateRecover = `
466+
variable "availability_zone" {
467+
default = "ap-guangzhou-3"
468+
}
469+
470+
resource "tencentcloud_vpc" "foo" {
471+
name = "clb-instance-internal-vpc"
472+
cidr_block = "10.0.0.0/16"
473+
}
474+
475+
resource "tencentcloud_subnet" "subnet" {
476+
availability_zone = var.availability_zone
477+
name = "tf-example-subnet-inc"
478+
vpc_id = tencentcloud_vpc.foo.id
479+
cidr_block = "10.0.20.0/28"
480+
is_multicast = false
481+
}
452482
483+
resource "tencentcloud_clb_instance" "clb_internal" {
484+
network_type = "INTERNAL"
485+
clb_name = "` + InternalClbNameUpdate + `"
486+
vpc_id = tencentcloud_vpc.foo.id
487+
subnet_id = tencentcloud_subnet.subnet.id
488+
project_id = 0
489+
delete_protect = false
490+
tags = {
491+
test = "test"
492+
}
493+
}
494+
`
453495
const testAccClbInstance_update_open = `
454496
resource "tencentcloud_security_group" "foo" {
455497
name = "clb-instance-sg"

website/docs/r/clb_instance.html.markdown

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ The following arguments are supported:
187187
* `network_type` - (Required, String, ForceNew) Type of CLB instance. Valid values: `OPEN` and `INTERNAL`.
188188
* `address_ip_version` - (Optional, String) IP version, only applicable to open CLB. Valid values are `ipv4`, `ipv6` and `IPv6FullChain`.
189189
* `bandwidth_package_id` - (Optional, String) Bandwidth package id. If set, the `internet_charge_type` must be `BANDWIDTH_PACKAGE`.
190+
* `delete_protect` - (Optional, Bool) Whether to enable delete protection.
190191
* `dynamic_vip` - (Optional, Bool) If create dynamic vip CLB instance, `true` or `false`.
191192
* `internet_bandwidth_max_out` - (Optional, Int) Max bandwidth out, only applicable to open CLB. Valid value ranges is [1, 2048]. Unit is MB.
192193
* `internet_charge_type` - (Optional, String) Internet charge type, only applicable to open CLB. Valid values are `TRAFFIC_POSTPAID_BY_HOUR`, `BANDWIDTH_POSTPAID_BY_HOUR` and `BANDWIDTH_PACKAGE`.

0 commit comments

Comments
 (0)