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

     1  package cloudstack
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/helper/resource"
     8  	"github.com/hashicorp/terraform/terraform"
     9  	"github.com/xanzy/go-cloudstack/cloudstack"
    10  )
    11  
    12  func TestAccCloudStackSecurityGroup_basic(t *testing.T) {
    13  	var sg cloudstack.SecurityGroup
    14  	resource.Test(t, resource.TestCase{
    15  		PreCheck:     func() { testAccPreCheck(t) },
    16  		Providers:    testAccProviders,
    17  		CheckDestroy: testAccCheckCloudStackSecurityGroupDestroy,
    18  		Steps: []resource.TestStep{
    19  			resource.TestStep{
    20  				Config: testAccCloudStackSecurityGroup_basic,
    21  				Check: resource.ComposeTestCheckFunc(
    22  					testAccCheckCloudStackSecurityGroupExists(
    23  						"cloudstack_security_group.foo", &sg),
    24  					testAccCheckCloudStackSecurityGroupBasicAttributes(&sg),
    25  				),
    26  			},
    27  		},
    28  	})
    29  }
    30  
    31  func testAccCheckCloudStackSecurityGroupExists(
    32  	n string, sg *cloudstack.SecurityGroup) resource.TestCheckFunc {
    33  	return func(s *terraform.State) error {
    34  		rs, ok := s.RootModule().Resources[n]
    35  		if !ok {
    36  			return fmt.Errorf("Not found: %s", n)
    37  		}
    38  
    39  		if rs.Primary.ID == "" {
    40  			return fmt.Errorf("No security group ID is set")
    41  		}
    42  
    43  		cs := testAccProvider.Meta().(*cloudstack.CloudStackClient)
    44  		resp, _, err := cs.SecurityGroup.GetSecurityGroupByID(rs.Primary.ID)
    45  		if err != nil {
    46  			return err
    47  		}
    48  
    49  		if resp.Id != rs.Primary.ID {
    50  			return fmt.Errorf("Network ACL not found")
    51  		}
    52  
    53  		*sg = *resp
    54  
    55  		return nil
    56  	}
    57  }
    58  
    59  func testAccCheckCloudStackSecurityGroupBasicAttributes(
    60  	sg *cloudstack.SecurityGroup) resource.TestCheckFunc {
    61  	return func(s *terraform.State) error {
    62  
    63  		if sg.Name != "terraform-security-group" {
    64  			return fmt.Errorf("Bad name: %s", sg.Name)
    65  		}
    66  
    67  		if sg.Description != "terraform-security-group-text" {
    68  			return fmt.Errorf("Bad description: %s", sg.Description)
    69  		}
    70  
    71  		return nil
    72  	}
    73  }
    74  
    75  func testAccCheckCloudStackSecurityGroupDestroy(s *terraform.State) error {
    76  	cs := testAccProvider.Meta().(*cloudstack.CloudStackClient)
    77  
    78  	for _, rs := range s.RootModule().Resources {
    79  		if rs.Type != "cloudstack_security_group" {
    80  			continue
    81  		}
    82  
    83  		if rs.Primary.ID == "" {
    84  			return fmt.Errorf("No security group ID is set")
    85  		}
    86  
    87  		_, _, err := cs.SecurityGroup.GetSecurityGroupByID(rs.Primary.ID)
    88  		if err == nil {
    89  			return fmt.Errorf("Security group list %s still exists", rs.Primary.ID)
    90  		}
    91  	}
    92  
    93  	return nil
    94  }
    95  
    96  var testAccCloudStackSecurityGroup_basic = fmt.Sprintf(`
    97  resource "cloudstack_security_group" "foo" {
    98    name = "terraform-security-group"
    99  	description = "terraform-security-group-text"
   100  }`)