github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/alicloud/resource_alicloud_instance_test.go (about)

     1  package alicloud
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/denverdino/aliyungo/common"
     8  	"github.com/denverdino/aliyungo/ecs"
     9  	"github.com/hashicorp/terraform/helper/resource"
    10  	"github.com/hashicorp/terraform/helper/schema"
    11  	"github.com/hashicorp/terraform/terraform"
    12  	"log"
    13  )
    14  
    15  func TestAccAlicloudInstance_basic(t *testing.T) {
    16  	var instance ecs.InstanceAttributesType
    17  
    18  	testCheck := func(*terraform.State) error {
    19  		log.Printf("[WARN] instances: %#v", instance)
    20  		if instance.ZoneId == "" {
    21  			return fmt.Errorf("bad availability zone")
    22  		}
    23  		if len(instance.SecurityGroupIds.SecurityGroupId) == 0 {
    24  			return fmt.Errorf("no security group: %#v", instance.SecurityGroupIds.SecurityGroupId)
    25  		}
    26  
    27  		return nil
    28  	}
    29  
    30  	resource.Test(t, resource.TestCase{
    31  		PreCheck: func() {
    32  			testAccPreCheck(t)
    33  		},
    34  
    35  		// module name
    36  		IDRefreshName: "alicloud_instance.foo",
    37  
    38  		Providers:    testAccProviders,
    39  		CheckDestroy: testAccCheckInstanceDestroy,
    40  		Steps: []resource.TestStep{
    41  			resource.TestStep{
    42  				Config: testAccInstanceConfig,
    43  				Check: resource.ComposeTestCheckFunc(
    44  					testAccCheckInstanceExists(
    45  						"alicloud_instance.foo", &instance),
    46  					testCheck,
    47  					resource.TestCheckResourceAttr(
    48  						"alicloud_instance.foo",
    49  						"image_id",
    50  						"ubuntu_140405_32_40G_cloudinit_20161115.vhd"),
    51  					resource.TestCheckResourceAttr(
    52  						"alicloud_instance.foo",
    53  						"instance_name",
    54  						"test_foo"),
    55  					resource.TestCheckResourceAttr(
    56  						"alicloud_instance.foo",
    57  						"internet_charge_type",
    58  						"PayByBandwidth"),
    59  				),
    60  			},
    61  
    62  			// test for multi steps
    63  			resource.TestStep{
    64  				Config: testAccInstanceConfig,
    65  				Check: resource.ComposeTestCheckFunc(
    66  					testAccCheckInstanceExists(
    67  						"alicloud_instance.foo", &instance),
    68  					testCheck,
    69  					resource.TestCheckResourceAttr(
    70  						"alicloud_instance.foo",
    71  						"image_id",
    72  						"ubuntu_140405_32_40G_cloudinit_20161115.vhd"),
    73  					resource.TestCheckResourceAttr(
    74  						"alicloud_instance.foo",
    75  						"instance_name",
    76  						"test_foo"),
    77  				),
    78  			},
    79  		},
    80  	})
    81  
    82  }
    83  
    84  func TestAccAlicloudInstance_vpc(t *testing.T) {
    85  	var instance ecs.InstanceAttributesType
    86  
    87  	resource.Test(t, resource.TestCase{
    88  		PreCheck: func() {
    89  			testAccPreCheck(t)
    90  		},
    91  		IDRefreshName: "alicloud_instance.foo",
    92  		Providers:     testAccProviders,
    93  		CheckDestroy:  testAccCheckInstanceDestroy,
    94  		Steps: []resource.TestStep{
    95  			resource.TestStep{
    96  				Config: testAccInstanceConfigVPC,
    97  				Check: resource.ComposeTestCheckFunc(
    98  					testAccCheckInstanceExists(
    99  						"alicloud_instance.foo", &instance),
   100  					resource.TestCheckResourceAttr(
   101  						"alicloud_instance.foo",
   102  						"system_disk_category",
   103  						"cloud_efficiency"),
   104  					resource.TestCheckResourceAttr(
   105  						"alicloud_instance.foo",
   106  						"internet_charge_type",
   107  						"PayByTraffic"),
   108  				),
   109  			},
   110  		},
   111  	})
   112  }
   113  
   114  func TestAccAlicloudInstance_userData(t *testing.T) {
   115  	var instance ecs.InstanceAttributesType
   116  
   117  	resource.Test(t, resource.TestCase{
   118  		PreCheck: func() {
   119  			testAccPreCheck(t)
   120  		},
   121  		IDRefreshName: "alicloud_instance.foo",
   122  		Providers:     testAccProviders,
   123  		CheckDestroy:  testAccCheckInstanceDestroy,
   124  		Steps: []resource.TestStep{
   125  			resource.TestStep{
   126  				Config: testAccInstanceConfigUserData,
   127  				Check: resource.ComposeTestCheckFunc(
   128  					testAccCheckInstanceExists(
   129  						"alicloud_instance.foo", &instance),
   130  					resource.TestCheckResourceAttr(
   131  						"alicloud_instance.foo",
   132  						"system_disk_category",
   133  						"cloud_efficiency"),
   134  					resource.TestCheckResourceAttr(
   135  						"alicloud_instance.foo",
   136  						"internet_charge_type",
   137  						"PayByTraffic"),
   138  					resource.TestCheckResourceAttr(
   139  						"alicloud_instance.foo",
   140  						"user_data",
   141  						"echo 'net.ipv4.ip_forward=1'>> /etc/sysctl.conf"),
   142  				),
   143  			},
   144  		},
   145  	})
   146  }
   147  
   148  func TestAccAlicloudInstance_multipleRegions(t *testing.T) {
   149  	var instance ecs.InstanceAttributesType
   150  
   151  	// multi provideris
   152  	var providers []*schema.Provider
   153  	providerFactories := map[string]terraform.ResourceProviderFactory{
   154  		"alicloud": func() (terraform.ResourceProvider, error) {
   155  			p := Provider()
   156  			providers = append(providers, p.(*schema.Provider))
   157  			return p, nil
   158  		},
   159  	}
   160  
   161  	resource.Test(t, resource.TestCase{
   162  		PreCheck: func() {
   163  			testAccPreCheck(t)
   164  		},
   165  		ProviderFactories: providerFactories,
   166  		CheckDestroy:      testAccCheckInstanceDestroyWithProviders(&providers),
   167  		Steps: []resource.TestStep{
   168  			resource.TestStep{
   169  				Config: testAccInstanceConfigMultipleRegions,
   170  				Check: resource.ComposeTestCheckFunc(
   171  					testAccCheckInstanceExistsWithProviders(
   172  						"alicloud_instance.foo", &instance, &providers),
   173  					testAccCheckInstanceExistsWithProviders(
   174  						"alicloud_instance.bar", &instance, &providers),
   175  				),
   176  			},
   177  		},
   178  	})
   179  }
   180  
   181  func TestAccAlicloudInstance_multiSecurityGroup(t *testing.T) {
   182  	var instance ecs.InstanceAttributesType
   183  
   184  	testCheck := func(sgCount int) resource.TestCheckFunc {
   185  		return func(*terraform.State) error {
   186  			if len(instance.SecurityGroupIds.SecurityGroupId) < 0 {
   187  				return fmt.Errorf("no security group: %#v", instance.SecurityGroupIds.SecurityGroupId)
   188  			}
   189  
   190  			if len(instance.SecurityGroupIds.SecurityGroupId) < sgCount {
   191  				return fmt.Errorf("less security group: %#v", instance.SecurityGroupIds.SecurityGroupId)
   192  			}
   193  
   194  			return nil
   195  		}
   196  	}
   197  
   198  	resource.Test(t, resource.TestCase{
   199  		PreCheck: func() {
   200  			testAccPreCheck(t)
   201  		},
   202  
   203  		// module name
   204  		IDRefreshName: "alicloud_instance.foo",
   205  
   206  		Providers:    testAccProviders,
   207  		CheckDestroy: testAccCheckInstanceDestroy,
   208  		Steps: []resource.TestStep{
   209  			resource.TestStep{
   210  				Config: testAccInstanceConfig_multiSecurityGroup,
   211  				Check: resource.ComposeTestCheckFunc(
   212  					testAccCheckInstanceExists(
   213  						"alicloud_instance.foo", &instance),
   214  					testCheck(2),
   215  					resource.TestCheckResourceAttr(
   216  						"alicloud_instance.foo",
   217  						"image_id",
   218  						"ubuntu_140405_32_40G_cloudinit_20161115.vhd"),
   219  					resource.TestCheckResourceAttr(
   220  						"alicloud_instance.foo",
   221  						"instance_name",
   222  						"test_foo"),
   223  				),
   224  			},
   225  			resource.TestStep{
   226  				Config: testAccInstanceConfig_multiSecurityGroup_add,
   227  				Check: resource.ComposeTestCheckFunc(
   228  					testAccCheckInstanceExists(
   229  						"alicloud_instance.foo", &instance),
   230  					testCheck(3),
   231  					resource.TestCheckResourceAttr(
   232  						"alicloud_instance.foo",
   233  						"image_id",
   234  						"ubuntu_140405_32_40G_cloudinit_20161115.vhd"),
   235  					resource.TestCheckResourceAttr(
   236  						"alicloud_instance.foo",
   237  						"instance_name",
   238  						"test_foo"),
   239  				),
   240  			},
   241  			resource.TestStep{
   242  				Config: testAccInstanceConfig_multiSecurityGroup_remove,
   243  				Check: resource.ComposeTestCheckFunc(
   244  					testAccCheckInstanceExists(
   245  						"alicloud_instance.foo", &instance),
   246  					testCheck(1),
   247  					resource.TestCheckResourceAttr(
   248  						"alicloud_instance.foo",
   249  						"image_id",
   250  						"ubuntu_140405_32_40G_cloudinit_20161115.vhd"),
   251  					resource.TestCheckResourceAttr(
   252  						"alicloud_instance.foo",
   253  						"instance_name",
   254  						"test_foo"),
   255  				),
   256  			},
   257  		},
   258  	})
   259  
   260  }
   261  
   262  func TestAccAlicloudInstance_multiSecurityGroupByCount(t *testing.T) {
   263  	var instance ecs.InstanceAttributesType
   264  
   265  	testCheck := func(sgCount int) resource.TestCheckFunc {
   266  		return func(*terraform.State) error {
   267  			if len(instance.SecurityGroupIds.SecurityGroupId) < 0 {
   268  				return fmt.Errorf("no security group: %#v", instance.SecurityGroupIds.SecurityGroupId)
   269  			}
   270  
   271  			if len(instance.SecurityGroupIds.SecurityGroupId) < sgCount {
   272  				return fmt.Errorf("less security group: %#v", instance.SecurityGroupIds.SecurityGroupId)
   273  			}
   274  
   275  			return nil
   276  		}
   277  	}
   278  
   279  	resource.Test(t, resource.TestCase{
   280  		PreCheck: func() {
   281  			testAccPreCheck(t)
   282  		},
   283  
   284  		// module name
   285  		IDRefreshName: "alicloud_instance.foo",
   286  
   287  		Providers:    testAccProviders,
   288  		CheckDestroy: testAccCheckInstanceDestroy,
   289  		Steps: []resource.TestStep{
   290  			resource.TestStep{
   291  				Config: testAccInstanceConfig_multiSecurityGroupByCount,
   292  				Check: resource.ComposeTestCheckFunc(
   293  					testAccCheckInstanceExists(
   294  						"alicloud_instance.foo", &instance),
   295  					testCheck(2),
   296  					resource.TestCheckResourceAttr(
   297  						"alicloud_instance.foo",
   298  						"image_id",
   299  						"ubuntu_140405_32_40G_cloudinit_20161115.vhd"),
   300  					resource.TestCheckResourceAttr(
   301  						"alicloud_instance.foo",
   302  						"instance_name",
   303  						"test_foo"),
   304  				),
   305  			},
   306  		},
   307  	})
   308  
   309  }
   310  
   311  func TestAccAlicloudInstance_NetworkInstanceSecurityGroups(t *testing.T) {
   312  	var instance ecs.InstanceAttributesType
   313  
   314  	resource.Test(t, resource.TestCase{
   315  		PreCheck: func() {
   316  			testAccPreCheck(t)
   317  		},
   318  		IDRefreshName: "alicloud_instance.foo",
   319  		Providers:     testAccProviders,
   320  		CheckDestroy:  testAccCheckInstanceDestroy,
   321  		Steps: []resource.TestStep{
   322  			resource.TestStep{
   323  				Config: testAccInstanceNetworkInstanceSecurityGroups,
   324  				Check: resource.ComposeTestCheckFunc(
   325  					testAccCheckInstanceExists(
   326  						"alicloud_instance.foo", &instance),
   327  				),
   328  			},
   329  		},
   330  	})
   331  }
   332  
   333  func TestAccAlicloudInstance_tags(t *testing.T) {
   334  	var instance ecs.InstanceAttributesType
   335  
   336  	resource.Test(t, resource.TestCase{
   337  		PreCheck: func() {
   338  			testAccPreCheck(t)
   339  		},
   340  		Providers:    testAccProviders,
   341  		CheckDestroy: testAccCheckInstanceDestroy,
   342  		Steps: []resource.TestStep{
   343  			resource.TestStep{
   344  				Config: testAccCheckInstanceConfigTags,
   345  				Check: resource.ComposeTestCheckFunc(
   346  					testAccCheckInstanceExists("alicloud_instance.foo", &instance),
   347  					resource.TestCheckResourceAttr(
   348  						"alicloud_instance.foo",
   349  						"tags.foo",
   350  						"bar"),
   351  				),
   352  			},
   353  
   354  			resource.TestStep{
   355  				Config: testAccCheckInstanceConfigTagsUpdate,
   356  				Check: resource.ComposeTestCheckFunc(
   357  					testAccCheckInstanceExists("alicloud_instance.foo", &instance),
   358  					resource.TestCheckResourceAttr(
   359  						"alicloud_instance.foo",
   360  						"tags.foo",
   361  						""),
   362  					resource.TestCheckResourceAttr(
   363  						"alicloud_instance.foo",
   364  						"tags.bar",
   365  						"zzz"),
   366  				),
   367  			},
   368  		},
   369  	})
   370  }
   371  
   372  func TestAccAlicloudInstance_update(t *testing.T) {
   373  	var instance ecs.InstanceAttributesType
   374  
   375  	resource.Test(t, resource.TestCase{
   376  		PreCheck: func() {
   377  			testAccPreCheck(t)
   378  		},
   379  		Providers:    testAccProviders,
   380  		CheckDestroy: testAccCheckInstanceDestroy,
   381  		Steps: []resource.TestStep{
   382  			resource.TestStep{
   383  				Config: testAccCheckInstanceConfigOrigin,
   384  				Check: resource.ComposeTestCheckFunc(
   385  					testAccCheckInstanceExists("alicloud_instance.foo", &instance),
   386  					resource.TestCheckResourceAttr(
   387  						"alicloud_instance.foo",
   388  						"instance_name",
   389  						"instance_foo"),
   390  					resource.TestCheckResourceAttr(
   391  						"alicloud_instance.foo",
   392  						"host_name",
   393  						"host-foo"),
   394  				),
   395  			},
   396  
   397  			resource.TestStep{
   398  				Config: testAccCheckInstanceConfigOriginUpdate,
   399  				Check: resource.ComposeTestCheckFunc(
   400  					testAccCheckInstanceExists("alicloud_instance.foo", &instance),
   401  					resource.TestCheckResourceAttr(
   402  						"alicloud_instance.foo",
   403  						"instance_name",
   404  						"instance_bar"),
   405  					resource.TestCheckResourceAttr(
   406  						"alicloud_instance.foo",
   407  						"host_name",
   408  						"host-bar"),
   409  				),
   410  			},
   411  		},
   412  	})
   413  }
   414  
   415  func TestAccAlicloudInstance_privateIP(t *testing.T) {
   416  	var instance ecs.InstanceAttributesType
   417  
   418  	testCheckPrivateIP := func() resource.TestCheckFunc {
   419  		return func(*terraform.State) error {
   420  			privateIP := instance.VpcAttributes.PrivateIpAddress.IpAddress[0]
   421  			if privateIP != "172.16.0.229" {
   422  				return fmt.Errorf("bad private IP: %s", privateIP)
   423  			}
   424  
   425  			return nil
   426  		}
   427  	}
   428  
   429  	resource.Test(t, resource.TestCase{
   430  		PreCheck: func() {
   431  			testAccPreCheck(t)
   432  		},
   433  		IDRefreshName: "alicloud_instance.foo",
   434  		Providers:     testAccProviders,
   435  		CheckDestroy:  testAccCheckInstanceDestroy,
   436  		Steps: []resource.TestStep{
   437  			resource.TestStep{
   438  				Config: testAccInstanceConfigPrivateIP,
   439  				Check: resource.ComposeTestCheckFunc(
   440  					testAccCheckInstanceExists("alicloud_instance.foo", &instance),
   441  					testCheckPrivateIP(),
   442  				),
   443  			},
   444  		},
   445  	})
   446  }
   447  
   448  func TestAccAlicloudInstance_associatePublicIPAndPrivateIP(t *testing.T) {
   449  	var instance ecs.InstanceAttributesType
   450  
   451  	testCheckPrivateIP := func() resource.TestCheckFunc {
   452  		return func(*terraform.State) error {
   453  			privateIP := instance.VpcAttributes.PrivateIpAddress.IpAddress[0]
   454  			if privateIP != "172.16.0.229" {
   455  				return fmt.Errorf("bad private IP: %s", privateIP)
   456  			}
   457  
   458  			return nil
   459  		}
   460  	}
   461  
   462  	resource.Test(t, resource.TestCase{
   463  		PreCheck: func() {
   464  			testAccPreCheck(t)
   465  		},
   466  		IDRefreshName: "alicloud_instance.foo",
   467  		Providers:     testAccProviders,
   468  		CheckDestroy:  testAccCheckInstanceDestroy,
   469  		Steps: []resource.TestStep{
   470  			resource.TestStep{
   471  				Config: testAccInstanceConfigAssociatePublicIPAndPrivateIP,
   472  				Check: resource.ComposeTestCheckFunc(
   473  					testAccCheckInstanceExists("alicloud_instance.foo", &instance),
   474  					testCheckPrivateIP(),
   475  				),
   476  			},
   477  		},
   478  	})
   479  }
   480  
   481  func TestAccAlicloudInstance_vpcRule(t *testing.T) {
   482  	var instance ecs.InstanceAttributesType
   483  
   484  	resource.Test(t, resource.TestCase{
   485  		PreCheck: func() {
   486  			testAccPreCheck(t)
   487  		},
   488  		IDRefreshName: "alicloud_instance.foo",
   489  		Providers:     testAccProviders,
   490  		CheckDestroy:  testAccCheckInstanceDestroy,
   491  		Steps: []resource.TestStep{
   492  			resource.TestStep{
   493  				Config: testAccVpcInstanceWithSecurityRule,
   494  				Check: resource.ComposeTestCheckFunc(
   495  					testAccCheckInstanceExists("alicloud_instance.foo", &instance),
   496  					resource.TestCheckResourceAttr(
   497  						"alicloud_instance.foo",
   498  						"internet_charge_type",
   499  						"PayByBandwidth"),
   500  					resource.TestCheckResourceAttr(
   501  						"alicloud_instance.foo",
   502  						"internet_max_bandwidth_out",
   503  						"5"),
   504  				),
   505  			},
   506  		},
   507  	})
   508  }
   509  
   510  func testAccCheckInstanceExists(n string, i *ecs.InstanceAttributesType) resource.TestCheckFunc {
   511  	providers := []*schema.Provider{testAccProvider}
   512  	return testAccCheckInstanceExistsWithProviders(n, i, &providers)
   513  }
   514  
   515  func testAccCheckInstanceExistsWithProviders(n string, i *ecs.InstanceAttributesType, providers *[]*schema.Provider) resource.TestCheckFunc {
   516  	return func(s *terraform.State) error {
   517  		rs, ok := s.RootModule().Resources[n]
   518  		if !ok {
   519  			return fmt.Errorf("Not found: %s", n)
   520  		}
   521  
   522  		if rs.Primary.ID == "" {
   523  			return fmt.Errorf("No ID is set")
   524  		}
   525  		for _, provider := range *providers {
   526  			// Ignore if Meta is empty, this can happen for validation providers
   527  			if provider.Meta() == nil {
   528  				continue
   529  			}
   530  
   531  			client := provider.Meta().(*AliyunClient)
   532  			instance, err := client.QueryInstancesById(rs.Primary.ID)
   533  			log.Printf("[WARN]get ecs instance %#v", instance)
   534  			if err == nil && instance != nil {
   535  				*i = *instance
   536  				return nil
   537  			}
   538  
   539  			// Verify the error is what we want
   540  			e, _ := err.(*common.Error)
   541  			if e.ErrorResponse.Message == InstanceNotfound {
   542  				continue
   543  			}
   544  			if err != nil {
   545  				return err
   546  
   547  			}
   548  		}
   549  
   550  		return fmt.Errorf("Instance not found")
   551  	}
   552  }
   553  
   554  func testAccCheckInstanceDestroy(s *terraform.State) error {
   555  	return testAccCheckInstanceDestroyWithProvider(s, testAccProvider)
   556  }
   557  
   558  func testAccCheckInstanceDestroyWithProviders(providers *[]*schema.Provider) resource.TestCheckFunc {
   559  	return func(s *terraform.State) error {
   560  		for _, provider := range *providers {
   561  			if provider.Meta() == nil {
   562  				continue
   563  			}
   564  			if err := testAccCheckInstanceDestroyWithProvider(s, provider); err != nil {
   565  				return err
   566  			}
   567  		}
   568  		return nil
   569  	}
   570  }
   571  
   572  func testAccCheckInstanceDestroyWithProvider(s *terraform.State, provider *schema.Provider) error {
   573  	client := provider.Meta().(*AliyunClient)
   574  
   575  	for _, rs := range s.RootModule().Resources {
   576  		if rs.Type != "alicloud_instance" {
   577  			continue
   578  		}
   579  
   580  		// Try to find the resource
   581  		instance, err := client.QueryInstancesById(rs.Primary.ID)
   582  		if err == nil {
   583  			if instance.Status != "" && instance.Status != "Stopped" {
   584  				return fmt.Errorf("Found unstopped instance: %s", instance.InstanceId)
   585  			}
   586  		}
   587  
   588  		// Verify the error is what we want
   589  		e, _ := err.(*common.Error)
   590  		if e.ErrorResponse.Message == InstanceNotfound {
   591  			continue
   592  		}
   593  
   594  		return err
   595  	}
   596  
   597  	return nil
   598  }
   599  
   600  const testAccInstanceConfig = `
   601  resource "alicloud_security_group" "tf_test_foo" {
   602  	name = "tf_test_foo"
   603  	description = "foo"
   604  }
   605  
   606  resource "alicloud_security_group" "tf_test_bar" {
   607  	name = "tf_test_bar"
   608  	description = "bar"
   609  }
   610  
   611  resource "alicloud_instance" "foo" {
   612  	# cn-beijing
   613  	availability_zone = "cn-beijing-b"
   614  	image_id = "ubuntu_140405_32_40G_cloudinit_20161115.vhd"
   615  
   616  	system_disk_category = "cloud_ssd"
   617  
   618  	instance_type = "ecs.n1.small"
   619  	internet_charge_type = "PayByBandwidth"
   620  	security_groups = ["${alicloud_security_group.tf_test_foo.id}"]
   621  	instance_name = "test_foo"
   622  	io_optimized = "optimized"
   623  
   624  	tags {
   625  		foo = "bar"
   626  		work = "test"
   627  	}
   628  }
   629  `
   630  const testAccInstanceConfigVPC = `
   631  resource "alicloud_vpc" "foo" {
   632    name = "tf_test_foo"
   633    cidr_block = "172.16.0.0/12"
   634  }
   635  
   636  resource "alicloud_vswitch" "foo" {
   637    vpc_id = "${alicloud_vpc.foo.id}"
   638    cidr_block = "172.16.0.0/21"
   639    availability_zone = "cn-beijing-b"
   640  }
   641  
   642  resource "alicloud_security_group" "tf_test_foo" {
   643  	name = "tf_test_foo"
   644  	description = "foo"
   645  	vpc_id = "${alicloud_vpc.foo.id}"
   646  }
   647  
   648  resource "alicloud_instance" "foo" {
   649  	# cn-beijing
   650  	availability_zone = "cn-beijing-b"
   651  	vswitch_id = "${alicloud_vswitch.foo.id}"
   652  	image_id = "ubuntu_140405_32_40G_cloudinit_20161115.vhd"
   653  
   654  	# series II
   655  	instance_type = "ecs.n1.medium"
   656  	io_optimized = "optimized"
   657  	system_disk_category = "cloud_efficiency"
   658  
   659  	internet_charge_type = "PayByTraffic"
   660  	internet_max_bandwidth_out = 5
   661  	allocate_public_ip = true
   662  	security_groups = ["${alicloud_security_group.tf_test_foo.id}"]
   663  	instance_name = "test_foo"
   664  }
   665  
   666  `
   667  
   668  const testAccInstanceConfigUserData = `
   669  resource "alicloud_vpc" "foo" {
   670    name = "tf_test_foo"
   671    cidr_block = "172.16.0.0/12"
   672  }
   673  
   674  resource "alicloud_vswitch" "foo" {
   675    vpc_id = "${alicloud_vpc.foo.id}"
   676    cidr_block = "172.16.0.0/21"
   677    availability_zone = "cn-beijing-b"
   678  }
   679  
   680  resource "alicloud_security_group" "tf_test_foo" {
   681  	name = "tf_test_foo"
   682  	description = "foo"
   683  	vpc_id = "${alicloud_vpc.foo.id}"
   684  }
   685  
   686  resource "alicloud_instance" "foo" {
   687  	# cn-beijing
   688  	availability_zone = "cn-beijing-b"
   689  	vswitch_id = "${alicloud_vswitch.foo.id}"
   690  	image_id = "ubuntu_140405_32_40G_cloudinit_20161115.vhd"
   691  	# series II
   692  	instance_type = "ecs.n1.medium"
   693  	io_optimized = "optimized"
   694  	system_disk_category = "cloud_efficiency"
   695  	internet_charge_type = "PayByTraffic"
   696  	internet_max_bandwidth_out = 5
   697  	allocate_public_ip = true
   698  	security_groups = ["${alicloud_security_group.tf_test_foo.id}"]
   699  	instance_name = "test_foo"
   700  	user_data = "echo 'net.ipv4.ip_forward=1'>> /etc/sysctl.conf"
   701  }
   702  `
   703  
   704  const testAccInstanceConfigMultipleRegions = `
   705  provider "alicloud" {
   706  	alias = "beijing"
   707  	region = "cn-beijing"
   708  }
   709  
   710  provider "alicloud" {
   711  	alias = "shanghai"
   712  	region = "cn-shanghai"
   713  }
   714  
   715  resource "alicloud_security_group" "tf_test_foo" {
   716  	name = "tf_test_foo"
   717  	provider = "alicloud.beijing"
   718  	description = "foo"
   719  }
   720  
   721  resource "alicloud_security_group" "tf_test_bar" {
   722  	name = "tf_test_bar"
   723  	provider = "alicloud.shanghai"
   724  	description = "bar"
   725  }
   726  
   727  resource "alicloud_instance" "foo" {
   728    # cn-beijing
   729    provider = "alicloud.beijing"
   730    availability_zone = "cn-beijing-b"
   731    image_id = "ubuntu_140405_32_40G_cloudinit_20161115.vhd"
   732  
   733    internet_charge_type = "PayByBandwidth"
   734  
   735    instance_type = "ecs.n1.medium"
   736    io_optimized = "optimized"
   737    system_disk_category = "cloud_efficiency"
   738    security_groups = ["${alicloud_security_group.tf_test_foo.id}"]
   739    instance_name = "test_foo"
   740  }
   741  
   742  resource "alicloud_instance" "bar" {
   743  	# cn-shanghai
   744  	provider = "alicloud.shanghai"
   745  	availability_zone = "cn-shanghai-b"
   746  	image_id = "ubuntu_140405_32_40G_cloudinit_20161115.vhd"
   747  
   748  	internet_charge_type = "PayByBandwidth"
   749  
   750  	instance_type = "ecs.n1.medium"
   751  	io_optimized = "optimized"
   752  	system_disk_category = "cloud_efficiency"
   753  	security_groups = ["${alicloud_security_group.tf_test_bar.id}"]
   754  	instance_name = "test_bar"
   755  }
   756  `
   757  
   758  const testAccInstanceConfig_multiSecurityGroup = `
   759  resource "alicloud_security_group" "tf_test_foo" {
   760  	name = "tf_test_foo"
   761  	description = "foo"
   762  }
   763  
   764  resource "alicloud_security_group" "tf_test_bar" {
   765  	name = "tf_test_bar"
   766  	description = "bar"
   767  }
   768  
   769  resource "alicloud_instance" "foo" {
   770  	# cn-beijing
   771  	availability_zone = "cn-beijing-b"
   772  	image_id = "ubuntu_140405_32_40G_cloudinit_20161115.vhd"
   773  
   774  	instance_type = "ecs.s2.large"
   775  	internet_charge_type = "PayByBandwidth"
   776  	security_groups = ["${alicloud_security_group.tf_test_foo.id}", "${alicloud_security_group.tf_test_bar.id}"]
   777  	instance_name = "test_foo"
   778  	io_optimized = "optimized"
   779  }`
   780  
   781  const testAccInstanceConfig_multiSecurityGroup_add = `
   782  resource "alicloud_security_group" "tf_test_foo" {
   783  	name = "tf_test_foo"
   784  	description = "foo"
   785  }
   786  
   787  resource "alicloud_security_group" "tf_test_bar" {
   788  	name = "tf_test_bar"
   789  	description = "bar"
   790  }
   791  
   792  resource "alicloud_security_group" "tf_test_add_sg" {
   793  	name = "tf_test_add_sg"
   794  	description = "sg"
   795  }
   796  
   797  resource "alicloud_instance" "foo" {
   798  	# cn-beijing
   799  	availability_zone = "cn-beijing-b"
   800  	image_id = "ubuntu_140405_32_40G_cloudinit_20161115.vhd"
   801  
   802  	instance_type = "ecs.s2.large"
   803  	internet_charge_type = "PayByBandwidth"
   804  	security_groups = ["${alicloud_security_group.tf_test_foo.id}", "${alicloud_security_group.tf_test_bar.id}",
   805  				"${alicloud_security_group.tf_test_add_sg.id}"]
   806  	instance_name = "test_foo"
   807  	io_optimized = "optimized"
   808  }
   809  `
   810  
   811  const testAccInstanceConfig_multiSecurityGroup_remove = `
   812  resource "alicloud_security_group" "tf_test_foo" {
   813  	name = "tf_test_foo"
   814  	description = "foo"
   815  }
   816  
   817  resource "alicloud_instance" "foo" {
   818  	# cn-beijing
   819  	availability_zone = "cn-beijing-b"
   820  	image_id = "ubuntu_140405_32_40G_cloudinit_20161115.vhd"
   821  
   822  	instance_type = "ecs.s2.large"
   823  	internet_charge_type = "PayByBandwidth"
   824  	security_groups = ["${alicloud_security_group.tf_test_foo.id}"]
   825  	instance_name = "test_foo"
   826  	io_optimized = "optimized"
   827  }
   828  `
   829  
   830  const testAccInstanceConfig_multiSecurityGroupByCount = `
   831  resource "alicloud_security_group" "tf_test_foo" {
   832  	name = "tf_test_foo"
   833  	count = 2
   834  	description = "foo"
   835  }
   836  
   837  resource "alicloud_instance" "foo" {
   838  	# cn-beijing
   839  	availability_zone = "cn-beijing-b"
   840  	image_id = "ubuntu_140405_32_40G_cloudinit_20161115.vhd"
   841  
   842  	instance_type = "ecs.s2.large"
   843  	internet_charge_type = "PayByBandwidth"
   844  	security_groups = ["${alicloud_security_group.tf_test_foo.*.id}"]
   845  	instance_name = "test_foo"
   846  	io_optimized = "none"
   847  }
   848  `
   849  
   850  const testAccInstanceNetworkInstanceSecurityGroups = `
   851  resource "alicloud_vpc" "foo" {
   852    name = "tf_test_foo"
   853    cidr_block = "172.16.0.0/12"
   854  }
   855  
   856  resource "alicloud_vswitch" "foo" {
   857    vpc_id = "${alicloud_vpc.foo.id}"
   858    cidr_block = "172.16.0.0/21"
   859    availability_zone = "cn-beijing-b"
   860  }
   861  
   862  resource "alicloud_security_group" "tf_test_foo" {
   863  	name = "tf_test_foo"
   864  	description = "foo"
   865  	vpc_id = "${alicloud_vpc.foo.id}"
   866  }
   867  
   868  resource "alicloud_instance" "foo" {
   869  	# cn-beijing
   870  	availability_zone = "cn-beijing-b"
   871  	vswitch_id = "${alicloud_vswitch.foo.id}"
   872  	image_id = "ubuntu_140405_32_40G_cloudinit_20161115.vhd"
   873  
   874  	# series II
   875  	instance_type = "ecs.n1.medium"
   876  	io_optimized = "optimized"
   877  	system_disk_category = "cloud_efficiency"
   878  
   879  	security_groups = ["${alicloud_security_group.tf_test_foo.id}"]
   880  	instance_name = "test_foo"
   881  
   882  	internet_max_bandwidth_out = 5
   883  	allocate_public_ip = "true"
   884  	internet_charge_type = "PayByBandwidth"
   885  }
   886  `
   887  const testAccCheckInstanceConfigTags = `
   888  resource "alicloud_security_group" "tf_test_foo" {
   889  	name = "tf_test_foo"
   890  	description = "foo"
   891  }
   892  
   893  resource "alicloud_instance" "foo" {
   894  	# cn-beijing
   895  	availability_zone = "cn-beijing-b"
   896  	image_id = "ubuntu_140405_32_40G_cloudinit_20161115.vhd"
   897  
   898  	# series II
   899  	instance_type = "ecs.n1.medium"
   900  	io_optimized = "optimized"
   901  	internet_charge_type = "PayByBandwidth"
   902  	system_disk_category = "cloud_efficiency"
   903  
   904  	security_groups = ["${alicloud_security_group.tf_test_foo.id}"]
   905  	instance_name = "test_foo"
   906  
   907  	tags {
   908  		foo = "bar"
   909  	}
   910  }
   911  `
   912  
   913  const testAccCheckInstanceConfigTagsUpdate = `
   914  resource "alicloud_security_group" "tf_test_foo" {
   915  	name = "tf_test_foo"
   916  	description = "foo"
   917  }
   918  
   919  resource "alicloud_instance" "foo" {
   920  	# cn-beijing
   921  	availability_zone = "cn-beijing-b"
   922  	image_id = "ubuntu_140405_32_40G_cloudinit_20161115.vhd"
   923  
   924  	# series II
   925  	instance_type = "ecs.n1.medium"
   926  	io_optimized = "optimized"
   927  	internet_charge_type = "PayByBandwidth"
   928  	system_disk_category = "cloud_efficiency"
   929  
   930  	security_groups = ["${alicloud_security_group.tf_test_foo.id}"]
   931  	instance_name = "test_foo"
   932  
   933  	tags {
   934  		bar = "zzz"
   935  	}
   936  }
   937  `
   938  const testAccCheckInstanceConfigOrigin = `
   939  resource "alicloud_security_group" "tf_test_foo" {
   940  	name = "tf_test_foo"
   941  	description = "foo"
   942  }
   943  
   944  resource "alicloud_instance" "foo" {
   945  	# cn-beijing
   946  	availability_zone = "cn-beijing-b"
   947  	image_id = "ubuntu_140405_32_40G_cloudinit_20161115.vhd"
   948  
   949  	# series II
   950  	instance_type = "ecs.n1.medium"
   951  	io_optimized = "optimized"
   952  	internet_charge_type = "PayByBandwidth"
   953  	system_disk_category = "cloud_efficiency"
   954  
   955  	security_groups = ["${alicloud_security_group.tf_test_foo.id}"]
   956  
   957  	instance_name = "instance_foo"
   958  	host_name = "host-foo"
   959  }
   960  `
   961  
   962  const testAccCheckInstanceConfigOriginUpdate = `
   963  resource "alicloud_security_group" "tf_test_foo" {
   964  	name = "tf_test_foo"
   965  	description = "foo"
   966  }
   967  
   968  resource "alicloud_instance" "foo" {
   969  	# cn-beijing
   970  	availability_zone = "cn-beijing-b"
   971  	image_id = "ubuntu_140405_32_40G_cloudinit_20161115.vhd"
   972  
   973  	# series II
   974  	instance_type = "ecs.n1.medium"
   975  	io_optimized = "optimized"
   976  	internet_charge_type = "PayByBandwidth"
   977  	system_disk_category = "cloud_efficiency"
   978  
   979  	security_groups = ["${alicloud_security_group.tf_test_foo.id}"]
   980  
   981  	instance_name = "instance_bar"
   982  	host_name = "host-bar"
   983  }
   984  `
   985  
   986  const testAccInstanceConfigPrivateIP = `
   987  resource "alicloud_vpc" "foo" {
   988    name = "tf_test_foo"
   989    cidr_block = "172.16.0.0/12"
   990  }
   991  
   992  resource "alicloud_vswitch" "foo" {
   993    vpc_id = "${alicloud_vpc.foo.id}"
   994    cidr_block = "172.16.0.0/24"
   995    availability_zone = "cn-beijing-b"
   996  }
   997  
   998  resource "alicloud_security_group" "tf_test_foo" {
   999  	name = "tf_test_foo"
  1000  	description = "foo"
  1001  	vpc_id = "${alicloud_vpc.foo.id}"
  1002  }
  1003  
  1004  resource "alicloud_instance" "foo" {
  1005  	# cn-beijing
  1006  	availability_zone = "cn-beijing-b"
  1007  	security_groups = ["${alicloud_security_group.tf_test_foo.id}"]
  1008  
  1009  	vswitch_id = "${alicloud_vswitch.foo.id}"
  1010  	private_ip = "172.16.0.229"
  1011  
  1012  	# series II
  1013  	instance_type = "ecs.n1.medium"
  1014  	io_optimized = "optimized"
  1015  	system_disk_category = "cloud_efficiency"
  1016  	image_id = "ubuntu_140405_32_40G_cloudinit_20161115.vhd"
  1017  	instance_name = "test_foo"
  1018  }
  1019  `
  1020  const testAccInstanceConfigAssociatePublicIPAndPrivateIP = `
  1021  resource "alicloud_vpc" "foo" {
  1022    name = "tf_test_foo"
  1023    cidr_block = "172.16.0.0/12"
  1024  }
  1025  
  1026  resource "alicloud_vswitch" "foo" {
  1027    vpc_id = "${alicloud_vpc.foo.id}"
  1028    cidr_block = "172.16.0.0/24"
  1029    availability_zone = "cn-beijing-b"
  1030  }
  1031  
  1032  resource "alicloud_security_group" "tf_test_foo" {
  1033  	name = "tf_test_foo"
  1034  	description = "foo"
  1035  	vpc_id = "${alicloud_vpc.foo.id}"
  1036  }
  1037  
  1038  resource "alicloud_instance" "foo" {
  1039  	# cn-beijing
  1040  	availability_zone = "cn-beijing-b"
  1041  	security_groups = ["${alicloud_security_group.tf_test_foo.id}"]
  1042  
  1043  	vswitch_id = "${alicloud_vswitch.foo.id}"
  1044  	private_ip = "172.16.0.229"
  1045  	allocate_public_ip = "true"
  1046  	internet_max_bandwidth_out = 5
  1047  	internet_charge_type = "PayByBandwidth"
  1048  
  1049  	# series II
  1050  	instance_type = "ecs.n1.medium"
  1051  	io_optimized = "optimized"
  1052  	system_disk_category = "cloud_efficiency"
  1053  	image_id = "ubuntu_140405_32_40G_cloudinit_20161115.vhd"
  1054  	instance_name = "test_foo"
  1055  }
  1056  `
  1057  const testAccVpcInstanceWithSecurityRule = `
  1058  resource "alicloud_vpc" "foo" {
  1059    name = "tf_test_foo"
  1060    cidr_block = "10.1.0.0/21"
  1061  }
  1062  
  1063  resource "alicloud_vswitch" "foo" {
  1064    vpc_id = "${alicloud_vpc.foo.id}"
  1065    cidr_block = "10.1.1.0/24"
  1066    availability_zone = "cn-beijing-c"
  1067  }
  1068  
  1069  resource "alicloud_security_group" "tf_test_foo" {
  1070      name = "tf_test_foo"
  1071      description = "foo"
  1072      vpc_id = "${alicloud_vpc.foo.id}"
  1073  }
  1074  
  1075  resource "alicloud_security_group_rule" "ingress" {
  1076    type = "ingress"
  1077    ip_protocol = "tcp"
  1078    nic_type = "intranet"
  1079    policy = "accept"
  1080    port_range = "22/22"
  1081    priority = 1
  1082    security_group_id = "${alicloud_security_group.tf_test_foo.id}"
  1083    cidr_ip = "0.0.0.0/0"
  1084  }
  1085  
  1086  resource "alicloud_instance" "foo" {
  1087      # cn-beijing
  1088      availability_zone = "cn-beijing-c"
  1089      security_groups = ["${alicloud_security_group.tf_test_foo.id}"]
  1090  
  1091      vswitch_id = "${alicloud_vswitch.foo.id}"
  1092      allocate_public_ip = true
  1093  
  1094      # series II
  1095      instance_charge_type = "PostPaid"
  1096      instance_type = "ecs.n1.small"
  1097      internet_charge_type = "PayByBandwidth"
  1098      internet_max_bandwidth_out = 5
  1099  
  1100      system_disk_category = "cloud_efficiency"
  1101      image_id = "ubuntu_140405_64_40G_cloudinit_20161115.vhd"
  1102      instance_name = "test_foo"
  1103      io_optimized = "optimized"
  1104  }
  1105  
  1106  `