-
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 resouce
azurerm_monitor_data_collection_endpoint
- Loading branch information
Showing
24 changed files
with
1,664 additions
and
0 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
298 changes: 298 additions & 0 deletions
298
internal/services/monitor/monitor_data_collection_endpoint_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,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 | ||
} |
Oops, something went wrong.