Skip to content

Commit 5f8dc6c

Browse files
jtopjianstack72
authored andcommitted
provider/openstack: Remove Default Security Group Rules (hashicorp#11466)
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.
1 parent 5f94b51 commit 5f8dc6c

2 files changed

Lines changed: 22 additions & 0 deletions

File tree

builtin/providers/openstack/resource_openstack_networking_secgroup_v2.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
"github.com/gophercloud/gophercloud"
1212
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/security/groups"
13+
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/security/rules"
1314
)
1415

1516
func resourceNetworkingSecGroupV2() *schema.Resource {
@@ -70,6 +71,14 @@ func resourceNetworkingSecGroupV2Create(d *schema.ResourceData, meta interface{}
7071
return err
7172
}
7273

74+
// Remove the default rules
75+
for _, rule := range security_group.Rules {
76+
if err := rules.Delete(networkingClient, rule.ID).ExtractErr(); err != nil {
77+
return fmt.Errorf(
78+
"There was a problem deleting a default security group rule: %s", err)
79+
}
80+
}
81+
7382
log.Printf("[DEBUG] OpenStack Neutron Security Group created: %#v", security_group)
7483

7584
d.SetId(security_group.ID)

builtin/providers/openstack/resource_openstack_networking_secgroup_v2_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ func TestAccNetworkingV2SecGroup_basic(t *testing.T) {
2323
Check: resource.ComposeTestCheckFunc(
2424
testAccCheckNetworkingV2SecGroupExists(
2525
"openstack_networking_secgroup_v2.secgroup_1", &security_group),
26+
testAccCheckNetworkingV2SecGroupRuleCount(&security_group, 0),
2627
),
2728
},
2829
resource.TestStep{
@@ -89,6 +90,18 @@ func testAccCheckNetworkingV2SecGroupExists(n string, security_group *groups.Sec
8990
}
9091
}
9192

93+
func testAccCheckNetworkingV2SecGroupRuleCount(
94+
sg *groups.SecGroup, count int) resource.TestCheckFunc {
95+
return func(s *terraform.State) error {
96+
if len(sg.Rules) == count {
97+
return nil
98+
}
99+
100+
return fmt.Errorf("Unexpected number of rules in group %s. Expected %d, got %d",
101+
sg.ID, count, len(sg.Rules))
102+
}
103+
}
104+
92105
const testAccNetworkingV2SecGroup_basic = `
93106
resource "openstack_networking_secgroup_v2" "secgroup_1" {
94107
name = "security_group"

0 commit comments

Comments
 (0)