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_container #5374

Merged
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
99 changes: 99 additions & 0 deletions azurerm/internal/services/storage/data_source_storage_container.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package storage

import (
"fmt"
"time"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

func dataSourceArmStorageContainer() *schema.Resource {
return &schema.Resource{
Read: dataSourceArmStorageContainerRead,

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

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

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

"container_access_type": {
Type: schema.TypeString,
Computed: true,
},

"metadata": MetaDataComputedSchema(),

// TODO: support for ACL's, Legal Holds and Immutability Policies
"has_immutability_policy": {
Type: schema.TypeBool,
Computed: true,
},

"has_legal_hold": {
Type: schema.TypeBool,
Computed: true,
},
},
}
}

func dataSourceArmStorageContainerRead(d *schema.ResourceData, meta interface{}) error {
storageClient := meta.(*clients.Client).Storage
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

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

account, err := storageClient.FindAccount(ctx, accountName)
if err != nil {
return fmt.Errorf("Error retrieving Account %q for Container %q: %s", accountName, containerName, err)
}
if account == nil {
return fmt.Errorf("Unable to locate Account %q for Storage Container %q", accountName, containerName)
}

client, err := storageClient.ContainersClient(ctx, *account)
if err != nil {
return fmt.Errorf("Error building Containers Client for Storage Account %q (Resource Group %q): %s", accountName, account.ResourceGroup, err)
}

d.SetId(client.GetResourceID(accountName, containerName))

props, err := client.GetProperties(ctx, accountName, containerName)
if err != nil {
if utils.ResponseWasNotFound(props.Response) {
return fmt.Errorf("Container %q was not found in Account %q / Resource Group %q", containerName, accountName, account.ResourceGroup)
}

return fmt.Errorf("Error retrieving Container %q (Account %q / Resource Group %q): %s", containerName, accountName, account.ResourceGroup, err)
}

d.Set("name", containerName)

d.Set("storage_account_name", accountName)

d.Set("container_access_type", flattenStorageContainerAccessLevel(props.AccessLevel))

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

d.Set("has_immutability_policy", props.HasImmutabilityPolicy)
d.Set("has_legal_hold", props.HasLegalHold)

return nil
}
1 change: 1 addition & 0 deletions azurerm/internal/services/storage/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func (r Registration) SupportedDataSources() map[string]*schema.Resource {
"azurerm_storage_account_blob_container_sas": dataSourceArmStorageAccountBlobContainerSharedAccessSignature(),
"azurerm_storage_account_sas": dataSourceArmStorageAccountSharedAccessSignature(),
"azurerm_storage_account": dataSourceArmStorageAccount(),
"azurerm_storage_container": dataSourceArmStorageContainer(),
minzhang28 marked this conversation as resolved.
Show resolved Hide resolved
"azurerm_storage_management_policy": dataSourceArmStorageManagementPolicy(),
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package tests

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/acceptance"
)

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

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acceptance.PreCheck(t) },
Providers: acceptance.SupportedProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAzureRMStorageContainer_basic(data),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(data.ResourceName, "container_access_type", "private"),
resource.TestCheckResourceAttr(data.ResourceName, "has_immutability_policy", "false"),
resource.TestCheckResourceAttr(data.ResourceName, "metadata.%", "2"),
resource.TestCheckResourceAttr(data.ResourceName, "metadata.k1", "v1"),
resource.TestCheckResourceAttr(data.ResourceName, "metadata.k2", "v2"),
),
},
},
})
}

func testAccDataSourceAzureRMStorageContainer_basic(data acceptance.TestData) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
name = "containerdstest-%s"
location = "%s"
}

resource "azurerm_storage_account" "test" {
name = "acctestsadsc%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_container" "test" {
name = "containerdstest-%s"
resource_group_name = "${azurerm_resource_group.test.name}"
storage_account_name = "${azurerm_storage_account.test.name}"
container_access_type = "private"
metadata = {
k1 = "v1"
k2 = "v2"
}
}

data "azurerm_storage_container" "test" {
name = azurerm_storage_container.test.name
storage_account_name = azurerm_storage_container.test.storage_account_name
}

`, data.RandomString, data.Locations.Primary, data.RandomString, data.RandomString)
}
4 changes: 4 additions & 0 deletions website/azurerm.erb
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,10 @@
<a href="/docs/providers/azurerm/d/storage_management_policy.html">azurerm_storage_management_policy</a>
</li>

<li>
<a href="/docs/providers/azurerm/d/storage_container.html">storage_container</a>
</li>

<li>
<a href="/docs/providers/azurerm/d/subnet.html">azurerm_subnet</a>
</li>
Expand Down
34 changes: 34 additions & 0 deletions website/docs/d/storage_container.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
---
subcategory: "Storage"
layout: "azurerm"
page_title: "Azure Resource Manager: azurerm_storage_container"
description: |-
Gets information about an existing Storage Container.
---

# Data Source: azurerm_storage_container

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

## Example Usage

```hcl
data "azurerm_storage_container" "example" {
name = "example-container-name"
storage_account_name = "example-storage-account-name"
}
```

## Argument Reference

The following arguments are supported:

* `name` - (Required) The name of the Container.
* `storage_account_name` - (Required) The name of the Storage Account where the Container was created.

## Attributes Reference

* `container_access_type` - The Access Level configured for this Container.
* `has_immutability_policy` - Is there an Immutability Policy configured on this Storage Container?
* `has_legal_hold` - Is there a Legal Hold configured on this Storage Container?
* `metadata` - A mapping of MetaData for this Container.