Skip to content

Commit

Permalink
testcase: Adds a new test case for resource alicloud_slb_load_balancer
Browse files Browse the repository at this point in the history
  • Loading branch information
thisnihonglei committed Jan 23, 2022
1 parent 848ff0b commit abf5f17
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 0 deletions.
4 changes: 4 additions & 0 deletions alicloud/resource_alicloud_slb_load_balancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,10 @@ func resourceAlicloudSlbLoadBalancerUpdate(d *schema.ResourceData, meta interfac
return resourceAlicloudSlbLoadBalancerRead(d, meta)
}
func resourceAlicloudSlbLoadBalancerDelete(d *schema.ResourceData, meta interface{}) error {
if d.Get("payment_type").(string) == "Subscription" || d.Get("instance_charge_type").(string) == "Prepaid" {
log.Printf("[WARN] Cannot destroy Subscription resource: alicloud_slb_load_balancer. Terraform will remove this resource from the state file, however resources may remain.")
return nil
}
client := meta.(*connectivity.AliyunClient)
slbService := SlbService{client}
action := "DeleteLoadBalancer"
Expand Down
109 changes: 109 additions & 0 deletions alicloud/resource_alicloud_slb_load_balancer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -508,3 +508,112 @@ data "alicloud_vpcs" "default"{
}
`, name)
}

func TestAccAlicloudSlbLoadBalancer_basic2(t *testing.T) {
var v map[string]interface{}
resourceId := "alicloud_slb_load_balancer.default"
ra := resourceAttrInit(resourceId, AlicloudSlbLoadBalancerMap2)
rc := resourceCheckInitWithDescribeMethod(resourceId, &v, func() interface{} {
return &SlbService{testAccProvider.Meta().(*connectivity.AliyunClient)}
}, "DescribeSlbLoadBalancer")
rac := resourceAttrCheckInit(rc, ra)
testAccCheck := rac.resourceAttrMapUpdateSet()
rand := acctest.RandIntRange(10000, 99999)
name := fmt.Sprintf("tf-testacc%sslbloadbalancer%d", defaultRegionToTest, rand)
testAccConfig := resourceTestAccConfigFunc(resourceId, name, AlicloudSlbLoadBalancerBasicDependence2)
resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
},

IDRefreshName: resourceId,
Providers: testAccProviders,
CheckDestroy: rac.checkResourceDestroy(),
Steps: []resource.TestStep{
{
Config: testAccConfig(map[string]interface{}{
"address_type": "intranet",
"name": name,
"specification": "slb.s1.small",
"vswitch_id": "${local.vswitch_id}",
"address": "${cidrhost(data.alicloud_vswitches.default.vswitches.0.cidr_block, 1)}",
"master_zone_id": "${data.alicloud_zones.default.zones.0.id}",
"modification_protection_status": "ConsoleProtection",
"modification_protection_reason": name,
"payment_type": "PayAsYouGo",
"resource_group_id": "${data.alicloud_resource_manager_resource_groups.default.ids.0}",
"slave_zone_id": "${data.alicloud_zones.default.zones.1.id}",
}),
Check: resource.ComposeTestCheckFunc(
testAccCheck(map[string]string{
"address_type": "intranet",
"name": name,
"specification": "slb.s1.small",
"vswitch_id": CHECKSET,
"address": CHECKSET,
"master_zone_id": CHECKSET,
"modification_protection_status": "ConsoleProtection",
"modification_protection_reason": name,
"payment_type": "PayAsYouGo",
"resource_group_id": CHECKSET,
"slave_zone_id": CHECKSET,
}),
),
},
{
ResourceName: resourceId,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

var AlicloudSlbLoadBalancerMap2 = map[string]string{
"address_ip_version": "ipv4",
"address_type": "intranet",
"bandwidth": CHECKSET,
"delete_protection": "off",
"internet_charge_type": "PayByTraffic",
"resource_group_id": CHECKSET,
"slave_zone_id": CHECKSET,
"load_balancer_spec": "slb.s1.small",
"status": "active",
"tags.#": "0",
}

func AlicloudSlbLoadBalancerBasicDependence2(name string) string {
return fmt.Sprintf(`
variable "name" {
default = "%s"
}
data "alicloud_zones" "default" {
available_resource_creation = "VSwitch"
}
data "alicloud_resource_manager_resource_groups" "default" {
name_regex = "^default$"
}
data "alicloud_vpcs" "default" {
name_regex = "default-NODELETING"
}
data "alicloud_vswitches" "default" {
vpc_id = data.alicloud_vpcs.default.ids.0
zone_id = data.alicloud_zones.default.zones.0.id
}
resource "alicloud_vswitch" "vswitch" {
count = length(data.alicloud_vswitches.default.ids) > 0 ? 0 : 1
vpc_id = data.alicloud_vpcs.default.ids.0
cidr_block = cidrsubnet(data.alicloud_vpcs.default.vpcs[0].cidr_block, 8, 8)
zone_id = data.alicloud_zones.default.zones.0.id
vswitch_name = var.name
}
locals {
vswitch_id = length(data.alicloud_vswitches.default.ids) > 0 ? data.alicloud_vswitches.default.ids[0] : concat(alicloud_vswitch.vswitch.*.id, [""])[0]
}
`, name)
}
10 changes: 10 additions & 0 deletions website/docs/r/slb_load_balancer.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ resource "alicloud_slb_load_balancer" "default" {
}
```

### Deleting `alicloud_slb_load_balancer` or removing it from your configuration

The `alicloud_slb_load_balancer` resource allows you to manage `payment_type = "Subscription"` or `instance_charge_type = "Prepaid"` load balancer, but Terraform cannot destroy it.
Deleting the subscription resource or removing it from your configuration will remove it from your state file and management, but will not destroy the Load Balancer.
You can resume managing the subscription load balancer via the AlibabaCloud Console.

## Argument Reference

The following arguments are supported:
Expand Down Expand Up @@ -80,6 +86,10 @@ Terraform will autogenerate a name beginning with `tf-lb`.
* `modification_protection_reason` - (Optional) The reason of modification protection. It's effective when `modification_protection_status` is `ConsoleProtection`.
* `modification_protection_status` - (Optional) The status of modification protection. Valid values: `ConsoleProtection` and `NonProtection`. Default value is `NonProtection`.
* `status` - (Optional) The status of slb load balancer. Valid values: `active` and `inactice`. The system default value is `active`.
* `name` - (Optional, Deprecated form v1.123.1) Field `name` has been deprecated from provider version 1.123.1 New field `load_balancer_name` instead.
* `instance_charge_type` - (Optional, Deprecated form v1.124.0) Field `instance_charge_type` has been deprecated from provider version 1.124.0 New field `payment_type` instead.
* `specification` - (Optional, Deprecated form v1.123.1) Field `specification` has been deprecated from provider version 1.123.1 New field `load_balancer_spec` instead.
* `internet` - (Optional, Deprecated form v1.124.0) Field `internet` has been deprecated from provider version 1.124.0 New field `address_type` instead.

-> **NOTE:** A "Shared-Performance" instance can be changed to "Performance-guaranteed", but the change is irreversible.

Expand Down

0 comments on commit abf5f17

Please sign in to comment.