Skip to content

Commit

Permalink
azurerm_shared_image_version - Support `replicated_region_deletion_…
Browse files Browse the repository at this point in the history
…enabled` and `target_region.exclude_from_latest_enabled` (#23147)

* add code

* resolve comments 0905
  • Loading branch information
ms-zhenhua authored Sep 18, 2023
1 parent 0d8a529 commit c22a8b4
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 0 deletions.
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,
},

"deletion_of_replicated_locations_enabled": {
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("deletion_of_replicated_locations_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("deletion_of_replicated_locations_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
deletion_of_replicated_locations_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`.

* `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.

* `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) Specifies whether this Shared Image Version should be excluded when querying for the `latest` version. Defaults to `false`.

* `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

0 comments on commit c22a8b4

Please sign in to comment.