github.com/chalford/terraform@v0.3.7-0.20150113080010-a78c69a8c81f/builtin/providers/cloudstack/resource_cloudstack_network_acl_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 TestAccCloudStackNetworkACL_basic(t *testing.T) {
    13  	var acl cloudstack.NetworkACLList
    14  	resource.Test(t, resource.TestCase{
    15  		PreCheck:     func() { testAccPreCheck(t) },
    16  		Providers:    testAccProviders,
    17  		CheckDestroy: testAccCheckCloudStackNetworkACLDestroy,
    18  		Steps: []resource.TestStep{
    19  			resource.TestStep{
    20  				Config: testAccCloudStackNetworkACL_basic,
    21  				Check: resource.ComposeTestCheckFunc(
    22  					testAccCheckCloudStackNetworkACLExists(
    23  						"cloudstack_network_acl.foo", &acl),
    24  					testAccCheckCloudStackNetworkACLBasicAttributes(&acl),
    25  					resource.TestCheckResourceAttr(
    26  						"cloudstack_network_acl.foo", "vpc", "terraform-vpc"),
    27  				),
    28  			},
    29  		},
    30  	})
    31  }
    32  
    33  func testAccCheckCloudStackNetworkACLExists(
    34  	n string, acl *cloudstack.NetworkACLList) resource.TestCheckFunc {
    35  	return func(s *terraform.State) error {
    36  		rs, ok := s.RootModule().Resources[n]
    37  		if !ok {
    38  			return fmt.Errorf("Not found: %s", n)
    39  		}
    40  
    41  		if rs.Primary.ID == "" {
    42  			return fmt.Errorf("No network ACL ID is set")
    43  		}
    44  
    45  		cs := testAccProvider.Meta().(*cloudstack.CloudStackClient)
    46  		acllist, _, err := cs.NetworkACL.GetNetworkACLListByID(rs.Primary.ID)
    47  		if err != nil {
    48  			return err
    49  		}
    50  
    51  		if acllist.Id != rs.Primary.ID {
    52  			return fmt.Errorf("Network ACL not found")
    53  		}
    54  
    55  		*acl = *acllist
    56  
    57  		return nil
    58  	}
    59  }
    60  
    61  func testAccCheckCloudStackNetworkACLBasicAttributes(
    62  	acl *cloudstack.NetworkACLList) resource.TestCheckFunc {
    63  	return func(s *terraform.State) error {
    64  
    65  		if acl.Name != "terraform-acl" {
    66  			return fmt.Errorf("Bad name: %s", acl.Name)
    67  		}
    68  
    69  		if acl.Description != "terraform-acl-text" {
    70  			return fmt.Errorf("Bad description: %s", acl.Description)
    71  		}
    72  
    73  		return nil
    74  	}
    75  }
    76  
    77  func testAccCheckCloudStackNetworkACLDestroy(s *terraform.State) error {
    78  	cs := testAccProvider.Meta().(*cloudstack.CloudStackClient)
    79  
    80  	for _, rs := range s.RootModule().Resources {
    81  		if rs.Type != "cloudstack_network_acl" {
    82  			continue
    83  		}
    84  
    85  		if rs.Primary.ID == "" {
    86  			return fmt.Errorf("No network ACL ID is set")
    87  		}
    88  
    89  		p := cs.NetworkACL.NewDeleteNetworkACLListParams(rs.Primary.ID)
    90  		err, _ := cs.NetworkACL.DeleteNetworkACLList(p)
    91  
    92  		if err != nil {
    93  			return fmt.Errorf(
    94  				"Error deleting network ACL (%s): %s",
    95  				rs.Primary.ID, err)
    96  		}
    97  	}
    98  
    99  	return nil
   100  }
   101  
   102  var testAccCloudStackNetworkACL_basic = fmt.Sprintf(`
   103  resource "cloudstack_vpc" "foobar" {
   104    name = "terraform-vpc"
   105    cidr = "%s"
   106    vpc_offering = "%s"
   107    zone = "%s"
   108  }
   109  
   110  resource "cloudstack_network_acl" "foo" {
   111    name = "terraform-acl"
   112    description = "terraform-acl-text"
   113    vpc = "${cloudstack_vpc.foobar.name}"
   114  }`,
   115  	CLOUDSTACK_VPC_CIDR,
   116  	CLOUDSTACK_VPC_OFFERING,
   117  	CLOUDSTACK_ZONE)