diff --git a/nutanix/data_source_nutanix_image.go b/nutanix/data_source_nutanix_image.go index bfa6bbec5..3584bb51b 100644 --- a/nutanix/data_source_nutanix_image.go +++ b/nutanix/data_source_nutanix_image.go @@ -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 diff --git a/nutanix/data_source_nutanix_image_test.go b/nutanix/data_source_nutanix_image_test.go index 2f4153acb..0219d9111 100644 --- a/nutanix/data_source_nutanix_image_test.go +++ b/nutanix/data_source_nutanix_image_test.go @@ -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"), ), }, }, @@ -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" + } } diff --git a/nutanix/resource_nutanix_image.go b/nutanix/resource_nutanix_image.go index 4e1e11c5e..a9d31974c 100644 --- a/nutanix/resource_nutanix_image.go +++ b/nutanix/resource_nutanix_image.go @@ -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 @@ -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") diff --git a/nutanix/resource_nutanix_image_test.go b/nutanix/resource_nutanix_image_test.go index 9f2906c93..583fd0939 100644 --- a/nutanix/resource_nutanix_image_test.go +++ b/nutanix/resource_nutanix_image_test.go @@ -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) @@ -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) +}