Skip to content

Commit

Permalink
added data source for storage container
Browse files Browse the repository at this point in the history
  • Loading branch information
minzhang28 committed Jan 12, 2020
1 parent 28abdf1 commit 46c049c
Show file tree
Hide file tree
Showing 6 changed files with 255 additions and 0 deletions.
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)

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

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

"storage_account_name": {
Type: schema.TypeString,
Computed: 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,
},

"resource_group_name": azure.SchemaResourceGroupNameDeprecated(),

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

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

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("")
return nil
}

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("")
return nil
}

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)

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

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),
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),
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}"
}
`, 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 @@ -446,6 +446,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>
</li>

<li>
<a href="/docs/providers/azurerm/d/subnet.html">azurerm_subnet</a>
</li>
Expand Down
46 changes: 46 additions & 0 deletions website/docs/d/storage_account_container.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
---
subcategory: "Storage"
layout: "azurerm"
page_title: "Azure Resource Manager: azurerm_storage_container"
sidebar_current: "docs-azurerm-datasource-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"
}
}
data "azurerm_storage_container" "example" {
storage_container_id = "${azurerm_storage_container.test.id}"
}
```

## Argument Reference

The following arguments are supported:

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

## 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.

0 comments on commit 46c049c

Please sign in to comment.