github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/triton/resource_firewall_rule_test.go (about) 1 package triton 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/hashicorp/terraform/helper/resource" 8 "github.com/hashicorp/terraform/terraform" 9 "github.com/joyent/triton-go" 10 ) 11 12 func TestAccTritonFirewallRule_basic(t *testing.T) { 13 config := testAccTritonFirewallRule_basic 14 15 resource.Test(t, resource.TestCase{ 16 PreCheck: func() { testAccPreCheck(t) }, 17 Providers: testAccProviders, 18 CheckDestroy: testCheckTritonFirewallRuleDestroy, 19 Steps: []resource.TestStep{ 20 { 21 Config: config, 22 Check: resource.ComposeTestCheckFunc( 23 testCheckTritonFirewallRuleExists("triton_firewall_rule.test"), 24 ), 25 }, 26 }, 27 }) 28 } 29 30 func TestAccTritonFirewallRule_update(t *testing.T) { 31 preConfig := testAccTritonFirewallRule_basic 32 postConfig := testAccTritonFirewallRule_update 33 34 resource.Test(t, resource.TestCase{ 35 PreCheck: func() { testAccPreCheck(t) }, 36 Providers: testAccProviders, 37 CheckDestroy: testCheckTritonFirewallRuleDestroy, 38 Steps: []resource.TestStep{ 39 { 40 Config: preConfig, 41 Check: resource.ComposeTestCheckFunc( 42 testCheckTritonFirewallRuleExists("triton_firewall_rule.test"), 43 resource.TestCheckResourceAttr("triton_firewall_rule.test", "rule", "FROM any TO tag \"www\" ALLOW tcp PORT 80"), 44 resource.TestCheckResourceAttr("triton_firewall_rule.test", "enabled", "false"), 45 ), 46 }, 47 48 { 49 Config: postConfig, 50 Check: resource.ComposeTestCheckFunc( 51 testCheckTritonFirewallRuleExists("triton_firewall_rule.test"), 52 resource.TestCheckResourceAttr("triton_firewall_rule.test", "rule", "FROM any TO tag \"www\" BLOCK tcp PORT 80"), 53 resource.TestCheckResourceAttr("triton_firewall_rule.test", "enabled", "true"), 54 ), 55 }, 56 }, 57 }) 58 } 59 60 func TestAccTritonFirewallRule_enable(t *testing.T) { 61 preConfig := testAccTritonFirewallRule_basic 62 postConfig := testAccTritonFirewallRule_enable 63 64 resource.Test(t, resource.TestCase{ 65 PreCheck: func() { testAccPreCheck(t) }, 66 Providers: testAccProviders, 67 CheckDestroy: testCheckTritonFirewallRuleDestroy, 68 Steps: []resource.TestStep{ 69 { 70 Config: preConfig, 71 Check: resource.ComposeTestCheckFunc( 72 testCheckTritonFirewallRuleExists("triton_firewall_rule.test"), 73 resource.TestCheckResourceAttr("triton_firewall_rule.test", "rule", "FROM any TO tag \"www\" ALLOW tcp PORT 80"), 74 resource.TestCheckResourceAttr("triton_firewall_rule.test", "enabled", "false"), 75 ), 76 }, 77 78 { 79 Config: postConfig, 80 Check: resource.ComposeTestCheckFunc( 81 testCheckTritonFirewallRuleExists("triton_firewall_rule.test"), 82 resource.TestCheckResourceAttr("triton_firewall_rule.test", "rule", "FROM any TO tag \"www\" ALLOW tcp PORT 80"), 83 resource.TestCheckResourceAttr("triton_firewall_rule.test", "enabled", "true"), 84 ), 85 }, 86 }, 87 }) 88 } 89 90 func testCheckTritonFirewallRuleExists(name string) resource.TestCheckFunc { 91 return func(s *terraform.State) error { 92 // Ensure we have enough information in state to look up in API 93 rs, ok := s.RootModule().Resources[name] 94 if !ok { 95 return fmt.Errorf("Not found: %s", name) 96 } 97 conn := testAccProvider.Meta().(*triton.Client) 98 99 resp, err := conn.Firewall().GetFirewallRule(&triton.GetFirewallRuleInput{ 100 ID: rs.Primary.ID, 101 }) 102 if err != nil && triton.IsResourceNotFound(err) { 103 return fmt.Errorf("Bad: Check Firewall Rule Exists: %s", err) 104 } else if err != nil { 105 return err 106 } 107 108 if resp == nil { 109 return fmt.Errorf("Bad: Firewall Rule %q does not exist", rs.Primary.ID) 110 } 111 112 return nil 113 } 114 } 115 116 func testCheckTritonFirewallRuleDestroy(s *terraform.State) error { 117 conn := testAccProvider.Meta().(*triton.Client) 118 119 for _, rs := range s.RootModule().Resources { 120 if rs.Type != "triton_firewall_rule" { 121 continue 122 } 123 124 resp, err := conn.Firewall().GetFirewallRule(&triton.GetFirewallRuleInput{ 125 ID: rs.Primary.ID, 126 }) 127 if triton.IsResourceNotFound(err) { 128 return nil 129 } else if err != nil { 130 return err 131 } 132 133 if resp != nil { 134 return fmt.Errorf("Bad: Firewall Rule %q still exists", rs.Primary.ID) 135 } 136 } 137 138 return nil 139 } 140 141 var testAccTritonFirewallRule_basic = ` 142 resource "triton_firewall_rule" "test" { 143 rule = "FROM any TO tag \"www\" ALLOW tcp PORT 80" 144 enabled = false 145 } 146 ` 147 148 var testAccTritonFirewallRule_update = ` 149 resource "triton_firewall_rule" "test" { 150 rule = "FROM any TO tag \"www\" BLOCK tcp PORT 80" 151 enabled = true 152 } 153 ` 154 155 var testAccTritonFirewallRule_enable = ` 156 resource "triton_firewall_rule" "test" { 157 rule = "FROM any TO tag \"www\" ALLOW tcp PORT 80" 158 enabled = true 159 } 160 `