github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/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 ), 26 }, 27 }, 28 }) 29 } 30 31 func testAccCheckCloudStackNetworkACLExists( 32 n string, acl *cloudstack.NetworkACLList) 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 network ACL ID is set") 41 } 42 43 cs := testAccProvider.Meta().(*cloudstack.CloudStackClient) 44 acllist, _, err := cs.NetworkACL.GetNetworkACLListByID(rs.Primary.ID) 45 if err != nil { 46 return err 47 } 48 49 if acllist.Id != rs.Primary.ID { 50 return fmt.Errorf("Network ACL not found") 51 } 52 53 *acl = *acllist 54 55 return nil 56 } 57 } 58 59 func testAccCheckCloudStackNetworkACLBasicAttributes( 60 acl *cloudstack.NetworkACLList) resource.TestCheckFunc { 61 return func(s *terraform.State) error { 62 63 if acl.Name != "terraform-acl" { 64 return fmt.Errorf("Bad name: %s", acl.Name) 65 } 66 67 if acl.Description != "terraform-acl-text" { 68 return fmt.Errorf("Bad description: %s", acl.Description) 69 } 70 71 return nil 72 } 73 } 74 75 func testAccCheckCloudStackNetworkACLDestroy(s *terraform.State) error { 76 cs := testAccProvider.Meta().(*cloudstack.CloudStackClient) 77 78 for _, rs := range s.RootModule().Resources { 79 if rs.Type != "cloudstack_network_acl" { 80 continue 81 } 82 83 if rs.Primary.ID == "" { 84 return fmt.Errorf("No network ACL ID is set") 85 } 86 87 _, _, err := cs.NetworkACL.GetNetworkACLListByID(rs.Primary.ID) 88 if err == nil { 89 return fmt.Errorf("Network ACl list %s still exists", rs.Primary.ID) 90 } 91 } 92 93 return nil 94 } 95 96 var testAccCloudStackNetworkACL_basic = fmt.Sprintf(` 97 resource "cloudstack_vpc" "foobar" { 98 name = "terraform-vpc" 99 cidr = "%s" 100 vpc_offering = "%s" 101 zone = "%s" 102 } 103 104 resource "cloudstack_network_acl" "foo" { 105 name = "terraform-acl" 106 description = "terraform-acl-text" 107 vpc_id = "${cloudstack_vpc.foobar.id}" 108 }`, 109 CLOUDSTACK_VPC_CIDR_1, 110 CLOUDSTACK_VPC_OFFERING, 111 CLOUDSTACK_ZONE)