github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/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  data "alicloud_zones" "default" {
   128  	"available_resource_creation"= "VSwitch"
   129  }
   130  
   131  resource "alicloud_vpc" "foo" {
   132  	name = "tf_test_foo"
   133  	cidr_block = "10.1.0.0/21"
   134  }
   135  
   136  resource "alicloud_vswitch" "foo" {
   137  	vpc_id = "${alicloud_vpc.foo.id}"
   138  	cidr_block = "10.1.1.0/24"
   139  	availability_zone = "${data.alicloud_zones.default.zones.0.id}"
   140  }
   141  
   142  resource "alicloud_route_entry" "foo" {
   143  	router_id = "${alicloud_vpc.foo.router_id}"
   144  	route_table_id = "${alicloud_vpc.foo.router_table_id}"
   145  	destination_cidrblock = "172.11.1.1/32"
   146  	nexthop_type = "Instance"
   147  	nexthop_id = "${alicloud_instance.foo.id}"
   148  }
   149  
   150  resource "alicloud_security_group" "tf_test_foo" {
   151  	name = "tf_test_foo"
   152  	description = "foo"
   153  	vpc_id = "${alicloud_vpc.foo.id}"
   154  }
   155  
   156  resource "alicloud_security_group_rule" "ingress" {
   157  	type = "ingress"
   158  	ip_protocol = "tcp"
   159  	nic_type = "intranet"
   160  	policy = "accept"
   161  	port_range = "22/22"
   162  	priority = 1
   163  	security_group_id = "${alicloud_security_group.tf_test_foo.id}"
   164  	cidr_ip = "0.0.0.0/0"
   165  }
   166  
   167  resource "alicloud_instance" "foo" {
   168  	# cn-beijing
   169  	security_groups = ["${alicloud_security_group.tf_test_foo.id}"]
   170  
   171  	vswitch_id = "${alicloud_vswitch.foo.id}"
   172  	allocate_public_ip = true
   173  
   174  	# series II
   175  	instance_charge_type = "PostPaid"
   176  	instance_type = "ecs.n1.small"
   177  	internet_charge_type = "PayByTraffic"
   178  	internet_max_bandwidth_out = 5
   179  	io_optimized = "optimized"
   180  
   181  	system_disk_category = "cloud_efficiency"
   182  	image_id = "ubuntu_140405_64_40G_cloudinit_20161115.vhd"
   183  	instance_name = "test_foo"
   184  }
   185  
   186  `