Skip to content

Commit

Permalink
New Data Source: azurerm_stack_hci_storage_path (#28602)
Browse files Browse the repository at this point in the history
  • Loading branch information
teowa authored Feb 11, 2025
1 parent 7db4561 commit 9b4871b
Show file tree
Hide file tree
Showing 4 changed files with 216 additions and 0 deletions.
1 change: 1 addition & 0 deletions internal/services/azurestackhci/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func (r Registration) SupportedResources() map[string]*pluginsdk.Resource {
func (r Registration) DataSources() []sdk.DataSource {
return []sdk.DataSource{
StackHCIClusterDataSource{},
StackHCIStoragePathDataSource{},
}
}

Expand Down
120 changes: 120 additions & 0 deletions internal/services/azurestackhci/stack_hci_storage_path_data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package azurestackhci

import (
"context"
"fmt"
"regexp"
"time"

"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
"github.com/hashicorp/go-azure-helpers/resourcemanager/location"
"github.com/hashicorp/go-azure-helpers/resourcemanager/tags"
"github.com/hashicorp/go-azure-sdk/resource-manager/azurestackhci/2024-01-01/storagecontainers"
"github.com/hashicorp/go-azure-sdk/resource-manager/extendedlocation/2021-08-15/customlocations"
"github.com/hashicorp/terraform-provider-azurerm/internal/sdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation"
)

type StackHCIStoragePathDataSource struct{}

var _ sdk.DataSource = StackHCIStoragePathDataSource{}

type StackHCIStoragePathDataSourceModel struct {
Name string `tfschema:"name"`
ResourceGroupName string `tfschema:"resource_group_name"`
Location string `tfschema:"location"`
CustomLocationId string `tfschema:"custom_location_id"`
Path string `tfschema:"path"`
Tags map[string]interface{} `tfschema:"tags"`
}

func (r StackHCIStoragePathDataSource) ResourceType() string {
return "azurerm_stack_hci_storage_path"
}

func (r StackHCIStoragePathDataSource) ModelObject() interface{} {
return &StackHCIStoragePathDataSourceModel{}
}

func (r StackHCIStoragePathDataSource) Arguments() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validation.StringMatch(
regexp.MustCompile(`^[a-zA-Z0-9][\-\.\_a-zA-Z0-9]{1,78}[a-zA-Z0-9]$`),
"name must begin and end with an alphanumeric character, be between 3 and 80 characters in length and can only contain alphanumeric characters, hyphens, periods or underscores.",
),
},

"resource_group_name": commonschema.ResourceGroupNameForDataSource(),
}
}

func (r StackHCIStoragePathDataSource) Attributes() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{
"location": commonschema.LocationComputed(),

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

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

"tags": commonschema.TagsDataSource(),
}
}

func (r StackHCIStoragePathDataSource) Read() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 5 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.AzureStackHCI.StorageContainers

var state StackHCIStoragePathDataSourceModel
if err := metadata.Decode(&state); err != nil {
return fmt.Errorf("decoding: %+v", err)
}

subscriptionId := metadata.Client.Account.SubscriptionId
id := storagecontainers.NewStorageContainerID(subscriptionId, state.ResourceGroupName, state.Name)

resp, err := client.Get(ctx, id)
if err != nil {
if response.WasNotFound(resp.HttpResponse) {
return fmt.Errorf("%s was not found", id)
}

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

if model := resp.Model; model != nil {
state.Location = location.Normalize(model.Location)
state.Tags = tags.Flatten(model.Tags)

if model.ExtendedLocation != nil && model.ExtendedLocation.Name != nil {
customLocationId, err := customlocations.ParseCustomLocationIDInsensitively(*model.ExtendedLocation.Name)
if err != nil {
return err
}

state.CustomLocationId = customLocationId.ID()
}

if props := model.Properties; props != nil {
state.Path = props.Path
}
}

metadata.SetID(id)

return metadata.Encode(&state)
},
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package azurestackhci_test

import (
"fmt"
"os"
"testing"

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

type StackHCIStoragePathDataSource struct{}

func TestAccStackHCIStoragePathDataSource_basic(t *testing.T) {
if os.Getenv(customLocationIdEnv) == "" {
t.Skipf("skipping since %q has not been specified", customLocationIdEnv)
}

data := acceptance.BuildTestData(t, "data.azurerm_stack_hci_storage_path", "test")
d := StackHCIStoragePathDataSource{}

data.DataSourceTestInSequence(t, []acceptance.TestStep{
{
Config: d.basic(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).Key("location").IsNotEmpty(),
check.That(data.ResourceName).Key("custom_location_id").IsNotEmpty(),
check.That(data.ResourceName).Key("path").IsNotEmpty(),
),
},
})
}

func (d StackHCIStoragePathDataSource) basic(data acceptance.TestData) string {
return fmt.Sprintf(`
%s
data "azurerm_stack_hci_storage_path" "test" {
name = azurerm_stack_hci_storage_path.test.name
resource_group_name = azurerm_stack_hci_storage_path.test.resource_group_name
}
`, StackHCIStoragePathResource{}.complete(data))
}
52 changes: 52 additions & 0 deletions website/docs/d/stack_hci_storage_path.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
---
subcategory: "Azure Stack HCI"
layout: "azurerm"
page_title: "Azure Resource Manager: Data Source: azurerm_stack_hci_storage_path"
description: |-
Gets information about an existing Stack HCI Storage Path.
---

# Data Source: azurerm_stack_hci_storage_path

Use this data source to access information about an existing Stack HCI Storage Path.

## Example Usage

```hcl
data "azurerm_stack_hci_storage_path" "example" {
name = "example-hci-storage-path-name"
resource_group_name = "example-rg"
}
output "id" {
value = data.azurerm_stack_hci_storage_path.example.id
}
```

## Arguments Reference

The following arguments are supported:

* `name` - (Required) The name of this Stack HCI Storage Path.

* `resource_group_name` - (Required) The name of the Resource Group where the Stack HCI Storage Path exists.

## Attributes Reference

In addition to the Arguments listed above - the following Attributes are exported:

* `id` - The ID of the Stack HCI Storage Path.

* `custom_location_id` - The ID of the Custom Location where the Stack HCI Storage Path exists.

* `location` - The Azure Region where the Stack HCI Storage Path exists.

* `path` - The file path on the disk where the Stack HCI Storage Path was created.

* `tags` - A mapping of tags assigned to the Stack HCI Storage Path.

## 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 Stack HCI Storage Path.

0 comments on commit 9b4871b

Please sign in to comment.