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_virtual_desktop_workspace #24732

Merged
merged 3 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions internal/provider/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ func SupportedTypedServices() []sdk.TypedServiceRegistration {
databoxedge.Registration{},
databricks.Registration{},
datafactory.Registration{},
desktopvirtualization.Registration{},
digitaltwins.Registration{},
disks.Registration{},
domainservices.Registration{},
Expand Down
15 changes: 14 additions & 1 deletion internal/services/desktopvirtualization/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import (

type Registration struct{}

var _ sdk.UntypedServiceRegistrationWithAGitHubLabel = Registration{}
var (
_ sdk.TypedServiceRegistration = Registration{}
_ sdk.UntypedServiceRegistrationWithAGitHubLabel = Registration{}
)

func (r Registration) AssociatedGitHubLabel() string {
return "service/virtual-desktops"
Expand All @@ -27,6 +30,16 @@ func (r Registration) WebsiteCategories() []string {
}
}

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

func (r Registration) Resources() []sdk.Resource {
return []sdk.Resource{}
}

// SupportedDataSources returns the supported Data Sources supported by this Service
func (r Registration) SupportedDataSources() map[string]*pluginsdk.Resource {
return map[string]*pluginsdk.Resource{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package desktopvirtualization

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-sdk/resource-manager/desktopvirtualization/2022-02-10-preview/workspace"
"github.com/hashicorp/terraform-provider-azurerm/internal/sdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/desktopvirtualization/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
)

type DesktopVirtualizationWorkspaceDataSource struct{}

type DesktopVirtualizationWorkspaceModel struct {
Name string `tfschema:"name"`
ResourceGroup string `tfschema:"resource_group_name"`
Location string `tfschema:"location"`
FriendlyName string `tfschema:"friendly_name"`
Description string `tfschema:"description"`
PublicNetworkAccess bool `tfschema:"public_network_access_enabled"`
Tags map[string]string `tfschema:"tags"`
}

var _ sdk.DataSource = DesktopVirtualizationWorkspaceDataSource{}

func (d DesktopVirtualizationWorkspaceDataSource) ModelObject() interface{} {
return &DesktopVirtualizationWorkspaceModel{}
}

func (d DesktopVirtualizationWorkspaceDataSource) ResourceType() string {
return "azurerm_virtual_desktop_workspace"
}

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

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

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

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

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

"public_network_access_enabled": {
Type: pluginsdk.TypeBool,
Computed: true,
},

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

func (d DesktopVirtualizationWorkspaceDataSource) Read() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 5 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.DesktopVirtualization.WorkspacesClient
subscriptionId := metadata.Client.Account.SubscriptionId

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

id := workspace.NewWorkspaceID(subscriptionId, state.ResourceGroup, 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)
}

model := resp.Model
if model == nil {
return fmt.Errorf("retrieving %s: model was nil", id)
}

state.Location = location.NormalizeNilable(model.Location)
state.Tags = pointer.From(model.Tags)

if properties := model.Properties; properties != nil {
if properties.FriendlyName != nil {
state.FriendlyName = pointer.From(properties.FriendlyName)
}

if properties.Description != nil {
state.Description = pointer.From(properties.Description)
}

publicNetworkAccess := true
if properties.PublicNetworkAccess != nil && pointer.From(properties.PublicNetworkAccess) != workspace.PublicNetworkAccessEnabled {
publicNetworkAccess = false
}
state.PublicNetworkAccess = publicNetworkAccess
}

metadata.SetID(id)

return metadata.Encode(&state)
},
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package desktopvirtualization_test

import (
"fmt"
"testing"

"github.com/hashicorp/go-azure-helpers/resourcemanager/location"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check"
)

type DesktopVirtualizationWorkspaceDataSource struct{}

func TestAccDesktopVirtualizationWorkspaceDataSource_complete(t *testing.T) {
data := acceptance.BuildTestData(t, "data.azurerm_virtual_desktop_workspace", "test")
d := DesktopVirtualizationWorkspaceDataSource{}

data.DataSourceTest(t, []acceptance.TestStep{
{
Config: d.complete(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).Key("name").IsNotEmpty(),
check.That(data.ResourceName).Key("description").IsNotEmpty(),
check.That(data.ResourceName).Key("resource_group_name").IsNotEmpty(),
check.That(data.ResourceName).Key("friendly_name").HasValue("Acceptance Test!"),
check.That(data.ResourceName).Key("public_network_access_enabled").HasValue("false"),
check.That(data.ResourceName).Key("location").HasValue(location.Normalize(data.Locations.Secondary)),
check.That(data.ResourceName).Key("tags.%").HasValue("0"),
),
},
})
}

func (DesktopVirtualizationWorkspaceDataSource) complete(data acceptance.TestData) string {
template := AzureRMDesktopVirtualizationWorkspaceResource{}.complete(data)
return fmt.Sprintf(`
%s

data "azurerm_virtual_desktop_workspace" "test" {
name = azurerm_virtual_desktop_workspace.test.name
resource_group_name = azurerm_virtual_desktop_workspace.test.resource_group_name
}
`, template)
}
54 changes: 54 additions & 0 deletions website/docs/d/virtual_desktop_workspace.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
---
subcategory: "Desktop Virtualization"
layout: "azurerm"
page_title: "Azure Resource Manager: azurerm_virtual_desktop_workspace"
description: |-
Gets information about an existing Virtual Desktop Workspace.
---

# Data Source: azurerm_virtual_desktop_workspace

Use this data source to access information about an existing Virtual Desktop Workspace.

## Example Usage

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

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

## Arguments Reference

The following arguments are supported:

* `name` - (Required) The name of this Virtual Desktop Workspace to retrieve.

* `resource_group_name` - (Required) The name of the Resource Group where the Virtual Desktop Workspace exists.

## Attributes Reference

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

* `id` - The ID of the Virtual Desktop Workspace.

* `description` - The description for the Virtual Desktop Workspace.

* `friendly_name` - The friendly name for the Virtual Desktop Workspace.

* `location` - The Azure Region where the Virtual Desktop Workspace exists.

* `public_network_access_enabled` - Is public network access enabled?

* `tags` - A mapping of tags assigned to the Virtual Desktop Workspace.

## 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 azurerm_virtual_desktop_workspace.