github.com/tam7t/terraform@v0.7.0-rc2.0.20160705125922-be2469a05c5e/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/gosdc/cloudapi"
    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  			resource.TestStep{
    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  			resource.TestStep{
    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  			resource.TestStep{
    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  			resource.TestStep{
    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  			resource.TestStep{
    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().(*cloudapi.Client)
    98  
    99  		rule, err := conn.GetFirewallRule(rs.Primary.ID)
   100  		if err != nil {
   101  			return fmt.Errorf("Bad: Check Firewall Rule Exists: %s", err)
   102  		}
   103  
   104  		if rule == nil {
   105  			return fmt.Errorf("Bad: Firewall rule %q does not exist", rs.Primary.ID)
   106  		}
   107  
   108  		return nil
   109  	}
   110  }
   111  
   112  func testCheckTritonFirewallRuleDestroy(s *terraform.State) error {
   113  	conn := testAccProvider.Meta().(*cloudapi.Client)
   114  
   115  	for _, rs := range s.RootModule().Resources {
   116  		if rs.Type != "triton_firewall_rule" {
   117  			continue
   118  		}
   119  
   120  		resp, err := conn.GetFirewallRule(rs.Primary.ID)
   121  		if err != nil {
   122  			return nil
   123  		}
   124  
   125  		if resp != nil {
   126  			return fmt.Errorf("Bad: Firewall rule %q still exists", rs.Primary.ID)
   127  		}
   128  	}
   129  
   130  	return nil
   131  }
   132  
   133  var testAccTritonFirewallRule_basic = `
   134  resource "triton_firewall_rule" "test" {
   135  	rule = "FROM any TO tag www ALLOW tcp PORT 80"
   136      enabled = false
   137  }
   138  `
   139  
   140  var testAccTritonFirewallRule_update = `
   141  resource "triton_firewall_rule" "test" {
   142  	rule = "FROM any TO tag www BLOCK tcp PORT 80"
   143  	enabled = true
   144  }
   145  `
   146  
   147  var testAccTritonFirewallRule_enable = `
   148  resource "triton_firewall_rule" "test" {
   149  	rule = "FROM any TO tag www ALLOW tcp PORT 80"
   150  	enabled = true
   151  }
   152  `