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 +