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
1 change: 1 addition & 0 deletions docs/data-sources/telemetryrouter_instance.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ data "stackit_telemetryrouter_instance" "router" {
- `display_name` (String) The display name of the TelemetryRouter instance
- `filter` (Attributes) The TelemetryRouter global filter settings (see [below for nested schema](#nestedatt--filter))
- `id` (String) Terraform's internal resource identifier. It is structured as "`project_id`,`region`,`instance_id`".
- `labels` (Map of String) Resource labels as described in the [STACKIT documentation](https://docs.stackit.cloud/platform/cost-and-billing/labels/).
- `status` (String, Deprecated) The status of the TelemetryRouter instance, possible values: Possible values are: `reconciling`, `active`, `deleting`.
- `uri` (String) The TelemetryRouter instance's URI

Expand Down
4 changes: 4 additions & 0 deletions docs/resources/telemetryrouter_instance.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ resource "stackit_telemetryrouter_instance" "router2" {
region = "eu01"
display_name = "router-instance"
description = "Example description"
labels = {
"key" = "value"
}
filter = {
attributes = [
{
Expand Down Expand Up @@ -55,6 +58,7 @@ resource "stackit_telemetryrouter_instance" "router2" {

- `description` (String) The description of the TelemetryRouter instance
- `filter` (Attributes) The TelemetryRouter global filter settings (see [below for nested schema](#nestedatt--filter))
- `labels` (Map of String) Resource labels as described in the [STACKIT documentation](https://docs.stackit.cloud/platform/cost-and-billing/labels/).
- `region` (String) STACKIT region name the resource is located in. If not defined, the provider region is used.

### Read-Only
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ resource "stackit_telemetryrouter_instance" "router2" {
region = "eu01"
display_name = "router-instance"
description = "Example description"
labels = {
"key" = "value"
}
filter = {
attributes = [
{
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ require (
github.com/stackitcloud/stackit-sdk-go/services/ske v1.22.0
github.com/stackitcloud/stackit-sdk-go/services/sqlserverflex v1.20.0
github.com/stackitcloud/stackit-sdk-go/services/telemetrylink v0.6.0
github.com/stackitcloud/stackit-sdk-go/services/telemetryrouter v0.6.1
github.com/stackitcloud/stackit-sdk-go/services/telemetryrouter v0.7.0
github.com/stackitcloud/stackit-sdk-go/services/valkey v0.4.0
github.com/stackitcloud/stackit-sdk-go/services/vpn v0.15.3
github.com/teambition/rrule-go v1.8.2
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,8 @@ github.com/stackitcloud/stackit-sdk-go/services/sqlserverflex v1.20.0 h1:dHsxhHt
github.com/stackitcloud/stackit-sdk-go/services/sqlserverflex v1.20.0/go.mod h1:chULz8M6OKvzWEMVH/3DWKDb1UQab87O1KLJfWV0Vp4=
github.com/stackitcloud/stackit-sdk-go/services/telemetrylink v0.6.0 h1:glsamuWGVTwzBk3yoWYmq8gDcDkJncepHRn/tDwSwjA=
github.com/stackitcloud/stackit-sdk-go/services/telemetrylink v0.6.0/go.mod h1:JyuStufMDibWblEo4wPOrh3B02aMgIUEhDF7YK5WGas=
github.com/stackitcloud/stackit-sdk-go/services/telemetryrouter v0.6.1 h1:RPMUjr73Zpko6mi+cwKb7omQF73i5QfuRrj3DTxHrKM=
github.com/stackitcloud/stackit-sdk-go/services/telemetryrouter v0.6.1/go.mod h1:e457u40HTJAVzw9YFP7Ri90otkJlgDqIphiDD5AZISI=
github.com/stackitcloud/stackit-sdk-go/services/telemetryrouter v0.7.0 h1:dky5GDANFoZXiGQeeFxoQwMOkxkgWtx+TGyyXCr2bHM=
github.com/stackitcloud/stackit-sdk-go/services/telemetryrouter v0.7.0/go.mod h1:e457u40HTJAVzw9YFP7Ri90otkJlgDqIphiDD5AZISI=
github.com/stackitcloud/stackit-sdk-go/services/valkey v0.4.0 h1:75iJs+mZPnrGSlSoTZtKYYCyNqtqgx6JVChwJokcKVg=
github.com/stackitcloud/stackit-sdk-go/services/valkey v0.4.0/go.mod h1:0crDO5v97ulHjVZVoFR2gJ4FCpEQeXGdM+V+AlZ6M6E=
github.com/stackitcloud/stackit-sdk-go/services/vpn v0.15.3 h1:rtY3Pdup5W3ZNQFiNo6STnoKKxpfM4YWjMbaps3OZb4=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ func (d *telemetryRouterInstanceDataSource) Schema(_ context.Context, _ datasour
},
},
},
"labels": schema.MapAttribute{
Description: schemaDescriptions["labels"],
ElementType: types.StringType,
Computed: true,
},
"creation_time": schema.StringAttribute{
Description: schemaDescriptions["creation_time"],
Computed: true,
Expand Down
34 changes: 30 additions & 4 deletions stackit/internal/services/telemetryrouter/instance/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ var schemaDescriptions = map[string]string{
"The status of the TelemetryRouter instance, possible values: %s",
tfutils.FormatPossibleValues(sdkUtils.EnumSliceToStringSlice(telemetryrouter.AllowedTelemetryRouterResponseStatusEnumValues)...),
),
"labels": "Resource labels as described in the [STACKIT documentation](https://docs.stackit.cloud/platform/cost-and-billing/labels/).",
}

type Model struct {
Expand All @@ -77,6 +78,7 @@ type Model struct {
CreationTime types.String `tfsdk:"creation_time"`
URI types.String `tfsdk:"uri"`
Status types.String `tfsdk:"status"`
Labels types.Map `tfsdk:"labels"`
}

// Struct corresponding to Model.Filter
Expand Down Expand Up @@ -260,6 +262,11 @@ func (r *telemetryRouterInstanceResource) Schema(_ context.Context, _ resource.S
},
},
},
"labels": schema.MapAttribute{
Description: schemaDescriptions["labels"],
ElementType: types.StringType,
Optional: true,
},
"creation_time": schema.StringAttribute{
Description: schemaDescriptions["creation_time"],
Computed: true,
Expand Down Expand Up @@ -516,9 +523,15 @@ func toCreatePayload(ctx context.Context, diags diag.Diagnostics, model *Model)
return nil, fmt.Errorf("missing model")
}

labels, err := tfutils.LabelsToPayload(ctx, model.Labels)
if err != nil {
return nil, fmt.Errorf("converting labels: %w", err)
}

payload := &telemetryrouter.CreateTelemetryRouterPayload{
DisplayName: model.DisplayName.ValueString(),
Description: conversion.StringValueToPointer(model.Description),
DisplayName: model.DisplayName.ValueString(),
Description: conversion.StringValueToPointer(model.Description),
ResourceLabels: &labels,
}

configFilter, err := toConfigFilter(ctx, diags, model)
Expand Down Expand Up @@ -605,6 +618,13 @@ func mapFields(ctx context.Context, instance *telemetryrouter.TelemetryRouterRes
return fmt.Errorf("map filter: %w", err)
}

labels, err := tfutils.MapLabels(ctx, instance.ResourceLabels, model.Labels)
if err != nil {
return fmt.Errorf("mapping labels: %w", err)
}

model.Labels = labels

return nil
}

Expand Down Expand Up @@ -664,9 +684,15 @@ func toUpdatePayload(ctx context.Context, diags diag.Diagnostics, model *Model)
return nil, fmt.Errorf("missing model")
}

labels, err := tfutils.LabelsToPayload(ctx, model.Labels)
if err != nil {
return nil, fmt.Errorf("converting labels: %w", err)
}

payload := &telemetryrouter.UpdateTelemetryRouterPayload{
DisplayName: conversion.StringValueToPointer(model.DisplayName),
Description: conversion.StringValueToPointer(model.Description),
DisplayName: conversion.StringValueToPointer(model.DisplayName),
Description: conversion.StringValueToPointer(model.Description),
ResourceLabels: &labels,
}

configFilter, err := toConfigFilter(ctx, diags, model)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

"github.com/google/go-cmp/cmp"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/types"
telemetryrouter "github.com/stackitcloud/stackit-sdk-go/services/telemetryrouter/v1api"
Expand Down Expand Up @@ -38,6 +39,7 @@ func fixtureModel(mods ...func(model *Model)) *Model {
CreationTime: types.StringValue(testTime.Format(time.RFC3339)),
URI: types.StringValue("uri"),
Status: types.StringValue("active"),
Labels: types.MapNull(types.StringType),
}
for _, mod := range mods {
mod(model)
Expand Down Expand Up @@ -141,7 +143,7 @@ func TestToCreatePayload(t *testing.T) {
{
description: "min values",
model: fixtureModel(),
expected: &telemetryrouter.CreateTelemetryRouterPayload{},
expected: &telemetryrouter.CreateTelemetryRouterPayload{ResourceLabels: &map[string]string{}},
},
{
description: "max values",
Expand All @@ -164,8 +166,9 @@ func TestToCreatePayload(t *testing.T) {
model.Description = types.StringValue("description")
}),
expected: &telemetryrouter.CreateTelemetryRouterPayload{
DisplayName: "display-name",
Description: new("description"),
DisplayName: "display-name",
Description: new("description"),
ResourceLabels: &map[string]string{},
Filter: &telemetryrouter.ConfigFilter{
Attributes: []telemetryrouter.ConfigFilterAttributes{
{
Expand Down Expand Up @@ -212,7 +215,7 @@ func TestToUpdatePayload(t *testing.T) {
{
description: "min values",
model: fixtureModel(),
expected: &telemetryrouter.UpdateTelemetryRouterPayload{},
expected: &telemetryrouter.UpdateTelemetryRouterPayload{ResourceLabels: &map[string]string{}},
},
{
description: "max values",
Expand All @@ -235,8 +238,9 @@ func TestToUpdatePayload(t *testing.T) {
model.Description = types.StringValue("description")
}),
expected: &telemetryrouter.UpdateTelemetryRouterPayload{
DisplayName: new("display-name"),
Description: new("description"),
DisplayName: new("display-name"),
Description: new("description"),
ResourceLabels: &map[string]string{},
Filter: &telemetryrouter.ConfigFilter{
Attributes: []telemetryrouter.ConfigFilterAttributes{
{
Expand Down Expand Up @@ -272,3 +276,75 @@ func TestToUpdatePayload(t *testing.T) {
})
}
}

func TestMapFieldsLabels(t *testing.T) {
tests := []struct {
description string
input *telemetryrouter.TelemetryRouterResponse
stateLabels types.Map
expected types.Map
}{
{
description: "labels from response",
input: fixtureInstance(func(instance *telemetryrouter.TelemetryRouterResponse) {
instance.ResourceLabels = &map[string]string{"key": "value"}
}),
stateLabels: types.MapNull(types.StringType),
expected: types.MapValueMust(types.StringType, map[string]attr.Value{"key": types.StringValue("value")}),
},
{
description: "no labels in response, null in state stays null",
input: fixtureInstance(),
stateLabels: types.MapNull(types.StringType),
expected: types.MapNull(types.StringType),
},
{
description: "no labels in response, empty map in state stays empty",
input: fixtureInstance(),
stateLabels: types.MapValueMust(types.StringType, map[string]attr.Value{}),
expected: types.MapValueMust(types.StringType, map[string]attr.Value{}),
},
}
for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
state := &Model{
ProjectID: types.StringValue("pid"),
Labels: tt.stateLabels,
}

if err := mapFields(context.Background(), tt.input, state, "reg"); err != nil {
t.Fatalf("Should not have failed: %v", err)
}

if diff := cmp.Diff(state.Labels, tt.expected); diff != "" {
t.Fatalf("Labels do not match: %s", diff)
}
})
}
}

func TestPayloadLabels(t *testing.T) {
model := fixtureModel(func(model *Model) {
model.Labels = types.MapValueMust(types.StringType, map[string]attr.Value{"key": types.StringValue("value")})
})

create, err := toCreatePayload(t.Context(), diag.Diagnostics{}, model)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}

update, err := toUpdatePayload(t.Context(), diag.Diagnostics{}, model)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}

want := &map[string]string{"key": "value"}
for name, got := range map[string]*map[string]string{
"create": create.ResourceLabels,
"update": update.ResourceLabels,
} {
if diff := cmp.Diff(got, want); diff != "" {
t.Fatalf("%s labels do not match: %s", name, diff)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ func testConfigVarsMinUpdated() config.Variables {
return newVars
}

const (
labelValueMax = "tf-acc-label"
labelValueUpdated = "tf-acc-label-updated"
)

var testConfigVarsMax = config.Variables{
"project_id": config.StringVariable(testutil.ProjectId),
"region": config.StringVariable(testutil.Region),
Expand All @@ -69,6 +74,7 @@ var testConfigVarsMax = config.Variables{
"filter_matcher": config.StringVariable("="),
"filter_value0": config.StringVariable("value1"),
"filter_value1": config.StringVariable("value2"),
"labels": config.MapVariable(map[string]config.Variable{"acc-test": config.StringVariable(labelValueMax)}),
}

func testConfigVarsMaxUpdated() config.Variables {
Expand All @@ -81,6 +87,15 @@ func testConfigVarsMaxUpdated() config.Variables {
newVars["filter_matcher"] = config.StringVariable("!=")
newVars["filter_value0"] = config.StringVariable("value3")
newVars["filter_value1"] = config.StringVariable("value4")
newVars["labels"] = config.MapVariable(map[string]config.Variable{"acc-test": config.StringVariable(labelValueUpdated)})

return newVars
}

// testConfigVarsMaxLabelsRemoved sets an empty labels map so that the previously set label gets removed.
func testConfigVarsMaxLabelsRemoved() config.Variables {
newVars := testConfigVarsMaxUpdated()
newVars["labels"] = config.MapVariable(map[string]config.Variable{})

return newVars
}
Expand Down Expand Up @@ -321,6 +336,8 @@ func TestAccTelemetryRouterInstanceMax(t *testing.T) {
resource.TestCheckResourceAttr("stackit_telemetryrouter_instance.router", "filter.attributes.0.matcher", testutil.ConvertConfigVariable(testConfigVarsMax["filter_matcher"])),
resource.TestCheckResourceAttr("stackit_telemetryrouter_instance.router", "filter.attributes.0.values.0", testutil.ConvertConfigVariable(testConfigVarsMax["filter_value0"])),
resource.TestCheckResourceAttr("stackit_telemetryrouter_instance.router", "filter.attributes.0.values.1", testutil.ConvertConfigVariable(testConfigVarsMax["filter_value1"])),
resource.TestCheckResourceAttr("stackit_telemetryrouter_instance.router", "labels.%", "1"),
resource.TestCheckResourceAttr("stackit_telemetryrouter_instance.router", "labels.acc-test", labelValueMax),
resource.TestCheckResourceAttrSet("stackit_telemetryrouter_instance.router", "id"),
resource.TestCheckResourceAttrSet("stackit_telemetryrouter_instance.router", "instance_id"),
resource.TestCheckResourceAttrSet("stackit_telemetryrouter_instance.router", "creation_time"),
Expand Down Expand Up @@ -392,6 +409,11 @@ func TestAccTelemetryRouterInstanceMax(t *testing.T) {
"stackit_telemetryrouter_instance.router", "filter.attributes.0.value.1",
"data.stackit_telemetryrouter_instance.router", "filter.attributes.0.value.1",
),
resource.TestCheckResourceAttr("data.stackit_telemetryrouter_instance.router", "labels.%", "1"),
resource.TestCheckResourceAttrPair(
"stackit_telemetryrouter_instance.router", "labels.acc-test",
"data.stackit_telemetryrouter_instance.router", "labels.acc-test",
),
),
},
// Import
Expand Down Expand Up @@ -428,13 +450,24 @@ func TestAccTelemetryRouterInstanceMax(t *testing.T) {
resource.TestCheckResourceAttr("stackit_telemetryrouter_instance.router", "filter.attributes.0.matcher", testutil.ConvertConfigVariable(testConfigVarsMaxUpdated()["filter_matcher"])),
resource.TestCheckResourceAttr("stackit_telemetryrouter_instance.router", "filter.attributes.0.values.0", testutil.ConvertConfigVariable(testConfigVarsMaxUpdated()["filter_value0"])),
resource.TestCheckResourceAttr("stackit_telemetryrouter_instance.router", "filter.attributes.0.values.1", testutil.ConvertConfigVariable(testConfigVarsMaxUpdated()["filter_value1"])),
resource.TestCheckResourceAttr("stackit_telemetryrouter_instance.router", "labels.%", "1"),
resource.TestCheckResourceAttr("stackit_telemetryrouter_instance.router", "labels.acc-test", labelValueUpdated),
resource.TestCheckResourceAttrSet("stackit_telemetryrouter_instance.router", "id"),
resource.TestCheckResourceAttrSet("stackit_telemetryrouter_instance.router", "instance_id"),
resource.TestCheckResourceAttrSet("stackit_telemetryrouter_instance.router", "creation_time"),
resource.TestCheckResourceAttrSet("stackit_telemetryrouter_instance.router", "uri"),
resource.TestCheckResourceAttrSet("stackit_telemetryrouter_instance.router", "status"),
),
},
// Remove labels - an empty labels map must clear the existing label
{
ConfigVariables: testConfigVarsMaxLabelsRemoved(),
Config: testutil.NewConfigBuilder().EnableBetaResources(true).BuildProviderConfig() + instanceMax,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("stackit_telemetryrouter_instance.router", "labels.%", "0"),
resource.TestCheckResourceAttr("stackit_telemetryrouter_instance.router", "display_name", testutil.ConvertConfigVariable(testConfigVarsMaxLabelsRemoved()["display_name"])),
),
},
// Deletion handled by framework
},
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ variable "filter_level" {}
variable "filter_matcher" {}
variable "filter_value0" {}
variable "filter_value1" {}
variable "labels" {}

resource "stackit_telemetryrouter_instance" "router" {
project_id = var.project_id
region = var.region
display_name = var.display_name
description = var.description
labels = var.labels
filter = {
attributes = [
{
Expand Down