github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/alicloud/resource_alicloud_vswitch_test.go (about)

     1  package alicloud
     2  
     3  import (
     4  	"testing"
     5  
     6  	"fmt"
     7  	"github.com/denverdino/aliyungo/common"
     8  	"github.com/denverdino/aliyungo/ecs"
     9  	"github.com/hashicorp/terraform/helper/resource"
    10  	"github.com/hashicorp/terraform/terraform"
    11  )
    12  
    13  func TestAccAlicloudVswitch_basic(t *testing.T) {
    14  	var vsw ecs.VSwitchSetType
    15  
    16  	resource.Test(t, resource.TestCase{
    17  		PreCheck: func() {
    18  			testAccPreCheck(t)
    19  		},
    20  
    21  		// module name
    22  		IDRefreshName: "alicloud_vswitch.foo",
    23  		Providers:     testAccProviders,
    24  		CheckDestroy:  testAccCheckVswitchDestroy,
    25  		Steps: []resource.TestStep{
    26  			resource.TestStep{
    27  				Config: testAccVswitchConfig,
    28  				Check: resource.ComposeTestCheckFunc(
    29  					testAccCheckVswitchExists("alicloud_vswitch.foo", &vsw),
    30  					resource.TestCheckResourceAttr(
    31  						"alicloud_vswitch.foo", "cidr_block", "172.16.0.0/21"),
    32  				),
    33  			},
    34  		},
    35  	})
    36  
    37  }
    38  
    39  func testAccCheckVswitchExists(n string, vpc *ecs.VSwitchSetType) resource.TestCheckFunc {
    40  	return func(s *terraform.State) error {
    41  		rs, ok := s.RootModule().Resources[n]
    42  		if !ok {
    43  			return fmt.Errorf("Not found: %s", n)
    44  		}
    45  
    46  		if rs.Primary.ID == "" {
    47  			return fmt.Errorf("No Vswitch ID is set")
    48  		}
    49  
    50  		client := testAccProvider.Meta().(*AliyunClient)
    51  		instance, err := client.QueryVswitchById(rs.Primary.Attributes["vpc_id"], rs.Primary.ID)
    52  
    53  		if err != nil {
    54  			return err
    55  		}
    56  		if instance == nil {
    57  			return fmt.Errorf("Vswitch not found")
    58  		}
    59  
    60  		*vpc = *instance
    61  		return nil
    62  	}
    63  }
    64  
    65  func testAccCheckVswitchDestroy(s *terraform.State) error {
    66  	client := testAccProvider.Meta().(*AliyunClient)
    67  
    68  	for _, rs := range s.RootModule().Resources {
    69  		if rs.Type != "alicloud_vswitch" {
    70  			continue
    71  		}
    72  
    73  		// Try to find the Vswitch
    74  		instance, err := client.QueryVswitchById(rs.Primary.Attributes["vpc_id"], rs.Primary.ID)
    75  
    76  		if instance != nil {
    77  			return fmt.Errorf("Vswitch still exist")
    78  		}
    79  
    80  		if err != nil {
    81  			// Verify the error is what we want
    82  			e, _ := err.(*common.Error)
    83  
    84  			if e.ErrorResponse.Code != "InvalidVswitchID.NotFound" {
    85  				return err
    86  			}
    87  		}
    88  
    89  	}
    90  
    91  	return nil
    92  }
    93  
    94  const testAccVswitchConfig = `
    95  data "alicloud_zones" "default" {
    96  	"available_resource_creation"= "VSwitch"
    97  }
    98  
    99  resource "alicloud_vpc" "foo" {
   100    name = "tf_test_foo"
   101    cidr_block = "172.16.0.0/12"
   102  }
   103  
   104  resource "alicloud_vswitch" "foo" {
   105    vpc_id = "${alicloud_vpc.foo.id}"
   106    cidr_block = "172.16.0.0/21"
   107    availability_zone = "${data.alicloud_zones.default.zones.0.id}"
   108  }
   109  `