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

[datadog_logs_metric][datadog_metric_metadata] Normalize invalid metric name #2433

Merged
merged 5 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
34 changes: 34 additions & 0 deletions datadog/internal/validators/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,40 @@ func ValidateNonEmptyStrings(v any, p cty.Path) diag.Diagnostics {
return diags
}

// ValidateDatadogMetricName ensures a string is a valid metric metadata name
func ValidateDatadogMetricName(v any, p cty.Path) diag.Diagnostics {
value, ok := v.(string)
var diags diag.Diagnostics
if !ok {
return append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "Invalid value",
Detail: "Metric name must be a string.",
})
}
// Validate metric name format
match, _ := regexp.MatchString("^[a-zA-Z][a-zA-Z0-9_.]*$", value)
if !match {
diags = append(diags, diag.Diagnostic{
Severity: diag.Warning,
Summary: "Invalid value",
Detail: "Metric name must start with a letter and can only contain ASCII alphanumerics, underscores, and periods.",
AttributePath: p,
})
}

// Validate metric name length
if len(value) > 200 {
diags = append(diags, diag.Diagnostic{
Severity: diag.Warning,
Summary: "Invalid value",
Detail: "Metric name cannot exceed 200 characters.",
AttributePath: p,
})
}
nkzou marked this conversation as resolved.
Show resolved Hide resolved
return diags
}

// ValidateDatadogDowntimeRecurrenceType ensures a string is a valid recurrence type
func ValidateDatadogDowntimeRecurrenceType(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
Expand Down
9 changes: 5 additions & 4 deletions datadog/resource_datadog_logs_metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,11 @@ func resourceDatadogLogsMetric() *schema.Resource {
},

"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The name of the log-based metric. This field can't be updated after creation.",
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The name of the log-based metric. This field can't be updated after creation.",
ValidateDiagFunc: validators.ValidateDatadogMetricName,
},
}
},
Expand Down
8 changes: 5 additions & 3 deletions datadog/resource_datadog_metric_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"

"github.com/terraform-providers/terraform-provider-datadog/datadog/internal/utils"
"github.com/terraform-providers/terraform-provider-datadog/datadog/internal/validators"

"github.com/DataDog/datadog-api-client-go/v2/api/datadog"
"github.com/DataDog/datadog-api-client-go/v2/api/datadogV1"
Expand All @@ -25,9 +26,10 @@ func resourceDatadogMetricMetadata() *schema.Resource {
SchemaFunc: func() map[string]*schema.Schema {
return map[string]*schema.Schema{
"metric": {
Description: "The name of the metric.",
Type: schema.TypeString,
Required: true,
Description: "The name of the metric.",
Type: schema.TypeString,
Required: true,
ValidateDiagFunc: validators.ValidateDatadogMetricName,
},
"type": {
Description: "Metric type such as `count`, `gauge`, or `rate`. Updating a metric of type `distribution` is not supported. If you would like to see the `distribution` type returned, contact [Datadog support](https://docs.datadoghq.com/help/).",
Expand Down
Loading