github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/alicloud/resource_alicloud_snat_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 TestAccAlicloudSnat_basic(t *testing.T) {
    13  	var snat ecs.SnatEntrySetType
    14  
    15  	resource.Test(t, resource.TestCase{
    16  		PreCheck: func() {
    17  			testAccPreCheck(t)
    18  		},
    19  
    20  		// module name
    21  		IDRefreshName: "alicloud_snat_entry.foo",
    22  		Providers:     testAccProviders,
    23  		CheckDestroy:  testAccCheckSnatEntryDestroy,
    24  		Steps: []resource.TestStep{
    25  			resource.TestStep{
    26  				Config: testAccSnatEntryConfig,
    27  				Check: resource.ComposeTestCheckFunc(
    28  					testAccCheckSnatEntryExists(
    29  						"alicloud_snat_entry.foo", &snat),
    30  				),
    31  			},
    32  			resource.TestStep{
    33  				Config: testAccSnatEntryUpdate,
    34  				Check: resource.ComposeTestCheckFunc(
    35  					testAccCheckSnatEntryExists(
    36  						"alicloud_snat_entry.foo", &snat),
    37  				),
    38  			},
    39  		},
    40  	})
    41  
    42  }
    43  
    44  func testAccCheckSnatEntryDestroy(s *terraform.State) error {
    45  	client := testAccProvider.Meta().(*AliyunClient)
    46  
    47  	for _, rs := range s.RootModule().Resources {
    48  		if rs.Type != "alicloud_snat_entry" {
    49  			continue
    50  		}
    51  
    52  		// Try to find the Snat entry
    53  		instance, err := client.DescribeSnatEntry(rs.Primary.Attributes["snat_table_id"], rs.Primary.ID)
    54  
    55  		//this special deal cause the DescribeSnatEntry can't find the records would be throw "cant find the snatTable error"
    56  		if instance.SnatEntryId == "" {
    57  			return nil
    58  		}
    59  
    60  		if instance.SnatEntryId != "" {
    61  			return fmt.Errorf("Snat entry still exist")
    62  		}
    63  
    64  		if err != nil {
    65  			// Verify the error is what we want
    66  			e, _ := err.(*common.Error)
    67  
    68  			if !notFoundError(e) {
    69  				return err
    70  			}
    71  		}
    72  
    73  	}
    74  
    75  	return nil
    76  }
    77  
    78  func testAccCheckSnatEntryExists(n string, snat *ecs.SnatEntrySetType) resource.TestCheckFunc {
    79  	return func(s *terraform.State) error {
    80  		rs, ok := s.RootModule().Resources[n]
    81  		if !ok {
    82  			return fmt.Errorf("Not found: %s", n)
    83  		}
    84  
    85  		if rs.Primary.ID == "" {
    86  			return fmt.Errorf("No SnatEntry ID is set")
    87  		}
    88  
    89  		client := testAccProvider.Meta().(*AliyunClient)
    90  		instance, err := client.DescribeSnatEntry(rs.Primary.Attributes["snat_table_id"], rs.Primary.ID)
    91  
    92  		if err != nil {
    93  			return err
    94  		}
    95  		if instance.SnatEntryId == "" {
    96  			return fmt.Errorf("SnatEntry not found")
    97  		}
    98  
    99  		*snat = instance
   100  		return nil
   101  	}
   102  }
   103  
   104  const testAccSnatEntryConfig = `
   105  data "alicloud_zones" "default" {
   106  	"available_resource_creation"= "VSwitch"
   107  }
   108  
   109  resource "alicloud_vpc" "foo" {
   110  	name = "tf_test_foo"
   111  	cidr_block = "172.16.0.0/12"
   112  }
   113  
   114  resource "alicloud_vswitch" "foo" {
   115  	vpc_id = "${alicloud_vpc.foo.id}"
   116  	cidr_block = "172.16.0.0/21"
   117  	availability_zone = "${data.alicloud_zones.default.zones.2.id}"
   118  }
   119  
   120  resource "alicloud_nat_gateway" "foo" {
   121  	vpc_id = "${alicloud_vpc.foo.id}"
   122  	spec = "Small"
   123  	name = "test_foo"
   124  	bandwidth_packages = [{
   125  	  ip_count = 2
   126  	  bandwidth = 5
   127  	  zone = "${data.alicloud_zones.default.zones.2.id}"
   128  	},{
   129  	  ip_count = 1
   130  	  bandwidth = 6
   131  	  zone = "${data.alicloud_zones.default.zones.2.id}"
   132  	}]
   133  	depends_on = [
   134      	"alicloud_vswitch.foo"]
   135  }
   136  resource "alicloud_snat_entry" "foo"{
   137  	snat_table_id = "${alicloud_nat_gateway.foo.snat_table_ids}"
   138  	source_vswitch_id = "${alicloud_vswitch.foo.id}"
   139  	snat_ip = "${alicloud_nat_gateway.foo.bandwidth_packages.0.public_ip_addresses}"
   140  }
   141  `
   142  
   143  const testAccSnatEntryUpdate = `
   144  data "alicloud_zones" "default" {
   145  	"available_resource_creation"= "VSwitch"
   146  }
   147  
   148  resource "alicloud_vpc" "foo" {
   149  	name = "tf_test_foo"
   150  	cidr_block = "172.16.0.0/12"
   151  }
   152  
   153  resource "alicloud_vswitch" "foo" {
   154  	vpc_id = "${alicloud_vpc.foo.id}"
   155  	cidr_block = "172.16.0.0/21"
   156  	availability_zone = "${data.alicloud_zones.default.zones.2.id}"
   157  }
   158  
   159  resource "alicloud_nat_gateway" "foo" {
   160  	vpc_id = "${alicloud_vpc.foo.id}"
   161  	spec = "Small"
   162  	name = "test_foo"
   163  	bandwidth_packages = [{
   164  	  ip_count = 2
   165  	  bandwidth = 5
   166  	  zone = "${data.alicloud_zones.default.zones.2.id}"
   167  	},{
   168  	  ip_count = 1
   169  	  bandwidth = 6
   170  	  zone = "${data.alicloud_zones.default.zones.2.id}"
   171  	}]
   172  	depends_on = [
   173      	"alicloud_vswitch.foo"]
   174  }
   175  resource "alicloud_snat_entry" "foo"{
   176  	snat_table_id = "${alicloud_nat_gateway.foo.snat_table_ids}"
   177  	source_vswitch_id = "${alicloud_vswitch.foo.id}"
   178  	snat_ip = "${alicloud_nat_gateway.foo.bandwidth_packages.1.public_ip_addresses}"
   179  }
   180  `