Skip to content

Commit

Permalink
New Data Source: azurerm_load_test (#26376)
Browse files Browse the repository at this point in the history
* Add Load Test Service Datasource

* Add docs

* Remove metadata.MarkAsGone

* Fix golancci-lint appendAssign
  • Loading branch information
nnstt1 authored Jun 21, 2024
1 parent 6acc23c commit 4f7a907
Show file tree
Hide file tree
Showing 4 changed files with 333 additions and 1 deletion.
173 changes: 173 additions & 0 deletions internal/services/loadtestservice/load_test_data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package loadtestservice

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/identity"
"github.com/hashicorp/go-azure-helpers/resourcemanager/location"
"github.com/hashicorp/go-azure-sdk/resource-manager/loadtestservice/2022-12-01/loadtests"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-azurerm/internal/sdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tags"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
)

type LoadTestDataSource struct{}

var _ sdk.DataSource = LoadTestDataSource{}

type LoadTestDataSourceModel struct {
DataPlaneURI string `tfschema:"data_plane_uri"`
Description string `tfschema:"description"`
Encryption []LoadTestEncryption `tfschema:"encryption"`
Identity []identity.ModelSystemAssignedUserAssigned `tfschema:"identity"`
Location string `tfschema:"location"`
Name string `tfschema:"name"`
ResourceGroupName string `tfschema:"resource_group_name"`
Tags map[string]string `tfschema:"tags"`
}

func (r LoadTestDataSource) ModelObject() interface{} {
return &LoadTestDataSourceModel{}
}

func (r LoadTestDataSource) ResourceType() string {
return "azurerm_load_test"
}

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

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

func (r LoadTestDataSource) Attributes() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{
"data_plane_uri": {
Type: pluginsdk.TypeString,
Computed: true,
},

"location": commonschema.LocationComputed(),

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

"identity": commonschema.SystemAssignedUserAssignedIdentityComputed(),

"encryption": {
Type: pluginsdk.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"key_url": {
Type: pluginsdk.TypeString,
Computed: true,
},
"identity": {
Type: pluginsdk.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"type": {
Type: pluginsdk.TypeString,
Computed: true,
},
"identity_id": {
Type: pluginsdk.TypeString,
Computed: true,
},
},
},
},
},
},
},

"tags": tags.SchemaDataSource(),
}
}

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

var loadTest LoadTestDataSourceModel
if err := metadata.Decode(&loadTest); err != nil {
return err
}

id := loadtests.NewLoadTestID(subscriptionId, loadTest.ResourceGroupName, loadTest.Name)

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

loadTest.Name = id.LoadTestName
loadTest.ResourceGroupName = id.ResourceGroupName

if model := existing.Model; model != nil {
identity, err := identity.FlattenLegacySystemAndUserAssignedMapToModel(model.Identity)
if err != nil {
return fmt.Errorf("flattening Legacy SystemAndUserAssigned Identity: %+v", err)
}
loadTest.Identity = identity

loadTest.Location = location.Normalize(model.Location)
loadTest.Tags = pointer.From(model.Tags)

if model.Properties == nil {
model.Properties = &loadtests.LoadTestProperties{}
}

loadTest.DataPlaneURI = pointer.From(model.Properties.DataPlaneURI)
loadTest.Description = pointer.From(model.Properties.Description)

if encryption := model.Properties.Encryption; encryption != nil {
outputEncryption := make([]LoadTestEncryption, 0)
outputEncryption = append(outputEncryption, LoadTestEncryption{
KeyURL: pointer.From(encryption.KeyUrl),
Identity: []LoadTestEncryptionIdentity{},
})
loadTest.Encryption = outputEncryption

if encryptionIdentity := encryption.Identity; encryptionIdentity != nil {
loadTest.Encryption[0].Identity = append(loadTest.Encryption[0].Identity, LoadTestEncryptionIdentity{
IdentityID: pointer.From(encryptionIdentity.ResourceId),
})

if encryptionIdentity.Type != nil {
loadTest.Encryption[0].Identity[0].Type = string(pointer.From(encryptionIdentity.Type))
}
}
}
}

metadata.SetID(id)

return metadata.Encode(&loadTest)
},
}
}
73 changes: 73 additions & 0 deletions internal/services/loadtestservice/load_test_data_source_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package loadtestservice_test

import (
"fmt"
"testing"

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

type LoadTestTestDataSource struct{}

func TestAccLoadTestDataSource_complete(t *testing.T) {
data := acceptance.BuildTestData(t, "data.azurerm_load_test", "test")
d := LoadTestTestDataSource{}

data.DataSourceTest(t, []acceptance.TestStep{
{
Config: d.complete(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).Key("description").HasValue("Description for the Load Test"),
check.That(data.ResourceName).Key("identity.#").HasValue("1"),
check.That(data.ResourceName).Key("identity.0.type").HasValue("SystemAssigned, UserAssigned"),
check.That(data.ResourceName).Key("location").HasValue(data.Locations.Primary),
check.That(data.ResourceName).Key("tags.%").HasValue("2"),
check.That(data.ResourceName).Key("tags.environment").HasValue("terraform-acctests"),
check.That(data.ResourceName).Key("tags.some_key").HasValue("some-value"),
),
},
})
}

func TestAccLoadTestDataSource_encryption(t *testing.T) {
data := acceptance.BuildTestData(t, "data.azurerm_load_test", "test")
d := LoadTestTestDataSource{}

data.DataSourceTest(t, []acceptance.TestStep{
{
Config: d.encryption(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).Key("identity.#").HasValue("1"),
check.That(data.ResourceName).Key("identity.0.type").HasValue("UserAssigned"),
check.That(data.ResourceName).Key("location").HasValue(data.Locations.Primary),
check.That(data.ResourceName).Key("encryption.0.identity.0.type").HasValue("UserAssigned"),
),
},
})
}

func (d LoadTestTestDataSource) complete(data acceptance.TestData) string {
return fmt.Sprintf(`
%s
data azurerm_load_test test {
name = azurerm_load_test.test.name
resource_group_name = azurerm_load_test.test.resource_group_name
}
`, LoadTestTestResource{}.complete(data))
}

func (d LoadTestTestDataSource) encryption(data acceptance.TestData) string {
return fmt.Sprintf(`
%s
data azurerm_load_test test {
name = azurerm_load_test.test.name
resource_group_name = azurerm_load_test.test.resource_group_name
}
`, LoadTestTestResource{}.encryption(data))
}
4 changes: 3 additions & 1 deletion internal/services/loadtestservice/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ func (r Registration) Name() string {
}

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

func (r Registration) Resources() []sdk.Resource {
Expand Down
84 changes: 84 additions & 0 deletions website/docs/d/load_test.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
---
subcategory: "Load Test"
layout: "azurerm"
page_title: "Azure Resource Manager: azurerm_load_test"
description: |-
Gets information about an existing Load Test Service.
---

# Data Source: azurerm_load_test

Use this data source to access information about a Load Test Service.

## Example Usage

```hcl
data "azurerm_load_test" "example" {
resource_group_name = "example-resources"
name = "example-load-test"
}
output "load_test_id" {
value = data.azurerm_load_test.example.id
}
```

## Arguments Reference

The following arguments are supported:

* `name` - The name of the Load Test Service.

* `resource_group_name` - The name of the Resource Group in which the Load Test Service exists.

## Attributes Reference

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

* `id` - The ID of the Load Test Service.

* `data_plane_uri` - Resource data plane URI.

* `description` - Description of the resource.

* `encryption` - An `encryption` block as defined below.

* `identity` - An `identity` block as defined below.

* `location` - The Azure Region where the Load Test exists.

* `tags` - A mapping of tags assigned to the Load Test Service.

---

A `identity` block exports the following:

* `identity_ids` - The list of the User Assigned Identity IDs that is assigned to this Load Test Service.

* `principal_id` - The Principal ID for the System-Assigned Managed Identity assigned to this Load Test Service.

* `tenant_id` - The Tenant ID for the System-Assigned Managed Identity assigned to this Load Test Service.

* `type` - Type of Managed Service Identity.

---

A `encryption` block exports the following:

* `identity` - An `identity` block as defined below.

* `key_url` - The URI specifying the Key vault and key to be used to encrypt data in this resource.

---

A `identity` block for `encryption` exports the following:

* `identity_id` - The User Assigned Identity ID that is assigned to this Load Test Encryption.

* `type` - Type of Managed Service Identity that is assigned to this Load Test Encryption.

## 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 Load Test Service.

0 comments on commit 4f7a907

Please sign in to comment.