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_arc_resource_bridge_appliance #25731

Merged
merged 3 commits into from
May 27, 2024
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package arcresourcebridge

import (
"context"
"fmt"
"regexp"
"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-helpers/resourcemanager/tags"
"github.com/hashicorp/go-azure-sdk/resource-manager/resourceconnector/2022-10-27/appliances"
"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 ArcResourceBridgeApplianceDataSource struct{}

var _ sdk.DataSource = ArcResourceBridgeApplianceDataSource{}

type ApplianceDataSourceModel struct {
Name string `tfschema:"name"`
ResourceGroupName string `tfschema:"resource_group_name"`
Location string `tfschema:"location"`
Distro appliances.Distro `tfschema:"distro"`
Identity []identity.ModelSystemAssigned `tfschema:"identity"`
Provider appliances.Provider `tfschema:"infrastructure_provider"`
PublicKeyBase64 string `tfschema:"public_key_base64"`
Tags map[string]interface{} `tfschema:"tags"`
}

func (r ArcResourceBridgeApplianceDataSource) ResourceType() string {
return "azurerm_arc_resource_bridge_appliance"
}

func (r ArcResourceBridgeApplianceDataSource) ModelObject() interface{} {
return &ApplianceDataSourceModel{}
}

func (r ArcResourceBridgeApplianceDataSource) Arguments() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validation.All(
validation.StringLenBetween(1, 260),
validation.StringMatch(regexp.MustCompile(`[^+#%&'?/,%\\]+$`), "any of '+', '#', '%', '&', ''', '?', '/', ',', '%', '&', '\\', are not allowed"),
),
},

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

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

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

"identity": commonschema.SystemAssignedIdentityComputed(),

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

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

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

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

var model ApplianceDataSourceModel
teowa marked this conversation as resolved.
Show resolved Hide resolved
if err := metadata.Decode(&model); err != nil {
return fmt.Errorf("decoding: %+v", err)
}

id := appliances.NewApplianceID(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 := ApplianceDataSourceModel{
Name: model.Name,
ResourceGroupName: model.ResourceGroupName,
}
teowa marked this conversation as resolved.
Show resolved Hide resolved

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

if props := model.Properties; props != nil {
state.Distro = pointer.From(props.Distro)
state.PublicKeyBase64 = pointer.From(props.PublicKey)

if infraConfig := props.InfrastructureConfig; infraConfig != nil {
state.Provider = pointer.From(infraConfig.Provider)
}
}
}

metadata.SetID(id)

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

import (
"fmt"
"testing"

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

type ArcResourceBridgeApplianceDataSource struct{}

func TestAccArcResourceBridgeApplianceDataSource_basic(t *testing.T) {
data := acceptance.BuildTestData(t, "data.azurerm_arc_resource_bridge_appliance", "test")
d := ArcResourceBridgeApplianceDataSource{}

data.DataSourceTestInSequence(t, []acceptance.TestStep{
{
Config: d.basic(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).Key("location").IsNotEmpty(),
check.That(data.ResourceName).Key("distro").IsNotEmpty(),
check.That(data.ResourceName).Key("infrastructure_provider").IsNotEmpty(),
check.That(data.ResourceName).Key("public_key_base64").IsNotEmpty(),
check.That(data.ResourceName).Key("identity.#").HasValue("1"),
check.That(data.ResourceName).Key("identity.0.type").HasValue("SystemAssigned"),
check.That(data.ResourceName).Key("identity.0.tenant_id").IsNotEmpty(),
check.That(data.ResourceName).Key("identity.0.principal_id").IsNotEmpty(),
check.That(data.ResourceName).Key("tags.%").HasValue("1"),
),
},
})
}

func (d ArcResourceBridgeApplianceDataSource) basic(data acceptance.TestData) string {
return fmt.Sprintf(`
%s
data "azurerm_arc_resource_bridge_appliance" "test" {
name = azurerm_arc_resource_bridge_appliance.test.name
resource_group_name = azurerm_arc_resource_bridge_appliance.test.resource_group_name
}
`, ArcResourceBridgeApplianceResource{}.complete(data))
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func TestAccArcResourceBridgeAppliance_complete(t *testing.T) {

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.complete(data, r.generatePublicKey()),
Config: r.complete(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
Expand Down Expand Up @@ -103,7 +103,6 @@ func (r ArcResourceBridgeApplianceResource) Exists(ctx context.Context, clients

client := clients.ArcResourceBridge.AppliancesClient
resp, err := client.Get(ctx, *id)

if err != nil {
return nil, fmt.Errorf("retrieving %q: %+v", *id, err)
}
Expand Down Expand Up @@ -150,7 +149,7 @@ resource "azurerm_arc_resource_bridge_appliance" "test" {
`, r.template(data), data.RandomInteger)
}

func (r ArcResourceBridgeApplianceResource) complete(data acceptance.TestData, publicKey string) string {
func (r ArcResourceBridgeApplianceResource) complete(data acceptance.TestData) string {
return fmt.Sprintf(`

%s
Expand All @@ -169,7 +168,7 @@ resource "azurerm_arc_resource_bridge_appliance" "test" {
"hello" = "world"
}
}
`, r.template(data), data.RandomInteger, publicKey)
`, r.template(data), data.RandomInteger, r.generatePublicKey())
}

func (r ArcResourceBridgeApplianceResource) requiresImport(data acceptance.TestData) string {
Expand Down
4 changes: 3 additions & 1 deletion internal/services/arcresourcebridge/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ func (r Registration) Name() string {
}

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

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

# Data Source: azurerm_arc_resource_bridge_appliance

Use this data source to access information about an existing Arc Resource Bridge Appliance.

## Example Usage

```hcl
data "azurerm_arc_resource_bridge_appliance" "example" {
name = "existing"
resource_group_name = "existing"
}

output "id" {
value = data.azurerm_arc_resource_bridge_appliance.example.id
}
```

## Arguments Reference

The following arguments are supported:

* `name` - (Required) The name of this Arc Resource Bridge Appliance.

* `resource_group_name` - (Required) The name of the Resource Group where the Arc Resource Bridge Appliance exists.

## Attributes Reference

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

* `id` - The ID of the Arc Resource Bridge Appliance.

* `distro` - Fabric/Infrastructure for this Arc Resource Bridge Appliance.

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

* `infrastructure_provider` - The infrastructure provider about the connected Arc Resource Bridge Appliance.

* `location` - The Azure Region where the Arc Resource Bridge Appliance exists.

* `public_key_base64` - RSA public key in PKCS1 format encoded in base64.

* `tags` - A mapping of tags assigned to the Arc Resource Bridge Appliance.

---

An `identity` block exports the following:

* `principal_id` - The Principal ID associated with this Managed Service Identity.

* `tenant_id` - The Tenant ID associated with this Managed Service Identity.

* `type` - The type of this Managed Service Identity.

## 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 Arc Resource Bridge Appliance.
9 changes: 9 additions & 0 deletions website/docs/r/arc_resource_bridge_appliance.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,15 @@ In addition to the Arguments listed above - the following Attributes are exporte

* `id` - The ID of the Arc Resource Bridge Appliance.

* `identity` - A `identity` block as defined below.
teowa marked this conversation as resolved.
Show resolved Hide resolved

---
An `identity` block exports the following:

* `principal_id` - The Principal ID associated with this Managed Service Identity.

* `tenant_id` - The Tenant ID associated with this Managed Service Identity.

## Timeouts

The `timeouts` block allows you to specify [timeouts](https://www.terraform.io/language/resources/syntax#operation-timeouts) for certain actions:
Expand Down
Loading