github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/alicloud/resource_alicloud_ess_scalingrule_test.go (about)

     1  package alicloud
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/denverdino/aliyungo/common"
     6  	"github.com/denverdino/aliyungo/ess"
     7  	"github.com/hashicorp/terraform/helper/resource"
     8  	"github.com/hashicorp/terraform/terraform"
     9  	"log"
    10  	"strings"
    11  	"testing"
    12  )
    13  
    14  func TestAccAlicloudEssScalingRule_basic(t *testing.T) {
    15  	var sc ess.ScalingRuleItemType
    16  
    17  	resource.Test(t, resource.TestCase{
    18  		PreCheck: func() {
    19  			testAccPreCheck(t)
    20  		},
    21  
    22  		// module name
    23  		IDRefreshName: "alicloud_ess_scaling_rule.foo",
    24  
    25  		Providers:    testAccProviders,
    26  		CheckDestroy: testAccCheckEssScalingRuleDestroy,
    27  		Steps: []resource.TestStep{
    28  			resource.TestStep{
    29  				Config: testAccEssScalingRuleConfig,
    30  				Check: resource.ComposeTestCheckFunc(
    31  					testAccCheckEssScalingRuleExists(
    32  						"alicloud_ess_scaling_rule.foo", &sc),
    33  					resource.TestCheckResourceAttr(
    34  						"alicloud_ess_scaling_rule.foo",
    35  						"adjustment_type",
    36  						"TotalCapacity"),
    37  					resource.TestCheckResourceAttr(
    38  						"alicloud_ess_scaling_rule.foo",
    39  						"adjustment_value",
    40  						"1"),
    41  				),
    42  			},
    43  		},
    44  	})
    45  }
    46  
    47  func TestAccAlicloudEssScalingRule_update(t *testing.T) {
    48  	var sc ess.ScalingRuleItemType
    49  
    50  	resource.Test(t, resource.TestCase{
    51  		PreCheck: func() {
    52  			testAccPreCheck(t)
    53  		},
    54  
    55  		// module name
    56  		IDRefreshName: "alicloud_ess_scaling_rule.foo",
    57  
    58  		Providers:    testAccProviders,
    59  		CheckDestroy: testAccCheckEssScalingRuleDestroy,
    60  		Steps: []resource.TestStep{
    61  			resource.TestStep{
    62  				Config: testAccEssScalingRule,
    63  				Check: resource.ComposeTestCheckFunc(
    64  					testAccCheckEssScalingRuleExists(
    65  						"alicloud_ess_scaling_rule.foo", &sc),
    66  					testAccCheckEssScalingRuleExists(
    67  						"alicloud_ess_scaling_rule.foo", &sc),
    68  					resource.TestCheckResourceAttr(
    69  						"alicloud_ess_scaling_rule.foo",
    70  						"adjustment_type",
    71  						"TotalCapacity"),
    72  					resource.TestCheckResourceAttr(
    73  						"alicloud_ess_scaling_rule.foo",
    74  						"adjustment_value",
    75  						"1"),
    76  				),
    77  			},
    78  
    79  			resource.TestStep{
    80  				Config: testAccEssScalingRule_update,
    81  				Check: resource.ComposeTestCheckFunc(
    82  					testAccCheckEssScalingRuleExists(
    83  						"alicloud_ess_scaling_rule.foo", &sc),
    84  					testAccCheckEssScalingRuleExists(
    85  						"alicloud_ess_scaling_rule.foo", &sc),
    86  					resource.TestCheckResourceAttr(
    87  						"alicloud_ess_scaling_rule.foo",
    88  						"adjustment_type",
    89  						"TotalCapacity"),
    90  					resource.TestCheckResourceAttr(
    91  						"alicloud_ess_scaling_rule.foo",
    92  						"adjustment_value",
    93  						"2"),
    94  				),
    95  			},
    96  		},
    97  	})
    98  }
    99  
   100  func testAccCheckEssScalingRuleExists(n string, d *ess.ScalingRuleItemType) resource.TestCheckFunc {
   101  	return func(s *terraform.State) error {
   102  		rs, ok := s.RootModule().Resources[n]
   103  		if !ok {
   104  			return fmt.Errorf("Not found: %s", n)
   105  		}
   106  
   107  		if rs.Primary.ID == "" {
   108  			return fmt.Errorf("No ESS Scaling Rule ID is set")
   109  		}
   110  
   111  		client := testAccProvider.Meta().(*AliyunClient)
   112  		ids := strings.Split(rs.Primary.ID, COLON_SEPARATED)
   113  		attr, err := client.DescribeScalingRuleById(ids[0], ids[1])
   114  		log.Printf("[DEBUG] check scaling rule %s attribute %#v", rs.Primary.ID, attr)
   115  
   116  		if err != nil {
   117  			return err
   118  		}
   119  
   120  		if attr == nil {
   121  			return fmt.Errorf("Scaling rule not found")
   122  		}
   123  
   124  		*d = *attr
   125  		return nil
   126  	}
   127  }
   128  
   129  func testAccCheckEssScalingRuleDestroy(s *terraform.State) error {
   130  	client := testAccProvider.Meta().(*AliyunClient)
   131  
   132  	for _, rs := range s.RootModule().Resources {
   133  		if rs.Type != "alicloud_ess_scaling_rule" {
   134  			continue
   135  		}
   136  		ids := strings.Split(rs.Primary.ID, COLON_SEPARATED)
   137  		ins, err := client.DescribeScalingRuleById(ids[0], ids[1])
   138  
   139  		if ins != nil {
   140  			return fmt.Errorf("Error ESS scaling rule still exist")
   141  		}
   142  
   143  		// Verify the error is what we want
   144  		if err != nil {
   145  			// Verify the error is what we want
   146  			e, _ := err.(*common.Error)
   147  			if e.ErrorResponse.Code == InstanceNotfound {
   148  				continue
   149  			}
   150  			return err
   151  		}
   152  	}
   153  
   154  	return nil
   155  }
   156  
   157  const testAccEssScalingRuleConfig = `
   158  data "alicloud_images" "ecs_image" {
   159    most_recent = true
   160    name_regex =  "^centos_6\\w{1,5}[64].*"
   161  }
   162  
   163  resource "alicloud_security_group" "tf_test_foo" {
   164  	description = "foo"
   165  }
   166  
   167  resource "alicloud_security_group_rule" "ssh-in" {
   168    	type = "ingress"
   169    	ip_protocol = "tcp"
   170    	nic_type = "internet"
   171    	policy = "accept"
   172    	port_range = "22/22"
   173    	priority = 1
   174    	security_group_id = "${alicloud_security_group.tf_test_foo.id}"
   175    	cidr_ip = "0.0.0.0/0"
   176  }
   177  
   178  resource "alicloud_ess_scaling_group" "bar" {
   179  	min_size = 1
   180  	max_size = 1
   181  	scaling_group_name = "bar"
   182  	removal_policies = ["OldestInstance", "NewestInstance"]
   183  }
   184  
   185  resource "alicloud_ess_scaling_configuration" "foo" {
   186  	scaling_group_id = "${alicloud_ess_scaling_group.bar.id}"
   187  
   188  	image_id = "${data.alicloud_images.ecs_image.images.0.id}"
   189  	instance_type = "ecs.s2.large"
   190  	io_optimized = "optimized"
   191  	security_group_id = "${alicloud_security_group.tf_test_foo.id}"
   192  }
   193  
   194  resource "alicloud_ess_scaling_rule" "foo" {
   195  	scaling_group_id = "${alicloud_ess_scaling_group.bar.id}"
   196  	adjustment_type = "TotalCapacity"
   197  	adjustment_value = 1
   198  	cooldown = 120
   199  }
   200  `
   201  
   202  const testAccEssScalingRule = `
   203  data "alicloud_images" "ecs_image" {
   204    most_recent = true
   205    name_regex =  "^centos_6\\w{1,5}[64].*"
   206  }
   207  
   208  resource "alicloud_security_group" "tf_test_foo" {
   209  	description = "foo"
   210  }
   211  
   212  resource "alicloud_security_group_rule" "ssh-in" {
   213    	type = "ingress"
   214    	ip_protocol = "tcp"
   215    	nic_type = "internet"
   216    	policy = "accept"
   217    	port_range = "22/22"
   218    	priority = 1
   219    	security_group_id = "${alicloud_security_group.tf_test_foo.id}"
   220    	cidr_ip = "0.0.0.0/0"
   221  }
   222  
   223  resource "alicloud_ess_scaling_group" "bar" {
   224  	min_size = 1
   225  	max_size = 1
   226  	scaling_group_name = "bar"
   227  	removal_policies = ["OldestInstance", "NewestInstance"]
   228  }
   229  
   230  resource "alicloud_ess_scaling_configuration" "foo" {
   231  	scaling_group_id = "${alicloud_ess_scaling_group.bar.id}"
   232  
   233  	image_id = "${data.alicloud_images.ecs_image.images.0.id}"
   234  	instance_type = "ecs.s2.large"
   235  	io_optimized = "optimized"
   236  	security_group_id = "${alicloud_security_group.tf_test_foo.id}"
   237  }
   238  
   239  resource "alicloud_ess_scaling_rule" "foo" {
   240  	scaling_group_id = "${alicloud_ess_scaling_group.bar.id}"
   241  	adjustment_type = "TotalCapacity"
   242  	adjustment_value = 1
   243  	cooldown = 120
   244  }
   245  `
   246  
   247  const testAccEssScalingRule_update = `
   248  data "alicloud_images" "ecs_image" {
   249    most_recent = true
   250    name_regex =  "^centos_6\\w{1,5}[64].*"
   251  }
   252  
   253  resource "alicloud_security_group" "tf_test_foo" {
   254  	description = "foo"
   255  }
   256  
   257  resource "alicloud_security_group_rule" "ssh-in" {
   258    	type = "ingress"
   259    	ip_protocol = "tcp"
   260    	nic_type = "internet"
   261    	policy = "accept"
   262    	port_range = "22/22"
   263    	priority = 1
   264    	security_group_id = "${alicloud_security_group.tf_test_foo.id}"
   265    	cidr_ip = "0.0.0.0/0"
   266  }
   267  
   268  resource "alicloud_ess_scaling_group" "bar" {
   269  	min_size = 1
   270  	max_size = 1
   271  	scaling_group_name = "bar"
   272  	removal_policies = ["OldestInstance", "NewestInstance"]
   273  }
   274  
   275  resource "alicloud_ess_scaling_configuration" "foo" {
   276  	scaling_group_id = "${alicloud_ess_scaling_group.bar.id}"
   277  
   278  	image_id = "${data.alicloud_images.ecs_image.images.0.id}"
   279  	instance_type = "ecs.s2.large"
   280  	io_optimized = "optimized"
   281  	security_group_id = "${alicloud_security_group.tf_test_foo.id}"
   282  }
   283  
   284  resource "alicloud_ess_scaling_rule" "foo" {
   285  	scaling_group_id = "${alicloud_ess_scaling_group.bar.id}"
   286  	adjustment_type = "TotalCapacity"
   287  	adjustment_value = 2
   288  	cooldown = 60
   289  }
   290  `