Skip to content
Draft
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions docs/data-sources/metric.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ data "launchdarkly_metric" "example" {
### Read-Only

- `analysis_type` (String) The method for analyzing metric events. Available choices are `mean` and `percentile`.
- `archived` (Boolean) Whether the metric is archived.
- `description` (String) The description of the metric's purpose.
- `event_key` (String) The event key for your metric (if custom metric)
- `id` (String) The ID of this resource.
Expand Down
1 change: 1 addition & 0 deletions docs/resources/metric.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ resource "launchdarkly_metric" "example" {
### Optional

- `analysis_type` (String) The method for analyzing metric events. Available choices are `mean` and `percentile`.
- `archived` (Boolean) Whether the metric is archived.
- `description` (String) The description of the metric's purpose.
- `event_key` (String) The event key for your metric (if custom metric)
- `include_units_without_events` (Boolean) Include units that did not send any events and set their value to 0.
Expand Down
121 changes: 121 additions & 0 deletions launchdarkly/data_source_launchdarkly_metric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,124 @@ func TestAccDataSourceMetric_exists(t *testing.T) {
},
})
}

func TestAccDataSourceMetric_ArchivedField(t *testing.T) {
accTest := os.Getenv("TF_ACC")
if accTest == "" {
t.SkipNow()
}
client, err := newClient(os.Getenv(LAUNCHDARKLY_ACCESS_TOKEN), os.Getenv(LAUNCHDARKLY_API_HOST), false, DEFAULT_HTTP_TIMEOUT_S, DEFAULT_MAX_CONCURRENCY)
require.NoError(t, err)
projectKey := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)
projectBody := ldapi.ProjectPost{
Name: "Terraform Metric Archived Test Project",
Key: projectKey,
}
project, err := testAccProjectScaffoldCreate(client, projectBody)
require.NoError(t, err)

defer func() {
err := testAccProjectScaffoldDelete(client, projectKey)
require.NoError(t, err)
}()

// Create archived metric (without Archived field since API client doesn't support it yet)
archivedMetricKey := "archived-metric"
archivedMetricName := "Archived Test Metric"
archivedMetricDescription := "Test metric for archived field testing"
archivedUrlKind := "substring"
archivedUrlSubstring := "archived-test"
archivedMetricBody := ldapi.MetricPost{
Name: &archivedMetricName,
Key: archivedMetricKey,
Description: ldapi.PtrString(archivedMetricDescription),
Kind: "pageview",
Tags: []string{"test", "archived"},
Urls: []ldapi.UrlPost{
{
Kind: &archivedUrlKind,
Substring: &archivedUrlSubstring,
},
},
}
// Create archived metric directly without scaffold (to avoid duplicate project creation)
_, _, err = client.ld.MetricsApi.PostMetric(client.ctx, project.Key).MetricPost(archivedMetricBody).Execute()
require.NoError(t, err)

// Create non-archived metric (without Archived field since API client doesn't support it yet)
nonArchivedMetricKey := "non-archived-metric"
nonArchivedMetricName := "Non-Archived Test Metric"
nonArchivedMetricDescription := "Test metric for non-archived field testing"
nonArchivedUrlKind := "substring"
nonArchivedUrlSubstring := "non-archived-test"
nonArchivedMetricBody := ldapi.MetricPost{
Name: &nonArchivedMetricName,
Key: nonArchivedMetricKey,
Description: ldapi.PtrString(nonArchivedMetricDescription),
Kind: "pageview",
Tags: []string{"test", "non-archived"},
Urls: []ldapi.UrlPost{
{
Kind: &nonArchivedUrlKind,
Substring: &nonArchivedUrlSubstring,
},
},
}
// Create non-archived metric directly without scaffold (to avoid duplicate project creation)
_, _, err = client.ld.MetricsApi.PostMetric(client.ctx, project.Key).MetricPost(nonArchivedMetricBody).Execute()
require.NoError(t, err)

// Test data source configurations
testAccDataSourceMetricArchived := `
data "launchdarkly_metric" "archived" {
key = "%s"
project_key = "%s"
}
`

testAccDataSourceMetricNonArchived := `
data "launchdarkly_metric" "non_archived" {
key = "%s"
project_key = "%s"
}
`

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
},
Providers: testAccProviders,
Steps: []resource.TestStep{
// Test reading archived metric (currently returns false since API client doesn't support Archived field yet)
{
Config: fmt.Sprintf(testAccDataSourceMetricArchived, archivedMetricKey, project.Key),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.launchdarkly_metric.archived", KEY, archivedMetricKey),
resource.TestCheckResourceAttr("data.launchdarkly_metric.archived", NAME, "Archived Test Metric"),
resource.TestCheckResourceAttr("data.launchdarkly_metric.archived", PROJECT_KEY, project.Key),
resource.TestCheckResourceAttr("data.launchdarkly_metric.archived", KIND, "pageview"),
resource.TestCheckResourceAttr("data.launchdarkly_metric.archived", ARCHIVED, "false"), // Default value until API client supports Archived field
resource.TestCheckResourceAttr("data.launchdarkly_metric.archived", "tags.0", "test"),
resource.TestCheckResourceAttr("data.launchdarkly_metric.archived", "tags.1", "archived"),
resource.TestCheckResourceAttr("data.launchdarkly_metric.archived", "urls.0.kind", "substring"),
resource.TestCheckResourceAttr("data.launchdarkly_metric.archived", "urls.0.substring", "archived-test"),
),
},
// Test reading non-archived metric
{
Config: fmt.Sprintf(testAccDataSourceMetricNonArchived, nonArchivedMetricKey, project.Key),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.launchdarkly_metric.non_archived", KEY, nonArchivedMetricKey),
resource.TestCheckResourceAttr("data.launchdarkly_metric.non_archived", NAME, "Non-Archived Test Metric"),
resource.TestCheckResourceAttr("data.launchdarkly_metric.non_archived", PROJECT_KEY, project.Key),
resource.TestCheckResourceAttr("data.launchdarkly_metric.non_archived", KIND, "pageview"),
resource.TestCheckResourceAttr("data.launchdarkly_metric.non_archived", ARCHIVED, "false"),
resource.TestCheckResourceAttr("data.launchdarkly_metric.non_archived", "tags.0", "test"),
resource.TestCheckResourceAttr("data.launchdarkly_metric.non_archived", "tags.1", "non-archived"),
resource.TestCheckResourceAttr("data.launchdarkly_metric.non_archived", "urls.0.kind", "substring"),
resource.TestCheckResourceAttr("data.launchdarkly_metric.non_archived", "urls.0.substring", "non-archived-test"),
),
},
},
})
}
7 changes: 7 additions & 0 deletions launchdarkly/metrics_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,12 @@ func baseMetricSchema(isDataSource bool) map[string]*schema.Schema {
Computed: true,
Description: "Version of the metric",
},
ARCHIVED: {
Type: schema.TypeBool,
Optional: !isDataSource,
Computed: true,
Description: "Whether the metric is archived.",
},
}

if isDataSource {
Expand Down Expand Up @@ -210,6 +216,7 @@ func metricRead(ctx context.Context, d *schema.ResourceData, metaRaw interface{}
_ = d.Set(ANALYSIS_TYPE, metric.AnalysisType)
_ = d.Set(PERCENTILE_VALUE, metric.PercentileValue)
_ = d.Set(VERSION, metric.Version)
// _ = d.Set(ARCHIVED, metric.Archived) // Uncomment when API client is updated with archived field

d.SetId(projectKey + "/" + key)

Expand Down
4 changes: 4 additions & 0 deletions launchdarkly/resource_launchdarkly_metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ func resourceMetricCreate(ctx context.Context, d *schema.ResourceData, metaRaw i
analysisType := d.Get(ANALYSIS_TYPE).(string)
includeUnitsWithoutEvents := d.Get(INCLUDE_UNITS_WITHOUT_EVENTS).(bool)
eventDefaultDisabled := !includeUnitsWithoutEvents
// archived := d.Get(ARCHIVED).(bool) // Uncomment when API client is updated with archived field

metric := ldapi.MetricPost{
Name: &name,
Expand All @@ -220,6 +221,7 @@ func resourceMetricCreate(ctx context.Context, d *schema.ResourceData, metaRaw i
UnitAggregationType: &unitAggregationType,
AnalysisType: &analysisType,
EventDefault: &ldapi.MetricEventDefaultRep{Disabled: &eventDefaultDisabled},
// Archived: &archived, // Uncomment when API client is updated with archived field
}
percentileValueData, hasPercentile := d.GetOk(PERCENTILE_VALUE)
if hasPercentile {
Expand Down Expand Up @@ -310,6 +312,7 @@ func resourceMetricUpdate(ctx context.Context, d *schema.ResourceData, metaRaw i
unitAggregationType := d.Get(UNIT_AGGREGATION_TYPE).(string)
analysisType := d.Get(ANALYSIS_TYPE).(string)
includeUnitsWithoutEvents := d.Get(INCLUDE_UNITS_WITHOUT_EVENTS).(bool)
// archived := d.Get(ARCHIVED).(bool) // Uncomment when API client is updated with archived field

patch := []ldapi.PatchOperation{
patchReplace("/name", name),
Expand All @@ -325,6 +328,7 @@ func resourceMetricUpdate(ctx context.Context, d *schema.ResourceData, metaRaw i
patchReplace("/unitAggregationType", unitAggregationType),
patchReplace("/analysisType", analysisType),
patchReplace("/eventDefault/disabled", !includeUnitsWithoutEvents),
// patchReplace("/archived", archived), // Uncomment when API client is updated with archived field
}

percentileValueData, ok := d.GetOk(PERCENTILE_VALUE)
Expand Down
134 changes: 134 additions & 0 deletions launchdarkly/resource_launchdarkly_metric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -650,3 +650,137 @@ func testAccCheckMetricExists(resourceName string) resource.TestCheckFunc {
return nil
}
}

func TestAccMetric_ArchivedField(t *testing.T) {
accTest := os.Getenv("TF_ACC")
if accTest == "" {
t.SkipNow()
}
projectKey := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)
resourceName := "launchdarkly_metric.archived_test"

// Test configuration with archived = true
testAccMetricArchivedTrue := `
resource "launchdarkly_metric" "archived_test" {
project_key = launchdarkly_project.test.key
key = "archived-metric"
name = "Archived Metric"
description = "Test metric for archived field"
kind = "pageview"
archived = true
tags = ["test", "archived"]
urls {
kind = "substring"
substring = "test"
}
}
`

// Test configuration with archived = false
testAccMetricArchivedFalse := `
resource "launchdarkly_metric" "archived_test" {
project_key = launchdarkly_project.test.key
key = "archived-metric"
name = "Archived Metric"
description = "Test metric for archived field"
kind = "pageview"
archived = false
tags = ["test", "archived"]
urls {
kind = "substring"
substring = "test"
}
}
`

// Test configuration without archived field (should default to false)
testAccMetricArchivedDefault := `
resource "launchdarkly_metric" "archived_test" {
project_key = launchdarkly_project.test.key
key = "archived-metric"
name = "Archived Metric"
description = "Test metric for archived field"
kind = "pageview"
tags = ["test", "archived"]
urls {
kind = "substring"
substring = "test"
}
}
`

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
},
Providers: testAccProviders,
Steps: []resource.TestStep{
// Step 1: Create metric with archived = true
{
Config: withRandomProject(projectKey, testAccMetricArchivedTrue),
Check: resource.ComposeTestCheckFunc(
testAccCheckProjectExists("launchdarkly_project.test"),
testAccCheckMetricExists(resourceName),
resource.TestCheckResourceAttr(resourceName, KEY, "archived-metric"),
resource.TestCheckResourceAttr(resourceName, NAME, "Archived Metric"),
resource.TestCheckResourceAttr(resourceName, PROJECT_KEY, projectKey),
resource.TestCheckResourceAttr(resourceName, KIND, "pageview"),
resource.TestCheckResourceAttr(resourceName, ARCHIVED, "true"),
resource.TestCheckResourceAttr(resourceName, "tags.0", "archived"),
resource.TestCheckResourceAttr(resourceName, "tags.1", "test"),
),
},
// Step 2: Import state verification for archived = true
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{ARCHIVED}, // TODO: Remove when API client supports archived field
},
// Step 3: Update metric to archived = false
{
Config: withRandomProject(projectKey, testAccMetricArchivedFalse),
Check: resource.ComposeTestCheckFunc(
testAccCheckProjectExists("launchdarkly_project.test"),
testAccCheckMetricExists(resourceName),
resource.TestCheckResourceAttr(resourceName, KEY, "archived-metric"),
resource.TestCheckResourceAttr(resourceName, NAME, "Archived Metric"),
resource.TestCheckResourceAttr(resourceName, PROJECT_KEY, projectKey),
resource.TestCheckResourceAttr(resourceName, KIND, "pageview"),
resource.TestCheckResourceAttr(resourceName, ARCHIVED, "false"),
resource.TestCheckResourceAttr(resourceName, "tags.0", "archived"),
resource.TestCheckResourceAttr(resourceName, "tags.1", "test"),
),
},
// Step 4: Import state verification for archived = false
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{ARCHIVED}, // TODO: Remove when API client supports archived field
},
// Step 5: Remove archived field (should default to false)
{
Config: withRandomProject(projectKey, testAccMetricArchivedDefault),
Check: resource.ComposeTestCheckFunc(
testAccCheckProjectExists("launchdarkly_project.test"),
testAccCheckMetricExists(resourceName),
resource.TestCheckResourceAttr(resourceName, KEY, "archived-metric"),
resource.TestCheckResourceAttr(resourceName, NAME, "Archived Metric"),
resource.TestCheckResourceAttr(resourceName, PROJECT_KEY, projectKey),
resource.TestCheckResourceAttr(resourceName, KIND, "pageview"),
resource.TestCheckResourceAttr(resourceName, ARCHIVED, "false"),
resource.TestCheckResourceAttr(resourceName, "tags.0", "archived"),
resource.TestCheckResourceAttr(resourceName, "tags.1", "test"),
),
},
// Step 6: Import state verification for default archived value
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{ARCHIVED}, // TODO: Remove when API client supports archived field
},
},
})
}
Loading