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_queue #26087

Merged
merged 6 commits into from
May 27, 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
1 change: 1 addition & 0 deletions internal/services/storage/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func (r Registration) SupportedDataSources() map[string]*pluginsdk.Resource {
"azurerm_storage_container": dataSourceStorageContainer(),
"azurerm_storage_encryption_scope": dataSourceStorageEncryptionScope(),
"azurerm_storage_management_policy": dataSourceStorageManagementPolicy(),
"azurerm_storage_queue": dataSourceStorageQueue(),
"azurerm_storage_share": dataSourceStorageShare(),
"azurerm_storage_sync": dataSourceStorageSync(),
"azurerm_storage_sync_group": dataSourceStorageSyncGroup(),
Expand Down
105 changes: 105 additions & 0 deletions internal/services/storage/storage_queue_data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// 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/queue/queues"
)

func dataSourceStorageQueue() *pluginsdk.Resource {
return &pluginsdk.Resource{
Read: dataSourceStorageQueueRead,

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,
},

"metadata": MetaDataComputedSchema(),

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

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

queueName := 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 Queue %q: %v", accountName, queueName, err)
}
if account == nil {
return fmt.Errorf("locating Storage Account %q for Queue %q", accountName, queueName)
}

queuesDataPlaneClient, err := storageClient.QueuesDataPlaneClient(ctx, *account, storageClient.DataPlaneOperationSupportingAnyAuthMethod())
if err != nil {
return fmt.Errorf("building Queues Client: %v", err)

}

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

// Parse the queue 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 := queues.NewQueueID(*accountId, queueName)

props, err := queuesDataPlaneClient.Get(ctx, queueName)
if err != nil {
return fmt.Errorf("retrieving %s: %v", id, err)
}

d.SetId(id.ID())

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

if props != nil {
if err = d.Set("metadata", FlattenMetaData(props.MetaData)); err != nil {
return fmt.Errorf("setting `metadata`: %v", err)
}
}

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

return nil
}
65 changes: 65 additions & 0 deletions internal/services/storage/storage_queue_data_source_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package storage_test

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check"
)

type StorageQueueDataSource struct{}

func TestAccDataSourceStorageQueue_basic(t *testing.T) {
data := acceptance.BuildTestData(t, "data.azurerm_storage_queue", "test")

data.DataSourceTest(t, []acceptance.TestStep{
{
Config: StorageQueueDataSource{}.basic(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).Key("metadata.%").HasValue("2"),
check.That(data.ResourceName).Key("metadata.k1").HasValue("v1"),
check.That(data.ResourceName).Key("metadata.k2").HasValue("v2"),
),
},
})
}

func (d StorageQueueDataSource) basic(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}

resource "azurerm_resource_group" "test" {
name = "queuedstest-%[1]s"
location = "%[2]s"
}

resource "azurerm_storage_account" "test" {
name = "acctestsadsc%[1]s"
resource_group_name = "${azurerm_resource_group.test.name}"

location = "${azurerm_resource_group.test.location}"
account_tier = "Standard"
account_replication_type = "LRS"
}

resource "azurerm_storage_queue" "test" {
name = "queuedstest-%[1]s"
storage_account_name = "${azurerm_storage_account.test.name}"
metadata = {
k1 = "v1"
k2 = "v2"
}
}

data "azurerm_storage_queue" "test" {
name = azurerm_storage_queue.test.name
storage_account_name = azurerm_storage_queue.test.storage_account_name
}
`, data.RandomString, data.Locations.Primary, data.RandomInteger)
}
40 changes: 40 additions & 0 deletions website/docs/d/storage_queue.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
---
subcategory: "Storage"
layout: "azurerm"
page_title: "Azure Resource Manager: azurerm_storage_queue"
description: |-
Gets information about an existing Storage Queue.
---

# Data Source: azurerm_storage_queue

Use this data source to access information about an existing Storage Queue.

## Example Usage

```hcl
data "azurerm_storage_queue" "example" {
name = "example-queue-name"
storage_account_name = "example-storage-account-name"
}
```

## Arguments Reference

The following arguments are supported:

* `name` - (Required) The name of the Queue.

* `storage_account_name` - (Required) The name of the Storage Account where the Queue exists.

## Attributes Reference

* `metadata` - A mapping of MetaData for this Queue.

* `resource_manager_id` - The Resource Manager ID of this Storage Queue.

## Timeouts

The `timeouts` block allows you to specify [timeouts](https://www.terraform.io/language/resources/syntax#operation-timeouts) for certain actions:

* `read` - (Defaults to 5 minutes) Used when retrieving the Storage Queue.
Loading