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_monitor_workspace #23928

Merged
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 123 additions & 0 deletions internal/services/monitor/monitor_workspace_data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package monitor

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-sdk/resource-manager/insights/2023-04-03/azuremonitorworkspaces"
"github.com/hashicorp/terraform-provider-azurerm/helpers/azure"
"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 WorkspaceDataSource struct{}

var _ sdk.DataSource = WorkspaceDataSource{}

type WorkspaceDataSourceModel struct {
Name string `tfschema:"name"`
ResourceGroupName string `tfschema:"resource_group_name"`
QueryEndpoint string `tfschema:"query_endpoint"`
PublicNetworkAccessEnabled bool `tfschema:"public_network_access_enabled"`
Location string `tfschema:"location"`
Tags map[string]string `tfschema:"tags"`
}

func (d WorkspaceDataSource) ModelObject() interface{} {
return &WorkspaceDataSource{}
}

func (d WorkspaceDataSource) ResourceType() string {
return "azurerm_monitor_workspace"
}

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

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

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

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

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

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

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

id := azuremonitorworkspaces.NewAccountID(subscriptionId, state.ResourceGroupName, state.Name)
metadata.Logger.Infof("retrieving %s", id)
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)
}

var enablePublicNetWorkAccess bool
var location, queryEndpoint string
var tag map[string]string

if model := resp.Model; model != nil {
location = azure.NormalizeLocation(model.Location)
tag = pointer.From(model.Tags)

if props := model.Properties; props != nil {
if props.PublicNetworkAccess != nil {
enablePublicNetWorkAccess = azuremonitorworkspaces.PublicNetworkAccessEnabled == *props.PublicNetworkAccess
}
if props.Metrics != nil && props.Metrics.PrometheusQueryEndpoint != nil {
queryEndpoint = *props.Metrics.PrometheusQueryEndpoint
}
}
}

metadata.SetID(id)

return metadata.Encode(&WorkspaceDataSourceModel{
Location: location,
Name: id.AccountName,
PublicNetworkAccessEnabled: enablePublicNetWorkAccess,
QueryEndpoint: queryEndpoint,
ResourceGroupName: id.ResourceGroupName,
Tags: tag,
})
},
}
}
39 changes: 39 additions & 0 deletions internal/services/monitor/monitor_workspace_data_source_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package monitor_test

import (
"fmt"
"testing"

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

type MonitorWorkspaceDataSource struct{}

func TestAccMonitorWorkspaceDataSourceDataSource_complete(t *testing.T) {
data := acceptance.BuildTestData(t, "data.azurerm_monitor_workspace", "test")
d := MonitorWorkspaceDataSource{}

data.DataSourceTest(t, []acceptance.TestStep{
{
Config: d.complete(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).Key("id").Exists(),
),
},
})
}

func (d MonitorWorkspaceDataSource) complete(data acceptance.TestData) string {
return fmt.Sprintf(`
%s

data "azurerm_monitor_workspace" "test" {
name = azurerm_monitor_workspace.test.name
resource_group_name = azurerm_monitor_workspace.test.resource_group_name
}
`, WorkspaceTestResource{}.complete(data))
}
1 change: 1 addition & 0 deletions internal/services/monitor/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func (r Registration) DataSources() []sdk.DataSource {
return []sdk.DataSource{
DataCollectionEndpointDataSource{},
DataCollectionRuleDataSource{},
WorkspaceDataSource{},
}
}

Expand Down
51 changes: 51 additions & 0 deletions website/docs/d/monitor_workspace.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
---
subcategory: "Monitor"
layout: "azurerm"
page_title: "Azure Resource Manager: azurerm_monitor_data_collection_endpoint"
description: |-
Get information about the specified Workspace.

---

# Data Source: azurerm_monitor_workspace

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

## Example Usage

```hcl
data "azurerm_monitor_workspace" "example" {
name = "example-workspace"
resource_group_name = azurerm_resource_group.example.name
}

output "query_endpoint" {
value = data.azurerm_monitor_workspace.example.query_endpoint
}
```

## Argument Reference

* `name` - Specifies the name of the Workspace.

* `resource_group_name` - Specifies the name of the resource group the Workspace is located in.

## Attributes Reference

* `id` - The ID of the Resource.

* `kind` - The kind of the Workspace. Possible values are `Linux` and `Windows`.

* `location` - The Azure Region where the Workspace is located.

* `query_endpoint` - The query endpoint for the Azure Monitor Workspace.

* `public_network_access_enabled` - Whether network access from public internet to the Workspace are allowed.

* `tags` - A mapping of tags that are assigned to the 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 Workspace.
Loading