-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added data source for storage container
- Loading branch information
1 parent
28abdf1
commit 46c049c
Showing
6 changed files
with
255 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 131 additions & 0 deletions
131
azurerm/internal/services/storage/data_source_storage_container.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
azurerm/internal/services/storage/tests/data_source_storage_container_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |