github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/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  				),
    27  			},
    28  			resource.TestStep{
    29  				Config: testAccNetworkingV2SecGroup_update,
    30  				Check: resource.ComposeTestCheckFunc(
    31  					resource.TestCheckResourceAttr(
    32  						"openstack_networking_secgroup_v2.secgroup_1", "name", "security_group_2"),
    33  				),
    34  			},
    35  		},
    36  	})
    37  }
    38  
    39  func testAccCheckNetworkingV2SecGroupDestroy(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  
    46  	for _, rs := range s.RootModule().Resources {
    47  		if rs.Type != "openstack_networking_secgroup_v2" {
    48  			continue
    49  		}
    50  
    51  		_, err := groups.Get(networkingClient, rs.Primary.ID).Extract()
    52  		if err == nil {
    53  			return fmt.Errorf("Security group still exists")
    54  		}
    55  	}
    56  
    57  	return nil
    58  }
    59  
    60  func testAccCheckNetworkingV2SecGroupExists(n string, security_group *groups.SecGroup) resource.TestCheckFunc {
    61  	return func(s *terraform.State) error {
    62  		rs, ok := s.RootModule().Resources[n]
    63  		if !ok {
    64  			return fmt.Errorf("Not found: %s", n)
    65  		}
    66  
    67  		if rs.Primary.ID == "" {
    68  			return fmt.Errorf("No ID is set")
    69  		}
    70  
    71  		config := testAccProvider.Meta().(*Config)
    72  		networkingClient, err := config.networkingV2Client(OS_REGION_NAME)
    73  		if err != nil {
    74  			return fmt.Errorf("Error creating OpenStack networking client: %s", err)
    75  		}
    76  
    77  		found, err := groups.Get(networkingClient, rs.Primary.ID).Extract()
    78  		if err != nil {
    79  			return err
    80  		}
    81  
    82  		if found.ID != rs.Primary.ID {
    83  			return fmt.Errorf("Security group not found")
    84  		}
    85  
    86  		*security_group = *found
    87  
    88  		return nil
    89  	}
    90  }
    91  
    92  const testAccNetworkingV2SecGroup_basic = `
    93  resource "openstack_networking_secgroup_v2" "secgroup_1" {
    94    name = "security_group"
    95    description = "terraform security group acceptance test"
    96  }
    97  `
    98  
    99  const testAccNetworkingV2SecGroup_update = `
   100  resource "openstack_networking_secgroup_v2" "secgroup_1" {
   101    name = "security_group_2"
   102    description = "terraform security group acceptance test"
   103  }
   104  `