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_data_collection_endpoint #17992

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,135 @@
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/datacollectionendpoints"
"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 DataCollectionEndpointDataSource struct{}

var _ sdk.DataSource = DataCollectionEndpointDataSource{}

func (d DataCollectionEndpointDataSource) ModelObject() interface{} {
return &DataCollectionEndpoint{}
}

func (d DataCollectionEndpointDataSource) ResourceType() string {
return "azurerm_monitor_data_collection_endpoint"
}

func (d DataCollectionEndpointDataSource) 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 DataCollectionEndpointDataSource) Attributes() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{
"configuration_access_endpoint": {
Type: pluginsdk.TypeString,
Computed: true,
},

"location": commonschema.LocationComputed(),

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

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

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

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

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

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

id := datacollectionendpoints.NewDataCollectionEndpointID(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 enablePublicNetWorkAccess bool
var description, kind, location, configurationAccessEndpoint, logsIngestionEndpoint string
var tag map[string]interface{}
if model := resp.Model; model != nil {
kind = flattenDataCollectionEndpointKind(model.Kind)
location = azure.NormalizeLocation(model.Location)
tag = tags.Flatten(model.Tags)
if prop := model.Properties; prop != nil {
description = flattenDataCollectionEndpointDescription(prop.Description)
if networkAcls := prop.NetworkAcls; networkAcls != nil {
enablePublicNetWorkAccess = flattenDataCollectionEndpointPublicNetworkAccess(networkAcls.PublicNetworkAccess)
}

if prop.ConfigurationAccess != nil && prop.ConfigurationAccess.Endpoint != nil {
configurationAccessEndpoint = *prop.ConfigurationAccess.Endpoint
}

if prop.LogsIngestion != nil && prop.LogsIngestion.Endpoint != nil {
logsIngestionEndpoint = *prop.LogsIngestion.Endpoint
}
}
}

metadata.SetID(id)

return metadata.Encode(&DataCollectionEndpoint{
ConfigurationAccessEndpoint: configurationAccessEndpoint,
Description: description,
Kind: kind,
Location: location,
LogsIngestionEndpoint: logsIngestionEndpoint,
Name: id.DataCollectionEndpointName,
EnablePublicNetworkAccess: enablePublicNetWorkAccess,
ResourceGroupName: id.ResourceGroupName,
Tags: tag,
})
},
Timeout: 5 * time.Minute,
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package monitor_test

import (
"fmt"
"testing"

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

type MonitorDataCollectionEndpointDataSource struct{}

func TestAccMonitorDataCollectionEndpointDataSource_complete(t *testing.T) {
data := acceptance.BuildTestData(t, "data.azurerm_monitor_data_collection_endpoint", "test")
d := MonitorDataCollectionEndpointDataSource{}

data.DataSourceTest(t, []acceptance.TestStep{
{
Config: d.complete(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).Key("kind").HasValue("Windows"),
check.That(data.ResourceName).Key("public_network_access_enabled").HasValue("false"),
check.That(data.ResourceName).Key("configuration_access_endpoint").Exists(),
check.That(data.ResourceName).Key("logs_ingestion_endpoint").Exists(),
),
},
})
}

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

data "azurerm_monitor_data_collection_endpoint" "test" {
name = azurerm_monitor_data_collection_endpoint.test.name
resource_group_name = azurerm_monitor_data_collection_endpoint.test.resource_group_name
}
`, MonitorDataCollectionEndpointResource{}.complete(data))
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,18 @@ import (
)

type DataCollectionEndpoint struct {
Description string `tfschema:"description"`
Kind string `tfschema:"kind"`
Name string `tfschema:"name"`
Location string `tfschema:"location"`
EnablePublicNetworkAccess bool `tfschema:"public_network_access_enabled"`
ResourceGroupName string `tfschema:"resource_group_name"`
Tags map[string]interface{} `tfschema:"tags"`
ConfigurationAccessEndpoint string `tfschema:"configuration_access_endpoint"`
Description string `tfschema:"description"`
Kind string `tfschema:"kind"`
Name string `tfschema:"name"`
Location string `tfschema:"location"`
LogsIngestionEndpoint string `tfschema:"logs_ingestion_endpoint"`
EnablePublicNetworkAccess bool `tfschema:"public_network_access_enabled"`
ResourceGroupName string `tfschema:"resource_group_name"`
Tags map[string]interface{} `tfschema:"tags"`
}

type DataCollectionEndpointResource struct {
}
type DataCollectionEndpointResource struct{}

func (r DataCollectionEndpointResource) Arguments() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{
Expand Down Expand Up @@ -65,7 +66,17 @@ func (r DataCollectionEndpointResource) Arguments() map[string]*pluginsdk.Schema
}

func (r DataCollectionEndpointResource) Attributes() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{}
return map[string]*pluginsdk.Schema{
"configuration_access_endpoint": {
Type: pluginsdk.TypeString,
Computed: true,
},

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

func (r DataCollectionEndpointResource) ResourceType() string {
Expand Down Expand Up @@ -146,7 +157,7 @@ func (r DataCollectionEndpointResource) Read() sdk.ResourceFunc {
return fmt.Errorf("retrieving %s: %+v", *id, err)
}
var enablePublicNetWorkAccess bool
var description, kind, location string
var description, kind, location, configurationAccessEndpoint, logsIngestionEndpoint string
var tag map[string]interface{}
if model := resp.Model; model != nil {
kind = flattenDataCollectionEndpointKind(model.Kind)
Expand All @@ -157,17 +168,27 @@ func (r DataCollectionEndpointResource) Read() sdk.ResourceFunc {
if networkAcls := prop.NetworkAcls; networkAcls != nil {
enablePublicNetWorkAccess = flattenDataCollectionEndpointPublicNetworkAccess(networkAcls.PublicNetworkAccess)
}

if prop.ConfigurationAccess != nil && prop.ConfigurationAccess.Endpoint != nil {
configurationAccessEndpoint = *prop.ConfigurationAccess.Endpoint
}

if prop.LogsIngestion != nil && prop.LogsIngestion.Endpoint != nil {
logsIngestionEndpoint = *prop.LogsIngestion.Endpoint
}
}
}

return metadata.Encode(&DataCollectionEndpoint{
Description: description,
Kind: kind,
Location: location,
Name: id.DataCollectionEndpointName,
EnablePublicNetworkAccess: enablePublicNetWorkAccess,
ResourceGroupName: id.ResourceGroupName,
Tags: tag,
ConfigurationAccessEndpoint: configurationAccessEndpoint,
Description: description,
Kind: kind,
Location: location,
LogsIngestionEndpoint: logsIngestionEndpoint,
Name: id.DataCollectionEndpointName,
EnablePublicNetworkAccess: enablePublicNetWorkAccess,
ResourceGroupName: id.ResourceGroupName,
Tags: tag,
})
},
Timeout: 5 * time.Minute,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,7 @@ type LogAnalytic struct {
WorkspaceResourceId string `tfschema:"workspace_resource_id"`
}

type DataCollectionRuleResource struct {
}
type DataCollectionRuleResource struct{}

func (r DataCollectionRuleResource) Arguments() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{
Expand Down
4 changes: 3 additions & 1 deletion internal/services/monitor/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ func (r Registration) AssociatedGitHubLabel() string {
}

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

func (r Registration) Resources() []sdk.Resource {
Expand Down
55 changes: 55 additions & 0 deletions website/docs/d/monitor_data_collection_endpoint.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
---
subcategory: "Monitor"
layout: "azurerm"
page_title: "Azure Resource Manager: azurerm_monitor_data_collection_endpoint"
description: |-
Get information about the specified Data Collection Endpoint.

---

# Data Source: azurerm_monitor_data_collection_endpoint

Use this data source to access information about an existing Data Collection Endpoint.

## Example Usage

```hcl
data "azurerm_data_collection_endpoint" "example" {
name = "example-mdce"
resource_group_name = azurerm_resource_group.example.name
}

output "endpoint_id" {
value = data.azurerm_monitor_data_collection_endpoint.example.id
}
```

## Argument Reference

* `name` - Specifies the name of the Data Collection Endpoint.

* `resource_group_name` - Specifies the name of the resource group the Data Collection Endpoint is located in.

## Attributes Reference

* `id` - The ID of the Resource.

* `configuration_access_endpoint` - The endpoint used for accessing configuration, e.g., `https://mydce-abcd.eastus-1.control.monitor.azure.com`.

* `description` - Specifies a description for the Data Collection Endpoint.

* `kind` - The kind of the Data Collection Endpoint. Possible values are `Linux` and `Windows`.

* `location` - The Azure Region where the Data Collection Endpoint should exist.

* `logs_ingestion_endpoint` - The endpoint used for ingesting logs, e.g., `https://mydce-abcd.eastus-1.ingest.monitor.azure.com`.

* `public_network_access_enabled` - Whether network access from public internet to the Data Collection Endpoint are allowed. Possible values are `true` and `false`.

* `tags` - A mapping of tags which should be assigned to the Data Collection Endpoint.

## 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 Data Collection Endpoint.
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ Manages a Data Collection Endpoint.
## Example Usage

```hcl
# Generated from AccTest TestAccMonitorDataCollectionEndpoint_complete
provider "azurerm" {
features {}
}

resource "azurerm_resource_group" "example" {
name = "example-rg"
location = "West Europe"
}

resource "azurerm_monitor_data_collection_endpoint" "example" {
name = "example-mdce"
resource_group_name = azurerm_resource_group.example.name
Expand Down Expand Up @@ -60,9 +61,13 @@ In addition to the Arguments listed above - the following Attributes are exporte

* `id` - The ID of the Data Collection Endpoint.

* `configuration_access_endpoint` - The endpoint used for accessing configuration, e.g., `https://mydce-abcd.eastus-1.control.monitor.azure.com`.

* `logs_ingestion_endpoint` - The endpoint used for ingesting logs, e.g., `https://mydce-abcd.eastus-1.ingest.monitor.azure.com`.

## Timeouts

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

* `create` - (Defaults to 30 minutes) Used when creating the Data Collection Endpoint.
* `read` - (Defaults to 5 minutes) Used when retrieving the Data Collection Endpoint.
Expand Down