diff --git a/azurerm/internal/services/compute/platform_image_data_source.go b/azurerm/internal/services/compute/platform_image_data_source.go index 6b4962d97a6b..51b2685c0077 100644 --- a/azurerm/internal/services/compute/platform_image_data_source.go +++ b/azurerm/internal/services/compute/platform_image_data_source.go @@ -4,6 +4,7 @@ import ( "fmt" "time" + "github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2019-07-01/compute" "github.com/hashicorp/terraform-plugin-sdk/helper/schema" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients" @@ -39,6 +40,7 @@ func dataSourceArmPlatformImage() *schema.Resource { "version": { Type: schema.TypeString, + Optional: true, Computed: true, }, }, @@ -60,18 +62,33 @@ func dataSourceArmPlatformImageRead(d *schema.ResourceData, meta interface{}) er return fmt.Errorf("Error reading Platform Images: %+v", err) } - // the last value is the latest, apparently. - latestVersion := (*result.Value)[len(*result.Value)-1] + var image *compute.VirtualMachineImageResource + if v, ok := d.GetOk("version"); ok { + version := v.(string) + for _, item := range *result.Value { + if item.Name != nil && *item.Name == version { + image = &item + break + } + } + if image == nil { + return fmt.Errorf("could not find image (location %q / publisher %q / offer %q / sku %q / version % q): %+v", location, publisher, offer, sku, version, err) + } + } else { + // get the latest image + // the last value is the latest, apparently. + image = &(*result.Value)[len(*result.Value)-1] + } - d.SetId(*latestVersion.ID) - if location := latestVersion.Location; location != nil { + d.SetId(*image.ID) + if location := image.Location; location != nil { d.Set("location", azure.NormalizeLocation(*location)) } d.Set("publisher", publisher) d.Set("offer", offer) d.Set("sku", sku) - d.Set("version", latestVersion.Name) + d.Set("version", image.Name) return nil } diff --git a/azurerm/internal/services/compute/tests/platform_image_data_source_test.go b/azurerm/internal/services/compute/tests/platform_image_data_source_test.go index e02d81ed8513..1da1e1490c32 100644 --- a/azurerm/internal/services/compute/tests/platform_image_data_source_test.go +++ b/azurerm/internal/services/compute/tests/platform_image_data_source_test.go @@ -28,6 +28,27 @@ func TestAccDataSourceAzureRMPlatformImage_basic(t *testing.T) { }) } +func TestAccDataSourceAzureRMPlatformImage_withVersion(t *testing.T) { + data := acceptance.BuildTestData(t, "data.azurerm_platform_image", "test") + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { acceptance.PreCheck(t) }, + Providers: acceptance.SupportedProviders, + Steps: []resource.TestStep{ + { + Config: testAccDataSourceAzureRMPlatformImageWithVersion(data), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrSet(data.ResourceName, "version"), + resource.TestCheckResourceAttr(data.ResourceName, "publisher", "Canonical"), + resource.TestCheckResourceAttr(data.ResourceName, "offer", "UbuntuServer"), + resource.TestCheckResourceAttr(data.ResourceName, "sku", "16.04-LTS"), + resource.TestCheckResourceAttr(data.ResourceName, "version", "16.04.201811010"), + ), + }, + }, + }) +} + func testAccDataSourceAzureRMPlatformImageBasic(data acceptance.TestData) string { return fmt.Sprintf(` provider "azurerm" { @@ -42,3 +63,19 @@ data "azurerm_platform_image" "test" { } `, data.Locations.Primary) } + +func testAccDataSourceAzureRMPlatformImageWithVersion(data acceptance.TestData) string { + return fmt.Sprintf(` +provider "azurerm" { + features {} +} + +data "azurerm_platform_image" "test" { + location = "%s" + publisher = "Canonical" + offer = "UbuntuServer" + sku = "16.04-LTS" + version = "16.04.201811010" +} +`, data.Locations.Primary) +} diff --git a/website/docs/d/platform_image.html.markdown b/website/docs/d/platform_image.html.markdown index 1e74fdb8734f..d5a8c7e55671 100644 --- a/website/docs/d/platform_image.html.markdown +++ b/website/docs/d/platform_image.html.markdown @@ -20,23 +20,26 @@ data "azurerm_platform_image" "example" { sku = "16.04-LTS" } -output "version" { - value = data.azurerm_platform_image.example.version +output "id" { + value = data.azurerm_platform_image.example.id } ``` ## Argument Reference -* `location` - Specifies the Location to pull information about this Platform Image from. -* `publisher` - Specifies the Publisher associated with the Platform Image. -* `offer` - Specifies the Offer associated with the Platform Image. -* `sku` - Specifies the SKU of the Platform Image. +* `location` - (Required) Specifies the Location to pull information about this Platform Image from. +* `publisher` - (Required) Specifies the Publisher associated with the Platform Image. + +* `offer` - (Required) Specifies the Offer associated with the Platform Image. + +* `sku` - (Required) Specifies the SKU of the Platform Image. + +* `version` - (Optional) The version of the Platform Image. ## Attributes Reference * `id` - The ID of the Platform Image. -* `version` - The latest version of the Platform Image. ## Timeouts