Skip to content

Commit

Permalink
new data source azurerm_elastic_san
Browse files Browse the repository at this point in the history
  • Loading branch information
teowa committed Apr 23, 2024
1 parent 3355bb9 commit 07c5d95
Show file tree
Hide file tree
Showing 4 changed files with 281 additions and 1 deletion.
156 changes: 156 additions & 0 deletions internal/services/elasticsan/elastic_san_data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
package elasticsan

import (
"context"
"fmt"
"time"

"github.com/hashicorp/go-azure-helpers/lang/pointer"
"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-helpers/resourcemanager/zones"
"github.com/hashicorp/go-azure-sdk/resource-manager/elasticsan/2023-01-01/elasticsans"
"github.com/hashicorp/terraform-provider-azurerm/internal/sdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/elasticsan/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
)

type ElasticSANDataSource struct{}

var _ sdk.DataSource = ElasticSANDataSource{}

func (r ElasticSANDataSource) ResourceType() string {
return "azurerm_elastic_san"
}

func (r ElasticSANDataSource) ModelObject() interface{} {
return &ElasticSANResourceModel{}
}

func (r ElasticSANDataSource) Arguments() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validate.ElasticSanName,
},

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

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

"zones": commonschema.ZonesMultipleComputed(),

"base_size_in_tib": {
Computed: true,
Type: pluginsdk.TypeInt,
},

"extended_size_in_tib": {
Computed: true,
Type: pluginsdk.TypeInt,
},

"sku": {
Computed: true,
Type: pluginsdk.TypeList,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"name": {
Computed: true,
Type: pluginsdk.TypeString,
},

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

"total_iops": {
Computed: true,
Type: pluginsdk.TypeInt,
},

"total_mbps": {
Computed: true,
Type: pluginsdk.TypeInt,
},

"total_size_in_tib": {
Computed: true,
Type: pluginsdk.TypeInt,
},

"total_volume_size_in_gib": {
Computed: true,
Type: pluginsdk.TypeInt,
},

"volume_group_count": {
Computed: true,
Type: pluginsdk.TypeInt,
},

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

func (r ElasticSANDataSource) Read() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 5 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.ElasticSan.ElasticSans
subscriptionId := metadata.Client.Account.SubscriptionId

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

id := elasticsans.NewElasticSanID(subscriptionId, model.ResourceGroupName, model.Name)

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

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

state := ElasticSANResourceModel{
Name: model.Name,
ResourceGroupName: model.ResourceGroupName,
}

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

prop := model.Properties
state.Sku = FlattenSku(prop.Sku)
state.Zones = zones.Flatten(prop.AvailabilityZones)
state.BaseSizeInTiB = int(prop.BaseSizeTiB)
state.ExtendedSizeInTiB = int(prop.ExtendedCapacitySizeTiB)
state.TotalIops = int(pointer.From(prop.TotalIops))
state.TotalMBps = int(pointer.From(prop.TotalMBps))
state.TotalSizeInTiB = int(pointer.From(prop.TotalSizeTiB))
state.TotalVolumeSizeInGiB = int(pointer.From(prop.TotalVolumeSizeGiB))
state.VolumeGroupCount = int(pointer.From(prop.VolumeGroupCount))
}

metadata.SetID(id)

return metadata.Encode(&state)
},
}
}
48 changes: 48 additions & 0 deletions internal/services/elasticsan/elastic_san_data_source_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package elasticsan_test

import (
"fmt"
"testing"

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

type ElasticSANDataSource struct{}

func TestAccElasticSANDataSource_basic(t *testing.T) {
data := acceptance.BuildTestData(t, "data.azurerm_elastic_san", "test")
d := ElasticSANDataSource{}

data.DataSourceTestInSequence(t, []acceptance.TestStep{
{
Config: d.basic(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).Key("sku.#").HasValue("1"),
check.That(data.ResourceName).Key("sku.0.name").HasValue("Premium_LRS"),
check.That(data.ResourceName).Key("sku.0.tier").HasValue("Premium"),
check.That(data.ResourceName).Key("location").IsNotEmpty(),
check.That(data.ResourceName).Key("base_size_in_tib").HasValue("2"),
check.That(data.ResourceName).Key("extended_size_in_tib").HasValue("4"),
check.That(data.ResourceName).Key("zones.#").HasValue("2"),
check.That(data.ResourceName).Key("tags.%").HasValue("2"),
check.That(data.ResourceName).Key("total_iops").Exists(),
check.That(data.ResourceName).Key("total_mbps").Exists(),
check.That(data.ResourceName).Key("total_size_in_tib").Exists(),
check.That(data.ResourceName).Key("total_volume_size_in_gib").Exists(),
check.That(data.ResourceName).Key("volume_group_count").Exists(),
),
},
})
}

func (d ElasticSANDataSource) basic(data acceptance.TestData) string {
return fmt.Sprintf(`
%s
data "azurerm_elastic_san" "test" {
name = azurerm_elastic_san.test.name
resource_group_name = azurerm_elastic_san.test.resource_group_name
}
`, ElasticSANTestResource{}.complete(data))
}
4 changes: 3 additions & 1 deletion internal/services/elasticsan/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ func (Registration) Name() string {
}

func (Registration) DataSources() []sdk.DataSource {
return []sdk.DataSource{}
return []sdk.DataSource{
ElasticSANDataSource{},
}
}

func (Registration) Resources() []sdk.Resource {
Expand Down
74 changes: 74 additions & 0 deletions website/docs/d/elastic_san.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
---
subcategory: "Elastic SAN"
layout: "azurerm"
page_title: "Azure Resource Manager: Data Source: azurerm_elastic_san"
description: |-
Gets information about an existing Elastic SAN.
---

# Data Source: azurerm_elastic_san

Use this data source to access information about an existing Elastic SAN.

## Example Usage

```hcl
data "azurerm_elastic_san" "example" {
name = "existing"
resource_group_name = "existing"
}
output "id" {
value = data.azurerm_elastic_san.example.id
}
```

## Arguments Reference

The following arguments are supported:

* `name` - (Required) The name of this Elastic SAN.

* `resource_group_name` - (Required) The name of the Resource Group where the Elastic SAN exists.

## Attributes Reference

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

* `id` - The ID of the Elastic SAN.

* `base_size_in_tib` - The base size of the Elastic SAN resource in TiB.

* `extended_size_in_tib` - The base size of the Elastic SAN resource in TiB.

* `location` - The Azure Region where the Elastic SAN exists.

* `sku` - A `sku` block as defined below.

* `tags` - A mapping of tags assigned to the Elastic SAN.

* `total_iops` - Total Provisioned IOps of the Elastic SAN resource.

* `total_mbps` - Total Provisioned MBps Elastic SAN resource.

* `total_size_in_tib` - Total size of the Elastic SAN resource in TB.

* `total_volume_size_in_gib` - Total size of the provisioned Volumes in GiB.

* `volume_group_count` - Total number of volume groups in this Elastic SAN resource.

* `zones` - Logical zone for the Elastic SAN resource.

---

A `sku` block exports the following:

* `name` - The SKU name.

* `tier` - The SKU tier.

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

0 comments on commit 07c5d95

Please sign in to comment.