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 Resource: azurerm_monitor_data_collection_rule_association #17948

Merged
Merged
62 changes: 34 additions & 28 deletions internal/services/monitor/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
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/datacollectionruleassociations"
"github.com/hashicorp/go-azure-sdk/resource-manager/insights/2021-04-01/datacollectionrules"
"github.com/hashicorp/terraform-provider-azurerm/internal/common"
)
Expand All @@ -23,18 +24,19 @@ type Client struct {
SmartDetectorAlertRulesClient *alertsmanagement.SmartDetectorAlertRulesClient

// Monitor
ActionGroupsClient *newActionGroupClient.ActionGroupsClient
ActivityLogAlertsClient *insights.ActivityLogAlertsClient
AlertRulesClient *classic.AlertRulesClient
DataCollectionRulesClient *datacollectionrules.DataCollectionRulesClient
DataCollectionEndpointsClient *datacollectionendpoints.DataCollectionEndpointsClient
DiagnosticSettingsClient *classic.DiagnosticSettingsClient
DiagnosticSettingsCategoryClient *classic.DiagnosticSettingsCategoryClient
LogProfilesClient *classic.LogProfilesClient
MetricAlertsClient *classic.MetricAlertsClient
PrivateLinkScopesClient *classic.PrivateLinkScopesClient
PrivateLinkScopedResourcesClient *classic.PrivateLinkScopedResourcesClient
ScheduledQueryRulesClient *classic.ScheduledQueryRulesClient
ActionGroupsClient *newActionGroupClient.ActionGroupsClient
ActivityLogAlertsClient *insights.ActivityLogAlertsClient
AlertRulesClient *classic.AlertRulesClient
DataCollectionEndpointsClient *datacollectionendpoints.DataCollectionEndpointsClient
DataCollectionRuleAssociationsClient *datacollectionruleassociations.DataCollectionRuleAssociationsClient
DataCollectionRulesClient *datacollectionrules.DataCollectionRulesClient
DiagnosticSettingsClient *classic.DiagnosticSettingsClient
DiagnosticSettingsCategoryClient *classic.DiagnosticSettingsCategoryClient
LogProfilesClient *classic.LogProfilesClient
MetricAlertsClient *classic.MetricAlertsClient
PrivateLinkScopesClient *classic.PrivateLinkScopesClient
PrivateLinkScopedResourcesClient *classic.PrivateLinkScopedResourcesClient
ScheduledQueryRulesClient *classic.ScheduledQueryRulesClient
}

func NewClient(o *common.ClientOptions) *Client {
Expand Down Expand Up @@ -62,6 +64,9 @@ func NewClient(o *common.ClientOptions) *Client {
DataCollectionEndpointsClient := datacollectionendpoints.NewDataCollectionEndpointsClientWithBaseURI(o.ResourceManagerEndpoint)
o.ConfigureClient(&DataCollectionEndpointsClient.Client, o.ResourceManagerAuthorizer)

DataCollectionRuleAssociationsClient := datacollectionruleassociations.NewDataCollectionRuleAssociationsClientWithBaseURI(o.ResourceManagerEndpoint)
o.ConfigureClient(&DataCollectionRuleAssociationsClient.Client, o.ResourceManagerAuthorizer)

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

Expand All @@ -87,21 +92,22 @@ func NewClient(o *common.ClientOptions) *Client {
o.ConfigureClient(&ScheduledQueryRulesClient.Client, o.ResourceManagerAuthorizer)

return &Client{
AADDiagnosticSettingsClient: &AADDiagnosticSettingsClient,
AutoscaleSettingsClient: &AutoscaleSettingsClient,
ActionRulesClient: &ActionRulesClient,
SmartDetectorAlertRulesClient: &SmartDetectorAlertRulesClient,
ActionGroupsClient: &ActionGroupsClient,
ActivityLogAlertsClient: &ActivityLogAlertsClient,
AlertRulesClient: &AlertRulesClient,
DataCollectionRulesClient: &DataCollectionRulesClient,
DataCollectionEndpointsClient: &DataCollectionEndpointsClient,
DiagnosticSettingsClient: &DiagnosticSettingsClient,
DiagnosticSettingsCategoryClient: &DiagnosticSettingsCategoryClient,
LogProfilesClient: &LogProfilesClient,
MetricAlertsClient: &MetricAlertsClient,
PrivateLinkScopesClient: &PrivateLinkScopesClient,
PrivateLinkScopedResourcesClient: &PrivateLinkScopedResourcesClient,
ScheduledQueryRulesClient: &ScheduledQueryRulesClient,
AADDiagnosticSettingsClient: &AADDiagnosticSettingsClient,
AutoscaleSettingsClient: &AutoscaleSettingsClient,
ActionRulesClient: &ActionRulesClient,
SmartDetectorAlertRulesClient: &SmartDetectorAlertRulesClient,
ActionGroupsClient: &ActionGroupsClient,
ActivityLogAlertsClient: &ActivityLogAlertsClient,
AlertRulesClient: &AlertRulesClient,
DataCollectionEndpointsClient: &DataCollectionEndpointsClient,
DataCollectionRuleAssociationsClient: &DataCollectionRuleAssociationsClient,
DataCollectionRulesClient: &DataCollectionRulesClient,
DiagnosticSettingsClient: &DiagnosticSettingsClient,
DiagnosticSettingsCategoryClient: &DiagnosticSettingsCategoryClient,
LogProfilesClient: &LogProfilesClient,
MetricAlertsClient: &MetricAlertsClient,
PrivateLinkScopesClient: &PrivateLinkScopesClient,
PrivateLinkScopedResourcesClient: &PrivateLinkScopedResourcesClient,
ScheduledQueryRulesClient: &ScheduledQueryRulesClient,
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,246 @@
package monitor

import (
"context"
"fmt"
"time"

"github.com/hashicorp/go-azure-helpers/lang/response"
"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/datacollectionruleassociations"
"github.com/hashicorp/go-azure-sdk/resource-manager/insights/2021-04-01/datacollectionrules"
"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 DataCollectionRuleAssociationModel struct {
Name string `tfschema:"name"`
TargetResourceId string `tfschema:"target_resource_id"`
DataCollectionEndpointId string `tfschema:"data_collection_endpoint_id"`
DataCollectionRuleId string `tfschema:"data_collection_rule_id"`
Description string `tfschema:"description"`
}

type DataCollectionRuleAssociationResource struct{}

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

"target_resource_id": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: azure.ValidateResourceID,
},

"data_collection_endpoint_id": {
Type: pluginsdk.TypeString,
Optional: true,
ValidateFunc: datacollectionendpoints.ValidateDataCollectionEndpointID,
ExactlyOneOf: []string{"data_collection_endpoint_id", "data_collection_rule_id"},
},

"data_collection_rule_id": {
Type: pluginsdk.TypeString,
Optional: true,
ValidateFunc: datacollectionrules.ValidateDataCollectionRuleID,
ExactlyOneOf: []string{"data_collection_endpoint_id", "data_collection_rule_id"},
},

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

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

func (r DataCollectionRuleAssociationResource) ResourceType() string {
return "azurerm_monitor_data_collection_rule_association"
}

func (r DataCollectionRuleAssociationResource) IDValidationFunc() pluginsdk.SchemaValidateFunc {
return datacollectionruleassociations.ValidateScopedDataCollectionRuleAssociationID
}

func (r DataCollectionRuleAssociationResource) ModelObject() interface{} {
return &DataCollectionRuleAssociationModel{}
}

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

client := metadata.Client.Monitor.DataCollectionRuleAssociationsClient

id := datacollectionruleassociations.NewScopedDataCollectionRuleAssociationID(model.TargetResourceId, model.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 := datacollectionruleassociations.DataCollectionRuleAssociationProxyOnlyResource{
Name: utils.String(model.Name),
Properties: &datacollectionruleassociations.DataCollectionRuleAssociation{
Description: utils.String(model.Description),
},
}

if model.DataCollectionEndpointId != "" {
input.Properties.DataCollectionEndpointId = utils.String(model.DataCollectionEndpointId)
}
if model.DataCollectionRuleId != "" {
input.Properties.DataCollectionRuleId = utils.String(model.DataCollectionRuleId)
}

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 DataCollectionRuleAssociationResource) Read() sdk.ResourceFunc {
return sdk.ResourceFunc{
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.Monitor.DataCollectionRuleAssociationsClient
id, err := datacollectionruleassociations.ParseScopedDataCollectionRuleAssociationID(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 description, dataCollectionEndpointId, dataCollectionRuleId string

if model := resp.Model; model != nil {
if prop := model.Properties; prop != nil {
dataCollectionEndpointId = flattenStringPtr(prop.DataCollectionEndpointId)
dataCollectionRuleId = flattenStringPtr(prop.DataCollectionRuleId)
description = flattenStringPtr(prop.Description)
}
}

return metadata.Encode(&DataCollectionRuleAssociationModel{
Name: id.AssociationName,
TargetResourceId: id.ResourceUri,
DataCollectionEndpointId: dataCollectionEndpointId,
DataCollectionRuleId: dataCollectionRuleId,
Description: description,
})
},
Timeout: 5 * time.Minute,
}
}

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

metadata.Logger.Infof("updating %s..", *id)
client := metadata.Client.Monitor.DataCollectionRuleAssociationsClient
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 model DataCollectionRuleAssociationModel
if err := metadata.Decode(&model); err != nil {
return err
}

if metadata.ResourceData.HasChange("data_collection_endpoint_id") {
if model.DataCollectionEndpointId != "" {
existing.Properties.DataCollectionEndpointId = utils.String(model.DataCollectionEndpointId)
} else {
existing.Properties.DataCollectionEndpointId = nil
}
}

if metadata.ResourceData.HasChange("data_collection_rule_id") {
if model.DataCollectionRuleId != "" {
existing.Properties.DataCollectionRuleId = utils.String(model.DataCollectionRuleId)
} else {
existing.Properties.DataCollectionRuleId = nil
}
}

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

existing.SystemData = nil

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 DataCollectionRuleAssociationResource) Delete() sdk.ResourceFunc {
return sdk.ResourceFunc{
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.Monitor.DataCollectionRuleAssociationsClient
id, err := datacollectionruleassociations.ParseScopedDataCollectionRuleAssociationID(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,
}
}
Loading