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