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

fix nutanix_image version #694

Open
wants to merge 1 commit into
base: feat/1.9.6
Choose a base branch
from
Open
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
10 changes: 10 additions & 0 deletions nutanix/data_source_nutanix_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,16 @@ func dataSourceNutanixImageRead(ctx context.Context, d *schema.ResourceData, met
return diag.FromErr(err)
}

version := make(map[string]string)
if resp.Status.Resources.Version != nil {
version["product_name"] = utils.StringValue(resp.Status.Resources.Version.ProductName)
version["product_version"] = utils.StringValue(resp.Status.Resources.Version.ProductVersion)
}

if err := d.Set("version", version); err != nil {
return diag.FromErr(err)
}

d.SetId(utils.StringValue(resp.Metadata.UUID))

return nil
Expand Down
6 changes: 6 additions & 0 deletions nutanix/data_source_nutanix_image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ func TestAccNutanixImageDataSource_name(t *testing.T) {
"data.nutanix_image.test",
"source_uri",
"http://archive.ubuntu.com/ubuntu/dists/bionic/main/installer-amd64/current/images/netboot/mini.iso"),
resource.TestCheckResourceAttr("data.nutanix_image.test", "version.product_name", fmt.Sprintf("Ubuntu-%d", rInt)),
resource.TestCheckResourceAttr("data.nutanix_image.test", "version.product_version", "mini.iso"),
),
},
},
Expand Down Expand Up @@ -85,6 +87,10 @@ resource "nutanix_image" "test" {
name = "Ubuntu-%d"
description = "Ubuntu mini ISO"
source_uri = "http://archive.ubuntu.com/ubuntu/dists/bionic/main/installer-amd64/current/images/netboot/mini.iso"
version = {
product_name = "Ubuntu-%[1]d"
product_version = "mini.iso"
}
}


Expand Down
21 changes: 21 additions & 0 deletions nutanix/resource_nutanix_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,8 @@ func resourceNutanixImageDelete(ctx context.Context, d *schema.ResourceData, met
func getImageResource(d *schema.ResourceData, image *v3.ImageResources) error {
cs, csok := d.GetOk("checksum")
checks := &v3.Checksum{}
version, versionOk := d.GetOk("version")
versionResource := &v3.ImageVersionResources{}
su, suok := d.GetOk("source_uri")
sp, spok := d.GetOk("source_path")
var furi string
Expand Down Expand Up @@ -590,6 +592,25 @@ func getImageResource(d *schema.ResourceData, image *v3.ImageResources) error {
image.Checksum = checks
}

if versionOk {
versionMap := version.(map[string]interface{})
productName, productNameOk := versionMap["product_name"]
productVersion, productVersionOk := versionMap["product_version"]

if productNameOk {
if productName.(string) == "" {
return fmt.Errorf("'product_name' is not given")
}
versionResource.ProductName = utils.StringPtr(productName.(string))
}
if productVersionOk {
if productVersion.(string) == "" {
return fmt.Errorf("'product_version' is not given")
}
versionResource.ProductVersion = utils.StringPtr(productVersion.(string))
}
image.Version = versionResource
}
// List of clusters where image is requested to be placed at time of creation
if refs, refsok := d.GetOk("cluster_references"); refsok && len(refs.([]interface{})) > 0 {
image.InitialPlacementRefList = validateArrayRefValues(refs, "cluster")
Expand Down
40 changes: 40 additions & 0 deletions nutanix/resource_nutanix_image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,31 @@ func TestAccNutanixImage_uploadLocal(t *testing.T) {
})
}

func TestAccNutanixImage_Version(t *testing.T) {
rInt := acctest.RandInt()

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckNutanixImageDestroy,
Steps: []resource.TestStep{
{
Config: testAccNutanixImageVersionConfig(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckNutanixImageExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "version.product_name", fmt.Sprintf("Ubuntu-%d", rInt)),
resource.TestCheckResourceAttr(resourceName, "version.product_version", "mini.iso"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func downloadFile(filepath string, url string) error {
// Create the file
out, err := os.Create(filepath)
Expand Down Expand Up @@ -406,3 +431,18 @@ func testAccNutanixImageConfigWithLargeImageURL(r int) string {
}
`, r)
}

func testAccNutanixImageVersionConfig(r int) string {
return fmt.Sprintf(`
resource "nutanix_image" "acctest-test" {
name = "Ubuntu-%[1]d"
description = "Ubuntu"
source_uri = "http://archive.ubuntu.com/ubuntu/dists/bionic/main/installer-amd64/current/images/netboot/mini.iso"
image_type = "ISO_IMAGE"
version = {
product_name = "Ubuntu-%[1]d"
product_version = "mini.iso"
}
}
`, r)
}
Loading