Skip to content

Commit

Permalink
r/blob_inventory_policy: refactoring to use hashicorp/go-azure-sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
tombuildsstuff committed Mar 15, 2024
1 parent 02bd312 commit bc18526
Show file tree
Hide file tree
Showing 9 changed files with 333 additions and 535 deletions.
36 changes: 16 additions & 20 deletions internal/services/storage/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,13 @@ import (
var StorageDomainSuffix *string

type Client struct {
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

Expand All @@ -51,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 @@ -72,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 @@ -87,15 +84,14 @@ 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,
SyncCloudEndpointsClient: syncCloudEndpointsClient,
SyncServiceClient: syncServiceClient,
SyncGroupsClient: syncGroupsClient,
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

0 comments on commit bc18526

Please sign in to comment.