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

     1  package alicloud
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/denverdino/aliyungo/ecs"
     8  	"github.com/hashicorp/terraform/helper/resource"
     9  	"github.com/hashicorp/terraform/terraform"
    10  	"log"
    11  )
    12  
    13  func TestAccAlicloudSecurityGroup_basic(t *testing.T) {
    14  	var sg ecs.DescribeSecurityGroupAttributeResponse
    15  
    16  	resource.Test(t, resource.TestCase{
    17  		PreCheck: func() {
    18  			testAccPreCheck(t)
    19  		},
    20  
    21  		// module name
    22  		IDRefreshName: "alicloud_security_group.foo",
    23  
    24  		Providers:    testAccProviders,
    25  		CheckDestroy: testAccCheckSecurityGroupDestroy,
    26  		Steps: []resource.TestStep{
    27  			resource.TestStep{
    28  				Config: testAccSecurityGroupConfig,
    29  				Check: resource.ComposeTestCheckFunc(
    30  					testAccCheckSecurityGroupExists(
    31  						"alicloud_security_group.foo", &sg),
    32  					resource.TestCheckResourceAttr(
    33  						"alicloud_security_group.foo",
    34  						"name",
    35  						"sg_test"),
    36  				),
    37  			},
    38  		},
    39  	})
    40  
    41  }
    42  
    43  func TestAccAlicloudSecurityGroup_withVpc(t *testing.T) {
    44  	var sg ecs.DescribeSecurityGroupAttributeResponse
    45  	var vpc ecs.VpcSetType
    46  
    47  	resource.Test(t, resource.TestCase{
    48  		PreCheck: func() {
    49  			testAccPreCheck(t)
    50  		},
    51  
    52  		// module name
    53  		IDRefreshName: "alicloud_security_group.foo",
    54  
    55  		Providers:    testAccProviders,
    56  		CheckDestroy: testAccCheckSecurityGroupDestroy,
    57  		Steps: []resource.TestStep{
    58  			resource.TestStep{
    59  				Config: testAccSecurityGroupConfig_withVpc,
    60  				Check: resource.ComposeTestCheckFunc(
    61  					testAccCheckSecurityGroupExists(
    62  						"alicloud_security_group.foo", &sg),
    63  					testAccCheckVpcExists(
    64  						"alicloud_vpc.vpc", &vpc),
    65  				),
    66  			},
    67  		},
    68  	})
    69  
    70  }
    71  
    72  func testAccCheckSecurityGroupExists(n string, sg *ecs.DescribeSecurityGroupAttributeResponse) resource.TestCheckFunc {
    73  	return func(s *terraform.State) error {
    74  		rs, ok := s.RootModule().Resources[n]
    75  		if !ok {
    76  			return fmt.Errorf("Not found: %s", n)
    77  		}
    78  
    79  		if rs.Primary.ID == "" {
    80  			return fmt.Errorf("No SecurityGroup ID is set")
    81  		}
    82  
    83  		client := testAccProvider.Meta().(*AliyunClient)
    84  		conn := client.ecsconn
    85  		args := &ecs.DescribeSecurityGroupAttributeArgs{
    86  			RegionId:        client.Region,
    87  			SecurityGroupId: rs.Primary.ID,
    88  		}
    89  		d, err := conn.DescribeSecurityGroupAttribute(args)
    90  
    91  		log.Printf("[WARN] security group id %#v", rs.Primary.ID)
    92  
    93  		if err != nil {
    94  			return err
    95  		}
    96  
    97  		if d == nil {
    98  			return fmt.Errorf("SecurityGroup not found")
    99  		}
   100  
   101  		*sg = *d
   102  		return nil
   103  	}
   104  }
   105  
   106  func testAccCheckSecurityGroupDestroy(s *terraform.State) error {
   107  	client := testAccProvider.Meta().(*AliyunClient)
   108  	conn := client.ecsconn
   109  
   110  	for _, rs := range s.RootModule().Resources {
   111  		if rs.Type != "alicloud_security_group" {
   112  			continue
   113  		}
   114  
   115  		// Try to find the SecurityGroup
   116  		args := &ecs.DescribeSecurityGroupsArgs{
   117  			RegionId: client.Region,
   118  		}
   119  
   120  		groups, _, err := conn.DescribeSecurityGroups(args)
   121  
   122  		for _, sg := range groups {
   123  			if sg.SecurityGroupId == rs.Primary.ID {
   124  				return fmt.Errorf("Error SecurityGroup still exist")
   125  			}
   126  		}
   127  
   128  		// Verify the error is what we want
   129  		if err != nil {
   130  			return err
   131  		}
   132  	}
   133  
   134  	return nil
   135  }
   136  
   137  const testAccSecurityGroupConfig = `
   138  resource "alicloud_security_group" "foo" {
   139    name = "sg_test"
   140  }
   141  `
   142  
   143  const testAccSecurityGroupConfig_withVpc = `
   144  resource "alicloud_security_group" "foo" {
   145    vpc_id = "${alicloud_vpc.vpc.id}"
   146  }
   147  
   148  resource "alicloud_vpc" "vpc" {
   149    cidr_block = "10.1.0.0/21"
   150  }
   151  `