github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/alicloud/resource_alicloud_forward_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 TestAccAlicloudForward_basic(t *testing.T) {
    13  	var forward ecs.ForwardTableEntrySetType
    14  
    15  	resource.Test(t, resource.TestCase{
    16  		PreCheck: func() {
    17  			testAccPreCheck(t)
    18  		},
    19  
    20  		// module name
    21  		IDRefreshName: "alicloud_forward_entry.foo",
    22  		Providers:     testAccProviders,
    23  		CheckDestroy:  testAccCheckForwardEntryDestroy,
    24  		Steps: []resource.TestStep{
    25  			resource.TestStep{
    26  				Config: testAccForwardEntryConfig,
    27  				Check: resource.ComposeTestCheckFunc(
    28  					testAccCheckForwardEntryExists(
    29  						"alicloud_forward_entry.foo", &forward),
    30  				),
    31  			},
    32  
    33  			resource.TestStep{
    34  				Config: testAccForwardEntryUpdate,
    35  				Check: resource.ComposeTestCheckFunc(
    36  					testAccCheckForwardEntryExists(
    37  						"alicloud_forward_entry.foo", &forward),
    38  				),
    39  			},
    40  		},
    41  	})
    42  
    43  }
    44  
    45  func testAccCheckForwardEntryDestroy(s *terraform.State) error {
    46  	client := testAccProvider.Meta().(*AliyunClient)
    47  
    48  	for _, rs := range s.RootModule().Resources {
    49  		if rs.Type != "alicloud_snat_entry" {
    50  			continue
    51  		}
    52  
    53  		// Try to find the Snat entry
    54  		instance, err := client.DescribeForwardEntry(rs.Primary.Attributes["forward_table_id"], rs.Primary.ID)
    55  
    56  		//this special deal cause the DescribeSnatEntry can't find the records would be throw "cant find the snatTable error"
    57  		if instance.ForwardEntryId == "" {
    58  			return nil
    59  		}
    60  
    61  		if instance.ForwardEntryId != "" {
    62  			return fmt.Errorf("Forward entry still exist")
    63  		}
    64  
    65  		if err != nil {
    66  			// Verify the error is what we want
    67  			e, _ := err.(*common.Error)
    68  
    69  			if !notFoundError(e) {
    70  				return err
    71  			}
    72  		}
    73  
    74  	}
    75  
    76  	return nil
    77  }
    78  
    79  func testAccCheckForwardEntryExists(n string, snat *ecs.ForwardTableEntrySetType) resource.TestCheckFunc {
    80  	return func(s *terraform.State) error {
    81  		rs, ok := s.RootModule().Resources[n]
    82  		if !ok {
    83  			return fmt.Errorf("Not found: %s", n)
    84  		}
    85  
    86  		if rs.Primary.ID == "" {
    87  			return fmt.Errorf("No ForwardEntry ID is set")
    88  		}
    89  
    90  		client := testAccProvider.Meta().(*AliyunClient)
    91  		instance, err := client.DescribeForwardEntry(rs.Primary.Attributes["forward_table_id"], rs.Primary.ID)
    92  
    93  		if err != nil {
    94  			return err
    95  		}
    96  		if instance.ForwardEntryId == "" {
    97  			return fmt.Errorf("ForwardEntry not found")
    98  		}
    99  
   100  		*snat = instance
   101  		return nil
   102  	}
   103  }
   104  
   105  const testAccForwardEntryConfig = `
   106  provider "alicloud"{
   107  	region = "cn-hangzhou"
   108  }
   109  
   110  data "alicloud_zones" "default" {
   111  	"available_resource_creation"= "VSwitch"
   112  }
   113  
   114  resource "alicloud_vpc" "foo" {
   115  	name = "tf_test_foo"
   116  	cidr_block = "172.16.0.0/12"
   117  }
   118  
   119  resource "alicloud_vswitch" "foo" {
   120  	vpc_id = "${alicloud_vpc.foo.id}"
   121  	cidr_block = "172.16.0.0/21"
   122  	availability_zone = "${data.alicloud_zones.default.zones.0.id}"
   123  }
   124  
   125  resource "alicloud_nat_gateway" "foo" {
   126  	vpc_id = "${alicloud_vpc.foo.id}"
   127  	spec = "Small"
   128  	name = "test_foo"
   129  	bandwidth_packages = [{
   130  	  ip_count = 1
   131  	  bandwidth = 5
   132  	  zone = "${data.alicloud_zones.default.zones.0.id}"
   133  	},{
   134  	  ip_count = 1
   135  	  bandwidth = 6
   136  	  zone = "${data.alicloud_zones.default.zones.0.id}"
   137  	}]
   138  	depends_on = [
   139      	"alicloud_vswitch.foo"]
   140  }
   141  
   142  resource "alicloud_forward_entry" "foo"{
   143  	forward_table_id = "${alicloud_nat_gateway.foo.forward_table_ids}"
   144  	external_ip = "${alicloud_nat_gateway.foo.bandwidth_packages.0.public_ip_addresses}"
   145  	external_port = "80"
   146  	ip_protocol = "tcp"
   147  	internal_ip = "172.16.0.3"
   148  	internal_port = "8080"
   149  }
   150  
   151  resource "alicloud_forward_entry" "foo1"{
   152  	forward_table_id = "${alicloud_nat_gateway.foo.forward_table_ids}"
   153  	external_ip = "${alicloud_nat_gateway.foo.bandwidth_packages.0.public_ip_addresses}"
   154  	external_port = "443"
   155  	ip_protocol = "udp"
   156  	internal_ip = "172.16.0.4"
   157  	internal_port = "8080"
   158  }
   159  `
   160  
   161  const testAccForwardEntryUpdate = `
   162  provider "alicloud"{
   163  	region = "cn-hangzhou"
   164  }
   165  
   166  data "alicloud_zones" "default" {
   167  	"available_resource_creation"= "VSwitch"
   168  }
   169  
   170  resource "alicloud_vpc" "foo" {
   171  	name = "tf_test_foo"
   172  	cidr_block = "172.16.0.0/12"
   173  }
   174  
   175  resource "alicloud_vswitch" "foo" {
   176  	vpc_id = "${alicloud_vpc.foo.id}"
   177  	cidr_block = "172.16.0.0/21"
   178  	availability_zone = "${data.alicloud_zones.default.zones.0.id}"
   179  }
   180  
   181  resource "alicloud_nat_gateway" "foo" {
   182  	vpc_id = "${alicloud_vpc.foo.id}"
   183  	spec = "Small"
   184  	name = "test_foo"
   185  	bandwidth_packages = [{
   186  	  ip_count = 1
   187  	  bandwidth = 5
   188  	  zone = "${data.alicloud_zones.default.zones.0.id}"
   189  	},{
   190  	  ip_count = 1
   191  	  bandwidth = 6
   192  	  zone = "${data.alicloud_zones.default.zones.0.id}"
   193  	}]
   194  	depends_on = [
   195      	"alicloud_vswitch.foo"]
   196  }
   197  
   198  resource "alicloud_forward_entry" "foo"{
   199  	forward_table_id = "${alicloud_nat_gateway.foo.forward_table_ids}"
   200  	external_ip = "${alicloud_nat_gateway.foo.bandwidth_packages.0.public_ip_addresses}"
   201  	external_port = "80"
   202  	ip_protocol = "tcp"
   203  	internal_ip = "172.16.0.3"
   204  	internal_port = "8081"
   205  }
   206  
   207  
   208  resource "alicloud_forward_entry" "foo1"{
   209  	forward_table_id = "${alicloud_nat_gateway.foo.forward_table_ids}"
   210  	external_ip = "${alicloud_nat_gateway.foo.bandwidth_packages.0.public_ip_addresses}"
   211  	external_port = "22"
   212  	ip_protocol = "udp"
   213  	internal_ip = "172.16.0.4"
   214  	internal_port = "8080"
   215  }
   216  `