Skip to content

Commit

Permalink
new resource: azurerm_storage_container_immutability_policy
Browse files Browse the repository at this point in the history
  • Loading branch information
manicminer committed Apr 30, 2024
1 parent 75d6f38 commit 24eddf6
Show file tree
Hide file tree
Showing 9 changed files with 1,030 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package parse

// NOTE: this file is generated via 'go:generate' - manual changes will be overwritten

import (
"fmt"
"strings"

"github.com/hashicorp/go-azure-helpers/resourcemanager/resourceids"
)

type StorageContainerImmutabilityPolicyId struct {
SubscriptionId string
ResourceGroup string
StorageAccountName string
BlobServiceName string
ContainerName string
ImmutabilityPolicyName string
}

func NewStorageContainerImmutabilityPolicyID(subscriptionId, resourceGroup, storageAccountName, blobServiceName, containerName, immutabilityPolicyName string) StorageContainerImmutabilityPolicyId {
return StorageContainerImmutabilityPolicyId{
SubscriptionId: subscriptionId,
ResourceGroup: resourceGroup,
StorageAccountName: storageAccountName,
BlobServiceName: blobServiceName,
ContainerName: containerName,
ImmutabilityPolicyName: immutabilityPolicyName,
}
}

func (id StorageContainerImmutabilityPolicyId) String() string {
segments := []string{
fmt.Sprintf("Immutability Policy Name %q", id.ImmutabilityPolicyName),
fmt.Sprintf("Container Name %q", id.ContainerName),
fmt.Sprintf("Blob Service Name %q", id.BlobServiceName),
fmt.Sprintf("Storage Account Name %q", id.StorageAccountName),
fmt.Sprintf("Resource Group %q", id.ResourceGroup),
}
segmentsStr := strings.Join(segments, " / ")
return fmt.Sprintf("%s: (%s)", "Storage Container Immutability Policy", segmentsStr)
}

func (id StorageContainerImmutabilityPolicyId) ID() string {
fmtString := "/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Storage/storageAccounts/%s/blobServices/%s/containers/%s/immutabilityPolicies/%s"
return fmt.Sprintf(fmtString, id.SubscriptionId, id.ResourceGroup, id.StorageAccountName, id.BlobServiceName, id.ContainerName, id.ImmutabilityPolicyName)
}

// StorageContainerImmutabilityPolicyID parses a StorageContainerImmutabilityPolicy ID into an StorageContainerImmutabilityPolicyId struct
func StorageContainerImmutabilityPolicyID(input string) (*StorageContainerImmutabilityPolicyId, error) {
id, err := resourceids.ParseAzureResourceID(input)
if err != nil {
return nil, fmt.Errorf("parsing %q as an StorageContainerImmutabilityPolicy ID: %+v", input, err)
}

resourceId := StorageContainerImmutabilityPolicyId{
SubscriptionId: id.SubscriptionID,
ResourceGroup: id.ResourceGroup,
}

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

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

if resourceId.StorageAccountName, err = id.PopSegment("storageAccounts"); err != nil {
return nil, err
}
if resourceId.BlobServiceName, err = id.PopSegment("blobServices"); err != nil {
return nil, err
}
if resourceId.ContainerName, err = id.PopSegment("containers"); err != nil {
return nil, err
}
if resourceId.ImmutabilityPolicyName, err = id.PopSegment("immutabilityPolicies"); err != nil {
return nil, err
}

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

return &resourceId, nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package parse

// NOTE: this file is generated via 'go:generate' - manual changes will be overwritten

import (
"testing"

"github.com/hashicorp/go-azure-helpers/resourcemanager/resourceids"
)

var _ resourceids.Id = StorageContainerImmutabilityPolicyId{}

func TestStorageContainerImmutabilityPolicyIDFormatter(t *testing.T) {
actual := NewStorageContainerImmutabilityPolicyID("12345678-1234-9876-4563-123456789012", "resGroup1", "storageAccount1", "default", "container1", "default").ID()
expected := "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Storage/storageAccounts/storageAccount1/blobServices/default/containers/container1/immutabilityPolicies/default"
if actual != expected {
t.Fatalf("Expected %q but got %q", expected, actual)
}
}

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

{
// 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 BlobServiceName
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Storage/storageAccounts/storageAccount1/",
Error: true,
},

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

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

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

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

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

{
// valid
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Storage/storageAccounts/storageAccount1/blobServices/default/containers/container1/immutabilityPolicies/default",
Expected: &StorageContainerImmutabilityPolicyId{
SubscriptionId: "12345678-1234-9876-4563-123456789012",
ResourceGroup: "resGroup1",
StorageAccountName: "storageAccount1",
BlobServiceName: "default",
ContainerName: "container1",
ImmutabilityPolicyName: "default",
},
},

{
// upper-cased
Input: "/SUBSCRIPTIONS/12345678-1234-9876-4563-123456789012/RESOURCEGROUPS/RESGROUP1/PROVIDERS/MICROSOFT.STORAGE/STORAGEACCOUNTS/STORAGEACCOUNT1/BLOBSERVICES/DEFAULT/CONTAINERS/CONTAINER1/IMMUTABILITYPOLICIES/DEFAULT",
Error: true,
},
}

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

actual, err := StorageContainerImmutabilityPolicyID(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.SubscriptionId != v.Expected.SubscriptionId {
t.Fatalf("Expected %q but got %q for SubscriptionId", v.Expected.SubscriptionId, actual.SubscriptionId)
}
if actual.ResourceGroup != v.Expected.ResourceGroup {
t.Fatalf("Expected %q but got %q for ResourceGroup", v.Expected.ResourceGroup, actual.ResourceGroup)
}
if actual.StorageAccountName != v.Expected.StorageAccountName {
t.Fatalf("Expected %q but got %q for StorageAccountName", v.Expected.StorageAccountName, actual.StorageAccountName)
}
if actual.BlobServiceName != v.Expected.BlobServiceName {
t.Fatalf("Expected %q but got %q for BlobServiceName", v.Expected.BlobServiceName, actual.BlobServiceName)
}
if actual.ContainerName != v.Expected.ContainerName {
t.Fatalf("Expected %q but got %q for ContainerName", v.Expected.ContainerName, actual.ContainerName)
}
if actual.ImmutabilityPolicyName != v.Expected.ImmutabilityPolicyName {
t.Fatalf("Expected %q but got %q for ImmutabilityPolicyName", v.Expected.ImmutabilityPolicyName, actual.ImmutabilityPolicyName)
}
}
}
1 change: 1 addition & 0 deletions internal/services/storage/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,6 @@ func (r Registration) DataSources() []sdk.DataSource {
func (r Registration) Resources() []sdk.Resource {
return []sdk.Resource{
LocalUserResource{},
StorageContainerImmutabilityPolicyResource{},
}
}
1 change: 1 addition & 0 deletions internal/services/storage/resourceids.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ package storage
//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=StorageQueueResourceManager -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Storage/storageAccounts/storageAccount1/queueServices/default/queues/queue1
//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=StorageShareResourceManager -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Storage/storageAccounts/storageAccount1/fileServices/fileService1/fileshares/share1
//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=StorageAccountManagementPolicy -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Storage/storageAccounts/storageAccount1/managementPolicies/policy1
//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=StorageContainerImmutabilityPolicy -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Storage/storageAccounts/storageAccount1/blobServices/default/containers/container1/immutabilityPolicies/default
Loading

0 comments on commit 24eddf6

Please sign in to comment.