github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/openstack/resource_openstack_fw_rule_v1_test.go (about)

     1  package openstack
     2  
     3  import (
     4  	"fmt"
     5  	"reflect"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/hashicorp/terraform/helper/resource"
    10  	"github.com/hashicorp/terraform/terraform"
    11  	"github.com/rackspace/gophercloud"
    12  	"github.com/rackspace/gophercloud/openstack/networking/v2/extensions/fwaas/rules"
    13  )
    14  
    15  func TestAccFWRuleV1_basic(t *testing.T) {
    16  	resource.Test(t, resource.TestCase{
    17  		PreCheck:     func() { testAccPreCheck(t) },
    18  		Providers:    testAccProviders,
    19  		CheckDestroy: testAccCheckFWRuleV1Destroy,
    20  		Steps: []resource.TestStep{
    21  			resource.TestStep{
    22  				Config: testFirewallRuleMinimalConfig,
    23  				Check: resource.ComposeTestCheckFunc(
    24  					testAccCheckFWRuleV1Exists(
    25  						"openstack_fw_rule_v1.accept_test_minimal",
    26  						&rules.Rule{
    27  							Protocol:  "udp",
    28  							Action:    "deny",
    29  							IPVersion: 4,
    30  							Enabled:   true,
    31  						}),
    32  				),
    33  			},
    34  			resource.TestStep{
    35  				Config: testFirewallRuleConfig,
    36  				Check: resource.ComposeTestCheckFunc(
    37  					testAccCheckFWRuleV1Exists(
    38  						"openstack_fw_rule_v1.accept_test",
    39  						&rules.Rule{
    40  							Name:                 "accept_test",
    41  							Protocol:             "udp",
    42  							Action:               "deny",
    43  							Description:          "Terraform accept test",
    44  							IPVersion:            4,
    45  							SourceIPAddress:      "1.2.3.4",
    46  							DestinationIPAddress: "4.3.2.0/24",
    47  							SourcePort:           "444",
    48  							DestinationPort:      "555",
    49  							Enabled:              true,
    50  						}),
    51  				),
    52  			},
    53  			resource.TestStep{
    54  				Config: testFirewallRuleUpdateAllFieldsConfig,
    55  				Check: resource.ComposeTestCheckFunc(
    56  					testAccCheckFWRuleV1Exists(
    57  						"openstack_fw_rule_v1.accept_test",
    58  						&rules.Rule{
    59  							Name:                 "accept_test_updated_2",
    60  							Protocol:             "tcp",
    61  							Action:               "allow",
    62  							Description:          "Terraform accept test updated",
    63  							IPVersion:            4,
    64  							SourceIPAddress:      "1.2.3.0/24",
    65  							DestinationIPAddress: "4.3.2.8",
    66  							SourcePort:           "666",
    67  							DestinationPort:      "777",
    68  							Enabled:              false,
    69  						}),
    70  				),
    71  			},
    72  		},
    73  	})
    74  }
    75  
    76  func testAccCheckFWRuleV1Destroy(s *terraform.State) error {
    77  
    78  	config := testAccProvider.Meta().(*Config)
    79  	networkingClient, err := config.networkingV2Client(OS_REGION_NAME)
    80  	if err != nil {
    81  		return fmt.Errorf("(testAccCheckOpenstackFirewallRuleDestroy) Error creating OpenStack networking client: %s", err)
    82  	}
    83  	for _, rs := range s.RootModule().Resources {
    84  		if rs.Type != "openstack_firewall_rule" {
    85  			continue
    86  		}
    87  		_, err = rules.Get(networkingClient, rs.Primary.ID).Extract()
    88  		if err == nil {
    89  			return fmt.Errorf("Firewall rule (%s) still exists.", rs.Primary.ID)
    90  		}
    91  		httpError, ok := err.(*gophercloud.UnexpectedResponseCodeError)
    92  		if !ok || httpError.Actual != 404 {
    93  			return httpError
    94  		}
    95  	}
    96  	return nil
    97  }
    98  
    99  func testAccCheckFWRuleV1Exists(n string, expected *rules.Rule) resource.TestCheckFunc {
   100  
   101  	return func(s *terraform.State) error {
   102  
   103  		rs, ok := s.RootModule().Resources[n]
   104  		if !ok {
   105  			return fmt.Errorf("Not found: %s", n)
   106  		}
   107  
   108  		if rs.Primary.ID == "" {
   109  			return fmt.Errorf("No ID is set")
   110  		}
   111  
   112  		config := testAccProvider.Meta().(*Config)
   113  		networkingClient, err := config.networkingV2Client(OS_REGION_NAME)
   114  		if err != nil {
   115  			return fmt.Errorf("(testAccCheckFirewallRuleExists) Error creating OpenStack networking client: %s", err)
   116  		}
   117  
   118  		var found *rules.Rule
   119  		for i := 0; i < 5; i++ {
   120  			// Firewall rule creation is asynchronous. Retry some times
   121  			// if we get a 404 error. Fail on any other error.
   122  			found, err = rules.Get(networkingClient, rs.Primary.ID).Extract()
   123  			if err != nil {
   124  				httpError, ok := err.(*gophercloud.UnexpectedResponseCodeError)
   125  				if !ok || httpError.Actual != 404 {
   126  					time.Sleep(time.Second)
   127  					continue
   128  				}
   129  			}
   130  			break
   131  		}
   132  
   133  		if err != nil {
   134  			return err
   135  		}
   136  
   137  		expected.ID = found.ID
   138  		// Erase the tenant id because we don't want to compare
   139  		// it as long it is not present in the expected
   140  		found.TenantID = ""
   141  
   142  		if !reflect.DeepEqual(expected, found) {
   143  			return fmt.Errorf("Expected:\n%#v\nFound:\n%#v", expected, found)
   144  		}
   145  
   146  		return nil
   147  	}
   148  }
   149  
   150  const testFirewallRuleMinimalConfig = `
   151  resource "openstack_fw_rule_v1" "accept_test_minimal" {
   152  	protocol = "udp"
   153  	action = "deny"
   154  }
   155  `
   156  
   157  const testFirewallRuleConfig = `
   158  resource "openstack_fw_rule_v1" "accept_test" {
   159  	name = "accept_test"
   160  	description = "Terraform accept test"
   161  	protocol = "udp"
   162  	action = "deny"
   163  	ip_version = 4
   164  	source_ip_address = "1.2.3.4"
   165  	destination_ip_address = "4.3.2.0/24"
   166  	source_port = "444"
   167  	destination_port = "555"
   168  	enabled = true
   169  }
   170  `
   171  
   172  const testFirewallRuleUpdateAllFieldsConfig = `
   173  resource "openstack_fw_rule_v1" "accept_test" {
   174  	name = "accept_test_updated_2"
   175  	description = "Terraform accept test updated"
   176  	protocol = "tcp"
   177  	action = "allow"
   178  	ip_version = 4
   179  	source_ip_address = "1.2.3.0/24"
   180  	destination_ip_address = "4.3.2.8"
   181  	source_port = "666"
   182  	destination_port = "777"
   183  	enabled = false
   184  }
   185  `