Skip to content

Commit

Permalink
New Resource: azurerm_dynatrace_tag_rules (#27985)
Browse files Browse the repository at this point in the history
* New resource: azurerm_dynatrace_tag_rules

* update vendors
  • Loading branch information
jiaweitao001 authored Jan 10, 2025
1 parent d50951d commit 9959dbc
Show file tree
Hide file tree
Showing 24 changed files with 1,791 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/labeler-issue-triage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ service/domain-services:
- '### (|New or )Affected Resource\(s\)\/Data Source\(s\)((.|\n)*)azurerm_active_directory_domain_service((.|\n)*)###'

service/dynatrace:
- '### (|New or )Affected Resource\(s\)\/Data Source\(s\)((.|\n)*)azurerm_dynatrace_monitor((.|\n)*)###'
- '### (|New or )Affected Resource\(s\)\/Data Source\(s\)((.|\n)*)azurerm_dynatrace_((.|\n)*)###'

service/elastic:
- '### (|New or )Affected Resource\(s\)\/Data Source\(s\)((.|\n)*)azurerm_elastic_cloud_elasticsearch((.|\n)*)###'
Expand Down
9 changes: 9 additions & 0 deletions internal/services/dynatrace/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ import (
"fmt"

"github.com/hashicorp/go-azure-sdk/resource-manager/dynatrace/2023-04-27/monitors"
"github.com/hashicorp/go-azure-sdk/resource-manager/dynatrace/2023-04-27/tagrules"
"github.com/hashicorp/terraform-provider-azurerm/internal/common"
)

type Client struct {
*monitors.MonitorsClient
*tagrules.TagRulesClient
}

func NewClient(o *common.ClientOptions) (*Client, error) {
Expand All @@ -21,7 +23,14 @@ func NewClient(o *common.ClientOptions) (*Client, error) {
}
o.Configure(monitorClient.Client, o.Authorizers.ResourceManager)

tagruleClient, err := tagrules.NewTagRulesClientWithBaseURI(o.Environment.ResourceManager)
if err != nil {
return nil, fmt.Errorf("building Dynatrace TagRule client: %+v", err)
}
o.Configure(tagruleClient.Client, o.Authorizers.ResourceManager)

return &Client{
MonitorsClient: monitorClient,
TagRulesClient: tagruleClient,
}, nil
}
287 changes: 287 additions & 0 deletions internal/services/dynatrace/dynatrace_tag_rules_resource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,287 @@
package dynatrace

import (
"context"
"fmt"
"time"

"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-sdk/resource-manager/dynatrace/2023-04-27/monitors"
"github.com/hashicorp/go-azure-sdk/resource-manager/dynatrace/2023-04-27/tagrules"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"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 TagRulesResource struct{}

type TagRulesResourceModel struct {
Name string `tfschema:"name"`
Monitor string `tfschema:"monitor_id"`
LogRules []LogRule `tfschema:"log_rule"`
MetricRules []MetricRule `tfschema:"metric_rule"`
}

type MetricRule struct {
FilteringTags []FilteringTag `tfschema:"filtering_tag"`
}

type LogRule struct {
FilteringTags []FilteringTag `tfschema:"filtering_tag"`
SendAadLogs bool `tfschema:"send_azure_active_directory_logs_enabled"`
SendActivityLogs bool `tfschema:"send_activity_logs_enabled"`
SendSubscriptionLogs bool `tfschema:"send_subscription_logs_enabled"`
}

type FilteringTag struct {
Name string `tfschema:"name"`
Value string `tfschema:"value"`
Action string `tfschema:"action"`
}

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

"monitor_id": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: monitors.ValidateMonitorID,
},

"log_rule": {
Type: pluginsdk.TypeList,
Optional: true,
ForceNew: true,
MaxItems: 1,
Elem: &pluginsdk.Resource{
Schema: map[string]*schema.Schema{
"send_azure_active_directory_logs_enabled": {
Type: pluginsdk.TypeBool,
Optional: true,
ForceNew: true,
Default: false,
},

"send_activity_logs_enabled": {
Type: pluginsdk.TypeBool,
Optional: true,
ForceNew: true,
Default: false,
},

"send_subscription_logs_enabled": {
Type: pluginsdk.TypeBool,
Optional: true,
ForceNew: true,
Default: false,
},

"filtering_tag": {
Type: pluginsdk.TypeList,
Required: true,
ForceNew: true,
MinItems: 1,
Elem: &pluginsdk.Resource{
Schema: map[string]*schema.Schema{
"action": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{
"Include",
"Exclude",
}, false),
},

"name": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringIsNotEmpty,
},

"value": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringIsNotEmpty,
},
},
},
},
},
},
},

"metric_rule": {
Type: pluginsdk.TypeList,
Optional: true,
ForceNew: true,
MaxItems: 1,
Elem: &pluginsdk.Resource{
Schema: map[string]*schema.Schema{
"filtering_tag": {
Type: pluginsdk.TypeList,
Required: true,
ForceNew: true,
MinItems: 1,
Elem: &pluginsdk.Resource{
Schema: map[string]*schema.Schema{
"action": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{
"Include",
"Exclude",
}, false),
},

"name": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringIsNotEmpty,
},

"value": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringIsNotEmpty,
},
},
},
},
},
},
},
}
}

func (r TagRulesResource) Attributes() map[string]*schema.Schema {
return map[string]*schema.Schema{}
}

func (r TagRulesResource) ModelObject() interface{} {
return &TagRulesResourceModel{}
}

func (r TagRulesResource) ResourceType() string {
return "azurerm_dynatrace_tag_rules"
}

func (r TagRulesResource) Create() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 30 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.Dynatrace.TagRulesClient
subscriptionId := metadata.Client.Account.SubscriptionId

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

monitorsId, err := monitors.ParseMonitorID(model.Monitor)
id := tagrules.NewTagRuleID(subscriptionId, monitorsId.ResourceGroupName, monitorsId.MonitorName, model.Name)
if err != nil {
return err
}

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

if !response.WasNotFound(existing.HttpResponse) {
return metadata.ResourceRequiresImport(r.ResourceType(), id)
}

tagRulesProps := tagrules.MonitoringTagRulesProperties{
LogRules: ExpandLogRule(model.LogRules),
MetricRules: ExpandMetricRules(model.MetricRules),
}
tagRules := tagrules.TagRule{
Name: &model.Name,
Properties: tagRulesProps,
}

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

metadata.SetID(id)

return nil
},
}
}

func (r TagRulesResource) Read() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 5 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.Dynatrace.TagRulesClient
id, err := tagrules.ParseTagRuleID(metadata.ResourceData.Id())
if err != nil {
return err
}

resp, err := client.Get(ctx, *id)
if err != nil {
if response.WasNotFound(resp.HttpResponse) {
return metadata.MarkAsGone(id)
}
return fmt.Errorf("reading %s: %+v", id, err)
}
if model := resp.Model; model != nil {
props := model.Properties
monitorId := monitors.NewMonitorID(id.SubscriptionId, id.ResourceGroupName, id.MonitorName)

state := TagRulesResourceModel{
Name: id.TagRuleName,
Monitor: monitorId.ID(),
LogRules: FlattenLogRules(props.LogRules),
MetricRules: FlattenMetricRules(props.MetricRules),
}

return metadata.Encode(&state)
}

return nil
},
}
}

func (r TagRulesResource) Delete() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 30 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.Dynatrace.TagRulesClient
id, err := tagrules.ParseTagRuleID(metadata.ResourceData.Id())
if err != nil {
return err
}

metadata.Logger.Infof("deleting %s", *id)

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

func (r TagRulesResource) IDValidationFunc() pluginsdk.SchemaValidateFunc {
return tagrules.ValidateTagRuleID
}
Loading

0 comments on commit 9959dbc

Please sign in to comment.