github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/openstack/resource_openstack_networking_secgroup_v2_test.go (about)

     1  package openstack
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/helper/resource"
     8  	"github.com/hashicorp/terraform/terraform"
     9  
    10  	"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/security/groups"
    11  )
    12  
    13  func TestAccNetworkingV2SecGroup_basic(t *testing.T) {
    14  	var security_group groups.SecGroup
    15  
    16  	resource.Test(t, resource.TestCase{
    17  		PreCheck:     func() { testAccPreCheck(t) },
    18  		Providers:    testAccProviders,
    19  		CheckDestroy: testAccCheckNetworkingV2SecGroupDestroy,
    20  		Steps: []resource.TestStep{
    21  			resource.TestStep{
    22  				Config: testAccNetworkingV2SecGroup_basic,
    23  				Check: resource.ComposeTestCheckFunc(
    24  					testAccCheckNetworkingV2SecGroupExists(
    25  						"openstack_networking_secgroup_v2.secgroup_1", &security_group),
    26  					testAccCheckNetworkingV2SecGroupRuleCount(&security_group, 2),
    27  				),
    28  			},
    29  			resource.TestStep{
    30  				Config: testAccNetworkingV2SecGroup_update,
    31  				Check: resource.ComposeTestCheckFunc(
    32  					resource.TestCheckResourceAttr(
    33  						"openstack_networking_secgroup_v2.secgroup_1", "name", "security_group_2"),
    34  				),
    35  			},
    36  		},
    37  	})
    38  }
    39  
    40  func TestAccNetworkingV2SecGroup_noDefaultRules(t *testing.T) {
    41  	var security_group groups.SecGroup
    42  
    43  	resource.Test(t, resource.TestCase{
    44  		PreCheck:     func() { testAccPreCheck(t) },
    45  		Providers:    testAccProviders,
    46  		CheckDestroy: testAccCheckNetworkingV2SecGroupDestroy,
    47  		Steps: []resource.TestStep{
    48  			resource.TestStep{
    49  				Config: testAccNetworkingV2SecGroup_noDefaultRules,
    50  				Check: resource.ComposeTestCheckFunc(
    51  					testAccCheckNetworkingV2SecGroupExists(
    52  						"openstack_networking_secgroup_v2.secgroup_1", &security_group),
    53  					testAccCheckNetworkingV2SecGroupRuleCount(&security_group, 0),
    54  				),
    55  			},
    56  		},
    57  	})
    58  }
    59  
    60  func TestAccNetworkingV2SecGroup_timeout(t *testing.T) {
    61  	var security_group groups.SecGroup
    62  
    63  	resource.Test(t, resource.TestCase{
    64  		PreCheck:     func() { testAccPreCheck(t) },
    65  		Providers:    testAccProviders,
    66  		CheckDestroy: testAccCheckNetworkingV2SecGroupDestroy,
    67  		Steps: []resource.TestStep{
    68  			resource.TestStep{
    69  				Config: testAccNetworkingV2SecGroup_timeout,
    70  				Check: resource.ComposeTestCheckFunc(
    71  					testAccCheckNetworkingV2SecGroupExists(
    72  						"openstack_networking_secgroup_v2.secgroup_1", &security_group),
    73  				),
    74  			},
    75  		},
    76  	})
    77  }
    78  
    79  func testAccCheckNetworkingV2SecGroupDestroy(s *terraform.State) error {
    80  	config := testAccProvider.Meta().(*Config)
    81  	networkingClient, err := config.networkingV2Client(OS_REGION_NAME)
    82  	if err != nil {
    83  		return fmt.Errorf("Error creating OpenStack networking client: %s", err)
    84  	}
    85  
    86  	for _, rs := range s.RootModule().Resources {
    87  		if rs.Type != "openstack_networking_secgroup_v2" {
    88  			continue
    89  		}
    90  
    91  		_, err := groups.Get(networkingClient, rs.Primary.ID).Extract()
    92  		if err == nil {
    93  			return fmt.Errorf("Security group still exists")
    94  		}
    95  	}
    96  
    97  	return nil
    98  }
    99  
   100  func testAccCheckNetworkingV2SecGroupExists(n string, security_group *groups.SecGroup) resource.TestCheckFunc {
   101  	return func(s *terraform.State) error {
   102  		rs, ok := s.RootModule().Resources[n]
   103  		if !ok {
   104  			return fmt.Errorf("Not found: %s", n)
   105  		}
   106  
   107  		if rs.Primary.ID == "" {
   108  			return fmt.Errorf("No ID is set")
   109  		}
   110  
   111  		config := testAccProvider.Meta().(*Config)
   112  		networkingClient, err := config.networkingV2Client(OS_REGION_NAME)
   113  		if err != nil {
   114  			return fmt.Errorf("Error creating OpenStack networking client: %s", err)
   115  		}
   116  
   117  		found, err := groups.Get(networkingClient, rs.Primary.ID).Extract()
   118  		if err != nil {
   119  			return err
   120  		}
   121  
   122  		if found.ID != rs.Primary.ID {
   123  			return fmt.Errorf("Security group not found")
   124  		}
   125  
   126  		*security_group = *found
   127  
   128  		return nil
   129  	}
   130  }
   131  
   132  func testAccCheckNetworkingV2SecGroupRuleCount(
   133  	sg *groups.SecGroup, count int) resource.TestCheckFunc {
   134  	return func(s *terraform.State) error {
   135  		if len(sg.Rules) == count {
   136  			return nil
   137  		}
   138  
   139  		return fmt.Errorf("Unexpected number of rules in group %s. Expected %d, got %d",
   140  			sg.ID, count, len(sg.Rules))
   141  	}
   142  }
   143  
   144  const testAccNetworkingV2SecGroup_basic = `
   145  resource "openstack_networking_secgroup_v2" "secgroup_1" {
   146    name = "security_group"
   147    description = "terraform security group acceptance test"
   148  }
   149  `
   150  
   151  const testAccNetworkingV2SecGroup_update = `
   152  resource "openstack_networking_secgroup_v2" "secgroup_1" {
   153    name = "security_group_2"
   154    description = "terraform security group acceptance test"
   155  }
   156  `
   157  
   158  const testAccNetworkingV2SecGroup_noDefaultRules = `
   159  resource "openstack_networking_secgroup_v2" "secgroup_1" {
   160  	name = "security_group_1"
   161  	description = "terraform security group acceptance test"
   162  	delete_default_rules = true
   163  }
   164  `
   165  
   166  const testAccNetworkingV2SecGroup_timeout = `
   167  resource "openstack_networking_secgroup_v2" "secgroup_1" {
   168    name = "security_group"
   169    description = "terraform security group acceptance test"
   170  
   171    timeouts {
   172      delete = "5m"
   173    }
   174  }
   175  `