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_blob_content #27492

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion .github/labeler-issue-triage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ service/spring:
- '### (|New or )Affected Resource\(s\)\/Data Source\(s\)((.|\n)*)azurerm_(spring_cloud_accelerator\W+|spring_cloud_active_deployment\W+|spring_cloud_api_portal\W+|spring_cloud_api_portal_custom_domain\W+|spring_cloud_app\W+|spring_cloud_app_cosmosdb_association\W+|spring_cloud_app_dynamics_application_performance_monitoring\W+|spring_cloud_app_mysql_association\W+|spring_cloud_app_redis_association\W+|spring_cloud_application_insights_application_performance_monitoring\W+|spring_cloud_application_live_view\W+|spring_cloud_build_deployment\W+|spring_cloud_build_pack_binding\W+|spring_cloud_builder\W+|spring_cloud_certificate\W+|spring_cloud_configuration_service\W+|spring_cloud_container_deployment\W+|spring_cloud_custom_domain\W+|spring_cloud_customized_accelerator\W+|spring_cloud_dev_tool_portal\W+|spring_cloud_dynatrace_application_performance_monitoring\W+|spring_cloud_elastic_application_performance_monitoring\W+|spring_cloud_gateway\W+|spring_cloud_gateway_custom_domain\W+|spring_cloud_gateway_route_config\W+|spring_cloud_java_deployment\W+|spring_cloud_new_relic_application_performance_monitoring\W+|spring_cloud_service\W+|spring_cloud_storage\W+)((.|\n)*)###'

service/storage:
- '### (|New or )Affected Resource\(s\)\/Data Source\(s\)((.|\n)*)azurerm_(storage_account\W+|storage_account_blob_container_sas\W+|storage_account_customer_managed_key\W+|storage_account_local_user\W+|storage_account_network_rules\W+|storage_account_sas\W+|storage_blob\W+|storage_blob_inventory_policy\W+|storage_container\W+|storage_container_immutability_policy\W+|storage_containers\W+|storage_data_lake_gen2_filesystem\W+|storage_data_lake_gen2_path\W+|storage_encryption_scope\W+|storage_management_policy\W+|storage_object_replication\W+|storage_queue\W+|storage_share\W+|storage_share_directory\W+|storage_share_file\W+|storage_sync\W+|storage_sync_cloud_endpoint\W+|storage_sync_group\W+|storage_sync_server_endpoint\W+|storage_table\W+|storage_table\W+|storage_table_entities\W+|storage_table_entity\W+)((.|\n)*)###'
- '### (|New or )Affected Resource\(s\)\/Data Source\(s\)((.|\n)*)azurerm_(storage_account\W+|storage_account_blob_container_sas\W+|storage_account_customer_managed_key\W+|storage_account_local_user\W+|storage_account_network_rules\W+|storage_account_sas\W+|storage_blob\W+|storage_blob_content\W+|storage_blob_inventory_policy\W+|storage_container\W+|storage_container_immutability_policy\W+|storage_containers\W+|storage_data_lake_gen2_filesystem\W+|storage_data_lake_gen2_path\W+|storage_encryption_scope\W+|storage_management_policy\W+|storage_object_replication\W+|storage_queue\W+|storage_share\W+|storage_share_directory\W+|storage_share_file\W+|storage_sync\W+|storage_sync_cloud_endpoint\W+|storage_sync_group\W+|storage_sync_server_endpoint\W+|storage_table\W+|storage_table\W+|storage_table_entities\W+|storage_table_entity\W+)((.|\n)*)###'

service/storagemover:
- '### (|New or )Affected Resource\(s\)\/Data Source\(s\)((.|\n)*)azurerm_storage_mover((.|\n)*)###'
Expand Down
1 change: 1 addition & 0 deletions internal/services/storage/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func (r Registration) SupportedDataSources() map[string]*pluginsdk.Resource {
"azurerm_storage_account_sas": dataSourceStorageAccountSharedAccessSignature(),
"azurerm_storage_account": dataSourceStorageAccount(),
"azurerm_storage_blob": dataSourceStorageBlob(),
"azurerm_storage_blob_content": dataSourceStorageBlobContent(),
"azurerm_storage_container": dataSourceStorageContainer(),
"azurerm_storage_encryption_scope": dataSourceStorageEncryptionScope(),
"azurerm_storage_management_policy": dataSourceStorageManagementPolicy(),
Expand Down
107 changes: 107 additions & 0 deletions internal/services/storage/storage_blob_content_data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package storage

import (
"fmt"
"log"
"time"

"github.com/hashicorp/go-azure-helpers/lang/response"
"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/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/blob/blobs"
)

func dataSourceStorageBlobContent() *pluginsdk.Resource {
return &pluginsdk.Resource{
Read: dataSourceStorageBlobContentRead,

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

"storage_container_name": {
Type: pluginsdk.TypeString,
Required: true,
},

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

func dataSourceStorageBlobContentRead(d *pluginsdk.ResourceData, meta interface{}) error {
storageClient := meta.(*clients.Client).Storage
subscriptionId := meta.(*clients.Client).Account.SubscriptionId
ctx, cancel := timeouts.ForCreate(meta.(*clients.Client).StopContext, d)
defer cancel()

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

account, err := storageClient.FindAccount(ctx, subscriptionId, accountName)
if err != nil {
return fmt.Errorf("retrieving Account %q for Blob %q (Container %q): %v", accountName, name, containerName, err)
}
if account == nil {
return fmt.Errorf("locating Storage Account %q", accountName)
}

blobsClient, err := storageClient.BlobsDataPlaneClient(ctx, *account, storageClient.DataPlaneOperationSupportingAnyAuthMethod())
if err != nil {
return fmt.Errorf("building Blobs Client: %v", err)
}

blobEndpoint, err := account.DataPlaneEndpoint(client.EndpointTypeBlob)
if err != nil {
return fmt.Errorf("retrieving the blob data plane endpoint: %v", err)
}

accountId, err := accounts.ParseAccountID(*blobEndpoint, storageClient.StorageDomainSuffix)
if err != nil {
return fmt.Errorf("parsing Account ID: %v", err)
}

id := blobs.NewBlobID(*accountId, containerName, name)

log.Printf("[INFO] Retrieving %s", id)
input := blobs.GetInput{}
content, err := blobsClient.Get(ctx, containerName, name, input)
if err != nil {
if response.WasNotFound(content.HttpResponse) {
return fmt.Errorf("%s was not found", id)
}

return fmt.Errorf("retrieving content for %s: %v", id, err)
}

d.Set("content", string(*content.Contents))

d.Set("name", name)
d.Set("storage_container_name", containerName)
d.Set("storage_account_name", accountName)

d.SetId(id.ID())

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

package storage_test

import (
"fmt"
"os"
"testing"

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

type StorageBlobContentDataSource struct{}

func TestAccDataSourceStorageBlobContent_basic(t *testing.T) {
sourceBlob, err := os.CreateTemp("", "")
if err != nil {
t.Fatalf("Failed to create local source blob file")
}

testString := "This is the test string."
_, err = sourceBlob.WriteString(testString)
if err != nil {
t.Fatalf("Failed to write test string to source blob file")
}

data := acceptance.BuildTestData(t, "data.azurerm_storage_blob_content", "test")

data.DataSourceTest(t, []acceptance.TestStep{
{
Config: StorageBlobContentDataSource{}.basicWithDataSource(data, sourceBlob.Name()),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).Key("content").HasValue(testString),
),
},
})
}

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

resource "azurerm_resource_group" "test" {
name = "blobdstest-%[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_encryption_scope" "test" {
name = "acctestEScontainer%[3]d"
storage_account_id = azurerm_storage_account.test.id
source = "Microsoft.Storage"
}

resource "azurerm_storage_container" "test" {
name = "containerdstest-%[1]s"
storage_account_name = "${azurerm_storage_account.test.name}"
container_access_type = "private"
}

resource "azurerm_storage_blob" "test" {
name = "example.vhd"
storage_account_name = azurerm_storage_account.test.name
storage_container_name = azurerm_storage_container.test.name
encryption_scope = azurerm_storage_encryption_scope.test.name
type = "Block"
source = "%[4]s"

metadata = {
k1 = "v1"
k2 = "v2"
}
}
`, data.RandomString, data.Locations.Primary, data.RandomInteger, fileName)
}

func (d StorageBlobContentDataSource) basicWithDataSource(data acceptance.TestData, fileName string) string {
config := d.basic(data, fileName)
return fmt.Sprintf(`
%s

data "azurerm_storage_blob_content" "test" {
name = azurerm_storage_blob.test.name
storage_account_name = azurerm_storage_blob.test.storage_account_name
storage_container_name = azurerm_storage_blob.test.storage_container_name
}
`, config)
}
43 changes: 43 additions & 0 deletions website/docs/d/storage_blob_content.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
---
subcategory: "Storage"
layout: "azurerm"
page_title: "Azure Resource Manager: azurerm_storage_blob_content"
description: |-
Gets the content of an existing Storage Blob.
---

# Data Source: azurerm_storage_blob

Use this data source to access the content of an existing Storage Blob.

## Example Usage

```hcl
data "azurerm_storage_blob_content" "example" {
name = "example-blob-name"
storage_account_name = "example-storage-account-name"
storage_container_name = "example-storage-container-name"
}
```

## Argument Reference

The following arguments are supported:

* `name` - The name of the Blob.

* `storage_account_name` - The name of the Storage Account where the Container exists.

* `storage_container_name` - The name of the Storage Container where the Blob exists.

## Attributes Reference

* `id` - The ID of the storage blob.

* `content` - The URL of the storage blob.

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