Skip to content

Commit

Permalink
Fix removing FW rule assigned to FW policy (#275)
Browse files Browse the repository at this point in the history
  • Loading branch information
niuzhenguo authored Mar 17, 2020
1 parent 5499916 commit 4f4da60
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions huaweicloud/resource_huaweicloud_fw_rule_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/huaweicloud/golangsdk"
"github.com/huaweicloud/golangsdk/openstack/networking/v2/extensions/fwaas_v2/policies"
"github.com/huaweicloud/golangsdk/openstack/networking/v2/extensions/fwaas_v2/rules"
"github.com/huaweicloud/golangsdk/pagination"
)

func resourceFWRuleV2() *schema.Resource {
Expand Down Expand Up @@ -236,8 +237,12 @@ func resourceFWRuleV2Delete(d *schema.ResourceData, meta interface{}) error {
return err
}

if rule.PolicyID != "" {
_, err := policies.RemoveRule(fwClient, rule.PolicyID, rule.ID).Extract()
policyID, err := assignedPolicyID(fwClient, rule.ID)
if err != nil {
return err
}
if policyID != "" {
_, err := policies.RemoveRule(fwClient, policyID, rule.ID).Extract()
if err != nil {
return err
}
Expand All @@ -246,6 +251,30 @@ func resourceFWRuleV2Delete(d *schema.ResourceData, meta interface{}) error {
return rules.Delete(fwClient, d.Id()).Err
}

func assignedPolicyID(fwClient *golangsdk.ServiceClient, ruleID string) (string, error) {
pager := policies.List(fwClient, policies.ListOpts{})
policyID := ""
err := pager.EachPage(func(page pagination.Page) (b bool, err error) {
policyList, err := policies.ExtractPolicies(page)
if err != nil {
return false, err
}
for _, policy := range policyList {
for _, rule := range policy.Rules {
if rule == ruleID {
policyID = policy.ID
return false, nil
}
}
}
return true, nil
})
if err != nil {
return "", err
}
return policyID, nil
}

func resourceFWRuleV2DetermineIPVersion(ipv int) golangsdk.IPVersion {
// Determine the IP Version
var ipVersion golangsdk.IPVersion
Expand Down

0 comments on commit 4f4da60

Please sign in to comment.