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

azurerm_shared_image_version - Support replicated_region_deletion_enabled and target_region.exclude_from_latest_enabled #23147

Merged
merged 2 commits into from
Sep 18, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions internal/services/compute/shared_image_version_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"time"

"github.com/Azure/go-autorest/autorest/date"
"github.com/hashicorp/go-azure-helpers/lang/pointer"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonids"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
"github.com/hashicorp/go-azure-helpers/resourcemanager/location"
Expand Down Expand Up @@ -99,6 +100,12 @@ func resourceSharedImageVersion() *pluginsdk.Resource {
ValidateFunc: validate.DiskEncryptionSetID,
},

"exclude_from_latest_enabled": {
Type: pluginsdk.TypeBool,
Optional: true,
Default: false,
},

// The Service API doesn't support to update `storage_account_type`. So it has to recreate the resource for updating `storage_account_type`.
// However, `ForceNew` cannot be used since resource would be recreated while adding or removing `target_region`.
// And `CustomizeDiff` also cannot be used since it doesn't support in a `Set`.
Expand Down Expand Up @@ -177,6 +184,13 @@ func resourceSharedImageVersion() *pluginsdk.Resource {
Default: false,
},

"replicated_region_deletion_enabled": {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tbh this'd be clearer/closer to the original name whilst still consistent as:

Suggested change
"replicated_region_deletion_enabled": {
"deletion_of_replicated_locations_enabled": {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

code updated. Thanks.

Type: pluginsdk.TypeBool,
Optional: true,
ForceNew: true,
Default: false,
},

"tags": tags.Schema(),
},

Expand Down Expand Up @@ -222,6 +236,9 @@ func resourceSharedImageVersionCreateUpdate(d *pluginsdk.ResourceData, meta inte
ReplicationMode: compute.ReplicationMode(d.Get("replication_mode").(string)),
TargetRegions: targetRegions,
},
SafetyProfile: &compute.GalleryImageVersionSafetyProfile{
AllowDeletionOfReplicatedLocations: utils.Bool(d.Get("replicated_region_deletion_enabled").(bool)),
},
StorageProfile: &compute.GalleryImageVersionStorageProfile{},
},
Tags: tags.Expand(d.Get("tags").(map[string]interface{})),
Expand Down Expand Up @@ -343,6 +360,10 @@ func resourceSharedImageVersionRead(d *pluginsdk.ResourceData, meta interface{})
d.Set("os_disk_snapshot_id", osDiskSnapShotID)
d.Set("storage_account_id", storageAccountID)
}

if safetyProfile := props.SafetyProfile; safetyProfile != nil {
d.Set("replicated_region_deletion_enabled", pointer.From(safetyProfile.AllowDeletionOfReplicatedLocations))
}
}

return tags.FlattenAndSet(d, resp.Tags)
Expand Down Expand Up @@ -415,9 +436,11 @@ func expandSharedImageVersionTargetRegions(d *pluginsdk.ResourceData) (*[]comput
regionalReplicaCount := input["regional_replica_count"].(int)
storageAccountType := input["storage_account_type"].(string)
diskEncryptionSetId := input["disk_encryption_set_id"].(string)
excludeFromLatest := input["exclude_from_latest_enabled"].(bool)

output := compute.TargetRegion{
Name: utils.String(name),
ExcludeFromLatest: utils.Bool(excludeFromLatest),
RegionalReplicaCount: utils.Int32(int32(regionalReplicaCount)),
StorageAccountType: compute.StorageAccountType(storageAccountType),
}
Expand Down Expand Up @@ -463,6 +486,8 @@ func flattenSharedImageVersionTargetRegions(input *[]compute.TargetRegion) []int
}
output["disk_encryption_set_id"] = diskEncryptionSetId

output["exclude_from_latest_enabled"] = pointer.From(v.ExcludeFromLatest)

results = append(results, output)
}
}
Expand Down
46 changes: 46 additions & 0 deletions internal/services/compute/shared_image_version_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,29 @@ func TestAccSharedImageVersion_replicationMode(t *testing.T) {
})
}

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

data.ResourceTest(t, r, []acceptance.TestStep{
{
// need to create a vm and then reference it in the image creation
Config: r.setup(data),
Check: acceptance.ComposeTestCheckFunc(
data.CheckWithClientForResource(ImageResource{}.virtualMachineExists, "azurerm_virtual_machine.testsource"),
data.CheckWithClientForResource(ImageResource{}.generalizeVirtualMachine(data), "azurerm_virtual_machine.testsource"),
),
},
{
Config: r.replicatedRegionDeletion(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
})
}

func TestAccSharedImageVersion_requiresImport(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_shared_image_version", "test")
r := SharedImageVersionResource{}
Expand Down Expand Up @@ -697,3 +720,26 @@ resource "azurerm_shared_image_version" "test" {
}
`, template)
}

func (r SharedImageVersionResource) replicatedRegionDeletion(data acceptance.TestData) string {
template := r.provision(data)
return fmt.Sprintf(`
%s

resource "azurerm_shared_image_version" "test" {
name = "0.0.1"
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
replicated_region_deletion_enabled = true

target_region {
name = azurerm_resource_group.test.location
regional_replica_count = 1
exclude_from_latest_enabled = true
}
}
`, template)
}
4 changes: 4 additions & 0 deletions website/docs/r/shared_image_version.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ The following arguments are supported:

-> **NOTE:** You must specify exact one of `blob_uri`, `managed_image_id` and `os_disk_snapshot_id`.

* `replicated_region_deletion_enabled` - (Optional) Whether removing this Gallery Image Version from replicated regions is allowed. Defaults to `false`. Changing this forces a new resource to be created.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Presumably this is?

Suggested change
* `replicated_region_deletion_enabled` - (Optional) Whether removing this Gallery Image Version from replicated regions is allowed. Defaults to `false`. Changing this forces a new resource to be created.
* `deletion_of_replicated_locations_enabled` - (Optional) Specifies whether this Shared Image Version can be deleted from the Azure Regions this is replicated to. Defaults to `false`. Changing this forces a new resource to be created.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doc updated. Thanks.


* `replication_mode` - (Optional) Mode to be used for replication. Possible values are `Full` and `Shallow`. Defaults to `Full`. Changing this forces a new resource to be created.

* `storage_account_id` - (Optional) The ID of the Storage Account where the Blob exists. Changing this forces a new resource to be created.
Expand All @@ -95,6 +97,8 @@ The `target_region` block supports the following:

* `disk_encryption_set_id` - (Optional) The ID of the Disk Encryption Set to encrypt the Image Version in the target region. Changing this forces a new resource to be created.

* `exclude_from_latest_enabled` - (Optional) Whether excluding this Image Version from the `latest` filter. If set to `true` this Image Version won't be returned for the `latest` version. Defaults to `false`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* `exclude_from_latest_enabled` - (Optional) Whether excluding this Image Version from the `latest` filter. If set to `true` this Image Version won't be returned for the `latest` version. Defaults to `false`.
* `exclude_from_latest_enabled` - (Optional) Specifies whether this Shared Image Version should be excluded when querying for the `latest` version. Defaults to `false`.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doc updated. Thanks.


* `storage_account_type` - (Optional) The storage account type for the image version. Possible values are `Standard_LRS`, `Premium_LRS` and `Standard_ZRS`. Defaults to `Standard_LRS`. You can store all of your image version replicas in Zone Redundant Storage by specifying `Standard_ZRS`.

## Attributes Reference
Expand Down