Skip to content

Commit 28c3446

Browse files
committed
feat(telemetryrouter): Add labels
STACKITCLI-570 Signed-off-by: Alexander Dahmen <alexander.dahmen@inovex.de>
1 parent ab7ed76 commit 28c3446

8 files changed

Lines changed: 103 additions & 15 deletions

File tree

‎docs/stackit_beta_telemetry-router_instance_create.md‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,21 @@ stackit beta telemetry-router instance create [flags]
1919
Create a Telemetry Router instance with name "my-instance" and a description
2020
$ stackit beta telemetry-router instance create --display-name "my-instance" --description "Description of the instance"
2121
22+
Create a Telemetry Router instance with name "my-instance" and labels
23+
$ stackit beta telemetry-router instance create --display-name "my-instance" --labels "env=prod,team=sre"
24+
2225
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/
2326
$ stackit beta telemetry-router instance create --display-name "my-instance" --filter @./filter.json
2427
```
2528

2629
### Options
2730

2831
```
29-
--description string Description
30-
--display-name string Display name
31-
--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.
32-
-h, --help[=true|false] Help for "stackit beta telemetry-router instance create"
32+
--description string Description
33+
--display-name string Display name
34+
--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.
35+
-h, --help[=true|false] Help for "stackit beta telemetry-router instance create"
36+
--labels stringToString Labels in key=value format, separated by commas. Example: --labels "key1=value1,key2=value2". (default [])
3337
```
3438

3539
### Options inherited from parent commands

‎docs/stackit_beta_telemetry-router_instance_update.md‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,21 @@ stackit beta telemetry-router instance update INSTANCE_ID [flags]
1919
Update the description of the Telemetry Router instance with ID "xxx"
2020
$ stackit beta telemetry-router instance update xxx --description new-description
2121
22+
Update the labels of the Telemetry Router instance with ID "xxx"
23+
$ stackit beta telemetry-router instance update xxx --labels "env=prod,team=sre"
24+
2225
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
2326
$ stackit beta telemetry-router instance update xxx --filter @./filter.json
2427
```
2528

2629
### Options
2730

2831
```
29-
--description string Description
30-
--display-name string Display name
31-
--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 <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.
32-
-h, --help[=true|false] Help for "stackit beta telemetry-router instance update"
32+
--description string Description
33+
--display-name string Display name
34+
--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 <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.
35+
-h, --help[=true|false] Help for "stackit beta telemetry-router instance update"
36+
--labels stringToString Labels in key=value format, separated by commas. Replaces all existing labels. Example: --labels "key1=value1,key2=value2". (default [])
3337
```
3438

3539
### Options inherited from parent commands

‎internal/cmd/beta/telemetryrouter/instance/create/create.go‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ const (
2929
displayNameFlag = "display-name"
3030
descriptionFlag = "description"
3131
filterFlag = "filter"
32+
labelsFlag = "labels"
3233
)
3334

3435
type inputModel struct {
@@ -37,6 +38,7 @@ type inputModel struct {
3738
DisplayName string
3839
Description *string
3940
Filter *telemetryrouter.ConfigFilter
41+
Labels *map[string]string
4042
}
4143

4244
func NewCmd(params *types.CmdParams) *cobra.Command {
@@ -52,6 +54,9 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
5254
examples.NewExample(
5355
`Create a Telemetry Router instance with name "my-instance" and a description`,
5456
`$ stackit beta telemetry-router instance create --display-name "my-instance" --description "Description of the instance"`),
57+
examples.NewExample(
58+
`Create a Telemetry Router instance with name "my-instance" and labels`,
59+
`$ stackit beta telemetry-router instance create --display-name "my-instance" --labels "env=prod,team=sre"`),
5560
examples.NewExample(
5661
`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; `+
5762
`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) {
128133
`Filter configuration as JSON, of the form {"attributes": [{"key": ..., "level": ..., "matcher": ..., "values": [...]}, ...]}. `+
129134
`Can be a JSON string or a file path, if prefixed with "@" (example: @./filter.json). `+
130135
`Run "stackit beta telemetry-router instance generate-filter" to generate a starting point.`)
136+
cmd.Flags().StringToString(labelsFlag, nil, `Labels in key=value format, separated by commas. Example: --labels "key1=value1,key2=value2".`)
131137

132138
err := flags.MarkFlagsRequired(cmd, displayNameFlag)
133139
cobra.CheckErr(err)
@@ -149,6 +155,7 @@ func parseInput(p *print.Printer, cmd *cobra.Command, _ []string) (*inputModel,
149155
DisplayName: flags.FlagToStringValue(p, cmd, displayNameFlag),
150156
Description: flags.FlagToStringPointer(p, cmd, descriptionFlag),
151157
Filter: filter,
158+
Labels: flags.FlagToStringToStringPointer(p, cmd, labelsFlag),
152159
}
153160

154161
p.DebugInputModel(model)
@@ -169,9 +176,10 @@ func parseFilter(p *print.Printer, cmd *cobra.Command) (*telemetryrouter.ConfigF
169176
func buildRequest(ctx context.Context, model *inputModel, apiClient telemetryrouter.DefaultAPI) telemetryrouter.ApiCreateTelemetryRouterRequest {
170177
req := apiClient.CreateTelemetryRouter(ctx, model.ProjectId, model.Region)
171178
req = req.CreateTelemetryRouterPayload(telemetryrouter.CreateTelemetryRouterPayload{
172-
DisplayName: model.DisplayName,
173-
Description: model.Description,
174-
Filter: model.Filter,
179+
DisplayName: model.DisplayName,
180+
Description: model.Description,
181+
Filter: model.Filter,
182+
ResourceLabels: model.Labels,
175183
})
176184

177185
return req

‎internal/cmd/beta/telemetryrouter/instance/create/create_test.go‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,16 @@ func TestParseInput(t *testing.T) {
132132
flagValues: map[string]string{},
133133
isValid: false,
134134
},
135+
{
136+
description: "with labels",
137+
flagValues: fixtureFlagValues(func(flagValues map[string]string) {
138+
flagValues[labelsFlag] = "env=prod,team=sre"
139+
}),
140+
isValid: true,
141+
expectedModel: fixtureInputModel(func(model *inputModel) {
142+
model.Labels = &map[string]string{"env": "prod", "team": "sre"}
143+
}),
144+
},
135145
{
136146
description: "no filter at all still works, Filter stays nil",
137147
flagValues: fixtureFlagValues(),
@@ -246,6 +256,17 @@ func TestBuildRequest(t *testing.T) {
246256
},
247257
}),
248258
},
259+
{
260+
description: "with labels",
261+
model: fixtureInputModel(func(model *inputModel) {
262+
model.Labels = &map[string]string{"env": "prod"}
263+
}),
264+
expectedRequest: fixtureRequest().CreateTelemetryRouterPayload(telemetryrouter.CreateTelemetryRouterPayload{
265+
DisplayName: testDisplayName,
266+
Description: new(testDescription),
267+
ResourceLabels: &map[string]string{"env": "prod"},
268+
}),
269+
},
249270
}
250271

251272
for _, tt := range tests {

‎internal/cmd/beta/telemetryrouter/instance/describe/describe.go‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,11 @@ func outputResult(p *print.Printer, outputFormat string, instance *telemetryrout
114114
table.AddSeparator()
115115
table.AddRow("CREATION TIME", instance.CreationTime.Format(time.DateTime))
116116

117+
if len(instance.GetResourceLabels()) > 0 {
118+
table.AddSeparator()
119+
table.AddRow("LABELS", utils.JoinStringMap(instance.GetResourceLabels(), ": ", "\n"))
120+
}
121+
117122
if instance.Filter != nil {
118123
table.AddSeparator()
119124
table.AddRow("FILTER DISABLED", utils.PtrString(instance.Filter.Disabled))

‎internal/cmd/beta/telemetryrouter/instance/describe/describe_test.go‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,13 @@ func TestOutputResult(t *testing.T) {
184184
}},
185185
wantErr: false,
186186
},
187+
{
188+
name: "default output with labels",
189+
args: args{outputFormat: "default", instance: &telemetryrouter.TelemetryRouterResponse{
190+
ResourceLabels: &map[string]string{"env": "prod", "team": "sre"},
191+
}},
192+
wantErr: false,
193+
},
187194
{
188195
name: "json output",
189196
args: args{outputFormat: print.JSONOutputFormat, instance: &telemetryrouter.TelemetryRouterResponse{}},

‎internal/cmd/beta/telemetryrouter/instance/update/update.go‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ const (
2929
displayNameFlag = "display-name"
3030
descriptionFlag = "description"
3131
filterFlag = "filter"
32+
labelsFlag = "labels"
3233
)
3334

3435
type inputModel struct {
@@ -37,6 +38,7 @@ type inputModel struct {
3738
DisplayName *string
3839
Description *string
3940
Filter *telemetryrouter.ConfigFilterPatch
41+
Labels *map[string]string
4042
}
4143

4244
func NewCmd(params *types.CmdParams) *cobra.Command {
@@ -52,6 +54,9 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
5254
examples.NewExample(
5355
`Update the description of the Telemetry Router instance with ID "xxx"`,
5456
"$ stackit beta telemetry-router instance update xxx --description new-description"),
57+
examples.NewExample(
58+
`Update the labels of the Telemetry Router instance with ID "xxx"`,
59+
`$ stackit beta telemetry-router instance update xxx --labels "env=prod,team=sre"`),
5560
examples.NewExample(
5661
`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; `+
5762
`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) {
131136
`the filter without changing its attributes (example: {"disabled": true}). Run "stackit beta telemetry-router instance generate-filter --instance-id <id>" `+
132137
`to generate a starting point from the instance's current filter. Clearing all filter attributes is not supported; `+
133138
`an empty "attributes" list leaves the existing attributes unchanged.`)
139+
cmd.Flags().StringToString(labelsFlag, nil, `Labels in key=value format, separated by commas. Replaces all existing labels. Example: --labels "key1=value1,key2=value2".`)
134140
}
135141

136142
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
144150
displayName := flags.FlagToStringPointer(p, cmd, displayNameFlag)
145151
description := flags.FlagToStringPointer(p, cmd, descriptionFlag)
146152
filterValue := flags.FlagToStringPointer(p, cmd, filterFlag)
153+
labels := flags.FlagToStringToStringPointer(p, cmd, labelsFlag)
147154

148155
var filter *telemetryrouter.ConfigFilterPatch
149156
if filterValue != nil {
@@ -155,7 +162,7 @@ func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) (*inpu
155162
filter = parsedFilter
156163
}
157164

158-
if displayName == nil && description == nil && filter == nil {
165+
if displayName == nil && description == nil && filter == nil && labels == nil {
159166
return nil, &errors.EmptyUpdateError{}
160167
}
161168

@@ -165,6 +172,7 @@ func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) (*inpu
165172
DisplayName: displayName,
166173
Description: description,
167174
Filter: filter,
175+
Labels: labels,
168176
}
169177

170178
p.DebugInputModel(model)
@@ -177,9 +185,10 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient telemetryrou
177185

178186
// Partial update: fields which are not set (nil) are left unchanged on API side
179187
return req.PartialUpdateTelemetryRouterPayload(telemetryrouter.PartialUpdateTelemetryRouterPayload{
180-
DisplayName: model.DisplayName,
181-
Description: model.Description,
182-
Filter: model.Filter,
188+
DisplayName: model.DisplayName,
189+
Description: model.Description,
190+
Filter: model.Filter,
191+
ResourceLabels: model.Labels,
183192
})
184193
}
185194

‎internal/cmd/beta/telemetryrouter/instance/update/update_test.go‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,25 @@ func TestParseInput(t *testing.T) {
182182
Description: new("description"),
183183
},
184184
},
185+
{
186+
description: "labels only",
187+
argValues: fixtureArgValues(),
188+
flagValues: map[string]string{
189+
globalflags.ProjectIdFlag: testProjectId,
190+
globalflags.RegionFlag: testRegion,
191+
labelsFlag: "env=prod,team=sre",
192+
},
193+
isValid: true,
194+
expectedModel: &inputModel{
195+
GlobalFlagModel: &globalflags.GlobalFlagModel{
196+
ProjectId: testProjectId,
197+
Region: testRegion,
198+
Verbosity: globalflags.VerbosityDefault,
199+
},
200+
InstanceId: testInstanceId,
201+
Labels: &map[string]string{"env": "prod", "team": "sre"},
202+
},
203+
},
185204
{
186205
description: "project id missing",
187206
argValues: fixtureArgValues(),
@@ -322,6 +341,17 @@ func TestBuildRequest(t *testing.T) {
322341
},
323342
}),
324343
},
344+
{
345+
description: "with labels",
346+
model: fixtureInputModel(func(model *inputModel) {
347+
model.Labels = &map[string]string{"env": "prod"}
348+
}),
349+
expectedRequest: fixtureRequest().PartialUpdateTelemetryRouterPayload(telemetryrouter.PartialUpdateTelemetryRouterPayload{
350+
DisplayName: new("name"),
351+
Description: new("Example"),
352+
ResourceLabels: &map[string]string{"env": "prod"},
353+
}),
354+
},
325355
}
326356

327357
for _, tt := range tests {

0 commit comments

Comments
 (0)