Skip to content

Commit

Permalink
data azurerm_shared_image_version - consider excludeFromLatest
Browse files Browse the repository at this point in the history
…for `latest` version (hashicorp#21822)

Co-authored-by: kt <[email protected]>
  • Loading branch information
myc2h6o and katbyte authored Sep 26, 2023
1 parent 5707f88 commit 90abb8c
Show file tree
Hide file tree
Showing 3 changed files with 189 additions and 13 deletions.
14 changes: 12 additions & 2 deletions internal/services/compute/shared_image_version_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/hashicorp/go-azure-helpers/resourcemanager/location"
"github.com/hashicorp/terraform-provider-azurerm/helpers/azure"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/features"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/compute/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/compute/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/tags"
Expand Down Expand Up @@ -196,8 +197,17 @@ func obtainImage(client *compute.GalleryImageVersionsClient, ctx context.Context
return nil, fmt.Errorf("parsing version(s): %v", errs)
}
}
image := images[len(images)-1]
return &image, nil

if !features.FourPointOhBeta() {
image := images[len(images)-1]
return &image, nil
}

for i := len(images) - 1; i >= 0; i-- {
if prop := images[i].GalleryImageVersionProperties; prop == nil || prop.PublishingProfile == nil || prop.PublishingProfile.ExcludeFromLatest == nil || !*prop.PublishingProfile.ExcludeFromLatest {
return &(images[i]), nil
}
}
}
return nil, notFoundError

Expand Down
186 changes: 175 additions & 11 deletions internal/services/compute/shared_image_version_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check"
"github.com/hashicorp/terraform-provider-azurerm/internal/features"
)

type SharedImageVersionDataSource struct{}
Expand All @@ -26,9 +27,6 @@ func TestAccDataSourceSharedImageVersion_basic(t *testing.T) {
data.CheckWithClientForResource(ImageResource{}.generalizeVirtualMachine(data), "azurerm_virtual_machine.testsource"),
),
},
{
Config: SharedImageVersionResource{}.imageVersion(data),
},
{
Config: r.basic(data),
Check: acceptance.ComposeTestCheckFunc(
Expand All @@ -54,11 +52,40 @@ func TestAccDataSourceSharedImageVersion_latest(t *testing.T) {
),
},
{
Config: r.additionalVersion(data),
Config: r.latest(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).Key("name").HasValue("0.0.2"),
check.That(data.ResourceName).Key("managed_image_id").Exists(),
check.That(data.ResourceName).Key("target_region.#").HasValue("1"),
check.That(data.ResourceName).Key("target_region.0.storage_account_type").HasValue("Standard_LRS"),
),
},
})
}

func TestAccDataSourceSharedImageVersion_excludeFromLatest(t *testing.T) {
data := acceptance.BuildTestData(t, "data.azurerm_shared_image_version", "test")
r := SharedImageVersionDataSource{}

expectedVersion := "0.0.1"
if !features.FourPointOhBeta() {
// `ExcludeFromLatest` is not considered in 3.0 so `0.0.2` will still be the latest image
expectedVersion = "0.0.2"
}

data.DataSourceTest(t, []acceptance.TestStep{
{
// need to create a vm and then reference it in the image creation
Config: SharedImageVersionResource{}.setup(data),
Check: acceptance.ComposeTestCheckFunc(
data.CheckWithClientForResource(ImageResource{}.virtualMachineExists, "azurerm_virtual_machine.testsource"),
data.CheckWithClientForResource(ImageResource{}.generalizeVirtualMachine(data), "azurerm_virtual_machine.testsource"),
),
},
{
Config: r.customName(data, "latest"),
Config: r.excludeFromLatest(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).Key("name").HasValue(expectedVersion),
check.That(data.ResourceName).Key("managed_image_id").Exists(),
check.That(data.ResourceName).Key("target_region.#").HasValue("1"),
check.That(data.ResourceName).Key("target_region.0.storage_account_type").HasValue("Standard_LRS"),
Expand All @@ -82,11 +109,34 @@ func TestAccDataSourceSharedImageVersion_recent(t *testing.T) {
),
},
{
Config: r.additionalVersion(data),
Config: r.recent(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).Key("name").HasValue("0.0.2"),
check.That(data.ResourceName).Key("managed_image_id").Exists(),
check.That(data.ResourceName).Key("target_region.#").HasValue("1"),
check.That(data.ResourceName).Key("target_region.0.storage_account_type").HasValue("Standard_LRS"),
),
},
})
}

func TestAccDataSourceSharedImageVersion_sortVersionsBySemver(t *testing.T) {
data := acceptance.BuildTestData(t, "data.azurerm_shared_image_version", "test")
r := SharedImageVersionDataSource{}

data.DataSourceTest(t, []acceptance.TestStep{
{
// need to create a vm and then reference it in the image creation
Config: SharedImageVersionResource{}.setup(data),
Check: acceptance.ComposeTestCheckFunc(
data.CheckWithClientForResource(ImageResource{}.virtualMachineExists, "azurerm_virtual_machine.testsource"),
data.CheckWithClientForResource(ImageResource{}.generalizeVirtualMachine(data), "azurerm_virtual_machine.testsource"),
),
},
{
Config: r.customName(data, "recent"),
Config: r.sortVersionsBySemver(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).Key("name").HasValue("0.0.10"),
check.That(data.ResourceName).Key("managed_image_id").Exists(),
check.That(data.ResourceName).Key("target_region.#").HasValue("1"),
check.That(data.ResourceName).Key("target_region.0.storage_account_type").HasValue("Standard_LRS"),
Expand All @@ -108,7 +158,7 @@ data "azurerm_shared_image_version" "test" {
`, SharedImageVersionResource{}.imageVersion(data))
}

func (SharedImageVersionDataSource) additionalVersion(data acceptance.TestData) string {
func (SharedImageVersionDataSource) latest(data acceptance.TestData) string {
return fmt.Sprintf(`
%s
Expand All @@ -125,18 +175,132 @@ resource "azurerm_shared_image_version" "test2" {
regional_replica_count = 1
}
}
data "azurerm_shared_image_version" "test" {
name = "latest"
gallery_name = azurerm_shared_image_version.test2.gallery_name
image_name = azurerm_shared_image_version.test2.image_name
resource_group_name = azurerm_shared_image_version.test2.resource_group_name
}
`, SharedImageVersionResource{}.imageVersion(data))
}

func (r SharedImageVersionDataSource) customName(data acceptance.TestData, name string) string {
func (SharedImageVersionDataSource) excludeFromLatest(data acceptance.TestData) string {
return fmt.Sprintf(`
%s
resource "azurerm_shared_image_version" "test2" {
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
exclude_from_latest = true
target_region {
name = azurerm_resource_group.test.location
regional_replica_count = 1
}
}
data "azurerm_shared_image_version" "test" {
name = "%s"
name = "latest"
gallery_name = azurerm_shared_image_version.test2.gallery_name
image_name = azurerm_shared_image_version.test2.image_name
resource_group_name = azurerm_shared_image_version.test2.resource_group_name
}
`, r.additionalVersion(data), name)
`, SharedImageVersionResource{}.imageVersion(data))
}

func (SharedImageVersionDataSource) recent(data acceptance.TestData) string {
return fmt.Sprintf(`
%s
resource "azurerm_shared_image_version" "test2" {
name = "0.0.3"
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
}
}
resource "azurerm_shared_image_version" "test3" {
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
}
depends_on = [
azurerm_shared_image_version.test2
]
}
data "azurerm_shared_image_version" "test" {
name = "recent"
gallery_name = azurerm_shared_image_version.test3.gallery_name
image_name = azurerm_shared_image_version.test3.image_name
resource_group_name = azurerm_shared_image_version.test3.resource_group_name
}
`, SharedImageVersionResource{}.imageVersion(data))
}

func (SharedImageVersionDataSource) sortVersionsBySemver(data acceptance.TestData) string {
return fmt.Sprintf(`
%s
resource "azurerm_shared_image_version" "test2" {
name = "0.0.9"
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
}
}
resource "azurerm_shared_image_version" "test3" {
name = "0.0.10"
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
}
depends_on = [
azurerm_shared_image_version.test2
]
}
data "azurerm_shared_image_version" "test" {
name = "latest"
gallery_name = azurerm_shared_image_version.test3.gallery_name
image_name = azurerm_shared_image_version.test3.image_name
resource_group_name = azurerm_shared_image_version.test3.resource_group_name
sort_versions_by_semver = true
}
`, SharedImageVersionResource{}.imageVersion(data))
}
2 changes: 2 additions & 0 deletions website/docs/d/shared_image_version.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ The following arguments are supported:

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

~> **Note:** In 3.0, `latest` may return an image version with `exclude_from_latest` set to `true`. Starting from 4.0 onwards `latest` will not return image versions with `exlude_from_latest` set to `true`.

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

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

0 comments on commit 90abb8c

Please sign in to comment.