Skip to content

Commit

Permalink
azurerm_datadog_monitor_tag_rule - correctly handle default rule (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
neil-yechenwei authored Nov 30, 2023
1 parent 2d85390 commit 277bf51
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
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

0 comments on commit 277bf51

Please sign in to comment.