github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/opc/resource_acl_test.go (about) 1 package opc 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/hashicorp/go-oracle-terraform/compute" 8 "github.com/hashicorp/terraform/helper/acctest" 9 "github.com/hashicorp/terraform/helper/resource" 10 "github.com/hashicorp/terraform/terraform" 11 ) 12 13 func TestAccOPCACL_Basic(t *testing.T) { 14 ri := acctest.RandInt() 15 config := fmt.Sprintf(testAccACLBasic, ri) 16 resource.Test(t, resource.TestCase{ 17 PreCheck: func() { testAccPreCheck(t) }, 18 Providers: testAccProviders, 19 CheckDestroy: testAccCheckACLDestroy, 20 Steps: []resource.TestStep{ 21 { 22 Config: config, 23 Check: resource.ComposeTestCheckFunc( 24 testAccCheckACLExists, 25 ), 26 }, 27 }, 28 }) 29 } 30 31 func TestAccOPCACL_Update(t *testing.T) { 32 ri := acctest.RandInt() 33 config := fmt.Sprintf(testAccACLBasic, ri) 34 updatedConfig := fmt.Sprintf(testAccACLDisabled, ri) 35 36 resource.Test(t, resource.TestCase{ 37 PreCheck: func() { testAccPreCheck(t) }, 38 Providers: testAccProviders, 39 CheckDestroy: testAccCheckACLDestroy, 40 Steps: []resource.TestStep{ 41 { 42 Config: config, 43 Check: testAccCheckACLExists, 44 }, 45 { 46 Config: updatedConfig, 47 Check: resource.ComposeTestCheckFunc( 48 testAccCheckACLExists, 49 resource.TestCheckResourceAttr("opc_compute_acl.test", "enabled", "false"), 50 ), 51 }, 52 }, 53 }) 54 } 55 56 func testAccCheckACLExists(s *terraform.State) error { 57 client := testAccProvider.Meta().(*compute.Client).ACLs() 58 59 for _, rs := range s.RootModule().Resources { 60 if rs.Type != "opc_compute_acl" { 61 continue 62 } 63 64 input := compute.GetACLInput{ 65 Name: rs.Primary.Attributes["name"], 66 } 67 if _, err := client.GetACL(&input); err != nil { 68 return fmt.Errorf("Error retrieving state of ACL %s: %s", input.Name, err) 69 } 70 } 71 72 return nil 73 } 74 75 func testAccCheckACLDestroy(s *terraform.State) error { 76 client := testAccProvider.Meta().(*compute.Client).ACLs() 77 78 for _, rs := range s.RootModule().Resources { 79 if rs.Type != "opc_compute_acl" { 80 continue 81 } 82 83 input := compute.GetACLInput{ 84 Name: rs.Primary.Attributes["name"], 85 } 86 if info, err := client.GetACL(&input); err == nil { 87 return fmt.Errorf("ACL %s still exists: %#v", input.Name, info) 88 } 89 } 90 91 return nil 92 } 93 94 var testAccACLBasic = ` 95 resource "opc_compute_acl" "test" { 96 name = "test_acl-%d" 97 description = "test acl" 98 } 99 ` 100 101 var testAccACLDisabled = ` 102 resource "opc_compute_acl" "test" { 103 name = "test_acl-%d" 104 description = "test acl" 105 enabled = false 106 } 107 `