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

New resource azurerm_storage_object_replication_policy #11744

Merged
merged 11 commits into from
Jun 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions azurerm/internal/services/storage/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type Client struct {
CloudEndpointsClient *storagesync.CloudEndpointsClient
EncryptionScopesClient *storage.EncryptionScopesClient
Environment az.Environment
ObjectReplicationClient *storage.ObjectReplicationPoliciesClient
SyncServiceClient *storagesync.ServicesClient
SyncGroupsClient *storagesync.SyncGroupsClient
SubscriptionId string
Expand Down Expand Up @@ -66,6 +67,9 @@ func NewClient(options *common.ClientOptions) *Client {
encryptionScopesClient := storage.NewEncryptionScopesClientWithBaseURI(options.ResourceManagerEndpoint, options.SubscriptionId)
options.ConfigureClient(&encryptionScopesClient.Client, options.ResourceManagerAuthorizer)

objectReplicationPolicyClient := storage.NewObjectReplicationPoliciesClientWithBaseURI(options.ResourceManagerEndpoint, options.SubscriptionId)
options.ConfigureClient(&objectReplicationPolicyClient.Client, options.ResourceManagerAuthorizer)

syncServiceClient := storagesync.NewServicesClientWithBaseURI(options.ResourceManagerEndpoint, options.SubscriptionId)
options.ConfigureClient(&syncServiceClient.Client, options.ResourceManagerAuthorizer)

Expand All @@ -84,6 +88,7 @@ func NewClient(options *common.ClientOptions) *Client {
CloudEndpointsClient: &cloudEndpointsClient,
EncryptionScopesClient: &encryptionScopesClient,
Environment: options.Environment,
ObjectReplicationClient: &objectReplicationPolicyClient,
SubscriptionId: options.SubscriptionId,
SyncServiceClient: &syncServiceClient,
SyncGroupsClient: &syncGroupsClient,
Expand Down
120 changes: 120 additions & 0 deletions azurerm/internal/services/storage/parse/object_replication.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package parse

import (
"fmt"
"strings"

"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
)

// This is manual for concat two ids are not supported in auto-generation

type ObjectReplicationId struct {
SrcSubscriptionId string
SrcResourceGroup string
SrcStorageAccountName string
SrcName string
DstSubscriptionId string
DstResourceGroup string
DstStorageAccountName string
DstName string
}

func NewObjectReplicationID(srcSubscriptionId, srcResourceGroup, strStorageAccountName, srcName, dstSubscriptionId, dstResourceGroup, dstStorageAccountName, dstName string) ObjectReplicationId {
return ObjectReplicationId{
SrcSubscriptionId: srcSubscriptionId,
SrcResourceGroup: srcResourceGroup,
SrcStorageAccountName: strStorageAccountName,
SrcName: srcName,
DstSubscriptionId: dstSubscriptionId,
DstResourceGroup: dstResourceGroup,
DstStorageAccountName: dstStorageAccountName,
DstName: dstName,
}
}

func (id ObjectReplicationId) String() string {
segments := []string{
fmt.Sprintf("Source Name %q", id.SrcName),
fmt.Sprintf("Source Storage Account Name %q", id.SrcStorageAccountName),
fmt.Sprintf("Source Resource Group %q", id.SrcResourceGroup),
fmt.Sprintf("Source Subscription Id %q", id.SrcSubscriptionId),
fmt.Sprintf("Destination Name %q", id.DstName),
fmt.Sprintf("Destination Storage Account Name %q", id.DstStorageAccountName),
fmt.Sprintf("Destination Resource Group %q", id.DstResourceGroup),
fmt.Sprintf("Destination Subscription Id %q", id.DstSubscriptionId),
}
segmentsStr := strings.Join(segments, " / ")
return fmt.Sprintf("%s: (%s)", "Object Replication", segmentsStr)
}

func (id ObjectReplicationId) ID() string {
fmtString := "/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Storage/storageAccounts/%s/objectReplicationPolicies/%s;/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Storage/storageAccounts/%s/objectReplicationPolicies/%s"
return fmt.Sprintf(fmtString, id.SrcSubscriptionId, id.SrcResourceGroup, id.SrcStorageAccountName, id.SrcName, id.DstSubscriptionId, id.DstResourceGroup, id.DstStorageAccountName, id.DstName)
}

func (id ObjectReplicationId) SourceObjectReplicationID() string {
fmtString := "/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Storage/storageAccounts/%s/objectReplicationPolicies/%s"
return fmt.Sprintf(fmtString, id.SrcSubscriptionId, id.SrcResourceGroup, id.SrcStorageAccountName, id.SrcName)
}

func (id ObjectReplicationId) DestinationObjectReplicationID() string {
fmtString := "/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Storage/storageAccounts/%s/objectReplicationPolicies/%s"
return fmt.Sprintf(fmtString, id.DstSubscriptionId, id.DstResourceGroup, id.DstStorageAccountName, id.DstName)
}

// ObjectReplicationID parses a ObjectReplication ID into an ObjectReplicationId struct
func ObjectReplicationID(input string) (*ObjectReplicationId, error) {
ids := strings.Split(input, ";")
if len(ids) != 2 {
return nil, fmt.Errorf("storage Object Replication Id is composed as format `sourceId;destinationId`")
}
srcId, err := azure.ParseAzureResourceID(ids[0])
if err != nil {
return nil, err
}

dstId, err := azure.ParseAzureResourceID(strings.TrimSuffix(ids[1], ";"))
if err != nil {
return nil, err
}

resourceId := ObjectReplicationId{
SrcSubscriptionId: srcId.SubscriptionID,
SrcResourceGroup: srcId.ResourceGroup,
DstSubscriptionId: dstId.SubscriptionID,
DstResourceGroup: dstId.ResourceGroup,
}

if resourceId.SrcSubscriptionId == "" || resourceId.DstSubscriptionId == "" {
return nil, fmt.Errorf("ID was missing the 'subscriptions' element")
}

if resourceId.SrcResourceGroup == "" || resourceId.DstResourceGroup == "" {
return nil, fmt.Errorf("ID was missing the 'resourceGroups' element")
}

if resourceId.SrcStorageAccountName, err = srcId.PopSegment("storageAccounts"); err != nil {
return nil, err
}
if resourceId.SrcName, err = srcId.PopSegment("objectReplicationPolicies"); err != nil {
return nil, err
}

if err := srcId.ValidateNoEmptySegments(input); err != nil {
return nil, err
}

if resourceId.DstStorageAccountName, err = dstId.PopSegment("storageAccounts"); err != nil {
return nil, err
}
if resourceId.DstName, err = dstId.PopSegment("objectReplicationPolicies"); err != nil {
return nil, err
}

if err := dstId.ValidateNoEmptySegments(input); err != nil {
return nil, err
}

return &resourceId, nil
}
192 changes: 192 additions & 0 deletions azurerm/internal/services/storage/parse/object_replication_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
package parse

// This is manual for concat two ids are not supported in auto-generation

import (
"testing"

"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/resourceid"
)

var _ resourceid.Formatter = ObjectReplicationId{}

func TestObjectReplicationIDFormatter(t *testing.T) {
actual := NewObjectReplicationID("12345678-1234-9876-4563-123456789012", "resGroup1", "storageAccount1", "objectReplicationPolicy1", "12345678-1234-9876-4563-123456789012", "resGroup2", "storageAccount2", "objectReplicationPolicy2").ID()
expected := "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Storage/storageAccounts/storageAccount1/objectReplicationPolicies/objectReplicationPolicy1;/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup2/providers/Microsoft.Storage/storageAccounts/storageAccount2/objectReplicationPolicies/objectReplicationPolicy2"
if actual != expected {
t.Fatalf("Expected %q but got %q", expected, actual)
}
}

func TestObjectReplicationID(t *testing.T) {
testData := []struct {
Input string
Error bool
Expected *ObjectReplicationId
}{

{
// empty
Input: "",
Error: true,
},

{
// missing SubscriptionId
Input: "/",
Error: true,
},

{
// missing value for SubscriptionId
Input: "/subscriptions/",
Error: true,
},

{
// missing ResourceGroup
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/",
Error: true,
},

{
// missing value for ResourceGroup
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/",
Error: true,
},

{
// missing StorageAccountName
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Storage/",
Error: true,
},

{
// missing value for StorageAccountName
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Storage/storageAccounts/",
Error: true,
},

{
// missing Name
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Storage/storageAccounts/storageAccount1/",
Error: true,
},

{
// missing value for Name
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Storage/storageAccounts/storageAccount1/objectReplicationPolicies/",
Error: true,
},

{
// missing SubscriptionName
Input: "/",
Error: true,
},

{
// missing value for SubscriptionName
Input: "/subscriptions/",
Error: true,
},

{
// missing ResourceGroup
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/",
Error: true,
},

{
// missing value for ResourceGroup
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Storage/storageAccounts/storageAccount1/objectReplicationPolicies/objectReplicationPolicy1;/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/",
Error: true,
},

{
// missing StorageAccountName
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Storage/",
Error: true,
},

{
// missing value for StorageAccountName
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Storage/storageAccounts/storageAccount1/objectReplicationPolicies/objectReplicationPolicy1;/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup2/providers/Microsoft.Storage/storageAccounts/",
Error: true,
},

{
// missing Name
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Storage/storageAccounts/storageAccount1/",
Error: true,
},

{
// missing value for Name
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Storage/storageAccounts/storageAccount1/objectReplicationPolicies/objectReplicationPolicy1;/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup2/providers/Microsoft.Storage/storageAccounts/storageAccount2/objectReplicationPolicies/",
Error: true,
},

{
// valid
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Storage/storageAccounts/storageAccount1/objectReplicationPolicies/objectReplicationPolicy1;/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup2/providers/Microsoft.Storage/storageAccounts/storageAccount2/objectReplicationPolicies/objectReplicationPolicy2",
Expected: &ObjectReplicationId{
SrcSubscriptionId: "12345678-1234-9876-4563-123456789012",
SrcResourceGroup: "resGroup1",
SrcStorageAccountName: "storageAccount1",
SrcName: "objectReplicationPolicy1",
DstSubscriptionId: "12345678-1234-9876-4563-123456789012",
DstResourceGroup: "resGroup2",
DstStorageAccountName: "storageAccount2",
DstName: "objectReplicationPolicy2",
},
},

{
// upper-cased
Input: "/SUBSCRIPTIONS/12345678-1234-9876-4563-123456789012/RESOURCEGROUPS/RESGROUP1/PROVIDERS/MICROSOFT.STORAGE/STORAGEACCOUNTS/STORAGEACCOUNT1/OBJECTREPLICATIONPOLICIES/OBJECTREPLICATIONPOLICY1;/SUBSCRIPTIONS/12345678-1234-9876-4563-123456789012/RESOURCEGROUPS/RESGROUP2/PROVIDERS/MICROSOFT.STORAGE/STORAGEACCOUNTS/STORAGEACCOUNT2/OBJECTREPLICATIONPOLICIES/OBJECTREPLICATIONPOLICY2",
Error: true,
},
}

for _, v := range testData {
t.Logf("[DEBUG] Testing %q", v.Input)

actual, err := ObjectReplicationID(v.Input)
if err != nil {
if v.Error {
continue
}

t.Fatalf("Expect a value but got an error: %s", err)
}
if v.Error {
t.Fatal("Expect an error but didn't get one")
}

if actual.SrcSubscriptionId != v.Expected.SrcSubscriptionId {
t.Fatalf("Expected %q but got %q for SubscriptionId", v.Expected.SrcSubscriptionId, actual.SrcSubscriptionId)
}
if actual.SrcResourceGroup != v.Expected.SrcResourceGroup {
t.Fatalf("Expected %q but got %q for ResourceGroup", v.Expected.SrcResourceGroup, actual.SrcResourceGroup)
}
if actual.SrcStorageAccountName != v.Expected.SrcStorageAccountName {
t.Fatalf("Expected %q but got %q for StorageAccountName", v.Expected.SrcStorageAccountName, actual.SrcStorageAccountName)
}
if actual.SrcName != v.Expected.SrcName {
t.Fatalf("Expected %q but got %q for Name", v.Expected.SrcName, actual.SrcName)
}
if actual.DstSubscriptionId != v.Expected.DstSubscriptionId {
t.Fatalf("Expected %q but got %q for SubscriptionName", v.Expected.DstSubscriptionId, actual.DstSubscriptionId)
}
if actual.DstResourceGroup != v.Expected.DstResourceGroup {
t.Fatalf("Expected %q but got %q for ResourceGroup", v.Expected.DstResourceGroup, actual.DstResourceGroup)
}
if actual.DstStorageAccountName != v.Expected.DstStorageAccountName {
t.Fatalf("Expected %q but got %q for StorageAccountName", v.Expected.DstStorageAccountName, actual.DstStorageAccountName)
}
if actual.DstName != v.Expected.DstName {
t.Fatalf("Expected %q but got %q for Name", v.Expected.DstName, actual.DstName)
}
}
}
1 change: 1 addition & 0 deletions azurerm/internal/services/storage/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func (r Registration) SupportedResources() map[string]*pluginsdk.Resource {
"azurerm_storage_data_lake_gen2_filesystem": resourceStorageDataLakeGen2FileSystem(),
"azurerm_storage_data_lake_gen2_path": resourceStorageDataLakeGen2Path(),
"azurerm_storage_management_policy": resourceStorageManagementPolicy(),
"azurerm_storage_object_replication": resourceStorageObjectReplication(),
"azurerm_storage_queue": resourceStorageQueue(),
"azurerm_storage_share": resourceStorageShare(),
"azurerm_storage_share_file": resourceStorageShareFile(),
Expand Down
Loading