From 28c3446641609e66c631b4d2bf40576dba02155d Mon Sep 17 00:00:00 2001 From: Alexander Dahmen Date: Wed, 2 Dec 2026 07:59:45 +0100 Subject: [PATCH] feat(telemetryrouter): Add labels STACKITCLI-570 Signed-off-by: Alexander Dahmen --- ...t_beta_telemetry-router_instance_create.md | 12 +++++--- ...t_beta_telemetry-router_instance_update.md | 12 +++++--- .../telemetryrouter/instance/create/create.go | 14 +++++++-- .../instance/create/create_test.go | 21 +++++++++++++ .../instance/describe/describe.go | 5 ++++ .../instance/describe/describe_test.go | 7 +++++ .../telemetryrouter/instance/update/update.go | 17 ++++++++--- .../instance/update/update_test.go | 30 +++++++++++++++++++ 8 files changed, 103 insertions(+), 15 deletions(-) diff --git a/docs/stackit_beta_telemetry-router_instance_create.md b/docs/stackit_beta_telemetry-router_instance_create.md index 7605c4afd..cf9e7445b 100644 --- a/docs/stackit_beta_telemetry-router_instance_create.md +++ b/docs/stackit_beta_telemetry-router_instance_create.md @@ -19,6 +19,9 @@ stackit beta telemetry-router instance create [flags] Create a Telemetry Router instance with name "my-instance" and a description $ stackit beta telemetry-router instance create --display-name "my-instance" --description "Description of the instance" + Create a Telemetry Router instance with name "my-instance" and labels + $ stackit beta telemetry-router instance create --display-name "my-instance" --labels "env=prod,team=sre" + Create a Telemetry Router instance with name "my-instance" that only routes telemetry data for HTTP GET or HEAD requests, matched at the resource level, using a filter configuration sourced from a JSON file; use "stackit beta telemetry-router instance generate-filter" to generate a starting point for "./filter.json"; available attributes are documented in https://docs.stackit.cloud/products/logging-and-monitoring/telemetry-router/basics/dataformats/ $ stackit beta telemetry-router instance create --display-name "my-instance" --filter @./filter.json ``` @@ -26,10 +29,11 @@ stackit beta telemetry-router instance create [flags] ### Options ``` - --description string Description - --display-name string Display name - --filter string Filter configuration as JSON, of the form {"attributes": [{"key": ..., "level": ..., "matcher": ..., "values": [...]}, ...]}. Can be a JSON string or a file path, if prefixed with "@" (example: @./filter.json). Run "stackit beta telemetry-router instance generate-filter" to generate a starting point. - -h, --help[=true|false] Help for "stackit beta telemetry-router instance create" + --description string Description + --display-name string Display name + --filter string Filter configuration as JSON, of the form {"attributes": [{"key": ..., "level": ..., "matcher": ..., "values": [...]}, ...]}. Can be a JSON string or a file path, if prefixed with "@" (example: @./filter.json). Run "stackit beta telemetry-router instance generate-filter" to generate a starting point. + -h, --help[=true|false] Help for "stackit beta telemetry-router instance create" + --labels stringToString Labels in key=value format, separated by commas. Example: --labels "key1=value1,key2=value2". (default []) ``` ### Options inherited from parent commands diff --git a/docs/stackit_beta_telemetry-router_instance_update.md b/docs/stackit_beta_telemetry-router_instance_update.md index 9934de21d..e93178394 100644 --- a/docs/stackit_beta_telemetry-router_instance_update.md +++ b/docs/stackit_beta_telemetry-router_instance_update.md @@ -19,6 +19,9 @@ stackit beta telemetry-router instance update INSTANCE_ID [flags] Update the description of the Telemetry Router instance with ID "xxx" $ stackit beta telemetry-router instance update xxx --description new-description + Update the labels of the Telemetry Router instance with ID "xxx" + $ stackit beta telemetry-router instance update xxx --labels "env=prod,team=sre" + Replace the filter attributes of the Telemetry Router instance with ID "xxx" with a filter configuration sourced from a JSON file so it only routes telemetry data for HTTP GET or HEAD requests, matched at the resource level; use "stackit beta telemetry-router instance generate-filter --instance-id xxx" to generate a starting point from the instance's current filter $ stackit beta telemetry-router instance update xxx --filter @./filter.json ``` @@ -26,10 +29,11 @@ stackit beta telemetry-router instance update INSTANCE_ID [flags] ### Options ``` - --description string Description - --display-name string Display name - --filter string Filter configuration as JSON, of the form {"attributes": [{"key": ..., "level": ..., "matcher": ..., "values": [...]}, ...], "disabled": ...}. Can be a JSON string or a file path, if prefixed with "@" (example: @./filter.json). Only the provided fields are updated: if "attributes" is set, it replaces any existing filter attributes on the instance; if "disabled" is set, it enables or disables the filter without changing its attributes (example: {"disabled": true}). Run "stackit beta telemetry-router instance generate-filter --instance-id " to generate a starting point from the instance's current filter. Clearing all filter attributes is not supported; an empty "attributes" list leaves the existing attributes unchanged. - -h, --help[=true|false] Help for "stackit beta telemetry-router instance update" + --description string Description + --display-name string Display name + --filter string Filter configuration as JSON, of the form {"attributes": [{"key": ..., "level": ..., "matcher": ..., "values": [...]}, ...], "disabled": ...}. Can be a JSON string or a file path, if prefixed with "@" (example: @./filter.json). Only the provided fields are updated: if "attributes" is set, it replaces any existing filter attributes on the instance; if "disabled" is set, it enables or disables the filter without changing its attributes (example: {"disabled": true}). Run "stackit beta telemetry-router instance generate-filter --instance-id " to generate a starting point from the instance's current filter. Clearing all filter attributes is not supported; an empty "attributes" list leaves the existing attributes unchanged. + -h, --help[=true|false] Help for "stackit beta telemetry-router instance update" + --labels stringToString Labels in key=value format, separated by commas. Replaces all existing labels. Example: --labels "key1=value1,key2=value2". (default []) ``` ### Options inherited from parent commands diff --git a/internal/cmd/beta/telemetryrouter/instance/create/create.go b/internal/cmd/beta/telemetryrouter/instance/create/create.go index 40fe88d9e..c1fd3a042 100644 --- a/internal/cmd/beta/telemetryrouter/instance/create/create.go +++ b/internal/cmd/beta/telemetryrouter/instance/create/create.go @@ -29,6 +29,7 @@ const ( displayNameFlag = "display-name" descriptionFlag = "description" filterFlag = "filter" + labelsFlag = "labels" ) type inputModel struct { @@ -37,6 +38,7 @@ type inputModel struct { DisplayName string Description *string Filter *telemetryrouter.ConfigFilter + Labels *map[string]string } func NewCmd(params *types.CmdParams) *cobra.Command { @@ -52,6 +54,9 @@ func NewCmd(params *types.CmdParams) *cobra.Command { examples.NewExample( `Create a Telemetry Router instance with name "my-instance" and a description`, `$ stackit beta telemetry-router instance create --display-name "my-instance" --description "Description of the instance"`), + examples.NewExample( + `Create a Telemetry Router instance with name "my-instance" and labels`, + `$ stackit beta telemetry-router instance create --display-name "my-instance" --labels "env=prod,team=sre"`), examples.NewExample( `Create a Telemetry Router instance with name "my-instance" that only routes telemetry data for HTTP GET or HEAD requests, matched at the resource level, using a filter configuration sourced from a JSON file; `+ `use "stackit beta telemetry-router instance generate-filter" to generate a starting point for "./filter.json"; `+ @@ -128,6 +133,7 @@ func configureFlags(cmd *cobra.Command) { `Filter configuration as JSON, of the form {"attributes": [{"key": ..., "level": ..., "matcher": ..., "values": [...]}, ...]}. `+ `Can be a JSON string or a file path, if prefixed with "@" (example: @./filter.json). `+ `Run "stackit beta telemetry-router instance generate-filter" to generate a starting point.`) + cmd.Flags().StringToString(labelsFlag, nil, `Labels in key=value format, separated by commas. Example: --labels "key1=value1,key2=value2".`) err := flags.MarkFlagsRequired(cmd, displayNameFlag) cobra.CheckErr(err) @@ -149,6 +155,7 @@ func parseInput(p *print.Printer, cmd *cobra.Command, _ []string) (*inputModel, DisplayName: flags.FlagToStringValue(p, cmd, displayNameFlag), Description: flags.FlagToStringPointer(p, cmd, descriptionFlag), Filter: filter, + Labels: flags.FlagToStringToStringPointer(p, cmd, labelsFlag), } p.DebugInputModel(model) @@ -169,9 +176,10 @@ func parseFilter(p *print.Printer, cmd *cobra.Command) (*telemetryrouter.ConfigF func buildRequest(ctx context.Context, model *inputModel, apiClient telemetryrouter.DefaultAPI) telemetryrouter.ApiCreateTelemetryRouterRequest { req := apiClient.CreateTelemetryRouter(ctx, model.ProjectId, model.Region) req = req.CreateTelemetryRouterPayload(telemetryrouter.CreateTelemetryRouterPayload{ - DisplayName: model.DisplayName, - Description: model.Description, - Filter: model.Filter, + DisplayName: model.DisplayName, + Description: model.Description, + Filter: model.Filter, + ResourceLabels: model.Labels, }) return req diff --git a/internal/cmd/beta/telemetryrouter/instance/create/create_test.go b/internal/cmd/beta/telemetryrouter/instance/create/create_test.go index 9bc332dde..604ac338e 100644 --- a/internal/cmd/beta/telemetryrouter/instance/create/create_test.go +++ b/internal/cmd/beta/telemetryrouter/instance/create/create_test.go @@ -132,6 +132,16 @@ func TestParseInput(t *testing.T) { flagValues: map[string]string{}, isValid: false, }, + { + description: "with labels", + flagValues: fixtureFlagValues(func(flagValues map[string]string) { + flagValues[labelsFlag] = "env=prod,team=sre" + }), + isValid: true, + expectedModel: fixtureInputModel(func(model *inputModel) { + model.Labels = &map[string]string{"env": "prod", "team": "sre"} + }), + }, { description: "no filter at all still works, Filter stays nil", flagValues: fixtureFlagValues(), @@ -246,6 +256,17 @@ func TestBuildRequest(t *testing.T) { }, }), }, + { + description: "with labels", + model: fixtureInputModel(func(model *inputModel) { + model.Labels = &map[string]string{"env": "prod"} + }), + expectedRequest: fixtureRequest().CreateTelemetryRouterPayload(telemetryrouter.CreateTelemetryRouterPayload{ + DisplayName: testDisplayName, + Description: new(testDescription), + ResourceLabels: &map[string]string{"env": "prod"}, + }), + }, } for _, tt := range tests { diff --git a/internal/cmd/beta/telemetryrouter/instance/describe/describe.go b/internal/cmd/beta/telemetryrouter/instance/describe/describe.go index db85c7dac..0c17825f6 100644 --- a/internal/cmd/beta/telemetryrouter/instance/describe/describe.go +++ b/internal/cmd/beta/telemetryrouter/instance/describe/describe.go @@ -114,6 +114,11 @@ func outputResult(p *print.Printer, outputFormat string, instance *telemetryrout table.AddSeparator() table.AddRow("CREATION TIME", instance.CreationTime.Format(time.DateTime)) + if len(instance.GetResourceLabels()) > 0 { + table.AddSeparator() + table.AddRow("LABELS", utils.JoinStringMap(instance.GetResourceLabels(), ": ", "\n")) + } + if instance.Filter != nil { table.AddSeparator() table.AddRow("FILTER DISABLED", utils.PtrString(instance.Filter.Disabled)) diff --git a/internal/cmd/beta/telemetryrouter/instance/describe/describe_test.go b/internal/cmd/beta/telemetryrouter/instance/describe/describe_test.go index a43699537..8062e47cf 100644 --- a/internal/cmd/beta/telemetryrouter/instance/describe/describe_test.go +++ b/internal/cmd/beta/telemetryrouter/instance/describe/describe_test.go @@ -184,6 +184,13 @@ func TestOutputResult(t *testing.T) { }}, wantErr: false, }, + { + name: "default output with labels", + args: args{outputFormat: "default", instance: &telemetryrouter.TelemetryRouterResponse{ + ResourceLabels: &map[string]string{"env": "prod", "team": "sre"}, + }}, + wantErr: false, + }, { name: "json output", args: args{outputFormat: print.JSONOutputFormat, instance: &telemetryrouter.TelemetryRouterResponse{}}, diff --git a/internal/cmd/beta/telemetryrouter/instance/update/update.go b/internal/cmd/beta/telemetryrouter/instance/update/update.go index 20276d3fa..25542e9b8 100644 --- a/internal/cmd/beta/telemetryrouter/instance/update/update.go +++ b/internal/cmd/beta/telemetryrouter/instance/update/update.go @@ -29,6 +29,7 @@ const ( displayNameFlag = "display-name" descriptionFlag = "description" filterFlag = "filter" + labelsFlag = "labels" ) type inputModel struct { @@ -37,6 +38,7 @@ type inputModel struct { DisplayName *string Description *string Filter *telemetryrouter.ConfigFilterPatch + Labels *map[string]string } func NewCmd(params *types.CmdParams) *cobra.Command { @@ -52,6 +54,9 @@ func NewCmd(params *types.CmdParams) *cobra.Command { examples.NewExample( `Update the description of the Telemetry Router instance with ID "xxx"`, "$ stackit beta telemetry-router instance update xxx --description new-description"), + examples.NewExample( + `Update the labels of the Telemetry Router instance with ID "xxx"`, + `$ stackit beta telemetry-router instance update xxx --labels "env=prod,team=sre"`), examples.NewExample( `Replace the filter attributes of the Telemetry Router instance with ID "xxx" with a filter configuration sourced from a JSON file so it only routes telemetry data for HTTP GET or HEAD requests, matched at the resource level; `+ `use "stackit beta telemetry-router instance generate-filter --instance-id xxx" to generate a starting point from the instance's current filter`, @@ -131,6 +136,7 @@ func configureFlags(cmd *cobra.Command) { `the filter without changing its attributes (example: {"disabled": true}). Run "stackit beta telemetry-router instance generate-filter --instance-id " `+ `to generate a starting point from the instance's current filter. Clearing all filter attributes is not supported; `+ `an empty "attributes" list leaves the existing attributes unchanged.`) + cmd.Flags().StringToString(labelsFlag, nil, `Labels in key=value format, separated by commas. Replaces all existing labels. Example: --labels "key1=value1,key2=value2".`) } func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) (*inputModel, error) { @@ -144,6 +150,7 @@ func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) (*inpu displayName := flags.FlagToStringPointer(p, cmd, displayNameFlag) description := flags.FlagToStringPointer(p, cmd, descriptionFlag) filterValue := flags.FlagToStringPointer(p, cmd, filterFlag) + labels := flags.FlagToStringToStringPointer(p, cmd, labelsFlag) var filter *telemetryrouter.ConfigFilterPatch if filterValue != nil { @@ -155,7 +162,7 @@ func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) (*inpu filter = parsedFilter } - if displayName == nil && description == nil && filter == nil { + if displayName == nil && description == nil && filter == nil && labels == nil { return nil, &errors.EmptyUpdateError{} } @@ -165,6 +172,7 @@ func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) (*inpu DisplayName: displayName, Description: description, Filter: filter, + Labels: labels, } p.DebugInputModel(model) @@ -177,9 +185,10 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient telemetryrou // Partial update: fields which are not set (nil) are left unchanged on API side return req.PartialUpdateTelemetryRouterPayload(telemetryrouter.PartialUpdateTelemetryRouterPayload{ - DisplayName: model.DisplayName, - Description: model.Description, - Filter: model.Filter, + DisplayName: model.DisplayName, + Description: model.Description, + Filter: model.Filter, + ResourceLabels: model.Labels, }) } diff --git a/internal/cmd/beta/telemetryrouter/instance/update/update_test.go b/internal/cmd/beta/telemetryrouter/instance/update/update_test.go index 19f0b1b02..092c31d4c 100644 --- a/internal/cmd/beta/telemetryrouter/instance/update/update_test.go +++ b/internal/cmd/beta/telemetryrouter/instance/update/update_test.go @@ -182,6 +182,25 @@ func TestParseInput(t *testing.T) { Description: new("description"), }, }, + { + description: "labels only", + argValues: fixtureArgValues(), + flagValues: map[string]string{ + globalflags.ProjectIdFlag: testProjectId, + globalflags.RegionFlag: testRegion, + labelsFlag: "env=prod,team=sre", + }, + isValid: true, + expectedModel: &inputModel{ + GlobalFlagModel: &globalflags.GlobalFlagModel{ + ProjectId: testProjectId, + Region: testRegion, + Verbosity: globalflags.VerbosityDefault, + }, + InstanceId: testInstanceId, + Labels: &map[string]string{"env": "prod", "team": "sre"}, + }, + }, { description: "project id missing", argValues: fixtureArgValues(), @@ -322,6 +341,17 @@ func TestBuildRequest(t *testing.T) { }, }), }, + { + description: "with labels", + model: fixtureInputModel(func(model *inputModel) { + model.Labels = &map[string]string{"env": "prod"} + }), + expectedRequest: fixtureRequest().PartialUpdateTelemetryRouterPayload(telemetryrouter.PartialUpdateTelemetryRouterPayload{ + DisplayName: new("name"), + Description: new("Example"), + ResourceLabels: &map[string]string{"env": "prod"}, + }), + }, } for _, tt := range tests {