Skip to content

Commit

Permalink
Merge pull request #1184 from vmware/flood-protection-fix
Browse files Browse the repository at this point in the history
Support revision in flood protection binding
  • Loading branch information
annakhm authored Apr 16, 2024
2 parents 332e0ac + 176ed70 commit d56a7f2
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ package nsxt
import (
"fmt"
"strings"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/vmware/terraform-provider-nsxt/api/infra/domains/groups"
utl "github.com/vmware/terraform-provider-nsxt/api/utl"
Expand Down Expand Up @@ -77,31 +75,8 @@ func resourceNsxtPolicyDistributedFloodProtectionProfileBindingPatch(d *schema.R
domain := getDomainFromResourcePath(groupPath)

if !isCreate {
// Regular API doesn't support UPDATE operation, response example below:
// Cannot create an object with path=[/infra/domains/default/groups/testgroup/firewall-flood-protection-profile-binding-maps/994019f3-aba0-4592-96ff-f00326e13976] as it already exists. (code 500127)
// Instead of using H-API to increase complexity, we choose to delete and then create the resource for UPDATE.
err := bindingClient.Delete(domain, groupID, id)
if err != nil {
return err
}
stateConf := &resource.StateChangeConf{
Pending: []string{"exist"},
Target: []string{"deleted"},
Refresh: func() (interface{}, string, error) {
state, err := bindingClient.Get(domain, groupID, id)
if isNotFoundError(err) {
return state, "deleted", nil
}
return state, "exist", nil
},
Timeout: 30 * time.Second,
PollInterval: 200 * time.Millisecond,
Delay: 200 * time.Millisecond,
}
_, err = stateConf.WaitForState()
if err != nil {
return fmt.Errorf("failed to update GatewayFloodProtectionProfileBinding %s: %v", id, err)
}
revision := int64(d.Get("revision").(int))
obj.Revision = &revision
}
return bindingClient.Patch(domain, groupID, id, obj)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func testAccResourceNsxtPolicyDistributedFloodProtectionProfileBindingBasic(t *t
name := getAccTestResourceName()
updatedName := fmt.Sprintf("%s-updated", name)

resource.ParallelTest(t, resource.TestCase{
resource.Test(t, resource.TestCase{
PreCheck: preCheck,
Providers: testAccProviders,
CheckDestroy: func(state *terraform.State) error {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func extractGatewayIDLocaleServiceID(parentPath string) (string, string, string,
return tier0ID, tier1ID, localeServiceID, nil
}

func resourceNsxtPolicyGatewayFloodProtectionProfileBindingPatch(d *schema.ResourceData, m interface{}, parentPath string, id string) error {
func resourceNsxtPolicyGatewayFloodProtectionProfileBindingPatch(d *schema.ResourceData, m interface{}, parentPath string, id string, isCreate bool) error {
connector := getPolicyConnector(m)

displayName := d.Get("display_name").(string)
Expand All @@ -86,6 +86,11 @@ func resourceNsxtPolicyGatewayFloodProtectionProfileBindingPatch(d *schema.Resou
ProfilePath: &profilePath,
}

if !isCreate {
revision := int64(d.Get("revision").(int))
obj.Revision = &revision
}

tier0ID, tier1ID, localeServiceID, err := extractGatewayIDLocaleServiceID(parentPath)
if err != nil {
return err
Expand Down Expand Up @@ -169,7 +174,7 @@ func resourceNsxtPolicyGatewayFloodProtectionProfileBindingCreate(d *schema.Reso
return fmt.Errorf("Resource with id %s already exists", id)
}

err = resourceNsxtPolicyGatewayFloodProtectionProfileBindingPatch(d, m, parentPath, id)
err = resourceNsxtPolicyGatewayFloodProtectionProfileBindingPatch(d, m, parentPath, id, true)
if err != nil {
return handleCreateError("GatewayFloodProtectionProfile", id, err)
}
Expand Down Expand Up @@ -220,7 +225,7 @@ func resourceNsxtPolicyGatewayFloodProtectionProfileBindingUpdate(d *schema.Reso

parentPath := d.Get("parent_path").(string)

err := resourceNsxtPolicyGatewayFloodProtectionProfileBindingPatch(d, m, parentPath, id)
err := resourceNsxtPolicyGatewayFloodProtectionProfileBindingPatch(d, m, parentPath, id, false)
if err != nil {
return handleUpdateError("GatewayFloodProtectionProfileBinding", id, err)
}
Expand Down

0 comments on commit d56a7f2

Please sign in to comment.