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