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

     1  package openstack
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/hashicorp/terraform/helper/resource"
     9  	"github.com/hashicorp/terraform/terraform"
    10  	"github.com/rackspace/gophercloud"
    11  	"github.com/rackspace/gophercloud/openstack/networking/v2/extensions/fwaas/firewalls"
    12  )
    13  
    14  func TestAccFWFirewallV1_basic(t *testing.T) {
    15  
    16  	var policyID *string
    17  
    18  	resource.Test(t, resource.TestCase{
    19  		PreCheck:     func() { testAccPreCheck(t) },
    20  		Providers:    testAccProviders,
    21  		CheckDestroy: testAccCheckFWFirewallV1Destroy,
    22  		Steps: []resource.TestStep{
    23  			resource.TestStep{
    24  				Config: testFirewallConfig,
    25  				Check: resource.ComposeTestCheckFunc(
    26  					testAccCheckFWFirewallV1Exists("openstack_fw_firewall_v1.accept_test", "", "", policyID),
    27  				),
    28  			},
    29  			resource.TestStep{
    30  				Config: testFirewallConfigUpdated,
    31  				Check: resource.ComposeTestCheckFunc(
    32  					testAccCheckFWFirewallV1Exists("openstack_fw_firewall_v1.accept_test", "accept_test", "terraform acceptance test", policyID),
    33  				),
    34  			},
    35  		},
    36  	})
    37  }
    38  
    39  func testAccCheckFWFirewallV1Destroy(s *terraform.State) error {
    40  
    41  	config := testAccProvider.Meta().(*Config)
    42  	networkingClient, err := config.networkingV2Client(OS_REGION_NAME)
    43  	if err != nil {
    44  		return fmt.Errorf("(testAccCheckOpenstackFirewallDestroy) Error creating OpenStack networking client: %s", err)
    45  	}
    46  	for _, rs := range s.RootModule().Resources {
    47  		if rs.Type != "openstack_firewall" {
    48  			continue
    49  		}
    50  		_, err = firewalls.Get(networkingClient, rs.Primary.ID).Extract()
    51  		if err == nil {
    52  			return fmt.Errorf("Firewall (%s) still exists.", rs.Primary.ID)
    53  		}
    54  		httpError, ok := err.(*gophercloud.UnexpectedResponseCodeError)
    55  		if !ok || httpError.Actual != 404 {
    56  			return httpError
    57  		}
    58  	}
    59  	return nil
    60  }
    61  
    62  func testAccCheckFWFirewallV1Exists(n, expectedName, expectedDescription string, policyID *string) resource.TestCheckFunc {
    63  
    64  	return func(s *terraform.State) error {
    65  
    66  		rs, ok := s.RootModule().Resources[n]
    67  		if !ok {
    68  			return fmt.Errorf("Not found: %s", n)
    69  		}
    70  
    71  		if rs.Primary.ID == "" {
    72  			return fmt.Errorf("No ID is set")
    73  		}
    74  
    75  		config := testAccProvider.Meta().(*Config)
    76  		networkingClient, err := config.networkingV2Client(OS_REGION_NAME)
    77  		if err != nil {
    78  			return fmt.Errorf("(testAccCheckFirewallExists) Error creating OpenStack networking client: %s", err)
    79  		}
    80  
    81  		var found *firewalls.Firewall
    82  		for i := 0; i < 5; i++ {
    83  			// Firewall creation is asynchronous. Retry some times
    84  			// if we get a 404 error. Fail on any other error.
    85  			found, err = firewalls.Get(networkingClient, rs.Primary.ID).Extract()
    86  			if err != nil {
    87  				httpError, ok := err.(*gophercloud.UnexpectedResponseCodeError)
    88  				if !ok || httpError.Actual != 404 {
    89  					time.Sleep(time.Second)
    90  					continue
    91  				}
    92  			}
    93  			break
    94  		}
    95  
    96  		if err != nil {
    97  			return err
    98  		}
    99  
   100  		if found.Name != expectedName {
   101  			return fmt.Errorf("Expected Name to be <%s> but found <%s>", expectedName, found.Name)
   102  		}
   103  		if found.Description != expectedDescription {
   104  			return fmt.Errorf("Expected Description to be <%s> but found <%s>", expectedDescription, found.Description)
   105  		}
   106  		if found.PolicyID == "" {
   107  			return fmt.Errorf("Policy should not be empty")
   108  		}
   109  		if policyID != nil && found.PolicyID == *policyID {
   110  			return fmt.Errorf("Policy had not been correctly updated. Went from <%s> to <%s>", expectedName, found.Name)
   111  		}
   112  
   113  		policyID = &found.PolicyID
   114  
   115  		return nil
   116  	}
   117  }
   118  
   119  const testFirewallConfig = `
   120  resource "openstack_fw_firewall_v1" "accept_test" {
   121  	policy_id = "${openstack_fw_policy_v1.accept_test_policy_1.id}"
   122  }
   123  
   124  resource "openstack_fw_policy_v1" "accept_test_policy_1" {
   125  	name = "policy-1"
   126  }
   127  `
   128  
   129  const testFirewallConfigUpdated = `
   130  resource "openstack_fw_firewall_v1" "accept_test" {
   131  	name = "accept_test"
   132  	description = "terraform acceptance test"
   133  	policy_id = "${openstack_fw_policy_v1.accept_test_policy_2.id}"
   134  }
   135  
   136  resource "openstack_fw_policy_v1" "accept_test_policy_2" {
   137  	name = "policy-2"
   138  }
   139  `