Skip to content

Commit

Permalink
Allow to configure timeout for ip allocation
Browse files Browse the repository at this point in the history
In IP address allocation resource, allow to configure timeout for
IP realization.

Signed-off-by: Anna Khmelnitsky <[email protected]>
  • Loading branch information
annakhm committed Jan 4, 2024
1 parent a52afcd commit f30c283
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
4 changes: 2 additions & 2 deletions nsxt/policy_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ func commaSeparatedStringToStringList(commaString string) []string {
return strList
}

func nsxtPolicyWaitForRealizationStateConf(connector client.Connector, d *schema.ResourceData, realizedEntityPath string) *resource.StateChangeConf {
func nsxtPolicyWaitForRealizationStateConf(connector client.Connector, d *schema.ResourceData, realizedEntityPath string, timeout int) *resource.StateChangeConf {
client := realized_state.NewRealizedEntitiesClient(connector)
pendingStates := []string{"UNKNOWN", "UNREALIZED"}
targetStates := []string{"REALIZED", "ERROR"}
Expand All @@ -327,7 +327,7 @@ func nsxtPolicyWaitForRealizationStateConf(connector client.Connector, d *schema
}
return nil, "", realizationError
},
Timeout: d.Timeout(schema.TimeoutCreate),
Timeout: time.Duration(timeout) * time.Second,
MinTimeout: 1 * time.Second,
Delay: 1 * time.Second,
}
Expand Down
17 changes: 15 additions & 2 deletions nsxt/resource_nsxt_policy_ip_address_allocation.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/google/uuid"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/vmware/vsphere-automation-sdk-go/runtime/protocol/client"
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/model"

Expand All @@ -19,6 +20,8 @@ import (
utl "github.com/vmware/terraform-provider-nsxt/api/utl"
)

var addressRealizationTimeoutDefaut = int(1200)

func resourceNsxtPolicyIPAddressAllocation() *schema.Resource {
return &schema.Resource{
Create: resourceNsxtPolicyIPAddressAllocationCreate,
Expand Down Expand Up @@ -46,6 +49,13 @@ func resourceNsxtPolicyIPAddressAllocation() *schema.Resource {
Computed: true,
ForceNew: true,
},
"timeout": {
Type: schema.TypeInt,
Description: "Realization timeout in seconds",
Optional: true,
Default: addressRealizationTimeoutDefaut,
ValidateFunc: validation.IntAtLeast(1),
},
},
}
}
Expand Down Expand Up @@ -149,9 +159,10 @@ func resourceNsxtPolicyIPAddressAllocationRead(d *schema.ResourceData, m interfa
d.Set("allocation_ip", obj.AllocationIp)

if d.Get("allocation_ip").(string) == "" {
timeout := d.Get("timeout").(int)
log.Printf("[DEBUG] Waiting for realization of IP Address for IP Allocation with ID %s", id)

stateConf := nsxtPolicyWaitForRealizationStateConf(connector, d, d.Get("path").(string))
stateConf := nsxtPolicyWaitForRealizationStateConf(connector, d, d.Get("path").(string), timeout)
entity, err := stateConf.WaitForState()
if err != nil {
return err
Expand Down Expand Up @@ -228,6 +239,9 @@ func resourceNsxtPolicyIPAddressAllocationDelete(d *schema.ResourceData, m inter
func resourceNsxtPolicyIPAddressAllocationImport(d *schema.ResourceData, m interface{}) ([]*schema.ResourceData, error) {
importID := d.Id()
s := strings.Split(importID, "/")

d.Set("timeout", addressRealizationTimeoutDefaut)

rd, err := nsxtPolicyPathResourceImporterHelper(d, m)
if err == nil {
poolPath, err := getParameterFromPolicyPath("", "/ip-allocations/", importID)
Expand All @@ -253,7 +267,6 @@ func resourceNsxtPolicyIPAddressAllocationImport(d *schema.ResourceData, m inter
return nil, err
}
d.Set("pool_path", pool.Path)

d.SetId(s[1])

return []*schema.ResourceData{d}, nil
Expand Down

0 comments on commit f30c283

Please sign in to comment.