-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #375 from terraform-providers/platform-images-data…
…-source New Data Source: `azurerm_platform_image`
- Loading branch information
Showing
5 changed files
with
151 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |