diff --git a/internal/services/storage/storage_object_replication_resource.go b/internal/services/storage/storage_object_replication_resource.go index 8b9d173d6df7..f5b5c487c466 100644 --- a/internal/services/storage/storage_object_replication_resource.go +++ b/internal/services/storage/storage_object_replication_resource.go @@ -3,6 +3,7 @@ package storage import ( "fmt" "log" + "strings" "time" "github.com/hashicorp/go-azure-helpers/lang/response" @@ -136,7 +137,11 @@ func resourceStorageObjectReplicationCreate(d *pluginsdk.ResourceData, meta inte if resp.Model != nil && resp.Model.Value != nil { for _, existing := range *resp.Model.Value { if existing.Name != nil && *existing.Name != "" { - if prop := existing.Properties; prop != nil && prop.SourceAccount == srcAccount.StorageAccountName && prop.DestinationAccount == dstAccount.StorageAccountName { + if prop := existing.Properties; prop != nil && ( + // Storage allows either a storage account name (only when allowCrossTenantReplication of the SA is false) or a full resource id (both cases). + // We should check for both cases. + (prop.SourceAccount == srcAccount.StorageAccountName && prop.DestinationAccount == dstAccount.StorageAccountName) || + (strings.EqualFold(prop.SourceAccount, srcAccount.ID()) && strings.EqualFold(prop.DestinationAccount, dstAccount.ID()))) { srcId.ObjectReplicationPolicyId = *existing.Name dstId.ObjectReplicationPolicyId = *existing.Name return tf.ImportAsExistsError("azurerm_storage_object_replication", parse.NewObjectReplicationID(srcId, dstId).ID()) @@ -147,8 +152,8 @@ func resourceStorageObjectReplicationCreate(d *pluginsdk.ResourceData, meta inte props := objectreplicationpolicies.ObjectReplicationPolicy{ Properties: &objectreplicationpolicies.ObjectReplicationPolicyProperties{ - SourceAccount: srcId.StorageAccountName, - DestinationAccount: dstId.StorageAccountName, + SourceAccount: srcAccount.ID(), + DestinationAccount: dstAccount.ID(), Rules: expandArmObjectReplicationRuleArray(d.Get("rules").(*pluginsdk.Set).List()), }, } @@ -197,10 +202,13 @@ func resourceStorageObjectReplicationUpdate(d *pluginsdk.ResourceData, meta inte return err } + srcAccount := objectreplicationpolicies.NewStorageAccountID(id.Src.SubscriptionId, id.Src.ResourceGroupName, id.Src.StorageAccountName) + dstAccount := objectreplicationpolicies.NewStorageAccountID(id.Dst.SubscriptionId, id.Dst.ResourceGroupName, id.Dst.StorageAccountName) + props := objectreplicationpolicies.ObjectReplicationPolicy{ Properties: &objectreplicationpolicies.ObjectReplicationPolicyProperties{ - SourceAccount: id.Src.StorageAccountName, - DestinationAccount: id.Dst.StorageAccountName, + SourceAccount: srcAccount.ID(), + DestinationAccount: dstAccount.ID(), Rules: expandArmObjectReplicationRuleArray(d.Get("rules").(*pluginsdk.Set).List()), }, } diff --git a/internal/services/storage/storage_object_replication_resource_test.go b/internal/services/storage/storage_object_replication_resource_test.go index cb0bc20b31f5..3dc2c59a3c0a 100644 --- a/internal/services/storage/storage_object_replication_resource_test.go +++ b/internal/services/storage/storage_object_replication_resource_test.go @@ -117,6 +117,22 @@ func TestAccStorageObjectReplication_update(t *testing.T) { }) } +func TestAccStorageObjectReplication_crossTenantDisabled(t *testing.T) { + data := acceptance.BuildTestData(t, "azurerm_storage_object_replication", "test") + r := StorageObjectReplicationResource{} + data.ResourceTest(t, r, []acceptance.TestStep{ + { + Config: r.crossTenantDisabled(data), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + check.That(data.ResourceName).Key("source_object_replication_id").Exists(), + check.That(data.ResourceName).Key("destination_object_replication_id").Exists(), + ), + }, + data.ImportStep(), + }) +} + func TestAccStorageObjectReplication_crossSubscription(t *testing.T) { data := acceptance.BuildTestData(t, "azurerm_storage_object_replication", "test") if data.Subscriptions.Secondary == "" { @@ -395,3 +411,80 @@ resource "azurerm_storage_object_replication" "test" { } `, data.Subscriptions.Secondary, data.RandomInteger, data.Locations.Primary, data.RandomString, data.Locations.Secondary) } + +func (r StorageObjectReplicationResource) crossTenantDisabled(data acceptance.TestData) string { + return fmt.Sprintf(` +provider "azurerm" { + features {} +} + +resource "azurerm_resource_group" "src" { + name = "acctest-storage-src-%[1]d" + location = "%[2]s" +} + +resource "azurerm_storage_account" "src" { + name = "stracctsrc%[3]s" + resource_group_name = azurerm_resource_group.src.name + location = azurerm_resource_group.src.location + account_tier = "Standard" + account_replication_type = "LRS" + cross_tenant_replication_enabled = false + blob_properties { + versioning_enabled = true + change_feed_enabled = true + } +} + +resource "azurerm_storage_container" "src" { + name = "strcsrc%[3]s" + storage_account_name = azurerm_storage_account.src.name + container_access_type = "private" +} + +resource "azurerm_storage_container" "src_second" { + name = "strcsrcsecond%[3]s" + storage_account_name = azurerm_storage_account.src.name + container_access_type = "private" +} + +resource "azurerm_resource_group" "dst" { + name = "acctest-storage-alt-%[1]d" + location = "%[4]s" +} + +resource "azurerm_storage_account" "dst" { + name = "stracctdst%[3]s" + resource_group_name = azurerm_resource_group.dst.name + location = azurerm_resource_group.dst.location + account_tier = "Standard" + account_replication_type = "LRS" + cross_tenant_replication_enabled = false + blob_properties { + versioning_enabled = true + change_feed_enabled = true + } +} + +resource "azurerm_storage_container" "dst" { + name = "strcdst%[3]s" + storage_account_name = azurerm_storage_account.dst.name + container_access_type = "private" +} + +resource "azurerm_storage_container" "dst_second" { + name = "strcdstsecond%[3]s" + storage_account_name = azurerm_storage_account.dst.name + container_access_type = "private" +} + +resource "azurerm_storage_object_replication" "test" { + source_storage_account_id = azurerm_storage_account.src.id + destination_storage_account_id = azurerm_storage_account.dst.id + rules { + source_container_name = azurerm_storage_container.src.name + destination_container_name = azurerm_storage_container.dst.name + } +} +`, data.RandomInteger, data.Locations.Primary, data.RandomString, data.Locations.Secondary) +}