Skip to content

Commit

Permalink
Working base version
Browse files Browse the repository at this point in the history
  • Loading branch information
hemeryar committed Nov 8, 2024
1 parent 8a81370 commit 74da99a
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 37 deletions.
31 changes: 16 additions & 15 deletions datadog/fwprovider/resource_datadog_rum_metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,20 +89,6 @@ func (r *rumMetricResource) Schema(_ context.Context, _ resource.SchemaRequest,
"id": utils.ResourceIDAttribute(),
},
Blocks: map[string]schema.Block{
"group_by": schema.ListNestedBlock{
NestedObject: schema.NestedBlockObject{
Attributes: map[string]schema.Attribute{
"path": schema.StringAttribute{
Description: "The path to the value the rum-based metric will be aggregated over.",
Optional: true,
},
"tag_name": schema.StringAttribute{
Description: "Eventual name of the tag that gets created. By default, `path` is used as the tag name.",
Optional: true,
},
},
},
},
"compute": schema.SingleNestedBlock{
Attributes: map[string]schema.Attribute{
"aggregation_type": schema.StringAttribute{
Expand Down Expand Up @@ -130,6 +116,20 @@ func (r *rumMetricResource) Schema(_ context.Context, _ resource.SchemaRequest,
},
},
},
"group_by": schema.SetNestedBlock{
NestedObject: schema.NestedBlockObject{
Attributes: map[string]schema.Attribute{
"path": schema.StringAttribute{
Description: "The path to the value the rum-based metric will be aggregated over.",
Optional: true,
},
"tag_name": schema.StringAttribute{
Description: "Eventual name of the tag that gets created. By default, `path` is used as the tag name.",
Optional: true,
},
},
},
},
"uniqueness": schema.SingleNestedBlock{
Attributes: map[string]schema.Attribute{
"when": schema.StringAttribute{
Expand Down Expand Up @@ -267,7 +267,6 @@ func (r *rumMetricResource) updateState(ctx context.Context, state *rumMetricMod
state.GroupBy = []*rumMetricGroupByModel{}
for _, groupByDdItem := range *groupBy {
groupByTfItem := rumMetricGroupByModel{}

if path, ok := groupByDdItem.GetPathOk(); ok {
groupByTfItem.Path = types.StringValue(*path)
}
Expand Down Expand Up @@ -385,6 +384,8 @@ func (r *rumMetricResource) buildRumMetricUpdateRequestBody(ctx context.Context,
if !groupByTFItem.TagName.IsNull() {
groupByDDItem.SetTagName(groupByTFItem.TagName.ValueString())
}

groupBy = append(groupBy, *groupByDDItem)
}
attributes.SetGroupBy(groupBy)
}
Expand Down
133 changes: 111 additions & 22 deletions datadog/tests/resource_datadog_rum_metric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,16 @@ import (
"github.com/terraform-providers/terraform-provider-datadog/datadog/internal/utils"
)

func TestAccRumMetric_import(t *testing.T) {
func TestAccRumMetricImport(t *testing.T) {
t.Parallel()
resourceName := "datadog_rum_metric.testing_rum_metric"
ctx, providers, accProviders := testAccFrameworkMuxProviders(context.Background(), t)

// The API will currently silently remap - to _, which makes terraform unhappy. This will
// just make the tests pass but this is a real issue.
// Being addressed in https://datadoghq.atlassian.net/browse/RUM-7124. Note that this is
// also an issue for other existing APIs using the same backend (spans metrics and maybe
// logs metrics?).
uniq := strings.ReplaceAll(uniqueEntityName(ctx, t), "-", "_")

resource.Test(t, resource.TestCase{
Expand All @@ -25,7 +31,7 @@ func TestAccRumMetric_import(t *testing.T) {
CheckDestroy: testAccCheckDatadogRumMetricDestroy(providers.frameworkProvider),
Steps: []resource.TestStep{
{
Config: testAccCheckDatadogRumMetric(uniq),
Config: minimalDatadogRumMetric(uniq),
},
{
ResourceName: resourceName,
Expand All @@ -36,7 +42,7 @@ func TestAccRumMetric_import(t *testing.T) {
})
}

func TestAccRumMetricBasic(t *testing.T) {
func TestAccRumMetricAttributes(t *testing.T) {
t.Parallel()
ctx, providers, accProviders := testAccFrameworkMuxProviders(context.Background(), t)
uniq := strings.ReplaceAll(uniqueEntityName(ctx, t), "-", "_")
Expand All @@ -46,56 +52,139 @@ func TestAccRumMetricBasic(t *testing.T) {
CheckDestroy: testAccCheckDatadogRumMetricDestroy(providers.frameworkProvider),
Steps: []resource.TestStep{
{
Config: testAccCheckDatadogRumMetric(uniq),
Config: minimalDatadogRumMetric(uniq),
Check: resource.ComposeTestCheckFunc(
testAccCheckDatadogRumMetricExists(providers.frameworkProvider),
resource.TestCheckResourceAttr(
"datadog_rum_metric.testing_rum_metric", "id", uniq),
resource.TestCheckResourceAttr(
"datadog_rum_metric.testing_rum_metric", "name", uniq),
resource.TestCheckResourceAttr(
"datadog_rum_metric.testing_rum_metric", "event_type", "session"),
"datadog_rum_metric.testing_rum_metric", "event_type", "action"),
resource.TestCheckResourceAttr(
"datadog_rum_metric.testing_rum_metric", "compute.aggregation_type", "count"),
),
},
},
})

resource.Test(t, resource.TestCase{
ProtoV5ProviderFactories: accProviders,
CheckDestroy: testAccCheckDatadogRumMetricDestroy(providers.frameworkProvider),
Steps: []resource.TestStep{
{
Config: distributionDatadogRumMetric(uniq),
Check: resource.ComposeTestCheckFunc(
testAccCheckDatadogRumMetricExists(providers.frameworkProvider),
resource.TestCheckResourceAttr(
"datadog_rum_metric.testing_rum_metric", "compute.aggregation_type", "distribution"),
resource.TestCheckResourceAttr(
"datadog_rum_metric.testing_rum_metric", "compute.include_percentiles", "true"),
resource.TestCheckResourceAttr(
"datadog_rum_metric.testing_rum_metric", "compute.path", "@duration"),
),
},
},
})

resource.Test(t, resource.TestCase{
ProtoV5ProviderFactories: accProviders,
CheckDestroy: testAccCheckDatadogRumMetricDestroy(providers.frameworkProvider),
Steps: []resource.TestStep{
{
Config: filterDatadogRumMetric(uniq),
Check: resource.ComposeTestCheckFunc(
testAccCheckDatadogRumMetricExists(providers.frameworkProvider),
resource.TestCheckResourceAttr(
"datadog_rum_metric.testing_rum_metric", "filter.query", "@service:web-ui"),
resource.TestCheckResourceAttr(
"datadog_rum_metric.testing_rum_metric", "group_by.#", "1"),
resource.TestCheckResourceAttr(
"datadog_rum_metric.testing_rum_metric", "group_by.0.path", "@browser.name"),
resource.TestCheckResourceAttr(
"datadog_rum_metric.testing_rum_metric", "group_by.0.tag_name", "browser_name"),
resource.TestCheckResourceAttr(
"datadog_rum_metric.testing_rum_metric", "uniqueness.when", "match"),
),
},
},
})

resource.Test(t, resource.TestCase{
ProtoV5ProviderFactories: accProviders,
CheckDestroy: testAccCheckDatadogRumMetricDestroy(providers.frameworkProvider),
Steps: []resource.TestStep{
{
Config: groupByDatadogRumMetric(uniq),
Check: testAccCheckDatadogRumMetricExists(providers.frameworkProvider),
// resource.ComposeTestCheckFunc(

// resource.TestCheckResourceAttr(
// "datadog_rum_metric.testing_rum_metric", "group_by.#", "2"),
// ),
},
},
})
}

func minimalDatadogRumMetric(uniq string) string {
return fmt.Sprintf(`resource "datadog_rum_metric" "testing_rum_metric" {
name = %q
event_type = "action"
compute {
aggregation_type = "count"
}
}
`, uniq)
}

func testAccCheckDatadogRumMetric(uniq string) string {
func distributionDatadogRumMetric(uniq string) string {
return fmt.Sprintf(`resource "datadog_rum_metric" "testing_rum_metric" {
name = %q
event_type = "session"
event_type = "action"
compute {
aggregation_type = "distribution"
include_percentiles = true
path = "@duration"
}
filter {
query = "@service:web-ui"
}
`, uniq)
}

func countDatadogRumMetric(uniq string) string {
return fmt.Sprintf(`resource "datadog_rum_metric" "testing_rum_metric" {
name = %q
event_type = "action"
compute {
aggregation_type = "count"
}
group_by {
path = "@browser.name"
tag_name = "browser_name"
}
`, uniq)
}

func filterDatadogRumMetric(uniq string) string {
return fmt.Sprintf(`resource "datadog_rum_metric" "testing_rum_metric" {
name = %q
event_type = "action"
compute {
aggregation_type = "count"
}
uniqueness {
when = "match"
filter {
query = "@service:web-ui"
}
}
`, uniq)
}

func groupByDatadogRumMetric(uniq string) string {
// Note: the group_bys are not defined in alphabetical order. This is on purpose to verify
// a set behavior rather than a list behavior on the terraform attribute.
return fmt.Sprintf(`resource "datadog_rum_metric" "testing_rum_metric" {
name = %q
event_type = "action"
compute {
aggregation_type = "count"
}
group_by {
path = "@service"
tag_name = "service"
}
group_by {
path = "@os"
tag_name = "os"
}
}
`, uniq)
}
Expand Down

0 comments on commit 74da99a

Please sign in to comment.