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

azurerm_datadog_monitor_tag_rule - correctly handle default rule #22806

Merged
merged 10 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 30 additions & 3 deletions internal/services/datadog/datadog_monitor_tag_rule_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func resourceDatadogTagRulesCreate(d *pluginsdk.ResourceData, meta interface{})
return fmt.Errorf("checking for an existing %s: %+v", id, err)
}
}
if !response.WasNotFound(existing.HttpResponse) {
if !response.WasNotFound(existing.HttpResponse) && !isDefaultSettings(existing.Model) {
return tf.ImportAsExistsError("azurerm_datadog_monitor_tag_rule", id.ID())
}

Expand Down Expand Up @@ -242,8 +242,15 @@ func resourceDatadogTagRulesDelete(d *pluginsdk.ResourceData, meta interface{})
// Tag Rules can't be removed on their own, they can only be nil'd out
payload := rules.MonitoringTagRules{
Properties: &rules.MonitoringTagRulesProperties{
LogRules: expandLogRules(d.Get("log").([]interface{})),
MetricRules: expandMetricRules(d.Get("metric").([]interface{})),
LogRules: &rules.LogRules{
SendAadLogs: utils.Bool(false),
SendSubscriptionLogs: utils.Bool(false),
SendResourceLogs: utils.Bool(false),
FilteringTags: &[]rules.FilteringTag{},
},
MetricRules: &rules.MetricRules{
FilteringTags: &[]rules.FilteringTag{},
},
},
}
if _, err := client.TagRulesCreateOrUpdate(ctx, *id, payload); err != nil {
Expand Down Expand Up @@ -363,3 +370,23 @@ func flattenFilteringTags(input *[]rules.FilteringTag) []interface{} {
}
return results
}

func isDefaultSettings(input *rules.MonitoringTagRules) bool {
if input == nil {
return false
}

if input.Properties == nil || input.Properties.LogRules == nil || input.Properties.MetricRules == nil {
return false
}

logRules := input.Properties.LogRules
metricRules := input.Properties.MetricRules
result := (logRules.SendAadLogs != nil && !*logRules.SendAadLogs) &&
(logRules.SendSubscriptionLogs != nil && !*logRules.SendSubscriptionLogs) &&
(logRules.SendResourceLogs != nil && !*logRules.SendResourceLogs) &&
(logRules.FilteringTags != nil && len(*logRules.FilteringTags) == 0) &&
(metricRules.FilteringTags != nil && len(*metricRules.FilteringTags) == 0)

return result
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,6 @@ func (r TagRulesDatadogMonitorResource) Exists(ctx context.Context, client *clie

func (r TagRulesDatadogMonitorResource) template(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}
resource "azurerm_resource_group" "test" {
name = "acctest-datadogrg-%[1]d"
location = %[2]q
Expand Down
Loading