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 Datasource: azurerm_monitor_data_collection_rule #18318

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
Original file line number Diff line number Diff line change
@@ -0,0 +1,303 @@
package monitor

import (
"context"
"fmt"
"time"

"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
"github.com/hashicorp/go-azure-helpers/resourcemanager/tags"
"github.com/hashicorp/go-azure-sdk/resource-manager/insights/2021-04-01/datacollectionrules"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"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 DataCollectionRuleDataSource struct{}

var _ sdk.DataSource = DataCollectionRuleDataSource{}

func (d DataCollectionRuleDataSource) ModelObject() interface{} {
return &DataCollectionRule{}
}

func (d DataCollectionRuleDataSource) ResourceType() string {
return "azurerm_monitor_data_collection_rule"
}

func (d DataCollectionRuleDataSource) 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 DataCollectionRuleDataSource) Attributes() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{

"location": commonschema.LocationComputed(),

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

"destinations": {
Type: pluginsdk.TypeList,
Computed: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*schema.Schema{
"azure_monitor_metrics": {
Type: pluginsdk.TypeList,
Computed: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: pluginsdk.TypeString,
Computed: true,
},
},
},
},
"log_analytics": {
Type: pluginsdk.TypeList,
Computed: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: pluginsdk.TypeString,
Computed: true,
},
"workspace_resource_id": {
Type: pluginsdk.TypeString,
Computed: true,
},
},
},
},
},
},
},

"data_sources": {
Type: pluginsdk.TypeList,
Computed: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"extension": {
Type: pluginsdk.TypeList,
Computed: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Computed: true,
},
"extension_name": {
Type: pluginsdk.TypeString,
Computed: true,
},
"streams": {
Type: pluginsdk.TypeList,
Computed: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
},
},
"extension_json": {
Type: pluginsdk.TypeString,
Computed: true,
},
"input_data_sources": {
Type: pluginsdk.TypeList,
Computed: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
},
},
},
},
},
"performance_counter": {
Type: pluginsdk.TypeList,
Computed: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Computed: true,
},
"sampling_frequency_in_seconds": {
Type: pluginsdk.TypeInt,
Computed: true,
},
"streams": {
Type: pluginsdk.TypeList,
Computed: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
},
},
"counter_specifiers": {
Type: pluginsdk.TypeList,
Computed: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
},
},
},
},
},
"syslog": {
Type: pluginsdk.TypeList,
Computed: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Computed: true,
},
"facility_names": {
Type: pluginsdk.TypeList,
Computed: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
},
},
"log_levels": {
Type: pluginsdk.TypeList,
Computed: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
},
},
},
},
},
"windows_event_log": {
Type: pluginsdk.TypeList,
Computed: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Computed: true,
},
"streams": {
Type: pluginsdk.TypeList,
Computed: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
},
},
"x_path_queries": {
Type: pluginsdk.TypeList,
Computed: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
},
},
},
},
},
},
},
},

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

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

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

func (d DataCollectionRuleDataSource) Read() sdk.ResourceFunc {
return sdk.ResourceFunc{
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.Monitor.DataCollectionRulesClient
subscriptionId := metadata.Client.Account.SubscriptionId

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

id := datacollectionrules.NewDataCollectionRuleID(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) {
metadata.Logger.Infof("%s was not found - removing from state!", id)
return metadata.MarkAsGone(id)
}
return fmt.Errorf("retrieving %s: %+v", id, err)
}

var description, kind, location string
var tag map[string]interface{}
var dataFlows []DataFlow
var dataSources []DataSource
var destinations []Destination

if model := resp.Model; model != nil {
kind = flattenDataCollectionRuleKind(model.Kind)
location = azure.NormalizeLocation(model.Location)
tag = tags.Flatten(model.Tags)
if prop := model.Properties; prop != nil {
description = flattenStringPtr(prop.Description)
dataFlows = flattenDataCollectionRuleDataFlows(prop.DataFlows)
dataSources = flattenDataCollectionRuleDataSources(prop.DataSources)
destinations = flattenDataCollectionRuleDestinations(prop.Destinations)
}
}

metadata.SetID(id)

return metadata.Encode(&DataCollectionRule{
Name: id.DataCollectionRuleName,
ResourceGroupName: id.ResourceGroupName,
DataFlows: dataFlows,
DataSources: dataSources,
Description: description,
Destinations: destinations,
Kind: kind,
Location: location,
Tags: tag,
})
},
Timeout: 5 * time.Minute,
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package monitor_test

import (
"fmt"
"testing"

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

type MonitorDataCollectionRuleDataSource struct{}

func TestAccMonitorDataCollectionRuleDataSource_complete(t *testing.T) {
data := acceptance.BuildTestData(t, "data.azurerm_monitor_data_collection_rule", "test")
d := MonitorDataCollectionRuleDataSource{}

data.DataSourceTest(t, []acceptance.TestStep{
{
Config: d.complete(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).Key("destinations.0.log_analytics.0.workspace_resource_id").Exists(),
check.That(data.ResourceName).Key("destinations.0.log_analytics.0.name").HasValue("test-destination-log1"),
check.That(data.ResourceName).Key("destinations.0.log_analytics.1.workspace_resource_id").Exists(),
check.That(data.ResourceName).Key("destinations.0.log_analytics.1.name").HasValue("test-destination-log2"),
check.That(data.ResourceName).Key("data_flow.#").HasValue("3"),
check.That(data.ResourceName).Key("data_flow.1.streams.#").HasValue("3"),
check.That(data.ResourceName).Key("data_flow.1.destinations.#").HasValue("1"),
check.That(data.ResourceName).Key("data_flow.2.streams.0").HasValue("Microsoft-Event"),
check.That(data.ResourceName).Key("data_flow.2.destinations.0").HasValue("test-destination-log1"),
check.That(data.ResourceName).Key("data_sources.0.syslog.0.facility_names.#").HasValue("5"),
check.That(data.ResourceName).Key("data_sources.0.performance_counter.#").HasValue("2"),
check.That(data.ResourceName).Key("data_sources.0.performance_counter.1.sampling_frequency_in_seconds").HasValue("20"),
check.That(data.ResourceName).Key("data_sources.0.performance_counter.1.name").HasValue("test-datasource-perfcounter2"),
check.That(data.ResourceName).Key("data_sources.0.windows_event_log.0.x_path_queries.0").HasValue("*[System/Level=1]"),
check.That(data.ResourceName).Key("data_sources.0.extension.0.extension_json").Exists(),
),
},
})
}

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

data "azurerm_monitor_data_collection_rule" "test" {
name = azurerm_monitor_data_collection_rule.test.name
resource_group_name = azurerm_monitor_data_collection_rule.test.resource_group_name
}
`, MonitorDataCollectionRuleResource{}.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 @@ -19,6 +19,7 @@ func (r Registration) AssociatedGitHubLabel() string {
func (r Registration) DataSources() []sdk.DataSource {
return []sdk.DataSource{
DataCollectionEndpointDataSource{},
DataCollectionRuleDataSource{},
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Use this data source to access information about an existing Data Collection End
## Example Usage

```hcl
data "azurerm_data_collection_endpoint" "example" {
data "azurerm_monitor_data_collection_endpoint" "example" {
name = "example-mdce"
resource_group_name = azurerm_resource_group.example.name
}
Expand Down
Loading