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