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 3 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
5 changes: 5 additions & 0 deletions azurerm/internal/services/storage/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type Client struct {
FileSystemsClient *filesystems.Client
ManagementPoliciesClient storage.ManagementPoliciesClient
BlobServicesClient storage.BlobServicesClient
ContainerClient storage.BlobContainersClient
environment az.Environment
}

Expand All @@ -40,13 +41,17 @@ func NewClient(options *common.ClientOptions) *Client {
blobServicesClient := storage.NewBlobServicesClientWithBaseURI(options.ResourceManagerEndpoint, options.SubscriptionId)
options.ConfigureClient(&blobServicesClient.Client, options.ResourceManagerAuthorizer)

containerClient := storage.NewBlobContainersClientWithBaseURI(options.ResourceManagerEndpoint, options.SubscriptionId)
options.ConfigureClient(&containerClient.Client, options.ResourceManagerAuthorizer)
minzhang28 marked this conversation as resolved.
Show resolved Hide resolved

// TODO: switch Storage Containers to using the storage.BlobContainersClient
// (which should fix #2977) when the storage clients have been moved in here
return &Client{
AccountsClient: &accountsClient,
FileSystemsClient: &fileSystemsClient,
ManagementPoliciesClient: managementPoliciesClient,
BlobServicesClient: blobServicesClient,
ContainerClient: containerClient,
environment: options.Environment,
}
}
Expand Down
131 changes: 131 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,131 @@
package storage

import (
"fmt"
"log"
"time"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"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"
"github.com/tombuildsstuff/giovanni/storage/2018-11-09/blob/containers"
)

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

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

Schema: map[string]*schema.Schema{
"storage_container_id": {
Type: schema.TypeString,
Required: true,
},
minzhang28 marked this conversation as resolved.
Show resolved Hide resolved

"name": {
Type: schema.TypeString,
Computed: true,
minzhang28 marked this conversation as resolved.
Show resolved Hide resolved
},

"storage_account_name": {
Type: schema.TypeString,
Computed: true,
minzhang28 marked this conversation as resolved.
Show resolved Hide resolved
},

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

"resource_group_name": azure.SchemaResourceGroupNameDeprecated(),
minzhang28 marked this conversation as resolved.
Show resolved Hide resolved

"properties": {
Type: schema.TypeMap,
Computed: true,
Deprecated: "This field will be removed in version 2.0 of the Azure Provider",
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
minzhang28 marked this conversation as resolved.
Show resolved Hide resolved
},
}
}

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

id, err := containers.ParseResourceID(d.Get("storage_container_id").(string))
minzhang28 marked this conversation as resolved.
Show resolved Hide resolved

if err != nil {
return err
}

account, err := storageClient.FindAccount(ctx, id.AccountName)
if err != nil {
return fmt.Errorf("Error retrieving Account %q for Container %q: %s", id.AccountName, id.ContainerName, err)
}
if account == nil {
log.Printf("[DEBUG] Unable to locate Account %q for Storage Container %q - assuming removed & removing from state", id.AccountName, id.ContainerName)
d.SetId("")
minzhang28 marked this conversation as resolved.
Show resolved Hide resolved
return nil
minzhang28 marked this conversation as resolved.
Show resolved Hide resolved
}

client, err := storageClient.ContainersClient(ctx, *account)
d.SetId(client.GetResourceID(id.AccountName, id.ContainerName))

if err != nil {
return fmt.Errorf("Error building Containers Client for Storage Account %q (Resource Group %q): %s", id.AccountName, account.ResourceGroup, err)
}

props, err := client.GetProperties(ctx, id.AccountName, id.ContainerName)
if err != nil {
if utils.ResponseWasNotFound(props.Response) {
log.Printf("[DEBUG] Container %q was not found in Account %q / Resource Group %q - assuming removed & removing from state", id.ContainerName, id.AccountName, account.ResourceGroup)
d.SetId("")
minzhang28 marked this conversation as resolved.
Show resolved Hide resolved
return nil
minzhang28 marked this conversation as resolved.
Show resolved Hide resolved
}

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

d.Set("name", id.ContainerName)

d.Set("storage_account_name", id.AccountName)

d.Set("resource_group_name", account.ResourceGroup)
minzhang28 marked this conversation as resolved.
Show resolved Hide resolved

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

if err := d.Set("properties", flattenStorageContainerProperties(props)); err != nil {
return fmt.Errorf("Error setting `properties`: %+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 @@ -18,6 +18,7 @@ func (r Registration) SupportedDataSources() map[string]*schema.Resource {
"azurerm_storage_account_sas": dataSourceArmStorageAccountSharedAccessSignature(),
"azurerm_storage_account": dataSourceArmStorageAccount(),
"azurerm_storage_management_policy": dataSourceArmStorageManagementPolicy(),
"azurerm_storage_container": dataSourceArmStorageContainer(),
minzhang28 marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
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, "name", "containerdstest-"+data.RandomString),
minzhang28 marked this conversation as resolved.
Show resolved Hide resolved
resource.TestCheckResourceAttr(data.ResourceName, "container_access_type", "private"),
resource.TestCheckResourceAttr(data.ResourceName, "has_immutability_policy", "false"),
resource.TestCheckResourceAttr(data.ResourceName, "resource_group_name", "containerdstest-"+data.RandomString),
resource.TestCheckResourceAttr(data.ResourceName, "storage_account_name", "acctestsadsc"+data.RandomString),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since we need this field to be set in order to look it up, we can remove this:

Suggested change
resource.TestCheckResourceAttr(data.ResourceName, "storage_account_name", "acctestsadsc"+data.RandomString),

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like this was missed - can we remove this?

resource.TestCheckResourceAttr(data.ResourceName, "properties.%", "4"),
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" {
storage_container_id = "${azurerm_storage_container.test.id}"
minzhang28 marked this conversation as resolved.
Show resolved Hide resolved
}

`, 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_account_container.html">storage_account_container</a>
minzhang28 marked this conversation as resolved.
Show resolved Hide resolved
</li>

<li>
<a href="/docs/providers/azurerm/d/subnet.html">azurerm_subnet</a>
</li>
Expand Down
45 changes: 45 additions & 0 deletions website/docs/d/storage_account_container.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
---
minzhang28 marked this conversation as resolved.
Show resolved Hide resolved
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

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 = {
key1 = "value1"
key2 = "value2"
}
}
minzhang28 marked this conversation as resolved.
Show resolved Hide resolved

data "azurerm_storage_container" "example" {
storage_container_id = "${azurerm_storage_container.test.id}"
minzhang28 marked this conversation as resolved.
Show resolved Hide resolved
}
```

## Argument Reference

The following arguments are supported:

* `storage_container_id` - (Required) Specifies the id of the storage container.

minzhang28 marked this conversation as resolved.
Show resolved Hide resolved
## 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?
* `storage_account_name` - The name of the Storage Account where the Container is created.
* `metadata` - A mapping of MetaData for this Container.