github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/alicloud/resource_alicloud_ess_scalingconfiguration_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  	"regexp"
    11  	"strings"
    12  	"testing"
    13  )
    14  
    15  func TestAccAlicloudEssScalingConfiguration_basic(t *testing.T) {
    16  	var sc ess.ScalingConfigurationItemType
    17  
    18  	resource.Test(t, resource.TestCase{
    19  		PreCheck: func() {
    20  			testAccPreCheck(t)
    21  		},
    22  
    23  		// module name
    24  		IDRefreshName: "alicloud_ess_scaling_configuration.foo",
    25  
    26  		Providers:    testAccProviders,
    27  		CheckDestroy: testAccCheckEssScalingConfigurationDestroy,
    28  		Steps: []resource.TestStep{
    29  			resource.TestStep{
    30  				Config: testAccEssScalingConfigurationConfig,
    31  				Check: resource.ComposeTestCheckFunc(
    32  					testAccCheckEssScalingConfigurationExists(
    33  						"alicloud_ess_scaling_configuration.foo", &sc),
    34  					resource.TestCheckResourceAttr(
    35  						"alicloud_ess_scaling_configuration.foo",
    36  						"instance_type",
    37  						"ecs.s2.large"),
    38  					resource.TestMatchResourceAttr(
    39  						"alicloud_ess_scaling_configuration.foo",
    40  						"image_id",
    41  						regexp.MustCompile("^centos_6")),
    42  				),
    43  			},
    44  		},
    45  	})
    46  }
    47  
    48  func TestAccAlicloudEssScalingConfiguration_multiConfig(t *testing.T) {
    49  	var sc ess.ScalingConfigurationItemType
    50  
    51  	resource.Test(t, resource.TestCase{
    52  		PreCheck: func() {
    53  			testAccPreCheck(t)
    54  		},
    55  
    56  		// module name
    57  		IDRefreshName: "alicloud_ess_scaling_configuration.bar",
    58  
    59  		Providers:    testAccProviders,
    60  		CheckDestroy: testAccCheckEssScalingConfigurationDestroy,
    61  		Steps: []resource.TestStep{
    62  			resource.TestStep{
    63  				Config: testAccEssScalingConfiguration_multiConfig,
    64  				Check: resource.ComposeTestCheckFunc(
    65  					testAccCheckEssScalingConfigurationExists(
    66  						"alicloud_ess_scaling_configuration.bar", &sc),
    67  					resource.TestCheckResourceAttr(
    68  						"alicloud_ess_scaling_configuration.bar",
    69  						"active",
    70  						"false"),
    71  					resource.TestCheckResourceAttr(
    72  						"alicloud_ess_scaling_configuration.bar",
    73  						"instance_type",
    74  						"ecs.s2.large"),
    75  					resource.TestMatchResourceAttr(
    76  						"alicloud_ess_scaling_configuration.bar",
    77  						"image_id",
    78  						regexp.MustCompile("^centos_6")),
    79  				),
    80  			},
    81  		},
    82  	})
    83  }
    84  
    85  func SkipTestAccAlicloudEssScalingConfiguration_active(t *testing.T) {
    86  	var sc ess.ScalingConfigurationItemType
    87  
    88  	resource.Test(t, resource.TestCase{
    89  		PreCheck: func() {
    90  			testAccPreCheck(t)
    91  		},
    92  
    93  		// module name
    94  		IDRefreshName: "alicloud_ess_scaling_configuration.bar",
    95  
    96  		Providers:    testAccProviders,
    97  		CheckDestroy: testAccCheckEssScalingConfigurationDestroy,
    98  		Steps: []resource.TestStep{
    99  			resource.TestStep{
   100  				Config: testAccEssScalingConfiguration_active,
   101  				Check: resource.ComposeTestCheckFunc(
   102  					testAccCheckEssScalingConfigurationExists(
   103  						"alicloud_ess_scaling_configuration.bar", &sc),
   104  					resource.TestCheckResourceAttr(
   105  						"alicloud_ess_scaling_configuration.bar",
   106  						"active",
   107  						"true"),
   108  					resource.TestCheckResourceAttr(
   109  						"alicloud_ess_scaling_configuration.bar",
   110  						"instance_type",
   111  						"ecs.s2.large"),
   112  					resource.TestMatchResourceAttr(
   113  						"alicloud_ess_scaling_configuration.bar",
   114  						"image_id",
   115  						regexp.MustCompile("^centos_6")),
   116  				),
   117  			},
   118  
   119  			resource.TestStep{
   120  				Config: testAccEssScalingConfiguration_inActive,
   121  				Check: resource.ComposeTestCheckFunc(
   122  					testAccCheckEssScalingConfigurationExists(
   123  						"alicloud_ess_scaling_configuration.bar", &sc),
   124  					resource.TestCheckResourceAttr(
   125  						"alicloud_ess_scaling_configuration.bar",
   126  						"active",
   127  						"false"),
   128  					resource.TestCheckResourceAttr(
   129  						"alicloud_ess_scaling_configuration.bar",
   130  						"instance_type",
   131  						"ecs.s2.large"),
   132  					resource.TestMatchResourceAttr(
   133  						"alicloud_ess_scaling_configuration.bar",
   134  						"image_id",
   135  						regexp.MustCompile("^centos_6")),
   136  				),
   137  			},
   138  		},
   139  	})
   140  }
   141  
   142  func SkipTestAccAlicloudEssScalingConfiguration_enable(t *testing.T) {
   143  	var sc ess.ScalingConfigurationItemType
   144  
   145  	resource.Test(t, resource.TestCase{
   146  		PreCheck: func() {
   147  			testAccPreCheck(t)
   148  		},
   149  
   150  		// module name
   151  		IDRefreshName: "alicloud_ess_scaling_configuration.foo",
   152  
   153  		Providers:    testAccProviders,
   154  		CheckDestroy: testAccCheckEssScalingConfigurationDestroy,
   155  		Steps: []resource.TestStep{
   156  			resource.TestStep{
   157  				Config: testAccEssScalingConfiguration_enable,
   158  				Check: resource.ComposeTestCheckFunc(
   159  					testAccCheckEssScalingConfigurationExists(
   160  						"alicloud_ess_scaling_configuration.foo", &sc),
   161  					resource.TestCheckResourceAttr(
   162  						"alicloud_ess_scaling_configuration.foo",
   163  						"enable",
   164  						"true"),
   165  					resource.TestCheckResourceAttr(
   166  						"alicloud_ess_scaling_configuration.foo",
   167  						"instance_type",
   168  						"ecs.s2.large"),
   169  					resource.TestMatchResourceAttr(
   170  						"alicloud_ess_scaling_configuration.foo",
   171  						"image_id",
   172  						regexp.MustCompile("^centos_6")),
   173  				),
   174  			},
   175  
   176  			resource.TestStep{
   177  				Config: testAccEssScalingConfiguration_disable,
   178  				Check: resource.ComposeTestCheckFunc(
   179  					testAccCheckEssScalingConfigurationExists(
   180  						"alicloud_ess_scaling_configuration.foo", &sc),
   181  					resource.TestCheckResourceAttr(
   182  						"alicloud_ess_scaling_configuration.foo",
   183  						"enable",
   184  						"false"),
   185  					resource.TestCheckResourceAttr(
   186  						"alicloud_ess_scaling_configuration.foo",
   187  						"instance_type",
   188  						"ecs.s2.large"),
   189  					resource.TestMatchResourceAttr(
   190  						"alicloud_ess_scaling_configuration.foo",
   191  						"image_id",
   192  						regexp.MustCompile("^centos_6")),
   193  				),
   194  			},
   195  		},
   196  	})
   197  }
   198  
   199  func testAccCheckEssScalingConfigurationExists(n string, d *ess.ScalingConfigurationItemType) resource.TestCheckFunc {
   200  	return func(s *terraform.State) error {
   201  		rs, ok := s.RootModule().Resources[n]
   202  		if !ok {
   203  			return fmt.Errorf("Not found: %s", n)
   204  		}
   205  
   206  		if rs.Primary.ID == "" {
   207  			return fmt.Errorf("No ESS Scaling Configuration ID is set")
   208  		}
   209  
   210  		client := testAccProvider.Meta().(*AliyunClient)
   211  		ids := strings.Split(rs.Primary.ID, COLON_SEPARATED)
   212  		attr, err := client.DescribeScalingConfigurationById(ids[0], ids[1])
   213  		log.Printf("[DEBUG] check scaling configuration %s attribute %#v", rs.Primary.ID, attr)
   214  
   215  		if err != nil {
   216  			return err
   217  		}
   218  
   219  		if attr == nil {
   220  			return fmt.Errorf("Scaling Configuration not found")
   221  		}
   222  
   223  		*d = *attr
   224  		return nil
   225  	}
   226  }
   227  
   228  func testAccCheckEssScalingConfigurationDestroy(s *terraform.State) error {
   229  	client := testAccProvider.Meta().(*AliyunClient)
   230  
   231  	for _, rs := range s.RootModule().Resources {
   232  		if rs.Type != "alicloud_ess_scaling_configuration" {
   233  			continue
   234  		}
   235  		ids := strings.Split(rs.Primary.ID, COLON_SEPARATED)
   236  		ins, err := client.DescribeScalingConfigurationById(ids[0], ids[1])
   237  
   238  		if ins != nil {
   239  			return fmt.Errorf("Error ESS scaling configuration still exist")
   240  		}
   241  
   242  		// Verify the error is what we want
   243  		if err != nil {
   244  			// Verify the error is what we want
   245  			e, _ := err.(*common.Error)
   246  			if e.ErrorResponse.Code == InstanceNotfound {
   247  				continue
   248  			}
   249  			return err
   250  		}
   251  	}
   252  
   253  	return nil
   254  }
   255  
   256  const testAccEssScalingConfigurationConfig = `
   257  data "alicloud_images" "ecs_image" {
   258    most_recent = true
   259    name_regex =  "^centos_6\\w{1,5}[64].*"
   260  }
   261  
   262  resource "alicloud_security_group" "tf_test_foo" {
   263  	description = "foo"
   264  }
   265  
   266  resource "alicloud_security_group_rule" "ssh-in" {
   267    	type = "ingress"
   268    	ip_protocol = "tcp"
   269    	nic_type = "internet"
   270    	policy = "accept"
   271    	port_range = "22/22"
   272    	priority = 1
   273    	security_group_id = "${alicloud_security_group.tf_test_foo.id}"
   274    	cidr_ip = "0.0.0.0/0"
   275  }
   276  
   277  resource "alicloud_ess_scaling_group" "foo" {
   278  	min_size = 1
   279  	max_size = 1
   280  	scaling_group_name = "foo"
   281  	removal_policies = ["OldestInstance", "NewestInstance"]
   282  }
   283  
   284  resource "alicloud_ess_scaling_configuration" "foo" {
   285  	scaling_group_id = "${alicloud_ess_scaling_group.foo.id}"
   286  
   287  	image_id = "${data.alicloud_images.ecs_image.images.0.id}"
   288  	instance_type = "ecs.s2.large"
   289  	io_optimized = "optimized"
   290  	security_group_id = "${alicloud_security_group.tf_test_foo.id}"
   291  }
   292  `
   293  
   294  const testAccEssScalingConfiguration_multiConfig = `
   295  data "alicloud_images" "ecs_image" {
   296    most_recent = true
   297    name_regex =  "^centos_6\\w{1,5}[64].*"
   298  }
   299  
   300  resource "alicloud_security_group" "tf_test_foo" {
   301  	description = "foo"
   302  }
   303  
   304  resource "alicloud_security_group_rule" "ssh-in" {
   305    	type = "ingress"
   306    	ip_protocol = "tcp"
   307    	nic_type = "internet"
   308    	policy = "accept"
   309    	port_range = "22/22"
   310    	priority = 1
   311    	security_group_id = "${alicloud_security_group.tf_test_foo.id}"
   312    	cidr_ip = "0.0.0.0/0"
   313  }
   314  
   315  resource "alicloud_ess_scaling_group" "foo" {
   316  	min_size = 1
   317  	max_size = 1
   318  	scaling_group_name = "foo"
   319  	removal_policies = ["OldestInstance", "NewestInstance"]
   320  }
   321  
   322  resource "alicloud_ess_scaling_configuration" "foo" {
   323  	scaling_group_id = "${alicloud_ess_scaling_group.foo.id}"
   324  
   325  	image_id = "${data.alicloud_images.ecs_image.images.0.id}"
   326  	instance_type = "ecs.s2.large"
   327  	io_optimized = "optimized"
   328  	security_group_id = "${alicloud_security_group.tf_test_foo.id}"
   329  }
   330  
   331  resource "alicloud_ess_scaling_configuration" "bar" {
   332  	scaling_group_id = "${alicloud_ess_scaling_group.foo.id}"
   333  
   334  	image_id = "${data.alicloud_images.ecs_image.images.0.id}"
   335  	instance_type = "ecs.s2.large"
   336  	io_optimized = "optimized"
   337  	security_group_id = "${alicloud_security_group.tf_test_foo.id}"
   338  }
   339  `
   340  
   341  const testAccEssScalingConfiguration_active = `
   342  data "alicloud_images" "ecs_image" {
   343    most_recent = true
   344    name_regex =  "^centos_6\\w{1,5}[64].*"
   345  }
   346  
   347  resource "alicloud_security_group" "tf_test_foo" {
   348  	description = "foo"
   349  }
   350  
   351  resource "alicloud_security_group_rule" "ssh-in" {
   352    	type = "ingress"
   353    	ip_protocol = "tcp"
   354    	nic_type = "internet"
   355    	policy = "accept"
   356    	port_range = "22/22"
   357    	priority = 1
   358    	security_group_id = "${alicloud_security_group.tf_test_foo.id}"
   359    	cidr_ip = "0.0.0.0/0"
   360  }
   361  
   362  resource "alicloud_ess_scaling_group" "foo" {
   363  	min_size = 1
   364  	max_size = 1
   365  	scaling_group_name = "foo"
   366  	removal_policies = ["OldestInstance", "NewestInstance"]
   367  }
   368  
   369  resource "alicloud_ess_scaling_configuration" "foo" {
   370  	scaling_group_id = "${alicloud_ess_scaling_group.foo.id}"
   371  	active = true
   372  
   373  	image_id = "${data.alicloud_images.ecs_image.images.0.id}"
   374  	instance_type = "ecs.s2.large"
   375  	io_optimized = "optimized"
   376  	security_group_id = "${alicloud_security_group.tf_test_foo.id}"
   377  }
   378  `
   379  
   380  const testAccEssScalingConfiguration_inActive = `
   381  data "alicloud_images" "ecs_image" {
   382    most_recent = true
   383    name_regex =  "^centos_6\\w{1,5}[64].*"
   384  }
   385  
   386  resource "alicloud_security_group" "tf_test_foo" {
   387  	description = "foo"
   388  }
   389  
   390  resource "alicloud_security_group_rule" "ssh-in" {
   391    	type = "ingress"
   392    	ip_protocol = "tcp"
   393    	nic_type = "internet"
   394    	policy = "accept"
   395    	port_range = "22/22"
   396    	priority = 1
   397    	security_group_id = "${alicloud_security_group.tf_test_foo.id}"
   398    	cidr_ip = "0.0.0.0/0"
   399  }
   400  
   401  resource "alicloud_ess_scaling_group" "foo" {
   402  	min_size = 1
   403  	max_size = 1
   404  	scaling_group_name = "foo"
   405  	removal_policies = ["OldestInstance", "NewestInstance"]
   406  }
   407  
   408  resource "alicloud_ess_scaling_configuration" "foo" {
   409  	scaling_group_id = "${alicloud_ess_scaling_group.foo.id}"
   410  	active = false
   411  
   412  	image_id = "${data.alicloud_images.ecs_image.images.0.id}"
   413  	instance_type = "ecs.s2.large"
   414  	io_optimized = "optimized"
   415  	security_group_id = "${alicloud_security_group.tf_test_foo.id}"
   416  }
   417  `
   418  
   419  const testAccEssScalingConfiguration_enable = `
   420  data "alicloud_images" "ecs_image" {
   421    most_recent = true
   422    name_regex =  "^centos_6\\w{1,5}[64].*"
   423  }
   424  
   425  resource "alicloud_security_group" "tf_test_foo" {
   426  	description = "foo"
   427  }
   428  
   429  resource "alicloud_security_group_rule" "ssh-in" {
   430    	type = "ingress"
   431    	ip_protocol = "tcp"
   432    	nic_type = "internet"
   433    	policy = "accept"
   434    	port_range = "22/22"
   435    	priority = 1
   436    	security_group_id = "${alicloud_security_group.tf_test_foo.id}"
   437    	cidr_ip = "0.0.0.0/0"
   438  }
   439  
   440  resource "alicloud_ess_scaling_group" "foo" {
   441  	min_size = 1
   442  	max_size = 1
   443  	scaling_group_name = "foo"
   444  	removal_policies = ["OldestInstance", "NewestInstance"]
   445  }
   446  
   447  resource "alicloud_ess_scaling_configuration" "foo" {
   448  	scaling_group_id = "${alicloud_ess_scaling_group.foo.id}"
   449  	enable = true
   450  
   451  	image_id = "${data.alicloud_images.ecs_image.images.0.id}"
   452  	instance_type = "ecs.s2.large"
   453  	io_optimized = "optimized"
   454  	security_group_id = "${alicloud_security_group.tf_test_foo.id}"
   455  }
   456  `
   457  
   458  const testAccEssScalingConfiguration_disable = `
   459  data "alicloud_images" "ecs_image" {
   460    most_recent = true
   461    name_regex =  "^centos_6\\w{1,5}[64].*"
   462  }
   463  
   464  resource "alicloud_security_group" "tf_test_foo" {
   465  	description = "foo"
   466  }
   467  
   468  resource "alicloud_security_group_rule" "ssh-in" {
   469    	type = "ingress"
   470    	ip_protocol = "tcp"
   471    	nic_type = "internet"
   472    	policy = "accept"
   473    	port_range = "22/22"
   474    	priority = 1
   475    	security_group_id = "${alicloud_security_group.tf_test_foo.id}"
   476    	cidr_ip = "0.0.0.0/0"
   477  }
   478  
   479  resource "alicloud_ess_scaling_group" "foo" {
   480  	min_size = 1
   481  	max_size = 1
   482  	scaling_group_name = "foo"
   483  	removal_policies = ["OldestInstance", "NewestInstance"]
   484  }
   485  
   486  resource "alicloud_ess_scaling_configuration" "foo" {
   487  	scaling_group_id = "${alicloud_ess_scaling_group.foo.id}"
   488  	enable = false
   489  
   490  	image_id = "${data.alicloud_images.ecs_image.images.0.id}"
   491  	instance_type = "ecs.s2.large"
   492  	io_optimized = "optimized"
   493  	security_group_id = "${alicloud_security_group.tf_test_foo.id}"
   494  }
   495  `