github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/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  		_, _, err := cs.NetworkACL.GetNetworkACLListByID(rs.Primary.ID)
    90  		if err == nil {
    91  			return fmt.Errorf("Network ACl list %s still exists", rs.Primary.ID)
    92  		}
    93  	}
    94  
    95  	return nil
    96  }
    97  
    98  var testAccCloudStackNetworkACL_basic = fmt.Sprintf(`
    99  resource "cloudstack_vpc" "foobar" {
   100    name = "terraform-vpc"
   101    cidr = "%s"
   102    vpc_offering = "%s"
   103    zone = "%s"
   104  }
   105  
   106  resource "cloudstack_network_acl" "foo" {
   107    name = "terraform-acl"
   108    description = "terraform-acl-text"
   109    vpc = "${cloudstack_vpc.foobar.name}"
   110  }`,
   111  	CLOUDSTACK_VPC_CIDR_1,
   112  	CLOUDSTACK_VPC_OFFERING,
   113  	CLOUDSTACK_ZONE)