Skip to content

Commit

Permalink
resource azurerm_storage_queue - add resource_manager_id
Browse files Browse the repository at this point in the history
  • Loading branch information
jennings committed Jan 9, 2023
1 parent fc7939e commit 7f6a8c9
Show file tree
Hide file tree
Showing 6 changed files with 324 additions and 0 deletions.
75 changes: 75 additions & 0 deletions internal/services/storage/parse/storage_queue_resource_manager.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/hashicorp/go-azure-helpers/resourcemanager/resourceids"
)

type StorageQueueResourceManagerId struct {
SubscriptionId string
ResourceGroup string
StorageAccountName string
QueueNameName string
}

func NewStorageQueueResourceManagerID(subscriptionId, resourceGroup, storageAccountName, queueNameName string) StorageQueueResourceManagerId {
return StorageQueueResourceManagerId{
SubscriptionId: subscriptionId,
ResourceGroup: resourceGroup,
StorageAccountName: storageAccountName,
QueueNameName: queueNameName,
}
}

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

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

// StorageQueueResourceManagerID parses a StorageQueueResourceManager ID into an StorageQueueResourceManagerId struct
func StorageQueueResourceManagerID(input string) (*StorageQueueResourceManagerId, error) {
id, err := resourceids.ParseAzureResourceID(input)
if err != nil {
return nil, err
}

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

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

return &resourceId, nil
}
128 changes: 128 additions & 0 deletions internal/services/storage/parse/storage_queue_resource_manager_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/hashicorp/go-azure-helpers/resourcemanager/resourceids"
)

var _ resourceids.Id = StorageQueueResourceManagerId{}

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

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

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

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

{
// valid
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Storage/storageAccounts/storageAccount1/queueName/queue1",
Expected: &StorageQueueResourceManagerId{
SubscriptionId: "12345678-1234-9876-4563-123456789012",
ResourceGroup: "resGroup1",
StorageAccountName: "storageAccount1",
QueueNameName: "queue1",
},
},

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

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

actual, err := StorageQueueResourceManagerID(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.QueueNameName != v.Expected.QueueNameName {
t.Fatalf("Expected %q but got %q for QueueNameName", v.Expected.QueueNameName, actual.QueueNameName)
}
}
}
1 change: 1 addition & 0 deletions internal/services/storage/resourceids.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package storage
//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=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=StorageContainerResourceManager -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Storage/storageAccounts/storageAccount1/blobServices/default/containers/container1
//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/queueName/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=StorageSyncGroup -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.StorageSync/storageSyncServices/storageSyncService1/syncGroups/syncGroup1
//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=StorageSyncService -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.StorageSync/storageSyncServices/storageSyncService1
Expand Down
9 changes: 9 additions & 0 deletions internal/services/storage/storage_queue_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ func resourceStorageQueue() *pluginsdk.Resource {
},

"metadata": MetaDataSchema(),

"resource_manager_id": {
Type: pluginsdk.TypeString,
Computed: true,
},
},
}
}
Expand Down Expand Up @@ -135,6 +140,7 @@ func resourceStorageQueueUpdate(d *pluginsdk.ResourceData, meta interface{}) err

func resourceStorageQueueRead(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()

Expand Down Expand Up @@ -175,6 +181,9 @@ func resourceStorageQueueRead(d *pluginsdk.ResourceData, meta interface{}) error
return fmt.Errorf("setting `metadata`: %s", err)
}

resourceManagerId := parse.NewStorageQueueResourceManagerID(subscriptionId, account.ResourceGroup, id.AccountName, id.Name)
d.Set("resource_manager_id", resourceManagerId.ID())

return nil
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package validate

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

import (
"fmt"

"github.com/hashicorp/terraform-provider-azurerm/internal/services/storage/parse"
)

func StorageQueueResourceManagerID(input interface{}, key string) (warnings []string, errors []error) {
v, ok := input.(string)
if !ok {
errors = append(errors, fmt.Errorf("expected %q to be a string", key))
return
}

if _, err := parse.StorageQueueResourceManagerID(v); err != nil {
errors = append(errors, err)
}

return
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package validate

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

import "testing"

func TestStorageQueueResourceManagerID(t *testing.T) {
cases := []struct {
Input string
Valid bool
}{

{
// empty
Input: "",
Valid: false,
},

{
// missing SubscriptionId
Input: "/",
Valid: false,
},

{
// missing value for SubscriptionId
Input: "/subscriptions/",
Valid: false,
},

{
// missing ResourceGroup
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/",
Valid: false,
},

{
// missing value for ResourceGroup
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/",
Valid: false,
},

{
// missing StorageAccountName
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Storage/",
Valid: false,
},

{
// missing value for StorageAccountName
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Storage/storageAccounts/",
Valid: false,
},

{
// missing QueueNameName
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Storage/storageAccounts/storageAccount1/",
Valid: false,
},

{
// missing value for QueueNameName
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Storage/storageAccounts/storageAccount1/queueName/",
Valid: false,
},

{
// valid
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Storage/storageAccounts/storageAccount1/queueName/queue1",
Valid: true,
},

{
// upper-cased
Input: "/SUBSCRIPTIONS/12345678-1234-9876-4563-123456789012/RESOURCEGROUPS/RESGROUP1/PROVIDERS/MICROSOFT.STORAGE/STORAGEACCOUNTS/STORAGEACCOUNT1/QUEUENAME/QUEUE1",
Valid: false,
},
}
for _, tc := range cases {
t.Logf("[DEBUG] Testing Value %s", tc.Input)
_, errors := StorageQueueResourceManagerID(tc.Input, "test")
valid := len(errors) == 0

if tc.Valid != valid {
t.Fatalf("Expected %t but got %t", tc.Valid, valid)
}
}
}

0 comments on commit 7f6a8c9

Please sign in to comment.