Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

provider/openstack: Remove Default Security Group Rules #11466

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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