Skip to content

Commit

Permalink
fix nutanix_image version, set the image version when create the imag…
Browse files Browse the repository at this point in the history
…e resource
  • Loading branch information
Haroon-Dweikat-Ntx committed Oct 1, 2024
1 parent 502c1ea commit 4c34808
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 0 deletions.
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)
}

0 comments on commit 4c34808

Please sign in to comment.