Skip to content

Commit

Permalink
provider/openstack: Remove Default Security Group Rules (hashicorp#11466
Browse files Browse the repository at this point in the history
)

This commit removes the default security group rules that are automatically
created when a security group is created. These rules are usually
permissive egress rules which makes it difficult to add more strict egress
security group rules.
  • Loading branch information
jtopjian authored and arcadiatea committed Feb 7, 2017
1 parent 482a38a commit b6cc9c6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/security/groups"
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/security/rules"
)

func resourceNetworkingSecGroupV2() *schema.Resource {
Expand Down Expand Up @@ -70,6 +71,14 @@ func resourceNetworkingSecGroupV2Create(d *schema.ResourceData, meta interface{}
return err
}

// Remove the default rules
for _, rule := range security_group.Rules {
if err := rules.Delete(networkingClient, rule.ID).ExtractErr(); err != nil {
return fmt.Errorf(
"There was a problem deleting a default security group rule: %s", err)
}
}

log.Printf("[DEBUG] OpenStack Neutron Security Group created: %#v", security_group)

d.SetId(security_group.ID)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func TestAccNetworkingV2SecGroup_basic(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
testAccCheckNetworkingV2SecGroupExists(
"openstack_networking_secgroup_v2.secgroup_1", &security_group),
testAccCheckNetworkingV2SecGroupRuleCount(&security_group, 0),
),
},
resource.TestStep{
Expand Down Expand Up @@ -89,6 +90,18 @@ func testAccCheckNetworkingV2SecGroupExists(n string, security_group *groups.Sec
}
}

func testAccCheckNetworkingV2SecGroupRuleCount(
sg *groups.SecGroup, count int) resource.TestCheckFunc {
return func(s *terraform.State) error {
if len(sg.Rules) == count {
return nil
}

return fmt.Errorf("Unexpected number of rules in group %s. Expected %d, got %d",
sg.ID, count, len(sg.Rules))
}
}

const testAccNetworkingV2SecGroup_basic = `
resource "openstack_networking_secgroup_v2" "secgroup_1" {
name = "security_group"
Expand Down

0 comments on commit b6cc9c6

Please sign in to comment.