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_blob_inventory_policy #11484

Merged
merged 10 commits into from
May 1, 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
49 changes: 27 additions & 22 deletions azurerm/internal/services/storage/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,18 @@ import (
)

type Client struct {
AccountsClient *storage.AccountsClient
FileSystemsClient *filesystems.Client
ADLSGen2PathsClient *paths.Client
ManagementPoliciesClient *storage.ManagementPoliciesClient
BlobServicesClient *storage.BlobServicesClient
CloudEndpointsClient *storagesync.CloudEndpointsClient
EncryptionScopesClient *storage.EncryptionScopesClient
Environment az.Environment
SyncServiceClient *storagesync.ServicesClient
SyncGroupsClient *storagesync.SyncGroupsClient
SubscriptionId string
AccountsClient *storage.AccountsClient
FileSystemsClient *filesystems.Client
ADLSGen2PathsClient *paths.Client
ManagementPoliciesClient *storage.ManagementPoliciesClient
BlobServicesClient *storage.BlobServicesClient
BlobInventoryPoliciesClient *storage.BlobInventoryPoliciesClient
CloudEndpointsClient *storagesync.CloudEndpointsClient
EncryptionScopesClient *storage.EncryptionScopesClient
Environment az.Environment
SyncServiceClient *storagesync.ServicesClient
SyncGroupsClient *storagesync.SyncGroupsClient
SubscriptionId string

resourceManagerAuthorizer autorest.Authorizer
storageAdAuth *autorest.Authorizer
Expand All @@ -56,6 +57,9 @@ func NewClient(options *common.ClientOptions) *Client {
blobServicesClient := storage.NewBlobServicesClientWithBaseURI(options.ResourceManagerEndpoint, options.SubscriptionId)
options.ConfigureClient(&blobServicesClient.Client, options.ResourceManagerAuthorizer)

blobInventoryPoliciesClient := storage.NewBlobInventoryPoliciesClientWithBaseURI(options.ResourceManagerEndpoint, options.SubscriptionId)
options.ConfigureClient(&blobInventoryPoliciesClient.Client, options.ResourceManagerAuthorizer)

cloudEndpointsClient := storagesync.NewCloudEndpointsClientWithBaseURI(options.ResourceManagerEndpoint, options.SubscriptionId)
options.ConfigureClient(&cloudEndpointsClient.Client, options.ResourceManagerAuthorizer)

Expand All @@ -71,17 +75,18 @@ func NewClient(options *common.ClientOptions) *Client {
// TODO: switch Storage Containers to using the storage.BlobContainersClient
// (which should fix #2977) when the storage clients have been moved in here
client := Client{
AccountsClient: &accountsClient,
FileSystemsClient: &fileSystemsClient,
ADLSGen2PathsClient: &adlsGen2PathsClient,
ManagementPoliciesClient: &managementPoliciesClient,
BlobServicesClient: &blobServicesClient,
CloudEndpointsClient: &cloudEndpointsClient,
EncryptionScopesClient: &encryptionScopesClient,
Environment: options.Environment,
SubscriptionId: options.SubscriptionId,
SyncServiceClient: &syncServiceClient,
SyncGroupsClient: &syncGroupsClient,
AccountsClient: &accountsClient,
FileSystemsClient: &fileSystemsClient,
ADLSGen2PathsClient: &adlsGen2PathsClient,
ManagementPoliciesClient: &managementPoliciesClient,
BlobServicesClient: &blobServicesClient,
BlobInventoryPoliciesClient: &blobInventoryPoliciesClient,
CloudEndpointsClient: &cloudEndpointsClient,
EncryptionScopesClient: &encryptionScopesClient,
Environment: options.Environment,
SubscriptionId: options.SubscriptionId,
SyncServiceClient: &syncServiceClient,
SyncGroupsClient: &syncGroupsClient,

resourceManagerAuthorizer: options.ResourceManagerAuthorizer,
}
Expand Down
75 changes: 75 additions & 0 deletions azurerm/internal/services/storage/parse/blob_inventory_policy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package parse

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

import (
"fmt"
"strings"

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

type BlobInventoryPolicyId struct {
SubscriptionId string
ResourceGroup string
StorageAccountName string
InventoryPolicyName string
}

func NewBlobInventoryPolicyID(subscriptionId, resourceGroup, storageAccountName, inventoryPolicyName string) BlobInventoryPolicyId {
return BlobInventoryPolicyId{
SubscriptionId: subscriptionId,
ResourceGroup: resourceGroup,
StorageAccountName: storageAccountName,
InventoryPolicyName: inventoryPolicyName,
}
}

func (id BlobInventoryPolicyId) String() string {
segments := []string{
fmt.Sprintf("Inventory Policy Name %q", id.InventoryPolicyName),
fmt.Sprintf("Storage Account Name %q", id.StorageAccountName),
fmt.Sprintf("Resource Group %q", id.ResourceGroup),
}
segmentsStr := strings.Join(segments, " / ")
return fmt.Sprintf("%s: (%s)", "Blob Inventory Policy", segmentsStr)
}

func (id BlobInventoryPolicyId) ID() string {
fmtString := "/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Storage/storageAccounts/%s/inventoryPolicies/%s"
return fmt.Sprintf(fmtString, id.SubscriptionId, id.ResourceGroup, id.StorageAccountName, id.InventoryPolicyName)
}

// BlobInventoryPolicyID parses a BlobInventoryPolicy ID into an BlobInventoryPolicyId struct
func BlobInventoryPolicyID(input string) (*BlobInventoryPolicyId, error) {
id, err := azure.ParseAzureResourceID(input)
if err != nil {
return nil, err
}

resourceId := BlobInventoryPolicyId{
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.InventoryPolicyName, err = id.PopSegment("inventoryPolicies"); err != nil {
return nil, err
}

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

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

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

import (
"testing"

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

var _ resourceid.Formatter = BlobInventoryPolicyId{}

func TestBlobInventoryPolicyIDFormatter(t *testing.T) {
actual := NewBlobInventoryPolicyID("12345678-1234-9876-4563-123456789012", "resGroup1", "storageAccount1", "inventoryPolicy1").ID()
expected := "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Storage/storageAccounts/storageAccount1/inventoryPolicies/inventoryPolicy1"
if actual != expected {
t.Fatalf("Expected %q but got %q", expected, actual)
}
}

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

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

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

{
// valid
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Storage/storageAccounts/storageAccount1/inventoryPolicies/inventoryPolicy1",
Expected: &BlobInventoryPolicyId{
SubscriptionId: "12345678-1234-9876-4563-123456789012",
ResourceGroup: "resGroup1",
StorageAccountName: "storageAccount1",
InventoryPolicyName: "inventoryPolicy1",
},
},

{
// upper-cased
Input: "/SUBSCRIPTIONS/12345678-1234-9876-4563-123456789012/RESOURCEGROUPS/RESGROUP1/PROVIDERS/MICROSOFT.STORAGE/STORAGEACCOUNTS/STORAGEACCOUNT1/INVENTORYPOLICIES/INVENTORYPOLICY1",
Error: true,
},
}

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

actual, err := BlobInventoryPolicyID(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.InventoryPolicyName != v.Expected.InventoryPolicyName {
t.Fatalf("Expected %q but got %q for InventoryPolicyName", v.Expected.InventoryPolicyName, actual.InventoryPolicyName)
}
}
}
1 change: 1 addition & 0 deletions azurerm/internal/services/storage/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func (r Registration) SupportedResources() map[string]*schema.Resource {
"azurerm_storage_account_customer_managed_key": resourceStorageAccountCustomerManagedKey(),
"azurerm_storage_account_network_rules": resourceStorageAccountNetworkRules(),
"azurerm_storage_blob": resourceStorageBlob(),
"azurerm_storage_blob_inventory_policy": resourceStorageBlobInventoryPolicy(),
"azurerm_storage_container": resourceStorageContainer(),
"azurerm_storage_encryption_scope": resourceStorageEncryptionScope(),
"azurerm_storage_data_lake_gen2_filesystem": resourceStorageDataLakeGen2FileSystem(),
Expand Down
1 change: 1 addition & 0 deletions azurerm/internal/services/storage/resourceids.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package storage

//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=BlobInventoryPolicy -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Storage/storageAccounts/storageAccount1/inventoryPolicies/inventoryPolicy1
//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=EncryptionScope -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Storage/storageAccounts/storageAccount1/encryptionScopes/encryptionScope1
//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=StorageAccount -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Storage/storageAccounts/storageAccount1
//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=StorageContainerResourceManager -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Storage/storageAccounts/storageAccount1/blobServices/default/containers/container1
Expand Down
Loading