Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Image wait timeout #20 #42

Merged
merged 2 commits into from
Apr 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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{

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please rename this variable? I think this is confusing as it has more use cases apart from a client.

A better name would be ctx (note: not suggesting context because we don't want to obfuscate the built-in context package)

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)
}
25 changes: 14 additions & 11 deletions website/docs/index.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ Use the navigation to the left to read about the available resources.

```hcl
provider "nutanix" {
username = "xxxx"
password = "xxxx"
endpoint = "xxxx"
insecure = true
port = 9440
username = "xxxx"
password = "xxxx"
endpoint = "xxxx"
insecure = true
port = 9440
wait_timeout = 10
}
```

Expand All @@ -40,11 +41,12 @@ Usage:

```hcl
provider "nutanix" {
username = "xxxx"
password = "xxxx"
endpoint = "xxxx"
insecure = true
port = 9440
username = "xxxx"
password = "xxxx"
endpoint = "xxxx"
insecure = true
port = 9440
wait_timeout = 10
}
```

Expand All @@ -59,12 +61,13 @@ provider "nutanix" {}

Usage:

```hcl
``` bash
$ export NUTANIX_USERNAME="xxxx"
$ export NUTANIX_PASSWORD="xxxx"
$ export NUTANIX_INSECURE="xxxx"
$ export NUTANIX_PORT="xxxx"
$ export NUTANIX_ENDPOINT="xxxx"
$ export NUTANIX_WAIT_TIMEOUT = "xxx"

$ terraform plan
```