Skip to content

Commit

Permalink
new resouce azurerm_monitor_data_collection_endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
teowa committed Jul 20, 2022
1 parent ad38896 commit 36805f5
Show file tree
Hide file tree
Showing 24 changed files with 1,664 additions and 0 deletions.
6 changes: 6 additions & 0 deletions internal/services/monitor/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/Azure/azure-sdk-for-go/services/preview/alertsmanagement/mgmt/2019-06-01-preview/alertsmanagement"
classic "github.com/Azure/azure-sdk-for-go/services/preview/monitor/mgmt/2021-07-01-preview/insights"
newActionGroupClient "github.com/Azure/azure-sdk-for-go/services/preview/monitor/mgmt/2021-09-01-preview/insights"
"github.com/hashicorp/go-azure-sdk/resource-manager/insights/2021-04-01/datacollectionendpoints"
"github.com/hashicorp/go-azure-sdk/resource-manager/insights/2021-04-01/datacollectionrules"
"github.com/hashicorp/terraform-provider-azurerm/internal/common"
)
Expand All @@ -26,6 +27,7 @@ type Client struct {
ActivityLogAlertsClient *insights.ActivityLogAlertsClient
AlertRulesClient *classic.AlertRulesClient
DataCollectionRulesClient *datacollectionrules.DataCollectionRulesClient
DataCollectionEndpointsClient *datacollectionendpoints.DataCollectionEndpointsClient
DiagnosticSettingsClient *classic.DiagnosticSettingsClient
DiagnosticSettingsCategoryClient *classic.DiagnosticSettingsCategoryClient
LogProfilesClient *classic.LogProfilesClient
Expand Down Expand Up @@ -57,6 +59,9 @@ func NewClient(o *common.ClientOptions) *Client {
AlertRulesClient := classic.NewAlertRulesClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&AlertRulesClient.Client, o.ResourceManagerAuthorizer)

DataCollectionEndpointsClient := datacollectionendpoints.NewDataCollectionEndpointsClientWithBaseURI(o.ResourceManagerEndpoint)
o.ConfigureClient(&DataCollectionEndpointsClient.Client, o.ResourceManagerAuthorizer)

DataCollectionRulesClient := datacollectionrules.NewDataCollectionRulesClientWithBaseURI(o.ResourceManagerEndpoint)
o.ConfigureClient(&DataCollectionRulesClient.Client, o.ResourceManagerAuthorizer)

Expand Down Expand Up @@ -90,6 +95,7 @@ func NewClient(o *common.ClientOptions) *Client {
ActivityLogAlertsClient: &ActivityLogAlertsClient,
AlertRulesClient: &AlertRulesClient,
DataCollectionRulesClient: &DataCollectionRulesClient,
DataCollectionEndpointsClient: &DataCollectionEndpointsClient,
DiagnosticSettingsClient: &DiagnosticSettingsClient,
DiagnosticSettingsCategoryClient: &DiagnosticSettingsCategoryClient,
LogProfilesClient: &LogProfilesClient,
Expand Down
298 changes: 298 additions & 0 deletions internal/services/monitor/monitor_data_collection_endpoint_resource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,298 @@
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"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)

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

type DataCollectionEndpointResource struct {
}

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

"resource_group_name": commonschema.ResourceGroupName(),

"location": commonschema.Location(),

"enable_public_network_access": {
Type: pluginsdk.TypeBool,
Optional: true,
Default: true,
},

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

"kind": {
Type: pluginsdk.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice(
datacollectionendpoints.PossibleValuesForKnownDataCollectionEndpointResourceKind(), false),
},

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

func (r DataCollectionEndpointResource) Attributes() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{}
}

func (r DataCollectionEndpointResource) ResourceType() string {
return "azurerm_monitor_data_collection_endpoint"
}

func (r DataCollectionEndpointResource) IDValidationFunc() pluginsdk.SchemaValidateFunc {
return datacollectionendpoints.ValidateDataCollectionEndpointID
}

func (r DataCollectionEndpointResource) ModelObject() interface{} {
return &DataCollectionEndpoint{}
}

func (r DataCollectionEndpointResource) Create() sdk.ResourceFunc {
return sdk.ResourceFunc{
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
metadata.Logger.Info("Decoding state..")
var state DataCollectionEndpoint
if err := metadata.Decode(&state); err != nil {
return err
}

client := metadata.Client.Monitor.DataCollectionEndpointsClient
subscriptionId := metadata.Client.Account.SubscriptionId

id := datacollectionendpoints.NewDataCollectionEndpointID(subscriptionId, state.ResourceGroupName, state.Name)
metadata.Logger.Infof("creating %s", id)

existing, err := client.Get(ctx, id)
if err != nil && !response.WasNotFound(existing.HttpResponse) {
return fmt.Errorf("checking for the presence of an existing %s: %+v", id, err)
}
if !response.WasNotFound(existing.HttpResponse) {
return metadata.ResourceRequiresImport(r.ResourceType(), id)
}

input := datacollectionendpoints.DataCollectionEndpointResource{
Kind: expandDataCollectionEndpointKind(state.Kind),
Location: azure.NormalizeLocation(state.Location),
Name: utils.String(state.Name),
Properties: &datacollectionendpoints.DataCollectionEndpoint{
Description: utils.String(state.Description),
NetworkAcls: &datacollectionendpoints.NetworkRuleSet{
PublicNetworkAccess: expandDataCollectionEndpointPublicNetworkAccess(state.EnablePublicNetworkAccess),
},
},
Tags: tags.Expand(state.Tags),
}

if _, err := client.Create(ctx, id, input); err != nil {
return fmt.Errorf("creating %s: %+v", id, err)
}

metadata.SetID(id)
return nil
},
Timeout: 30 * time.Minute,
}
}

func (r DataCollectionEndpointResource) Read() sdk.ResourceFunc {
return sdk.ResourceFunc{
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.Monitor.DataCollectionEndpointsClient
id, err := datacollectionendpoints.ParseDataCollectionEndpointID(metadata.ResourceData.Id())
if err != nil {
return err
}

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 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)
}
}
}

return metadata.Encode(&DataCollectionEndpoint{
Description: description,
Kind: kind,
Location: location,
Name: id.DataCollectionEndpointName,
EnablePublicNetworkAccess: enablePublicNetWorkAccess,
ResourceGroupName: id.ResourceGroupName,
Tags: tag,
})
},
Timeout: 5 * time.Minute,
}
}

func (r DataCollectionEndpointResource) Update() sdk.ResourceFunc {
return sdk.ResourceFunc{
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
id, err := datacollectionendpoints.ParseDataCollectionEndpointID(metadata.ResourceData.Id())
if err != nil {
return err
}

metadata.Logger.Infof("updating %s..", *id)
client := metadata.Client.Monitor.DataCollectionEndpointsClient
resp, err := client.Get(ctx, *id)
if err != nil {
return fmt.Errorf("retrieving %s: %+v", *id, err)
}
if resp.Model == nil {
return fmt.Errorf("unexpected null model of %s", *id)
}
existing := resp.Model
if existing.Properties == nil {
return fmt.Errorf("unexpected null properties of %s", *id)
}

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

if metadata.ResourceData.HasChange("description") {
existing.Properties.Description = utils.String(state.Description)
}

if metadata.ResourceData.HasChange("kind") {
existing.Kind = expandDataCollectionEndpointKind(state.Kind)
}

if metadata.ResourceData.HasChange("public_network_access") {
existing.Properties.NetworkAcls = &datacollectionendpoints.NetworkRuleSet{
PublicNetworkAccess: expandDataCollectionEndpointPublicNetworkAccess(state.EnablePublicNetworkAccess),
}
}

if metadata.ResourceData.HasChange("tags") {
existing.Tags = tags.Expand(state.Tags)
}

if _, err := client.Create(ctx, *id, *existing); err != nil {
return fmt.Errorf("updating %s: %+v", *id, err)
}
return nil

},
Timeout: 30 * time.Minute,
}
}

func (r DataCollectionEndpointResource) Delete() sdk.ResourceFunc {
return sdk.ResourceFunc{
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.Monitor.DataCollectionEndpointsClient
id, err := datacollectionendpoints.ParseDataCollectionEndpointID(metadata.ResourceData.Id())
if err != nil {
return err
}

metadata.Logger.Infof("deleting %s..", *id)
resp, err := client.Delete(ctx, *id)
if err != nil && !response.WasNotFound(resp.HttpResponse) {
return fmt.Errorf("deleting %s: %+v", *id, err)
}
return nil
},
Timeout: 30 * time.Minute,
}
}

func expandDataCollectionEndpointKind(input string) *datacollectionendpoints.KnownDataCollectionEndpointResourceKind {
if input == "" {
return nil
}

result := datacollectionendpoints.KnownDataCollectionEndpointResourceKind(input)
return &result
}

func expandDataCollectionEndpointPublicNetworkAccess(input bool) *datacollectionendpoints.KnownPublicNetworkAccessOptions {
var result datacollectionendpoints.KnownPublicNetworkAccessOptions
if input {
result = datacollectionendpoints.KnownPublicNetworkAccessOptionsEnabled
} else {
result = datacollectionendpoints.KnownPublicNetworkAccessOptionsDisabled
}
return &result
}

func flattenDataCollectionEndpointKind(input *datacollectionendpoints.KnownDataCollectionEndpointResourceKind) string {
if input == nil {
return ""
}

return string(*input)
}

func flattenDataCollectionEndpointDescription(input *string) string {
if input == nil {
return ""
}

return *input
}

func flattenDataCollectionEndpointPublicNetworkAccess(input *datacollectionendpoints.KnownPublicNetworkAccessOptions) bool {
if input == nil {
return false
}
var result bool
if *input == datacollectionendpoints.KnownPublicNetworkAccessOptionsEnabled {
result = true
} else if *input == datacollectionendpoints.KnownPublicNetworkAccessOptionsDisabled {
result = false
}
return result
}
Loading

0 comments on commit 36805f5

Please sign in to comment.