Skip to content

Commit

Permalink
used Timeouts instead of a new wait_timeout field
Browse files Browse the repository at this point in the history
  • Loading branch information
venkatavivek-ntnx committed Feb 10, 2022
1 parent 59c168f commit 7a74b6a
Show file tree
Hide file tree
Showing 23 changed files with 140 additions and 463 deletions.
52 changes: 8 additions & 44 deletions nutanix/resource_nutanix_access_control_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,12 @@ func resourceNutanixAccessControlPolicy() *schema.Resource {
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(DEFAULTWAITTIMEOUT * time.Minute),
Update: schema.DefaultTimeout(DEFAULTWAITTIMEOUT * time.Minute),
Delete: schema.DefaultTimeout(DEFAULTWAITTIMEOUT * time.Minute),
},
Schema: map[string]*schema.Schema{
"wait_timeout": {
Type: schema.TypeInt,
Optional: true,
Default: DEFAULTWAITTIMEOUT,
ValidateFunc: validation.IntAtLeast(MINIMUMWAITTIMEOUT),
},
"api_version": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -315,18 +314,8 @@ func resourceNutanixAccessControlPolicy() *schema.Resource {
}
}

func getWaitTimeout(d *schema.ResourceData) (int64, error) {
timeoutInput, okTimeout := d.GetOk("wait_timeout")
if !okTimeout {
return 0, fmt.Errorf("unable to retrieve mandatory parameter wait_timeout")
}
return int64(timeoutInput.(int)), nil
}

func resourceNutanixAccessControlPolicyCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*Client).API
setTimeout(meta)

request := &v3.AccessControlPolicy{}
spec := &v3.AccessControlPolicySpec{}
metadata := &v3.Metadata{}
Expand Down Expand Up @@ -357,16 +346,6 @@ func resourceNutanixAccessControlPolicyCreate(d *schema.ResourceData, meta inter
request.Metadata = metadata
request.Spec = spec

waitTimeout, waitTimeoutErr := getWaitTimeout(d)

if waitTimeoutErr != nil {
return waitTimeoutErr
}

if waitTimeout == 0 {
waitTimeout = 10
}

resp, err := conn.V3.CreateAccessControlPolicy(request)
if err != nil {
return fmt.Errorf("error creating Nutanix AccessControlPolicy %s: %+v", utils.StringValue(spec.Name), err)
Expand All @@ -379,7 +358,7 @@ func resourceNutanixAccessControlPolicyCreate(d *schema.ResourceData, meta inter
Pending: []string{"QUEUED", "RUNNING", "PENDING"},
Target: []string{"SUCCEEDED"},
Refresh: taskStateRefreshFunc(conn, taskUUID),
Timeout: time.Duration(waitTimeout) * time.Minute,
Timeout: d.Timeout(schema.TimeoutCreate),
Delay: subnetDelay,
MinTimeout: subnetMinTimeout,
}
Expand All @@ -400,7 +379,6 @@ func resourceNutanixAccessControlPolicyCreate(d *schema.ResourceData, meta inter

func resourceNutanixAccessControlPolicyRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*Client).API
setTimeout(meta)
id := d.Id()
resp, err := conn.V3.GetAccessControlPolicy(id)
if err != nil {
Expand Down Expand Up @@ -477,8 +455,6 @@ func resourceNutanixAccessControlPolicyUpdate(d *schema.ResourceData, meta inter
metadata := &v3.Metadata{}
res := &v3.AccessControlPolicyResources{}
spec := &v3.AccessControlPolicySpec{}
setTimeout(meta)

id := d.Id()
response, err := conn.V3.GetAccessControlPolicy(id)

Expand All @@ -501,12 +477,6 @@ func resourceNutanixAccessControlPolicyUpdate(d *schema.ResourceData, meta inter
}
}

waitTimeout, waitTimeoutErr := getWaitTimeout(d)

if waitTimeoutErr != nil {
return waitTimeoutErr
}

if d.HasChange("categories") {
metadata.Categories = expandCategories(d.Get("categories"))
}
Expand Down Expand Up @@ -552,7 +522,7 @@ func resourceNutanixAccessControlPolicyUpdate(d *schema.ResourceData, meta inter
Pending: []string{"QUEUED", "RUNNING"},
Target: []string{"SUCCEEDED"},
Refresh: taskStateRefreshFunc(conn, taskUUID),
Timeout: time.Duration(waitTimeout) * time.Second,
Timeout: d.Timeout(schema.TimeoutUpdate),
Delay: subnetDelay,
MinTimeout: subnetMinTimeout,
}
Expand All @@ -570,12 +540,6 @@ func resourceNutanixAccessControlPolicyUpdate(d *schema.ResourceData, meta inter
func resourceNutanixAccessControlPolicyDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*Client).API

waitTimeout, waitTimeoutErr := getWaitTimeout(d)

if waitTimeoutErr != nil {
return waitTimeoutErr
}

resp, err := conn.V3.DeleteAccessControlPolicy(d.Id())
if err != nil {
return fmt.Errorf("error deleting access control policy id %s): %s", d.Id(), err)
Expand All @@ -586,7 +550,7 @@ func resourceNutanixAccessControlPolicyDelete(d *schema.ResourceData, meta inter
Pending: []string{"QUEUED", "RUNNING", "DELETED_PENDING"},
Target: []string{"SUCCEEDED"},
Refresh: taskStateRefreshFunc(conn, cast.ToString(resp.Status.ExecutionContext.TaskUUID)),
Timeout: time.Duration(waitTimeout) * time.Second,
Timeout: d.Timeout(schema.TimeoutDelete),
Delay: subnetDelay,
MinTimeout: subnetMinTimeout,
}
Expand Down
21 changes: 9 additions & 12 deletions nutanix/resource_nutanix_access_control_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,9 @@ func TestAccNutanixAccessControlPolicy_basic(t *testing.T) {
),
},
{
ResourceName: resourceAccessPolicy,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"wait_timeout"},
ResourceName: resourceAccessPolicy,
ImportState: true,
ImportStateVerify: true,
},
},
})
Expand Down Expand Up @@ -80,10 +79,9 @@ func TestAccNutanixAccessControlPolicy_WithUser(t *testing.T) {
),
},
{
ResourceName: resourceAccessPolicy,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"wait_timeout"},
ResourceName: resourceAccessPolicy,
ImportState: true,
ImportStateVerify: true,
},
},
})
Expand Down Expand Up @@ -125,10 +123,9 @@ func TestAccNutanixAccessControlPolicy_WithCategory(t *testing.T) {
),
},
{
ResourceName: resourceAccessPolicy,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"wait_timeout"},
ResourceName: resourceAccessPolicy,
ImportState: true,
ImportStateVerify: true,
},
},
})
Expand Down
7 changes: 0 additions & 7 deletions nutanix/resource_nutanix_category_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"strings"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
v3 "github.com/terraform-providers/terraform-provider-nutanix/client/v3"
"github.com/terraform-providers/terraform-provider-nutanix/utils"
)
Expand Down Expand Up @@ -38,12 +37,6 @@ func resourceNutanixCategoryKey() *schema.Resource {
Type: schema.TypeString,
Required: true,
},
"wait_timeout": {
Type: schema.TypeInt,
Optional: true,
Default: DEFAULTWAITTIMEOUT,
ValidateFunc: validation.IntAtLeast(MINIMUMWAITTIMEOUT),
},
},
}
}
Expand Down
14 changes: 6 additions & 8 deletions nutanix/resource_nutanix_category_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,9 @@ func TestAccNutanixCategoryKey_basic(t *testing.T) {
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"wait_timeout"},
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
Expand Down Expand Up @@ -61,10 +60,9 @@ func TestAccNutanixCategoryKey_update(t *testing.T) {
},

{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"wait_timeout"},
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
Expand Down
7 changes: 0 additions & 7 deletions nutanix/resource_nutanix_category_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"strings"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
v3 "github.com/terraform-providers/terraform-provider-nutanix/client/v3"
"github.com/terraform-providers/terraform-provider-nutanix/utils"
)
Expand Down Expand Up @@ -40,12 +39,6 @@ func resourceNutanixCategoryValue() *schema.Resource {
Type: schema.TypeString,
Required: true,
},
"wait_timeout": {
Type: schema.TypeInt,
Optional: true,
Default: DEFAULTWAITTIMEOUT,
ValidateFunc: validation.IntAtLeast(MINIMUMWAITTIMEOUT),
},
},
}
}
Expand Down
62 changes: 8 additions & 54 deletions nutanix/resource_nutanix_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
)

const (
Expand Down Expand Up @@ -49,17 +48,16 @@ func resourceNutanixImage() *schema.Resource {
Version: 0,
},
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(DEFAULTWAITTIMEOUT * time.Minute),
Update: schema.DefaultTimeout(DEFAULTWAITTIMEOUT * time.Minute),
Delete: schema.DefaultTimeout(DEFAULTWAITTIMEOUT * time.Minute),
},
Schema: map[string]*schema.Schema{
"api_version": {
Type: schema.TypeString,
Computed: true,
},
"wait_timeout": {
Type: schema.TypeInt,
Optional: true,
Default: DEFAULTWAITTIMEOUT,
ValidateFunc: validation.IntAtLeast(MINIMUMWAITTIMEOUT),
},
"metadata": {
Type: schema.TypeMap,
Computed: true,
Expand Down Expand Up @@ -249,23 +247,12 @@ func resourceNutanixImageCreate(d *schema.ResourceData, meta interface{}) error
log.Printf("[DEBUG] Creating Image: %s", d.Get("name").(string))
client := meta.(*Client)
conn := client.API
setTimeout(meta)

request := &v3.ImageIntentInput{}
spec := &v3.Image{}
metadata := &v3.Metadata{}
image := &v3.ImageResources{}

waitTimeout, waitTimeoutErr := getWaitTimeout(d)

if waitTimeoutErr != nil {
return waitTimeoutErr
}

if waitTimeout == 0 {
waitTimeout = 10
}

n, nok := d.GetOk("name")
desc, descok := d.GetOk("description")

Expand Down Expand Up @@ -317,7 +304,7 @@ func resourceNutanixImageCreate(d *schema.ResourceData, meta interface{}) error
Pending: []string{"QUEUED", "RUNNING"},
Target: []string{"SUCCEEDED"},
Refresh: taskStateRefreshFunc(conn, taskUUID),
Timeout: time.Duration(waitTimeout) * time.Minute,
Timeout: d.Timeout(schema.TimeoutCreate),
Delay: imageDelay,
MinTimeout: imageMinTimeout,
}
Expand Down Expand Up @@ -356,8 +343,6 @@ func resourceNutanixImageRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*Client).API
uuid := d.Id()

setTimeout(meta)

// Make request to the API
resp, err := conn.V3.GetImage(uuid)
if err != nil {
Expand Down Expand Up @@ -443,8 +428,6 @@ func resourceNutanixImageUpdate(d *schema.ResourceData, meta interface{}) error
client := meta.(*Client)
conn := client.API

setTimeout(meta)

// get state
request := &v3.ImageIntentInput{}
metadata := &v3.Metadata{}
Expand Down Expand Up @@ -472,16 +455,6 @@ func resourceNutanixImageUpdate(d *schema.ResourceData, meta interface{}) error
}
}

waitTimeout, waitTimeoutErr := getWaitTimeout(d)

if waitTimeoutErr != nil {
return waitTimeoutErr
}

if waitTimeout == 0 {
waitTimeout = 10
}

if d.HasChange("categories") {
metadata.Categories = expandCategories(d.Get("categories"))
}
Expand Down Expand Up @@ -526,7 +499,7 @@ func resourceNutanixImageUpdate(d *schema.ResourceData, meta interface{}) error
Pending: []string{"QUEUED", "RUNNING"},
Target: []string{"SUCCEEDED"},
Refresh: taskStateRefreshFunc(conn, taskUUID),
Timeout: time.Duration(waitTimeout) * time.Minute,
Timeout: d.Timeout(schema.TimeoutUpdate),
Delay: imageDelay,
MinTimeout: imageMinTimeout,
}
Expand All @@ -549,9 +522,6 @@ func resourceNutanixImageDelete(d *schema.ResourceData, meta interface{}) error

client := meta.(*Client)
conn := client.API

setTimeout(meta)

UUID := d.Id()

resp, err := conn.V3.DeleteImage(UUID)
Expand All @@ -562,24 +532,14 @@ func resourceNutanixImageDelete(d *schema.ResourceData, meta interface{}) error
return err
}

waitTimeout, waitTimeoutErr := getWaitTimeout(d)

if waitTimeoutErr != nil {
return waitTimeoutErr
}

if waitTimeout == 0 {
waitTimeout = 10
}

taskUUID := resp.Status.ExecutionContext.TaskUUID.(string)

// Wait for the Image to be available
stateConf := &resource.StateChangeConf{
Pending: []string{"QUEUED", "RUNNING"},
Target: []string{"SUCCEEDED"},
Refresh: taskStateRefreshFunc(conn, taskUUID),
Timeout: time.Duration(waitTimeout) * time.Minute,
Timeout: d.Timeout(schema.TimeoutDelete),
Delay: imageDelay,
MinTimeout: imageMinTimeout,
}
Expand Down Expand Up @@ -656,12 +616,6 @@ func resourceNutanixImageInstanceResourceV0() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"wait_timeout": {
Type: schema.TypeInt,
Optional: true,
Default: DEFAULTWAITTIMEOUT,
ValidateFunc: validation.IntAtLeast(MINIMUMWAITTIMEOUT),
},
"metadata": {
Type: schema.TypeMap,
Computed: true,
Expand Down
Loading

0 comments on commit 7a74b6a

Please sign in to comment.