github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/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  					testAccCheckNatgatewayIpAddress("alicloud_nat_gateway.foo", &nat),
    52  				),
    53  			},
    54  		},
    55  	})
    56  
    57  }
    58  
    59  func TestAccAlicloudNatGateway_spec(t *testing.T) {
    60  	var nat ecs.NatGatewaySetType
    61  
    62  	resource.Test(t, resource.TestCase{
    63  		PreCheck: func() {
    64  			testAccPreCheck(t)
    65  		},
    66  
    67  		// module name
    68  		IDRefreshName: "alicloud_nat_gateway.foo",
    69  		Providers:     testAccProviders,
    70  		CheckDestroy:  testAccCheckNatGatewayDestroy,
    71  		Steps: []resource.TestStep{
    72  			resource.TestStep{
    73  				Config: testAccNatGatewayConfigSpec,
    74  				Check: resource.ComposeTestCheckFunc(
    75  					testAccCheckNatGatewayExists(
    76  						"alicloud_nat_gateway.foo", &nat),
    77  					resource.TestCheckResourceAttr(
    78  						"alicloud_nat_gateway.foo",
    79  						"spec",
    80  						"Middle"),
    81  				),
    82  			},
    83  
    84  			resource.TestStep{
    85  				Config: testAccNatGatewayConfigSpecUpgrade,
    86  				Check: resource.ComposeTestCheckFunc(
    87  					testAccCheckNatGatewayExists(
    88  						"alicloud_nat_gateway.foo", &nat),
    89  					resource.TestCheckResourceAttr(
    90  						"alicloud_nat_gateway.foo",
    91  						"spec",
    92  						"Large"),
    93  				),
    94  			},
    95  		},
    96  	})
    97  
    98  }
    99  
   100  func testAccCheckNatgatewayIpAddress(n string, nat *ecs.NatGatewaySetType) 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 NatGateway ID is set")
   109  		}
   110  
   111  		client := testAccProvider.Meta().(*AliyunClient)
   112  		natGateway, err := client.DescribeNatGateway(rs.Primary.ID)
   113  
   114  		if err != nil {
   115  			return err
   116  		}
   117  		if natGateway == nil {
   118  			return fmt.Errorf("Natgateway not found")
   119  		}
   120  
   121  		return nil
   122  	}
   123  }
   124  
   125  func testAccCheckNatGatewayExists(n string, nat *ecs.NatGatewaySetType) resource.TestCheckFunc {
   126  	return func(s *terraform.State) error {
   127  		rs, ok := s.RootModule().Resources[n]
   128  		if !ok {
   129  			return fmt.Errorf("Not found: %s", n)
   130  		}
   131  
   132  		if rs.Primary.ID == "" {
   133  			return fmt.Errorf("No Gateway ID is set")
   134  		}
   135  
   136  		client := testAccProvider.Meta().(*AliyunClient)
   137  		instance, err := client.DescribeNatGateway(rs.Primary.ID)
   138  
   139  		if err != nil {
   140  			return err
   141  		}
   142  		if instance == nil {
   143  			return fmt.Errorf("Nat gateway not found")
   144  		}
   145  
   146  		*nat = *instance
   147  		return nil
   148  	}
   149  }
   150  
   151  func testAccCheckNatGatewayDestroy(s *terraform.State) error {
   152  	client := testAccProvider.Meta().(*AliyunClient)
   153  
   154  	for _, rs := range s.RootModule().Resources {
   155  		if rs.Type != "alicloud_nat_gateway" {
   156  			continue
   157  		}
   158  
   159  		// Try to find the Nat gateway
   160  		instance, err := client.DescribeNatGateway(rs.Primary.ID)
   161  
   162  		if instance != nil {
   163  			return fmt.Errorf("Nat gateway still exist")
   164  		}
   165  
   166  		if err != nil {
   167  			// Verify the error is what we want
   168  			e, _ := err.(*common.Error)
   169  
   170  			if !notFoundError(e) {
   171  				return err
   172  			}
   173  		}
   174  
   175  	}
   176  
   177  	return nil
   178  }
   179  
   180  const testAccNatGatewayConfig = `
   181  data "alicloud_zones" "default" {
   182  	"available_resource_creation"= "VSwitch"
   183  }
   184  
   185  resource "alicloud_vpc" "foo" {
   186  	name = "tf_test_foo"
   187  	cidr_block = "172.16.0.0/12"
   188  }
   189  
   190  resource "alicloud_vswitch" "foo" {
   191  	vpc_id = "${alicloud_vpc.foo.id}"
   192  	cidr_block = "172.16.0.0/21"
   193  	availability_zone = "${data.alicloud_zones.default.zones.2.id}"
   194  }
   195  
   196  resource "alicloud_nat_gateway" "foo" {
   197  	vpc_id = "${alicloud_vpc.foo.id}"
   198  	spec = "Small"
   199  	name = "test_foo"
   200  	bandwidth_packages = [{
   201  	  ip_count = 1
   202  	  bandwidth = 5
   203  	  zone = "${data.alicloud_zones.default.zones.2.id}"
   204  	}, {
   205  	  ip_count = 2
   206  	  bandwidth = 6
   207  	  zone = "${data.alicloud_zones.default.zones.2.id}"
   208  	}, {
   209  	  ip_count = 3
   210  	  bandwidth = 7
   211  	  zone = "${data.alicloud_zones.default.zones.2.id}"
   212  	}, {
   213  	  ip_count = 1
   214  	  bandwidth = 8
   215  	  zone = "${data.alicloud_zones.default.zones.2.id}"
   216  	}]
   217  	depends_on = [
   218      	"alicloud_vswitch.foo"]
   219  }
   220  `
   221  
   222  const testAccNatGatewayConfigSpec = `
   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 = "Middle"
   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  `
   255  
   256  const testAccNatGatewayConfigSpecUpgrade = `
   257  data "alicloud_zones" "default" {
   258  	"available_resource_creation"= "VSwitch"
   259  }
   260  
   261  resource "alicloud_vpc" "foo" {
   262  	name = "tf_test_foo"
   263  	cidr_block = "172.16.0.0/12"
   264  }
   265  
   266  resource "alicloud_vswitch" "foo" {
   267  	vpc_id = "${alicloud_vpc.foo.id}"
   268  	cidr_block = "172.16.0.0/21"
   269  	availability_zone = "${data.alicloud_zones.default.zones.0.id}"
   270  }
   271  
   272  resource "alicloud_nat_gateway" "foo" {
   273  	vpc_id = "${alicloud_vpc.foo.id}"
   274  	spec = "Large"
   275  	name = "test_foo"
   276  	bandwidth_packages = [{
   277  	  ip_count = 1
   278  	  bandwidth = 5
   279  	  zone = "${data.alicloud_zones.default.zones.0.id}"
   280  	}, {
   281  	  ip_count = 2
   282  	  bandwidth = 10
   283  	  zone = "${data.alicloud_zones.default.zones.0.id}"
   284  	}]
   285  	depends_on = [
   286      	"alicloud_vswitch.foo"]
   287  }
   288  `