-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New Resource:
azurerm_monitor_data_collection_rule_association
(#17948
- Loading branch information
Showing
20 changed files
with
1,795 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
248 changes: 248 additions & 0 deletions
248
internal/services/monitor/monitor_data_collection_rule_association_resource.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,248 @@ | ||
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{ | ||
"target_resource_id": { | ||
Type: pluginsdk.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
ValidateFunc: azure.ValidateResourceID, | ||
}, | ||
|
||
"name": { | ||
Type: pluginsdk.TypeString, | ||
Optional: true, | ||
ForceNew: true, | ||
Default: "configurationAccessEndpoint", | ||
ValidateFunc: validation.StringIsNotEmpty, | ||
}, | ||
|
||
"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"}, | ||
RequiredWith: []string{"name"}, | ||
}, | ||
|
||
"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, | ||
} | ||
} |
Oops, something went wrong.