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

data.azurerm_shared_image_version - name supports latest and recent #6707

Merged
merged 4 commits into from Apr 30, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions azurerm/helpers/validate/compute.go
Expand Up @@ -43,8 +43,8 @@ func SharedImageName(v interface{}, k string) (warnings []string, errors []error
func SharedImageVersionName(v interface{}, k string) (warnings []string, errors []error) {
value := v.(string)

if !regexp.MustCompile(`^([0-9]{1,10}\.[0-9]{1,10}\.[0-9]{1,10})$`).MatchString(value) {
errors = append(errors, fmt.Errorf("Expected %s to be in the format `1.2.3` but got %q.", k, value))
if !regexp.MustCompile(`^([0-9]{1,10}\.[0-9]{1,10}\.[0-9]{1,10})$`).MatchString(value) && value != "latest" && value != "recent" {
errors = append(errors, fmt.Errorf("Expected %s to be in the format `1.2.3`, `latest`, or `recent` but got %q.", k, value))
}

return warnings, errors
Expand Down
@@ -1,6 +1,7 @@
package compute

import (
"context"
"fmt"
"log"
"time"
Expand Down Expand Up @@ -94,28 +95,28 @@ func dataSourceArmSharedImageVersionRead(d *schema.ResourceData, meta interface{
galleryName := d.Get("gallery_name").(string)
resourceGroup := d.Get("resource_group_name").(string)

resp, err := client.Get(ctx, resourceGroup, galleryName, imageName, imageVersion, compute.ReplicationStatusTypesReplicationStatus)
image, found, err := obtainImage(client, ctx, resourceGroup, galleryName, imageName, imageVersion)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
log.Printf("[DEBUG] Shared Image Version %q (Image %q / Gallery %q / Resource Group %q) was not found - removing from state", imageVersion, imageName, galleryName, resourceGroup)
d.SetId("")
return nil
}
return fmt.Errorf("Error retrieving Shared Image Version %q (Image %q / Gallery %q / Resource Group %q): %+v", imageVersion, imageName, galleryName, resourceGroup, err)
d.SetId("")
return err
}

d.SetId(*resp.ID)
if !found {
d.SetId("")
return nil
}

d.Set("name", resp.Name)
d.SetId(*image.ID)
d.Set("name", image.Name)
d.Set("image_name", imageName)
d.Set("gallery_name", galleryName)
d.Set("resource_group_name", resourceGroup)

if location := resp.Location; location != nil {
if location := image.Location; location != nil {
d.Set("location", azure.NormalizeLocation(*location))
}

if props := resp.GalleryImageVersionProperties; props != nil {
if props := image.GalleryImageVersionProperties; props != nil {
if profile := props.PublishingProfile; profile != nil {
d.Set("exclude_from_latest", profile.ExcludeFromLatest)

Expand All @@ -132,7 +133,63 @@ func dataSourceArmSharedImageVersionRead(d *schema.ResourceData, meta interface{
}
}

return tags.FlattenAndSet(d, resp.Tags)
return tags.FlattenAndSet(d, image.Tags)
}

func obtainImage(client *compute.GalleryImageVersionsClient, ctx context.Context, resourceGroup string, galleryName string, galleryImageName string, galleryImageVersionName string) (compute.GalleryImageVersion, bool, error) {
var (
image compute.GalleryImageVersion
err error
found bool
)
switch galleryImageVersionName {
case "latest":
images, err := client.ListByGalleryImage(ctx, resourceGroup, galleryName, galleryImageName)
if err != nil {
if utils.ResponseWasNotFound(images.Response().Response) {
log.Printf("[DEBUG] Shared Image Versions (Image %q / Gallery %q / Resource Group %q) was not found - removing from state", galleryImageName, galleryName, resourceGroup)
return image, false, nil
}
return image, false, fmt.Errorf("retrieving Shared Image Versions (Image %q / Gallery %q / Resource Group %q): %+v", galleryImageName, galleryName, resourceGroup, err)
}
// the last image in the list is the latest version
if len(images.Values()) > 0 {
image = images.Values()[len(images.Values())-1]
found = true
}
case "recent":
images, err := client.ListByGalleryImage(ctx, resourceGroup, galleryName, galleryImageName)
if err != nil {
if utils.ResponseWasNotFound(images.Response().Response) {
log.Printf("[DEBUG] Shared Image Versions (Image %q / Gallery %q / Resource Group %q) was not found - removing from state", galleryImageName, galleryName, resourceGroup)
return image, false, nil
}
return image, false, fmt.Errorf("retrieving Shared Image Versions (Image %q / Gallery %q / Resource Group %q): %+v", galleryImageName, galleryName, resourceGroup, err)
}
var recentDate time.Time
// compare dates until we find the image that was updated most recently
for _, currImage := range images.Values() {
if profile := currImage.PublishingProfile; profile != nil {
if profile.PublishedDate != nil && profile.PublishedDate.Time.After(recentDate) {
recentDate = profile.PublishedDate.Time
image = currImage
found = true
}
}
}
default:
image, err = client.Get(ctx, resourceGroup, galleryName, galleryImageName, galleryImageVersionName, compute.ReplicationStatusTypesReplicationStatus)
if err != nil {
if utils.ResponseWasNotFound(image.Response) {
log.Printf("[DEBUG] Shared Image Version %q (Image %q / Gallery %q / Resource Group %q) was not found - removing from state", galleryImageVersionName, galleryImageName, galleryName, resourceGroup)
return image, false, nil
}
return image, false, fmt.Errorf("Error retrieving Shared Image Version %q (Image %q / Gallery %q / Resource Group %q): %+v", galleryImageVersionName, galleryImageName, galleryName, resourceGroup, err)
}
found = true
}

return image, found, nil
}

func flattenSharedImageVersionDataSourceTargetRegions(input *[]compute.TargetRegion) []interface{} {
Expand Down
Expand Up @@ -42,6 +42,74 @@ func TestAccDataSourceAzureRMSharedImageVersion_basic(t *testing.T) {
})
}

func TestAccDataSourceAzureRMSharedImageVersion_latest(t *testing.T) {
data := acceptance.BuildTestData(t, "data.azurerm_shared_image_version", "test")
username := "testadmin"
password := "Password1234!"
hostname := fmt.Sprintf("tftestcustomimagesrc%d", data.RandomInteger)
resourceGroup := fmt.Sprintf("acctestRG-%d", data.RandomInteger)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acceptance.PreCheck(t) },
Providers: acceptance.SupportedProviders,
CheckDestroy: testCheckAzureRMSharedImageVersionDestroy,
Steps: []resource.TestStep{
{
// need to create a vm and then reference it in the image creation
Config: testAccAzureRMSharedImageVersion_setup(data, username, password, hostname),
Destroy: false,
Check: resource.ComposeTestCheckFunc(
testCheckAzureVMExists("azurerm_virtual_machine.testsource", true),
testGeneralizeVMImage(resourceGroup, "testsource", username, password, hostname, "22", data.Locations.Primary),
),
},
{
Config: testAccDataSourceSharedImageVersion_customName(data, username, password, hostname, "latest"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(data.ResourceName, "tags.%", "0"),
resource.TestCheckResourceAttrSet(data.ResourceName, "managed_image_id"),
resource.TestCheckResourceAttr(data.ResourceName, "target_region.#", "1"),
resource.TestCheckResourceAttr(data.ResourceName, "target_region.0.storage_account_type", "Standard_LRS"),
),
},
},
})
}

func TestAccDataSourceAzureRMSharedImageVersion_recent(t *testing.T) {
data := acceptance.BuildTestData(t, "data.azurerm_shared_image_version", "test")
username := "testadmin"
password := "Password1234!"
hostname := fmt.Sprintf("tftestcustomimagesrc%d", data.RandomInteger)
resourceGroup := fmt.Sprintf("acctestRG-%d", data.RandomInteger)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acceptance.PreCheck(t) },
Providers: acceptance.SupportedProviders,
CheckDestroy: testCheckAzureRMSharedImageVersionDestroy,
Steps: []resource.TestStep{
{
// need to create a vm and then reference it in the image creation
Config: testAccAzureRMSharedImageVersion_setup(data, username, password, hostname),
Destroy: false,
Check: resource.ComposeTestCheckFunc(
testCheckAzureVMExists("azurerm_virtual_machine.testsource", true),
testGeneralizeVMImage(resourceGroup, "testsource", username, password, hostname, "22", data.Locations.Primary),
),
},
{
Config: testAccDataSourceSharedImageVersion_customName(data, username, password, hostname, "recent"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(data.ResourceName, "tags.%", "0"),
resource.TestCheckResourceAttrSet(data.ResourceName, "managed_image_id"),
resource.TestCheckResourceAttr(data.ResourceName, "target_region.#", "1"),
resource.TestCheckResourceAttr(data.ResourceName, "target_region.0.storage_account_type", "Standard_LRS"),
),
},
},
})
}

func testAccDataSourceSharedImageVersion_basic(data acceptance.TestData, username, password, hostname string) string {
template := testAccAzureRMSharedImageVersion_imageVersion(data, username, password, hostname)
return fmt.Sprintf(`
Expand All @@ -55,3 +123,31 @@ data "azurerm_shared_image_version" "test" {
}
`, template)
}

func testAccDataSourceSharedImageVersion_customName(data acceptance.TestData, username, password, hostname, name string) string {
template := testAccAzureRMSharedImageVersion_imageVersion(data, username, password, hostname)
return fmt.Sprintf(`
%s

resource "azurerm_shared_image_version" "tes2" {
name = "0.0.2"
gallery_name = azurerm_shared_image_gallery.test.name
image_name = azurerm_shared_image.test.name
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
managed_image_id = azurerm_image.test.id

target_region {
name = azurerm_resource_group.test.location
regional_replica_count = 1
}
}

data "azurerm_shared_image_version" "test" {
name = %s
gallery_name = azurerm_shared_image_version.test.gallery_name
image_name = azurerm_shared_image_version.test.image_name
resource_group_name = azurerm_shared_image_version.test.resource_group_name
}
`, template, name)
}
2 changes: 2 additions & 0 deletions website/docs/d/shared_image_version.html.markdown
Expand Up @@ -28,6 +28,8 @@ The following arguments are supported:

* `name` - The name of the Image Version.

~> **Note:** You may specify `latest` to obtain the latest version or `recent` to obtain the most recently updated version.

* `image_name` - The name of the Shared Image in which this Version exists.

* `gallery_name` - The name of the Shared Image in which the Shared Image exists.
Expand Down