Skip to content

Commit

Permalink
azurerm_dashboard_grafana: add support for data source
Browse files Browse the repository at this point in the history
  • Loading branch information
jiaweitao001 committed Dec 15, 2023
1 parent 09e6d1e commit 53d08e4
Show file tree
Hide file tree
Showing 5 changed files with 347 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package dashboard_test

import (
"fmt"
"testing"

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

type DashboardGrafanaDataSource struct{}

func TestAccDashboardGrafanaDataSource_basic(t *testing.T) {
data := acceptance.BuildTestData(t, "data.azurerm_dashboard_grafana", "test")
r := DashboardGrafanaDataSource{}

data.DataSourceTest(t, []acceptance.TestStep{
{
Config: r.basic(data),
Check: acceptance.ComposeTestCheckFunc(),
},
})
}

func (d DashboardGrafanaDataSource) basic(data acceptance.TestData) string {
return fmt.Sprintf(`
%s
data "azurerm_dashboard_grafana" "test" {
name = azurerm_dashboard_grafana.test.name
resource_group_name = azurerm_dashboard_grafana.test.resource_group_name
}
`, DashboardGrafanaResource{}.basic(data))
}
236 changes: 236 additions & 0 deletions internal/services/dashboard/dashboard_grafana_data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,236 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package dashboard

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/location"
"github.com/hashicorp/go-azure-sdk/resource-manager/dashboard/2023-09-01/grafanaresource"
"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 DashboardGrafanaDataSource struct{}

type DashboardGrafanaDataSourceModel struct {
Name string `tfschema:"name"`
ResourceGroupName string `tfschema:"resource_group_name"`
ApiKeyEnabled bool `tfschema:"api_key_enabled"`
AutoGeneratedDomainNameLabelScope grafanaresource.AutoGeneratedDomainNameLabelScope `tfschema:"auto_generated_domain_name_label_scope"`
DeterministicOutboundIPEnabled bool `tfschema:"deterministic_outbound_ip_enabled"`
AzureMonitorWorkspaceIntegrations []AzureMonitorWorkspaceIntegrationModel `tfschema:"azure_monitor_workspace_integrations"`
Location string `tfschema:"location"`
PublicNetworkAccessEnabled bool `tfschema:"public_network_access_enabled"`
Sku string `tfschema:"sku"`
Tags map[string]string `tfschema:"tags"`
ZoneRedundancyEnabled bool `tfschema:"zone_redundancy_enabled"`
Endpoint string `tfschema:"endpoint"`
GrafanaVersion string `tfschema:"grafana_version"`
GrafanaMajorVersion string `tfschema:"grafana_major_version"`
OutboundIPs []string `tfschema:"outbound_ip"`
}

var _ sdk.DataSource = DashboardGrafanaDataSource{}

func (d DashboardGrafanaDataSource) ResourceType() string {
return "azurerm_dashboard_grafana"
}

func (d DashboardGrafanaDataSource) ModelObject() interface{} {
return &DashboardGrafanaDataSourceModel{}
}

func (d DashboardGrafanaDataSource) Arguments() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validation.StringMatch(
regexp.MustCompile(`^[a-zA-Z][-a-zA-Z\d]{0,21}[a-zA-Z\d]$`),
`The name length must be from 2 to 23 characters. The name can only contain letters, numbers and dashes, and it must begin with a letter and end with a letter or digit.`,
),
},

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

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

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

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

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

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

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

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

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

"tags": commonschema.TagsDataSource(),

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

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

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

"outbound_ip": {
Type: pluginsdk.TypeList,
Computed: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
},
},
}
}

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

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

id := grafanaresource.NewGrafanaID(subscriptionId, dashboard.ResourceGroupName, dashboard.Name)

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

if model := resp.Model; model != nil {
dashboard.Location = location.NormalizeNilable(model.Location)

if props := model.Properties; props != nil {
if props.ApiKey != nil {
if pointer.From(props.ApiKey) == grafanaresource.ApiKeyEnabled {
dashboard.ApiKeyEnabled = true
} else {
dashboard.ApiKeyEnabled = false
}
}

if props.DeterministicOutboundIP != nil {
if pointer.From(props.DeterministicOutboundIP) == grafanaresource.DeterministicOutboundIPEnabled {
dashboard.DeterministicOutboundIPEnabled = true
} else {
dashboard.DeterministicOutboundIPEnabled = false
}
}

if props.Endpoint != nil {
dashboard.Endpoint = pointer.From(props.Endpoint)
}

if props.GrafanaIntegrations != nil {
dashboard.AzureMonitorWorkspaceIntegrations = FlattenAzureMonitorWorkspaceIntegrationModelArray(props.GrafanaIntegrations.AzureMonitorWorkspaceIntegrations)
}

if props.AutoGeneratedDomainNameLabelScope != nil {
dashboard.AutoGeneratedDomainNameLabelScope = pointer.From(props.AutoGeneratedDomainNameLabelScope)
}

if props.GrafanaVersion != nil {
dashboard.GrafanaVersion = pointer.From(props.GrafanaVersion)
}

if props.GrafanaMajorVersion != nil {
dashboard.GrafanaMajorVersion = pointer.From(props.GrafanaMajorVersion)
}

if props.OutboundIPs != nil {
dashboard.OutboundIPs = pointer.From(props.OutboundIPs)
}

if props.PublicNetworkAccess != nil {
if pointer.From(props.PublicNetworkAccess) == grafanaresource.PublicNetworkAccessEnabled {
dashboard.PublicNetworkAccessEnabled = true
} else {
dashboard.PublicNetworkAccessEnabled = false
}
}

if props.ZoneRedundancy != nil {
if pointer.From(props.ZoneRedundancy) == grafanaresource.ZoneRedundancyEnabled {
dashboard.ZoneRedundancyEnabled = true
} else {
dashboard.ZoneRedundancyEnabled = false
}
}
}
if model.Sku != nil {
dashboard.Sku = model.Sku.Name
}

if model.Tags != nil {
dashboard.Tags = pointer.From(model.Tags)
}
}

metadata.SetID(id)
return metadata.Encode(&dashboard)
},
}
}
4 changes: 2 additions & 2 deletions internal/services/dashboard/dashboard_grafana_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ func (r DashboardGrafanaResource) Read() sdk.ResourceFunc {
}

if properties.GrafanaIntegrations != nil {
state.AzureMonitorWorkspaceIntegrations = flattenAzureMonitorWorkspaceIntegrationModelArray(properties.GrafanaIntegrations.AzureMonitorWorkspaceIntegrations)
state.AzureMonitorWorkspaceIntegrations = FlattenAzureMonitorWorkspaceIntegrationModelArray(properties.GrafanaIntegrations.AzureMonitorWorkspaceIntegrations)
}

if properties.GrafanaVersion != nil {
Expand Down Expand Up @@ -483,7 +483,7 @@ func expandLegacySystemAndUserAssignedMap(input []interface{}) *identity.LegacyS
}
}

func flattenAzureMonitorWorkspaceIntegrationModelArray(inputList *[]grafanaresource.AzureMonitorWorkspaceIntegration) []AzureMonitorWorkspaceIntegrationModel {
func FlattenAzureMonitorWorkspaceIntegrationModelArray(inputList *[]grafanaresource.AzureMonitorWorkspaceIntegration) []AzureMonitorWorkspaceIntegrationModel {
var outputList []AzureMonitorWorkspaceIntegrationModel
if inputList == nil {
return outputList
Expand Down
4 changes: 3 additions & 1 deletion internal/services/dashboard/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ func (r Registration) SupportedResources() map[string]*pluginsdk.Resource {

// DataSources returns a list of Data Sources supported by this Service
func (r Registration) DataSources() []sdk.DataSource {
return []sdk.DataSource{}
return []sdk.DataSource{
DashboardGrafanaDataSource{},
}
}

// Resources returns a list of Resources supported by this Service
Expand Down
70 changes: 70 additions & 0 deletions website/docs/d/dashboard_grafana.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
---
subcategory: "Dashboard"
layout: "azurerm"
page_title: "Azure Resource Manager: azurerm_dashboard_grafana"
description: |-
Get information of a Dashboard Grafana.
---

# Data Source: azurerm_dashboard_grafana

Use this data to access information about an existing Dashboard Grafana.

## Example Usage

```hcl
data "azurerm_dashboard_grafana" "example" {
name = "example-dg"
resource_group_name = "example-resources"
}
```

## Arguments Reference

The following arguments are supported:

- `name` - (Required) The name of the Grafana dashboard.

- `resource_group_name` - (Required) The name of the resource group in which the Grafana dashboard is located.

## Attributes Reference

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

- `api_key_enabled` - Whether the api key setting of the Grafana instance is enabled.

- `auto_generated_domain_name_label_scope` - Scope for dns deterministic name hash calculation.

- `deterministic_outbound_ip_enabled` - Whether the Grafana instance is enabled to use deterministic outbound IPs.

- `azure_monitor_workspace_integrations` - A `azure_monitor_workspace_integrations` block as defined below.

- `location` - The Azure Region where the Grafana dashboard is located.

- `public_network_access_enabled` - Whether the Grafana instance is enabled to use public network access.

- `sku` - The SKU of the Grafana instance.

- `zone_redundancy_enabled` - Whether the Grafana instance is enabled to use zone redundancy.

- `endpoint` - The endpoint of the Grafana instance.

- `grafana_major_version` - The major version of Grafana deployed.

- `grafana_version` - The full Grafana software semantic version deployed.

- `outbound_ip` - List of outbound IPs if deterministicOutboundIP is enabled.

- `tags` - A mapping of tags assigned to the resource.

---

A `azure_monitor_workspace_integrations` block exports the following:

- `resource_id` - The resource ID of the connected Azure Monitor Workspace.

## Timeouts

The `timeouts` block allows you to specify [timeouts](https://www.terraform.io/docs/configuration/resources.html#timeouts) for certain actions:

- `read` - (Defaults to 5 minutes) Used when retrieving the Dashboard Grafana.

0 comments on commit 53d08e4

Please sign in to comment.