diff --git a/nutanix/config.go b/nutanix/config.go index c3aa2eb3f..45f6b0482 100644 --- a/nutanix/config.go +++ b/nutanix/config.go @@ -12,11 +12,12 @@ const Version = "3.1" // Config ... type Config struct { - Endpoint string - Username string - Password string - Port string - Insecure bool + Endpoint string + Username string + Password string + Port string + Insecure bool + WaitTimeout int64 } // Client ... @@ -36,13 +37,14 @@ func (c *Config) Client() (*Client, error) { return nil, err } client := &Client{ - API: v3, + API: v3, + WaitTimeout: c.WaitTimeout, } - return client, nil } // Client represents the nutanix API client type Client struct { - API *v3.Client + API *v3.Client + WaitTimeout int64 } diff --git a/nutanix/provider.go b/nutanix/provider.go index 36ae90ed9..dedd281b3 100644 --- a/nutanix/provider.go +++ b/nutanix/provider.go @@ -1,6 +1,8 @@ package nutanix import ( + "log" + "github.com/hashicorp/terraform/helper/schema" "github.com/hashicorp/terraform/terraform" ) @@ -42,6 +44,12 @@ func Provider() terraform.ResourceProvider { DefaultFunc: schema.EnvDefaultFunc("NUTANIX_ENDPOINT", nil), Description: descriptions["endpoint"], }, + "wait_timeout": { + Type: schema.TypeInt, + Optional: true, + DefaultFunc: schema.EnvDefaultFunc("NUTANIX_WAIT_TIMEOUT", nil), + Description: descriptions["wait_timeout"], + }, }, DataSourcesMap: map[string]*schema.Resource{ "nutanix_image": dataSourceNutanixImage(), @@ -85,18 +93,23 @@ func init() { "note, this is never the data services VIP, and should not be an\n" + "individual CVM address, as this would cause calls to fail during\n" + "cluster lifecycle management operations, such as AOS upgrades.", + + "wait_timeout": "Set if you know that the creation o update of a resource may take long time (minutes)", } } // This function used to fetch the configuration params given to our provider which // we will use to initialize a dummy client that interacts with API. func providerConfigure(d *schema.ResourceData) (interface{}, error) { + log.Printf("[DEBUG] config wait_timeout %d", d.Get("wait_timeout").(int)) + config := Config{ - Endpoint: d.Get("endpoint").(string), - Username: d.Get("username").(string), - Password: d.Get("password").(string), - Insecure: d.Get("insecure").(bool), - Port: d.Get("port").(string), + Endpoint: d.Get("endpoint").(string), + Username: d.Get("username").(string), + Password: d.Get("password").(string), + Insecure: d.Get("insecure").(bool), + Port: d.Get("port").(string), + WaitTimeout: int64(d.Get("wait_timeout").(int)), } return config.Client() diff --git a/nutanix/resource_nutanix_image.go b/nutanix/resource_nutanix_image.go index 551aa2aff..2ca3934aa 100644 --- a/nutanix/resource_nutanix_image.go +++ b/nutanix/resource_nutanix_image.go @@ -227,8 +227,13 @@ func resourceNutanixImage() *schema.Resource { func resourceNutanixImageCreate(d *schema.ResourceData, meta interface{}) error { log.Printf("[DEBUG] Creating Image: %s", d.Get("name").(string)) + client := meta.(*Client) + conn := client.API + timeout := client.WaitTimeout - conn := meta.(*Client).API + if client.WaitTimeout == 0 { + timeout = 10 + } request := &v3.ImageIntentInput{} spec := &v3.Image{} @@ -295,7 +300,7 @@ func resourceNutanixImageCreate(d *schema.ResourceData, meta interface{}) error Pending: []string{"QUEUED", "RUNNING"}, Target: []string{"SUCCEEDED"}, Refresh: taskStateRefreshFunc(conn, taskUUID), - Timeout: 10 * time.Minute, + Timeout: time.Duration(timeout) * time.Minute, Delay: 10 * time.Second, MinTimeout: 3 * time.Second, } @@ -411,7 +416,13 @@ func resourceNutanixImageRead(d *schema.ResourceData, meta interface{}) error { } func resourceNutanixImageUpdate(d *schema.ResourceData, meta interface{}) error { - conn := meta.(*Client).API + client := meta.(*Client) + conn := client.API + timeout := client.WaitTimeout + + if client.WaitTimeout == 0 { + timeout = 10 + } // get state request := &v3.ImageIntentInput{} @@ -485,7 +496,7 @@ func resourceNutanixImageUpdate(d *schema.ResourceData, meta interface{}) error Pending: []string{"QUEUED", "RUNNING"}, Target: []string{"SUCCEEDED"}, Refresh: taskStateRefreshFunc(conn, taskUUID), - Timeout: 10 * time.Minute, + Timeout: time.Duration(timeout) * time.Minute, Delay: 10 * time.Second, MinTimeout: 3 * time.Second, } @@ -500,7 +511,14 @@ func resourceNutanixImageUpdate(d *schema.ResourceData, meta interface{}) error func resourceNutanixImageDelete(d *schema.ResourceData, meta interface{}) error { log.Printf("[DEBUG] Deleting Image: %s", d.Get("name").(string)) - conn := meta.(*Client).API + client := meta.(*Client) + conn := client.API + timeout := client.WaitTimeout + + if client.WaitTimeout == 0 { + timeout = 10 + } + UUID := d.Id() resp, err := conn.V3.DeleteImage(UUID) @@ -518,7 +536,7 @@ func resourceNutanixImageDelete(d *schema.ResourceData, meta interface{}) error Pending: []string{"QUEUED", "RUNNING"}, Target: []string{"SUCCEEDED"}, Refresh: taskStateRefreshFunc(conn, taskUUID), - Timeout: 10 * time.Minute, + Timeout: time.Duration(timeout) * time.Minute, Delay: 10 * time.Second, MinTimeout: 3 * time.Second, } diff --git a/nutanix/resource_nutanix_image_test.go b/nutanix/resource_nutanix_image_test.go index 733f9918e..baa341a42 100644 --- a/nutanix/resource_nutanix_image_test.go +++ b/nutanix/resource_nutanix_image_test.go @@ -70,7 +70,7 @@ func TestAccNutanixImage_Update(t *testing.T) { }) } -func TestAccNutanixImageWithCategories(t *testing.T) { +func TestAccNutanixImage_WithCategories(t *testing.T) { rInt := acctest.RandInt() resourceName := "nutanix_image.acctest-test-categories" @@ -106,6 +106,25 @@ func TestAccNutanixImageWithCategories(t *testing.T) { }) } +func TestAccNutanixImage_WithLargeImageURL(t *testing.T) { + rInt := acctest.RandInt() + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckNutanixImageDestroy, + Steps: []resource.TestStep{ + { + Config: testAccNutanixImageConfigWithLargeImageURL(rInt), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr(resourceName, "name", fmt.Sprintf("Ubuntu-%d-server", rInt)), + testAccCheckNutanixImageExists(resourceName), + ), + }, + }, + }) +} + func TestAccNutanixImage_basic_uploadLocal(t *testing.T) { //Skipping Because in GCP still failing if os.Getenv("NUTANIX_GCP") == "true" { @@ -287,3 +306,17 @@ resource "nutanix_image" "acctest-test-categories" { } `, r) } + +func testAccNutanixImageConfigWithLargeImageURL(r int) string { + return fmt.Sprintf(` +provider "nutanix" { + wait_timeout = 50 +} + +resource "nutanix_image" "acctest-test" { + name = "Ubuntu-%d-server" + description = "Ubuntu Server" + source_uri = "http://releases.ubuntu.com/18.04/ubuntu-18.04.2-live-server-amd64.iso" +} +`, r) +}