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

storage: refactoring azurerm_blob_inventory_policy to use hashicorp/go-azure-sdk #25268

Merged
merged 4 commits into from
Mar 20, 2024
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
44 changes: 17 additions & 27 deletions internal/services/storage/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"fmt"

"github.com/Azure/azure-sdk-for-go/services/storage/mgmt/2021-09-01/storage" // nolint: staticcheck
"github.com/Azure/go-autorest/autorest/azure"
storage_v2023_01_01 "github.com/hashicorp/go-azure-sdk/resource-manager/storage/2023-01-01"
"github.com/hashicorp/go-azure-sdk/resource-manager/storagesync/2020-03-01/cloudendpointresource"
"github.com/hashicorp/go-azure-sdk/resource-manager/storagesync/2020-03-01/storagesyncservicesresource"
Expand All @@ -21,20 +20,16 @@ import (
var StorageDomainSuffix *string

type Client struct {
SubscriptionId string

AccountsClient *storage.AccountsClient
BlobInventoryPoliciesClient *storage.BlobInventoryPoliciesClient
BlobServicesClient *storage.BlobServicesClient
EncryptionScopesClient *storage.EncryptionScopesClient
FileServicesClient *storage.FileServicesClient
SyncCloudEndpointsClient *cloudendpointresource.CloudEndpointResourceClient
SyncGroupsClient *syncgroupresource.SyncGroupResourceClient
SyncServiceClient *storagesyncservicesresource.StorageSyncServicesResourceClient
AccountsClient *storage.AccountsClient
BlobServicesClient *storage.BlobServicesClient
EncryptionScopesClient *storage.EncryptionScopesClient
FileServicesClient *storage.FileServicesClient
SyncCloudEndpointsClient *cloudendpointresource.CloudEndpointResourceClient
SyncGroupsClient *syncgroupresource.SyncGroupResourceClient
SyncServiceClient *storagesyncservicesresource.StorageSyncServicesResourceClient

ResourceManager *storage_v2023_01_01.Client

AzureEnvironment azure.Environment
StorageDomainSuffix string

authorizerForAad auth.Authorizer
Expand All @@ -55,9 +50,6 @@ func NewClient(o *common.ClientOptions) (*Client, error) {
blobServicesClient := storage.NewBlobServicesClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&blobServicesClient.Client, o.ResourceManagerAuthorizer)

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

encryptionScopesClient := storage.NewEncryptionScopesClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&encryptionScopesClient.Client, o.ResourceManagerAuthorizer)

Expand All @@ -76,6 +68,7 @@ func NewClient(o *common.ClientOptions) (*Client, error) {
return nil, fmt.Errorf("building CloudEndpoint client: %+v", err)
}
o.Configure(syncCloudEndpointsClient.Client, o.Authorizers.ResourceManager)

syncServiceClient, err := storagesyncservicesresource.NewStorageSyncServicesResourceClientWithBaseURI(o.Environment.ResourceManager)
if err != nil {
return nil, fmt.Errorf("building StorageSyncService client: %+v", err)
Expand All @@ -91,18 +84,15 @@ func NewClient(o *common.ClientOptions) (*Client, error) {
// 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,
BlobServicesClient: &blobServicesClient,
BlobInventoryPoliciesClient: &blobInventoryPoliciesClient,
EncryptionScopesClient: &encryptionScopesClient,
FileServicesClient: &fileServicesClient,
ResourceManager: resourceManager,
SubscriptionId: o.SubscriptionId,
SyncCloudEndpointsClient: syncCloudEndpointsClient,
SyncServiceClient: syncServiceClient,
SyncGroupsClient: syncGroupsClient,

AzureEnvironment: o.AzureEnvironment,
AccountsClient: &accountsClient,
BlobServicesClient: &blobServicesClient,
EncryptionScopesClient: &encryptionScopesClient,
FileServicesClient: &fileServicesClient,
ResourceManager: resourceManager,
SyncCloudEndpointsClient: syncCloudEndpointsClient,
SyncServiceClient: syncServiceClient,
SyncGroupsClient: syncGroupsClient,

StorageDomainSuffix: *storageSuffix,
}

Expand Down
134 changes: 134 additions & 0 deletions internal/services/storage/migration/blob_inventory_policy_v0_to_v1.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
package migration

import (
"context"
"log"

"github.com/hashicorp/go-azure-helpers/resourcemanager/commonids"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
)

type BlobInventoryPolicyV0ToV1 struct {
}

func (BlobInventoryPolicyV0ToV1) Schema() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{
"storage_account_id": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
},

"rules": {
Type: pluginsdk.TypeSet,
Required: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Required: true,
},

"storage_container_name": {
Type: pluginsdk.TypeString,
Required: true,
},

"format": {
Type: pluginsdk.TypeString,
Required: true,
},

"schedule": {
Type: pluginsdk.TypeString,
Required: true,
},

"scope": {
Type: pluginsdk.TypeString,
Required: true,
},

"schema_fields": {
Type: pluginsdk.TypeList,
Required: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
},
},

"filter": {
Type: pluginsdk.TypeList,
Optional: true,
MaxItems: 1,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"blob_types": {
Type: pluginsdk.TypeSet,
Required: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
},
},

"include_blob_versions": {
Type: pluginsdk.TypeBool,
Optional: true,
Default: false,
},

"include_deleted": {
Type: pluginsdk.TypeBool,
Optional: true,
Default: false,
},

"include_snapshots": {
Type: pluginsdk.TypeBool,
Optional: true,
Default: false,
},

"prefix_match": {
Type: pluginsdk.TypeSet,
Optional: true,
MaxItems: 10,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
},
},

"exclude_prefixes": {
Type: pluginsdk.TypeSet,
Optional: true,
MaxItems: 10,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
},
},
},
},
},
},
},
},
}
}

func (BlobInventoryPolicyV0ToV1) UpgradeFunc() pluginsdk.StateUpgraderFunc {
return func(ctx context.Context, rawState map[string]interface{}, meta interface{}) (map[string]interface{}, error) {
oldIdRaw := rawState["id"].(string)

// This now uses the Storage Account ID since it's a 1:1 resource
storageAccountIdRaw := rawState["storage_account_id"].(string)
newId, err := commonids.ParseStorageAccountID(storageAccountIdRaw)
if err != nil {
return nil, err
}
newIdRaw := newId.ID()

log.Printf("[DEBUG] Updating the Resource ID from %q to %q", oldIdRaw, newIdRaw)
rawState["id"] = newIdRaw
return rawState, nil
}
}
78 changes: 0 additions & 78 deletions internal/services/storage/parse/blob_inventory_policy.go

This file was deleted.

Loading
Loading