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 Data Source: azurerm_storage_table #26126

Merged
merged 6 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// 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 StorageTableResourceManagerId struct {
SubscriptionId string
ResourceGroup string
StorageAccountName string
TableServiceName string
TableName string
}

func NewStorageTableResourceManagerID(subscriptionId, resourceGroup, storageAccountName, tableServiceName, tableName string) StorageTableResourceManagerId {
return StorageTableResourceManagerId{
SubscriptionId: subscriptionId,
ResourceGroup: resourceGroup,
StorageAccountName: storageAccountName,
TableServiceName: tableServiceName,
TableName: tableName,
}
}

func (id StorageTableResourceManagerId) String() string {
segments := []string{
fmt.Sprintf("Table Name %q", id.TableName),
fmt.Sprintf("Table Service Name %q", id.TableServiceName),
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 Table Resource Manager", segmentsStr)
}

func (id StorageTableResourceManagerId) ID() string {
fmtString := "/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Storage/storageAccounts/%s/tableServices/%s/tables/%s"
return fmt.Sprintf(fmtString, id.SubscriptionId, id.ResourceGroup, id.StorageAccountName, id.TableServiceName, id.TableName)
}

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

resourceId := StorageTableResourceManagerId{
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.TableServiceName, err = id.PopSegment("tableServices"); err != nil {
return nil, err
}
if resourceId.TableName, err = id.PopSegment("tables"); 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,147 @@
// 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 = StorageTableResourceManagerId{}

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

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

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

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

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

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

{
// valid
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Storage/storageAccounts/storageAccount1/tableServices/tableService1/tables/table1",
Expected: &StorageTableResourceManagerId{
SubscriptionId: "12345678-1234-9876-4563-123456789012",
ResourceGroup: "resGroup1",
StorageAccountName: "storageAccount1",
TableServiceName: "tableService1",
TableName: "table1",
},
},

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

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

actual, err := StorageTableResourceManagerID(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.TableServiceName != v.Expected.TableServiceName {
t.Fatalf("Expected %q but got %q for TableServiceName", v.Expected.TableServiceName, actual.TableServiceName)
}
if actual.TableName != v.Expected.TableName {
t.Fatalf("Expected %q but got %q for TableName", v.Expected.TableName, actual.TableName)
}
}
}
1 change: 1 addition & 0 deletions internal/services/storage/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func (r Registration) SupportedDataSources() map[string]*pluginsdk.Resource {
"azurerm_storage_share": dataSourceStorageShare(),
"azurerm_storage_sync": dataSourceStorageSync(),
"azurerm_storage_sync_group": dataSourceStorageSyncGroup(),
"azurerm_storage_table": dataSourceStorageTable(),
"azurerm_storage_table_entity": dataSourceStorageTableEntity(),
}
}
Expand Down
1 change: 1 addition & 0 deletions internal/services/storage/resourceids.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ package storage
//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=StorageAccountDefaultBlob -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Storage/storageAccounts/storageAccount1/blobServices/default
//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=StorageTableResourceManager -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Storage/storageAccounts/storageAccount1/tableServices/tableService1/tables/table1
//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
133 changes: 133 additions & 0 deletions internal/services/storage/storage_table_data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package storage

import (
"fmt"
"time"

"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/storage/client"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/storage/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/timeouts"
"github.com/tombuildsstuff/giovanni/storage/2023-11-03/blob/accounts"
"github.com/tombuildsstuff/giovanni/storage/2023-11-03/table/tables"
)

func dataSourceStorageTable() *pluginsdk.Resource {
return &pluginsdk.Resource{
Read: dataSourceStorageTableRead,

Timeouts: &pluginsdk.ResourceTimeout{
Read: pluginsdk.DefaultTimeout(5 * time.Minute),
},

Schema: map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Required: true,
},

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

"acl": {
Type: pluginsdk.TypeSet,
Optional: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"id": {
Type: pluginsdk.TypeString,
Computed: true,
},
"access_policy": {
Type: pluginsdk.TypeList,
Computed: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"start": {
Type: pluginsdk.TypeString,
Computed: true,
},
"expiry": {
Type: pluginsdk.TypeString,
Computed: true,
},
"permissions": {
Type: pluginsdk.TypeString,
Computed: true,
},
},
},
},
},
},
},

"resource_manager_id": {
Type: pluginsdk.TypeString,
Computed: true,
},
},
}
}

func dataSourceStorageTableRead(d *pluginsdk.ResourceData, meta interface{}) error {
storageClient := meta.(*clients.Client).Storage
subscriptionId := meta.(*clients.Client).Account.SubscriptionId
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

tableName := d.Get("name").(string)
accountName := d.Get("storage_account_name").(string)

account, err := storageClient.FindAccount(ctx, subscriptionId, accountName)
if err != nil {
return fmt.Errorf("retrieving Storage Account %q for Table %q: %v", accountName, tableName, err)
}
if account == nil {
return fmt.Errorf("locating Storage Account %q for Table %q", accountName, tableName)
}

// Determine the table endpoint, so we can build a data plane ID
endpoint, err := account.DataPlaneEndpoint(client.EndpointTypeTable)
if err != nil {
return fmt.Errorf("determining Table endpoint: %v", err)
}

// Parse the table endpoint as a data plane account ID
accountId, err := accounts.ParseAccountID(*endpoint, storageClient.StorageDomainSuffix)
if err != nil {
return fmt.Errorf("parsing Account ID: %v", err)
}

id := tables.NewTableID(*accountId, tableName)

aclClient, err := storageClient.TablesDataPlaneClient(ctx, *account, storageClient.DataPlaneOperationSupportingOnlySharedKeyAuth())
if err != nil {
return fmt.Errorf("building Tables Client: %v", err)
}

acls, err := aclClient.GetACLs(ctx, id.TableName)
if err != nil {
return fmt.Errorf("retrieving ACLs for %s: %v", id, err)
}

d.SetId(id.ID())

d.Set("name", tableName)
d.Set("storage_account_name", accountName)

if err = d.Set("acl", flattenStorageTableACLs(acls)); err != nil {
return fmt.Errorf("setting acl: %v", err)
}

resourceManagerId := parse.NewStorageTableResourceManagerID(account.StorageAccountId.SubscriptionId, account.StorageAccountId.ResourceGroupName, account.StorageAccountId.StorageAccountName, "default", tableName)
d.Set("resource_manager_id", resourceManagerId.ID())

return nil
}
Loading
Loading