Skip to content
Open
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
135 changes: 131 additions & 4 deletions axiom/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func TestAccAxiomResources_basic(t *testing.T) {
testAccCheckAxiomResourcesExist(client, "axiom_dataset.test_without_description"),
resource.TestCheckResourceAttr("axiom_dataset.test", "name", "terraform-provider-dataset"),
resource.TestCheckResourceAttr("axiom_dataset.test", "description", "A test dataset"),
resource.TestCheckResourceAttr("axiom_dataset.test", "kind", "axiom:events:v1"),
resource.TestCheckResourceAttr("axiom_dataset.test", "use_retention_period", "false"),
resource.TestCheckResourceAttr("axiom_dataset.test", "retention_days", "0"),
resource.TestCheckResourceAttr("axiom_virtual_field.test", "name", "VF"),
Expand Down Expand Up @@ -108,6 +109,69 @@ func TestAccAxiomResources_data(t *testing.T) {
})
}

func TestAccAxiomDataset_kind(t *testing.T) {
client, err := ax.NewClient()
assert.NoError(t, err)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){
"axiom": providerserver.NewProtocol6WithError(NewAxiomProvider()),
},
CheckDestroy: testAccCheckAxiomResourcesDestroyed(client),
Steps: []resource.TestStep{
{
Config: `
provider "axiom" {
api_token = "` + os.Getenv("AXIOM_TOKEN") + `"
base_url = "` + os.Getenv("AXIOM_URL") + `"
}

resource "axiom_dataset" "test_otel_metrics" {
name = "test-otel-metrics-` + uuid.NewString() + `"
kind = "otel:metrics:v1"
description = "Test OTEL metrics dataset"
}

resource "axiom_dataset" "test_otel_traces" {
name = "test-otel-traces-` + uuid.NewString() + `"
kind = "otel:traces:v1"
description = "Test OTEL traces dataset"
}

resource "axiom_dataset" "test_otel_logs" {
name = "test-otel-logs-` + uuid.NewString() + `"
kind = "otel:logs:v1"
description = "Test OTEL logs dataset"
}

resource "axiom_dataset" "test_default_kind" {
name = "test-default-kind-` + uuid.NewString() + `"
description = "Test dataset with default kind"
}
`,
Check: resource.ComposeTestCheckFunc(
testAccCheckAxiomResourcesExist(client, "axiom_dataset.test_otel_metrics"),
resource.TestCheckResourceAttr("axiom_dataset.test_otel_metrics", "kind", "otel:metrics:v1"),
testAccCheckResourcesCreatesCorrectValues(client, "axiom_dataset.test_otel_metrics", "kind", "kind"),

testAccCheckAxiomResourcesExist(client, "axiom_dataset.test_otel_traces"),
resource.TestCheckResourceAttr("axiom_dataset.test_otel_traces", "kind", "otel:traces:v1"),
testAccCheckResourcesCreatesCorrectValues(client, "axiom_dataset.test_otel_traces", "kind", "kind"),

testAccCheckAxiomResourcesExist(client, "axiom_dataset.test_otel_logs"),
resource.TestCheckResourceAttr("axiom_dataset.test_otel_logs", "kind", "otel:logs:v1"),
testAccCheckResourcesCreatesCorrectValues(client, "axiom_dataset.test_otel_logs", "kind", "kind"),

testAccCheckAxiomResourcesExist(client, "axiom_dataset.test_default_kind"),
resource.TestCheckResourceAttr("axiom_dataset.test_default_kind", "kind", "axiom:events:v1"),
testAccCheckResourcesCreatesCorrectValues(client, "axiom_dataset.test_default_kind", "kind", "kind"),
),
},
},
})
}

func TestAccAxiomResources_resolvable(t *testing.T) {
client, err := ax.NewClient()
assert.NoError(t, err)
Expand Down Expand Up @@ -567,7 +631,7 @@ func TestAccAxiomResources_dataset_map_fields(t *testing.T) {
}

resource "axiom_dataset" "test" {
name = "` + datasetName + `"
name = "` + datasetName + `"
map_fields = ["dupe", "dupe"]
}
`,
Expand All @@ -582,7 +646,7 @@ func TestAccAxiomResources_dataset_map_fields(t *testing.T) {
}

resource "axiom_dataset" "test" {
name = "` + datasetName + `"
name = "` + datasetName + `"
map_fields = []
}
`,
Expand All @@ -595,6 +659,69 @@ func TestAccAxiomResources_dataset_map_fields(t *testing.T) {
})
}

func TestAccAxiomResources_dataset_map_kind(t *testing.T) {
client, err := ax.NewClient()
assert.NoError(t, err)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){
"axiom": providerserver.NewProtocol6WithError(NewAxiomProvider()),
},
CheckDestroy: testAccCheckAxiomResourcesDestroyed(client),
Steps: []resource.TestStep{
{
Config: `
provider "axiom" {
api_token = "` + os.Getenv("AXIOM_TOKEN") + `"
base_url = "` + os.Getenv("AXIOM_URL") + `"
}

resource "axiom_dataset" "test_otel_metrics" {
name = "test-otel-metrics-` + uuid.NewString() + `"
kind = "otel:metrics:v1"
description = "Test OTEL metrics dataset"
}

resource "axiom_dataset" "test_otel_traces" {
name = "test-otel-traces-` + uuid.NewString() + `"
kind = "otel:traces:v1"
description = "Test OTEL traces dataset"
}

resource "axiom_dataset" "test_otel_logs" {
name = "test-otel-logs-` + uuid.NewString() + `"
kind = "otel:logs:v1"
description = "Test OTEL logs dataset"
}

resource "axiom_dataset" "test_default_kind" {
name = "test-default-kind-` + uuid.NewString() + `"
description = "Test dataset with default kind"
}
`,
Check: resource.ComposeTestCheckFunc(
testAccCheckAxiomResourcesExist(client, "axiom_dataset.test_otel_metrics"),
resource.TestCheckResourceAttr("axiom_dataset.test_otel_metrics", "kind", "otel:metrics:v1"),
testAccCheckResourcesCreatesCorrectValues(client, "axiom_dataset.test_otel_metrics", "kind", "kind"),

testAccCheckAxiomResourcesExist(client, "axiom_dataset.test_otel_traces"),
resource.TestCheckResourceAttr("axiom_dataset.test_otel_traces", "kind", "otel:traces:v1"),
testAccCheckResourcesCreatesCorrectValues(client, "axiom_dataset.test_otel_traces", "kind", "kind"),

testAccCheckAxiomResourcesExist(client, "axiom_dataset.test_otel_logs"),
resource.TestCheckResourceAttr("axiom_dataset.test_otel_logs", "kind", "otel:logs:v1"),
testAccCheckResourcesCreatesCorrectValues(client, "axiom_dataset.test_otel_logs", "kind", "kind"),

testAccCheckAxiomResourcesExist(client, "axiom_dataset.test_default_kind"),
resource.TestCheckResourceAttr("axiom_dataset.test_default_kind", "kind", "axiom:events:v1"),
testAccCheckResourcesCreatesCorrectValues(client, "axiom_dataset.test_default_kind", "kind", "kind"),
),
},
},
})
}

func testAccPreCheck(t *testing.T) {
if os.Getenv("AXIOM_TOKEN") == "" {
t.Fatalf("AXIOM_TOKEN must be set for acceptance tests")
Expand Down Expand Up @@ -732,7 +859,7 @@ resource "axiom_dataset" "test" {
}

resource "axiom_virtual_field" "test" {
depends_on = [axiom_dataset.test]
depends_on = [axiom_dataset.test]
name = "VF"
description = "my virtual field"
expression = "a * b"
Expand Down Expand Up @@ -790,7 +917,7 @@ resource "axiom_monitor" "test_monitor_without_description" {
]
alert_on_no_data = false
notify_by_group = false
type = "Threshold"
type = "Threshold"
}

resource "axiom_monitor" "test_monitor_match_event" {
Expand Down
18 changes: 14 additions & 4 deletions axiom/resource_dataset.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,12 @@ func (r *DatasetResource) Create(ctx context.Context, req resource.CreateRequest
return
}

// Set state immediately after creation to avoid orphaned resources
resp.Diagnostics.Append(resp.State.Set(ctx, flattenDataset(ds))...)
if resp.Diagnostics.HasError() {
return
}

if !plan.MapFields.IsUnknown() {
mapFields := axiom.MapFields{}
diags := plan.MapFields.ElementsAs(ctx, &mapFields, false)
Expand All @@ -197,9 +203,8 @@ func (r *DatasetResource) Create(ctx context.Context, req resource.CreateRequest
}

ds.MapFields = resMapFields
resp.Diagnostics.Append(resp.State.Set(ctx, flattenDataset(ds))...)
}

resp.Diagnostics.Append(resp.State.Set(ctx, flattenDataset(ds))...)
}

func (r *DatasetResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
Expand Down Expand Up @@ -255,6 +260,12 @@ func (r *DatasetResource) Update(ctx context.Context, req resource.UpdateRequest
return
}

// Set state immediately after update to preserve changes
resp.Diagnostics.Append(resp.State.Set(ctx, flattenDataset(ds))...)
if resp.Diagnostics.HasError() {
return
}

if !plan.MapFields.IsUnknown() {
mapFields := axiom.MapFields{}
diags := plan.MapFields.ElementsAs(ctx, &mapFields, false)
Expand All @@ -270,9 +281,8 @@ func (r *DatasetResource) Update(ctx context.Context, req resource.UpdateRequest
}

ds.MapFields = resMapFields
resp.Diagnostics.Append(resp.State.Set(ctx, flattenDataset(ds))...)
}

resp.Diagnostics.Append(resp.State.Set(ctx, flattenDataset(ds))...)
}

func (r *DatasetResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
Expand Down
1 change: 1 addition & 0 deletions docs/resources/dataset.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ description: |-
### Optional

- `description` (String) Dataset description
- `kind` (String) Dataset kind. Must be one of: 'axiom:events:v1', 'otel:metrics:v1', 'otel:traces:v1', 'otel:logs:v1'. Defaults to 'axiom:events:v1'
- `map_fields` (List of String) Map fields for the dataset
- `retention_days` (Number) Retention days for the dataset
- `use_retention_period` (Boolean) Use retention for the dataset
Expand Down
Loading