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

azurerm_platform_image - support for version filter #6948

Merged
merged 2 commits into from May 20, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 22 additions & 5 deletions azurerm/internal/services/compute/platform_image_data_source.go
Expand Up @@ -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"
Expand Down Expand Up @@ -39,6 +40,7 @@ func dataSourceArmPlatformImage() *schema.Resource {

"version": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
},
Expand All @@ -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
}
Expand Up @@ -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" {
Expand All @@ -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)
}
17 changes: 10 additions & 7 deletions website/docs/d/platform_image.html.markdown
Expand Up @@ -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

Expand Down