-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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_dynatrace_tag_rules
#27985
Open
jiaweitao001
wants to merge
19
commits into
hashicorp:main
Choose a base branch
from
jiaweitao001:dynatrace
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,752
−3
Open
Changes from 18 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
98e2e8f
New resources: azurerm_dynatrace_monitors, azurerm_dynatrace_tag_rules
jiaweitao001 d501e48
Adding documentation
jiaweitao001 6c9301b
Add subcategory
jiaweitao001 1c8d590
Renaming parameters
jiaweitao001 d379b76
Fix acc tests
jiaweitao001 b5eaf49
Renaming variables
jiaweitao001 7df2f50
fix schema
jiaweitao001 76c7545
fix imports
jiaweitao001 ed1eafb
retrigger teamcity run
jiaweitao001 2c250aa
New resource: dynatrace_tag_rules
jiaweitao001 9db94f8
New resource: azurerm_dynatrace_tag_rules
jiaweitao001 b761eff
prevent acc test auto trigger
jiaweitao001 1f7ffb1
fix vendor
jiaweitao001 00c66c4
fix acc tests
jiaweitao001 35d2f44
fix go generate
jiaweitao001 9164bee
address comments, change attribute types
jiaweitao001 cb2e1f1
fix indent
jiaweitao001 1007af6
sort imports
jiaweitao001 55584b9
prevent acc tests auto triggering
jiaweitao001 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
273 changes: 273 additions & 0 deletions
273
internal/services/dynatrace/dynatrace_tag_rules_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,273 @@ | ||
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, | ||
}, | ||
|
||
"send_activity_logs_enabled": { | ||
Type: pluginsdk.TypeBool, | ||
Optional: true, | ||
}, | ||
|
||
"send_subscription_logs_enabled": { | ||
Type: pluginsdk.TypeBool, | ||
Optional: true, | ||
}, | ||
|
||
"filtering_tag": { | ||
Type: pluginsdk.TypeList, | ||
Required: true, | ||
MinItems: 1, | ||
Elem: &pluginsdk.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"action": { | ||
Type: pluginsdk.TypeString, | ||
Required: true, | ||
ValidateFunc: validation.StringInSlice([]string{ | ||
"Include", | ||
"Exclude", | ||
}, false), | ||
}, | ||
|
||
"name": { | ||
Type: pluginsdk.TypeString, | ||
Required: true, | ||
ValidateFunc: validation.StringIsNotEmpty, | ||
}, | ||
|
||
"value": { | ||
Type: pluginsdk.TypeString, | ||
Required: 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, | ||
MinItems: 1, | ||
Elem: &pluginsdk.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"action": { | ||
Type: pluginsdk.TypeString, | ||
Required: true, | ||
ValidateFunc: validation.StringInSlice([]string{ | ||
"Include", | ||
"Exclude", | ||
}, false), | ||
}, | ||
|
||
"name": { | ||
Type: pluginsdk.TypeString, | ||
Required: true, | ||
ValidateFunc: validation.StringIsNotEmpty, | ||
}, | ||
|
||
"value": { | ||
Type: pluginsdk.TypeString, | ||
Required: 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 | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do these have default values?