Skip to content

Commit

Permalink
refactor: add wait_timeout to configure the max wait timeout in image…
Browse files Browse the repository at this point in the history
… creation, deletion, and uptate
  • Loading branch information
marinsalinas committed Apr 2, 2019
1 parent 9c09b5a commit fb812e3
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 20 deletions.
18 changes: 10 additions & 8 deletions nutanix/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 ...
Expand All @@ -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
}
23 changes: 18 additions & 5 deletions nutanix/provider.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package nutanix

import (
"log"

"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform"
)
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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()
Expand Down
30 changes: 24 additions & 6 deletions nutanix/resource_nutanix_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down Expand Up @@ -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,
}
Expand Down Expand Up @@ -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{}
Expand Down Expand Up @@ -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,
}
Expand All @@ -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)
Expand All @@ -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,
}
Expand Down
35 changes: 34 additions & 1 deletion nutanix/resource_nutanix_image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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" {
Expand Down Expand Up @@ -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)
}

0 comments on commit fb812e3

Please sign in to comment.