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

     1  package alicloud
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/denverdino/aliyungo/ecs"
     6  	"github.com/hashicorp/terraform/helper/resource"
     7  	"github.com/hashicorp/terraform/terraform"
     8  	"strings"
     9  	"testing"
    10  )
    11  
    12  func TestAccAlicloudRouteEntry_Basic(t *testing.T) {
    13  	var rt ecs.RouteTableSetType
    14  	var rn ecs.RouteEntrySetType
    15  
    16  	resource.Test(t, resource.TestCase{
    17  		PreCheck: func() {
    18  			testAccPreCheck(t)
    19  		},
    20  
    21  		// module name
    22  		IDRefreshName: "alicloud_route_entry.foo",
    23  		Providers:     testAccProviders,
    24  		CheckDestroy:  testAccCheckRouteEntryDestroy,
    25  		Steps: []resource.TestStep{
    26  			resource.TestStep{
    27  				Config: testAccRouteEntryConfig,
    28  				Check: resource.ComposeTestCheckFunc(
    29  					testAccCheckRouteTableEntryExists(
    30  						"alicloud_route_entry.foo", &rt, &rn),
    31  					resource.TestCheckResourceAttrSet(
    32  						"alicloud_route_entry.foo", "nexthop_id"),
    33  				),
    34  			},
    35  		},
    36  	})
    37  
    38  }
    39  
    40  func testAccCheckRouteTableExists(rtId string, t *ecs.RouteTableSetType) error {
    41  	client := testAccProvider.Meta().(*AliyunClient)
    42  	//query route table
    43  	rt, terr := client.QueryRouteTableById(rtId)
    44  
    45  	if terr != nil {
    46  		return terr
    47  	}
    48  
    49  	if rt == nil {
    50  		return fmt.Errorf("Route Table not found")
    51  	}
    52  
    53  	*t = *rt
    54  	return nil
    55  }
    56  
    57  func testAccCheckRouteEntryExists(routeTableId, cidrBlock, nextHopType, nextHopId string, e *ecs.RouteEntrySetType) error {
    58  	client := testAccProvider.Meta().(*AliyunClient)
    59  	//query route table entry
    60  	re, rerr := client.QueryRouteEntry(routeTableId, cidrBlock, nextHopType, nextHopId)
    61  
    62  	if rerr != nil {
    63  		return rerr
    64  	}
    65  
    66  	if re == nil {
    67  		return fmt.Errorf("Route Table Entry not found")
    68  	}
    69  
    70  	*e = *re
    71  	return nil
    72  }
    73  
    74  func testAccCheckRouteTableEntryExists(n string, t *ecs.RouteTableSetType, e *ecs.RouteEntrySetType) resource.TestCheckFunc {
    75  	return func(s *terraform.State) error {
    76  		rs, ok := s.RootModule().Resources[n]
    77  		if !ok {
    78  			return fmt.Errorf("Not found: %s", n)
    79  		}
    80  
    81  		if rs.Primary.ID == "" {
    82  			return fmt.Errorf("No Route Entry ID is set")
    83  		}
    84  
    85  		parts := strings.Split(rs.Primary.ID, ":")
    86  
    87  		//query route table
    88  		err := testAccCheckRouteTableExists(parts[0], t)
    89  
    90  		if err != nil {
    91  			return err
    92  		}
    93  		//query route table entry
    94  		err = testAccCheckRouteEntryExists(parts[0], parts[2], parts[3], parts[4], e)
    95  		return err
    96  	}
    97  }
    98  
    99  func testAccCheckRouteEntryDestroy(s *terraform.State) error {
   100  	client := testAccProvider.Meta().(*AliyunClient)
   101  
   102  	for _, rs := range s.RootModule().Resources {
   103  		if rs.Type != "alicloud_route_entry" {
   104  			continue
   105  		}
   106  
   107  		parts := strings.Split(rs.Primary.ID, ":")
   108  		re, err := client.QueryRouteEntry(parts[0], parts[2], parts[3], parts[4])
   109  
   110  		if re != nil {
   111  			return fmt.Errorf("Error Route Entry still exist")
   112  		}
   113  
   114  		// Verify the error is what we want
   115  		if err != nil {
   116  			if notFoundError(err) {
   117  				return nil
   118  			}
   119  			return err
   120  		}
   121  	}
   122  
   123  	return nil
   124  }
   125  
   126  const testAccRouteEntryConfig = `
   127  resource "alicloud_vpc" "foo" {
   128  	name = "tf_test_foo"
   129  	cidr_block = "10.1.0.0/21"
   130  }
   131  
   132  resource "alicloud_vswitch" "foo" {
   133  	vpc_id = "${alicloud_vpc.foo.id}"
   134  	cidr_block = "10.1.1.0/24"
   135  	availability_zone = "cn-beijing-c"
   136  }
   137  
   138  resource "alicloud_route_entry" "foo" {
   139  	router_id = "${alicloud_vpc.foo.router_id}"
   140  	route_table_id = "${alicloud_vpc.foo.router_table_id}"
   141  	destination_cidrblock = "172.11.1.1/32"
   142  	nexthop_type = "Instance"
   143  	nexthop_id = "${alicloud_instance.foo.id}"
   144  }
   145  
   146  resource "alicloud_security_group" "tf_test_foo" {
   147  	name = "tf_test_foo"
   148  	description = "foo"
   149  	vpc_id = "${alicloud_vpc.foo.id}"
   150  }
   151  
   152  resource "alicloud_security_group_rule" "ingress" {
   153  	type = "ingress"
   154  	ip_protocol = "tcp"
   155  	nic_type = "intranet"
   156  	policy = "accept"
   157  	port_range = "22/22"
   158  	priority = 1
   159  	security_group_id = "${alicloud_security_group.tf_test_foo.id}"
   160  	cidr_ip = "0.0.0.0/0"
   161  }
   162  
   163  resource "alicloud_instance" "foo" {
   164  	# cn-beijing
   165  	availability_zone = "cn-beijing-c"
   166  	security_groups = ["${alicloud_security_group.tf_test_foo.id}"]
   167  
   168  	vswitch_id = "${alicloud_vswitch.foo.id}"
   169  	allocate_public_ip = true
   170  
   171  	# series II
   172  	instance_charge_type = "PostPaid"
   173  	instance_type = "ecs.n1.small"
   174  	internet_charge_type = "PayByTraffic"
   175  	internet_max_bandwidth_out = 5
   176  	io_optimized = "optimized"
   177  
   178  	system_disk_category = "cloud_efficiency"
   179  	image_id = "ubuntu_140405_64_40G_cloudinit_20161115.vhd"
   180  	instance_name = "test_foo"
   181  }
   182  
   183  `