Skip to content

Commit

Permalink
azurerm_storage_object_replication - Use storage account id as `pro…
Browse files Browse the repository at this point in the history
…perties.sourceAccount` and `properties.destinationAccount` (#20132)
  • Loading branch information
magodo authored Feb 27, 2023
1 parent 2b7efb6 commit 9fdf464
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 5 deletions.
18 changes: 13 additions & 5 deletions internal/services/storage/storage_object_replication_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package storage
import (
"fmt"
"log"
"strings"
"time"

"github.com/hashicorp/go-azure-helpers/lang/response"
Expand Down Expand Up @@ -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())
Expand All @@ -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()),
},
}
Expand Down Expand Up @@ -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()),
},
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 == "" {
Expand Down Expand Up @@ -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)
}

0 comments on commit 9fdf464

Please sign in to comment.