diff --git a/azurerm/data_source_platform_image.go b/azurerm/data_source_platform_image.go new file mode 100644 index 000000000000..73d601953a6b --- /dev/null +++ b/azurerm/data_source_platform_image.go @@ -0,0 +1,64 @@ +package azurerm + +import ( + "fmt" + + "github.com/hashicorp/terraform/helper/schema" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" +) + +func dataSourceArmPlatformImage() *schema.Resource { + return &schema.Resource{ + Read: dataSourceArmPlatformImageRead, + Schema: map[string]*schema.Schema{ + "location": locationSchema(), + + "publisher": { + Type: schema.TypeString, + Required: true, + }, + + "offer": { + Type: schema.TypeString, + Required: true, + }, + + "sku": { + Type: schema.TypeString, + Required: true, + }, + + "version": { + Type: schema.TypeString, + Computed: true, + }, + }, + } +} + +func dataSourceArmPlatformImageRead(d *schema.ResourceData, meta interface{}) error { + client := meta.(*ArmClient).vmImageClient + + location := azureRMNormalizeLocation(d.Get("location").(string)) + publisher := d.Get("publisher").(string) + offer := d.Get("offer").(string) + sku := d.Get("sku").(string) + + result, err := client.List(location, publisher, offer, sku, "", utils.Int32(int32(1000)), "name") + if err != nil { + return fmt.Errorf("Error reading Platform Images: %+v", err) + } + + // the last value is the latest, apparently. + latestVersion := (*result.Value)[len(*result.Value)-1] + + d.SetId(*latestVersion.ID) + + d.Set("location", azureRMNormalizeLocation(*latestVersion.Location)) + d.Set("publisher", publisher) + d.Set("offer", offer) + d.Set("sku", sku) + d.Set("version", latestVersion.Name) + + return nil +} diff --git a/azurerm/data_source_platform_image_test.go b/azurerm/data_source_platform_image_test.go new file mode 100644 index 000000000000..361a609e5b94 --- /dev/null +++ b/azurerm/data_source_platform_image_test.go @@ -0,0 +1,41 @@ +package azurerm + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform/helper/resource" +) + +func TestAccDataSourceAzureRMPlatformImage_basic(t *testing.T) { + dataSourceName := "data.azurerm_platform_image.test" + config := testAccDataSourceAzureRMPlatformImageBasic(testLocation()) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testCheckAzureRMPublicIpDestroy, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrSet(dataSourceName, "version"), + resource.TestCheckResourceAttr(dataSourceName, "publisher", "Canonical"), + resource.TestCheckResourceAttr(dataSourceName, "offer", "UbuntuServer"), + resource.TestCheckResourceAttr(dataSourceName, "sku", "16.04-LTS"), + ), + }, + }, + }) +} + +func testAccDataSourceAzureRMPlatformImageBasic(location string) string { + return fmt.Sprintf(` +data "azurerm_platform_image" "test" { + location = "%s" + publisher = "Canonical" + offer = "UbuntuServer" + sku = "16.04-LTS" +} +`, location) +} diff --git a/azurerm/provider.go b/azurerm/provider.go index ea7b5a255f2a..0d1d7ff48308 100644 --- a/azurerm/provider.go +++ b/azurerm/provider.go @@ -69,9 +69,10 @@ func Provider() terraform.ResourceProvider { DataSourcesMap: map[string]*schema.Resource{ "azurerm_client_config": dataSourceArmClientConfig(), - "azurerm_resource_group": dataSourceArmResourceGroup(), - "azurerm_public_ip": dataSourceArmPublicIP(), "azurerm_managed_disk": dataSourceArmManagedDisk(), + "azurerm_platform_image": dataSourceArmPlatformImage(), + "azurerm_public_ip": dataSourceArmPublicIP(), + "azurerm_resource_group": dataSourceArmResourceGroup(), "azurerm_subscription": dataSourceArmSubscription(), }, diff --git a/website/azurerm.erb b/website/azurerm.erb index bbdb325507b3..c4b0db1c733d 100644 --- a/website/azurerm.erb +++ b/website/azurerm.erb @@ -31,6 +31,10 @@ azurerm_managed_disk + > + azurerm_platform_image + + > azurerm_public_ip diff --git a/website/docs/d/platform_image.html.markdown b/website/docs/d/platform_image.html.markdown new file mode 100644 index 000000000000..bc8c71bee6e4 --- /dev/null +++ b/website/docs/d/platform_image.html.markdown @@ -0,0 +1,39 @@ +--- +layout: "azurerm" +page_title: "Azure Resource Manager: azurerm_platform_image" +sidebar_current: "docs-azurerm-datasource-platform-image" +description: |- + Get information about the specified Platform Image. +--- + +# azurerm_platform_image + +Use this data source to access the properties of an Azure Platform Image. + +## Example Usage + +```hcl +data "azurerm_platform_image" "test" { + location = "West Europe" + publisher = "Canonical" + offer = "UbuntuServer" + sku = "16.04-LTS" +} + +output "version" { + value = "${data.azurerm_platform_image.test.version}" +} +``` + +## Argument Reference + +* `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. + + +## Attributes Reference + +* `id` - The ID of the Platform Image. +* `version` - The latest version of the Platform Image. \ No newline at end of file