github.com/cbroglie/terraform@v0.7.0-rc3.0.20170410193827-735dfc416d46/builtin/providers/alicloud/resource_alicloud_nat_gateway_test.go (about)

     1  package alicloud
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/denverdino/aliyungo/common"
     6  	"github.com/denverdino/aliyungo/ecs"
     7  	"github.com/hashicorp/terraform/helper/resource"
     8  	"github.com/hashicorp/terraform/terraform"
     9  	"testing"
    10  )
    11  
    12  func TestAccAlicloudNatGateway_basic(t *testing.T) {
    13  	var nat ecs.NatGatewaySetType
    14  
    15  	testCheck := func(*terraform.State) error {
    16  		if nat.BusinessStatus != "Normal" {
    17  			return fmt.Errorf("abnormal instance status")
    18  		}
    19  
    20  		if len(nat.BandwidthPackageIds.BandwidthPackageId) == 0 {
    21  			return fmt.Errorf("no bandwidth package: %#v", nat.BandwidthPackageIds.BandwidthPackageId)
    22  		}
    23  
    24  		return nil
    25  	}
    26  
    27  	resource.Test(t, resource.TestCase{
    28  		PreCheck: func() {
    29  			testAccPreCheck(t)
    30  		},
    31  
    32  		// module name
    33  		IDRefreshName: "alicloud_nat_gateway.foo",
    34  		Providers:     testAccProviders,
    35  		CheckDestroy:  testAccCheckNatGatewayDestroy,
    36  		Steps: []resource.TestStep{
    37  			resource.TestStep{
    38  				Config: testAccNatGatewayConfig,
    39  				Check: resource.ComposeTestCheckFunc(
    40  					testAccCheckNatGatewayExists(
    41  						"alicloud_nat_gateway.foo", &nat),
    42  					testCheck,
    43  					resource.TestCheckResourceAttr(
    44  						"alicloud_nat_gateway.foo",
    45  						"spec",
    46  						"Small"),
    47  					resource.TestCheckResourceAttr(
    48  						"alicloud_nat_gateway.foo",
    49  						"name",
    50  						"test_foo"),
    51  				),
    52  			},
    53  		},
    54  	})
    55  
    56  }
    57  
    58  func TestAccAlicloudNatGateway_spec(t *testing.T) {
    59  	var nat ecs.NatGatewaySetType
    60  
    61  	resource.Test(t, resource.TestCase{
    62  		PreCheck: func() {
    63  			testAccPreCheck(t)
    64  		},
    65  
    66  		// module name
    67  		IDRefreshName: "alicloud_nat_gateway.foo",
    68  		Providers:     testAccProviders,
    69  		CheckDestroy:  testAccCheckNatGatewayDestroy,
    70  		Steps: []resource.TestStep{
    71  			resource.TestStep{
    72  				Config: testAccNatGatewayConfigSpec,
    73  				Check: resource.ComposeTestCheckFunc(
    74  					testAccCheckNatGatewayExists(
    75  						"alicloud_nat_gateway.foo", &nat),
    76  					resource.TestCheckResourceAttr(
    77  						"alicloud_nat_gateway.foo",
    78  						"spec",
    79  						"Middle"),
    80  				),
    81  			},
    82  
    83  			resource.TestStep{
    84  				Config: testAccNatGatewayConfigSpecUpgrade,
    85  				Check: resource.ComposeTestCheckFunc(
    86  					testAccCheckNatGatewayExists(
    87  						"alicloud_nat_gateway.foo", &nat),
    88  					resource.TestCheckResourceAttr(
    89  						"alicloud_nat_gateway.foo",
    90  						"spec",
    91  						"Large"),
    92  				),
    93  			},
    94  		},
    95  	})
    96  
    97  }
    98  
    99  func testAccCheckNatGatewayExists(n string, nat *ecs.NatGatewaySetType) resource.TestCheckFunc {
   100  	return func(s *terraform.State) error {
   101  		rs, ok := s.RootModule().Resources[n]
   102  		if !ok {
   103  			return fmt.Errorf("Not found: %s", n)
   104  		}
   105  
   106  		if rs.Primary.ID == "" {
   107  			return fmt.Errorf("No Gateway ID is set")
   108  		}
   109  
   110  		client := testAccProvider.Meta().(*AliyunClient)
   111  		instance, err := client.DescribeNatGateway(rs.Primary.ID)
   112  
   113  		if err != nil {
   114  			return err
   115  		}
   116  		if instance == nil {
   117  			return fmt.Errorf("Nat gateway not found")
   118  		}
   119  
   120  		*nat = *instance
   121  		return nil
   122  	}
   123  }
   124  
   125  func testAccCheckNatGatewayDestroy(s *terraform.State) error {
   126  	client := testAccProvider.Meta().(*AliyunClient)
   127  
   128  	for _, rs := range s.RootModule().Resources {
   129  		if rs.Type != "alicloud_nat_gateway" {
   130  			continue
   131  		}
   132  
   133  		// Try to find the Nat gateway
   134  		instance, err := client.DescribeNatGateway(rs.Primary.ID)
   135  
   136  		if instance != nil {
   137  			return fmt.Errorf("Nat gateway still exist")
   138  		}
   139  
   140  		if err != nil {
   141  			// Verify the error is what we want
   142  			e, _ := err.(*common.Error)
   143  
   144  			if !notFoundError(e) {
   145  				return err
   146  			}
   147  		}
   148  
   149  	}
   150  
   151  	return nil
   152  }
   153  
   154  const testAccNatGatewayConfig = `
   155  data "alicloud_zones" "default" {
   156  	"available_resource_creation"= "VSwitch"
   157  }
   158  
   159  resource "alicloud_vpc" "foo" {
   160  	name = "tf_test_foo"
   161  	cidr_block = "172.16.0.0/12"
   162  }
   163  
   164  resource "alicloud_vswitch" "foo" {
   165  	vpc_id = "${alicloud_vpc.foo.id}"
   166  	cidr_block = "172.16.0.0/21"
   167  	availability_zone = "${data.alicloud_zones.default.zones.0.id}"
   168  }
   169  
   170  resource "alicloud_nat_gateway" "foo" {
   171  	vpc_id = "${alicloud_vpc.foo.id}"
   172  	spec = "Small"
   173  	name = "test_foo"
   174  	bandwidth_packages = [{
   175  	  ip_count = 1
   176  	  bandwidth = 5
   177  	  zone = "${data.alicloud_zones.default.zones.0.id}"
   178  	}, {
   179  	  ip_count = 2
   180  	  bandwidth = 10
   181  	  zone = "${data.alicloud_zones.default.zones.0.id}"
   182  	}]
   183  	depends_on = [
   184      	"alicloud_vswitch.foo"]
   185  }
   186  `
   187  
   188  const testAccNatGatewayConfigSpec = `
   189  data "alicloud_zones" "default" {
   190  	"available_resource_creation"= "VSwitch"
   191  }
   192  
   193  resource "alicloud_vpc" "foo" {
   194  	name = "tf_test_foo"
   195  	cidr_block = "172.16.0.0/12"
   196  }
   197  
   198  resource "alicloud_vswitch" "foo" {
   199  	vpc_id = "${alicloud_vpc.foo.id}"
   200  	cidr_block = "172.16.0.0/21"
   201  	availability_zone = "${data.alicloud_zones.default.zones.0.id}"
   202  }
   203  
   204  resource "alicloud_nat_gateway" "foo" {
   205  	vpc_id = "${alicloud_vpc.foo.id}"
   206  	spec = "Middle"
   207  	name = "test_foo"
   208  	bandwidth_packages = [{
   209  	  ip_count = 1
   210  	  bandwidth = 5
   211  	  zone = "${data.alicloud_zones.default.zones.0.id}"
   212  	}, {
   213  	  ip_count = 2
   214  	  bandwidth = 10
   215  	  zone = "${data.alicloud_zones.default.zones.0.id}"
   216  	}]
   217  	depends_on = [
   218      	"alicloud_vswitch.foo"]
   219  }
   220  `
   221  
   222  const testAccNatGatewayConfigSpecUpgrade = `
   223  data "alicloud_zones" "default" {
   224  	"available_resource_creation"= "VSwitch"
   225  }
   226  
   227  resource "alicloud_vpc" "foo" {
   228  	name = "tf_test_foo"
   229  	cidr_block = "172.16.0.0/12"
   230  }
   231  
   232  resource "alicloud_vswitch" "foo" {
   233  	vpc_id = "${alicloud_vpc.foo.id}"
   234  	cidr_block = "172.16.0.0/21"
   235  	availability_zone = "${data.alicloud_zones.default.zones.0.id}"
   236  }
   237  
   238  resource "alicloud_nat_gateway" "foo" {
   239  	vpc_id = "${alicloud_vpc.foo.id}"
   240  	spec = "Large"
   241  	name = "test_foo"
   242  	bandwidth_packages = [{
   243  	  ip_count = 1
   244  	  bandwidth = 5
   245  	  zone = "${data.alicloud_zones.default.zones.0.id}"
   246  	}, {
   247  	  ip_count = 2
   248  	  bandwidth = 10
   249  	  zone = "${data.alicloud_zones.default.zones.0.id}"
   250  	}]
   251  	depends_on = [
   252      	"alicloud_vswitch.foo"]
   253  }
   254  `