Skip to content

Commit ba70be0

Browse files
committed
chore: update the client library version and use new facilities
1 parent a2b1036 commit ba70be0

46 files changed

Lines changed: 104 additions & 1944 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

go.mod

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ module github.com/dash0hq/dash0-cli
33
go 1.25.1
44

55
require (
6-
github.com/dash0hq/dash0-api-client-go v1.5.1
6+
github.com/dash0hq/dash0-api-client-go v1.6.0
77
github.com/muesli/termenv v0.16.0
88
github.com/pmezard/go-difflib v1.0.0
99
github.com/spf13/cobra v1.8.0
10+
github.com/spf13/pflag v1.0.5
1011
github.com/stretchr/testify v1.11.1
1112
go.opentelemetry.io/collector/pdata v1.51.0
1213
golang.org/x/term v0.40.0
@@ -22,14 +23,12 @@ require (
2223
github.com/hashicorp/go-version v1.8.0 // indirect
2324
github.com/inconshreveable/mousetrap v1.1.0 // indirect
2425
github.com/json-iterator/go v1.1.12 // indirect
25-
github.com/kr/text v0.2.0 // indirect
2626
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
2727
github.com/mattn/go-isatty v0.0.20 // indirect
2828
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
2929
github.com/modern-go/reflect2 v1.0.3-0.20250322232337-35a7c28c31ee // indirect
3030
github.com/oapi-codegen/runtime v1.1.2 // indirect
3131
github.com/rivo/uniseg v0.4.7 // indirect
32-
github.com/spf13/pflag v1.0.5 // indirect
3332
go.opentelemetry.io/collector/featuregate v1.51.0 // indirect
3433
go.uber.org/multierr v1.11.0 // indirect
3534
go.yaml.in/yaml/v2 v2.4.2 // indirect

go.sum

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@ github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiE
55
github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=
66
github.com/bmatcuk/doublestar v1.1.1/go.mod h1:UD6OnuiIn0yFxxA2le/rnRU1G4RaI4UvFv1sNto9p6w=
77
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
8-
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
9-
github.com/dash0hq/dash0-api-client-go v1.5.1 h1:SijzjiUwOpufHu7hMnOP+b2T0Wz/lE4d/ve38PwS2Bg=
10-
github.com/dash0hq/dash0-api-client-go v1.5.1/go.mod h1:BmoXtnoCOVK/PcuzogTf3ys59LSGGIppZC5PXLYCl1I=
8+
github.com/dash0hq/dash0-api-client-go v1.6.0 h1:58Qy3NlCdwa3PKbQ3uRTK4mgP9G6pK0YxDJ3uGnXQjs=
9+
github.com/dash0hq/dash0-api-client-go v1.6.0/go.mod h1:OB1lQ5AKhszeNlNDt+jKgmrzt7iIo/Hqx4ScxiRfoHY=
1110
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1211
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
1312
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=

internal/apply/apply.go

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"strings"
1212

1313
dash0api "github.com/dash0hq/dash0-api-client-go"
14+
dash0yaml "github.com/dash0hq/dash0-api-client-go/yaml"
1415
"github.com/dash0hq/dash0-cli/internal"
1516
"github.com/dash0hq/dash0-cli/internal/asset"
1617
"github.com/dash0hq/dash0-cli/internal/client"
@@ -285,59 +286,66 @@ func formatNameAndId(name, id string) string {
285286
// Extract* functions in internal/asset/ so that apply and the per-asset
286287
// CRUD commands use the same extraction logic.
287288
func parseDocumentHeader(data []byte) (kind, name, id string, err error) {
288-
kind = asset.DetectKind(data)
289+
kind, err = dash0yaml.DetectKind(data)
290+
if err != nil {
291+
return "", "", "", err
292+
}
289293

290294
switch normalizeKind(kind) {
291295
case "dashboard":
292296
var dashboard dash0api.DashboardDefinition
293297
if err := sigsyaml.Unmarshal(data, &dashboard); err != nil {
294298
return "", "", "", fmt.Errorf("failed to decode document: %w", err)
295299
}
296-
name = asset.ExtractDashboardDisplayName(&dashboard)
300+
name = dash0api.GetDashboardName(&dashboard)
297301
if name == "" {
298302
name = dashboard.Metadata.Name
299303
}
300-
id = asset.ExtractDashboardID(&dashboard)
304+
id = dash0api.GetDashboardID(&dashboard)
301305

302306
case "checkrule":
303307
var rule dash0api.PrometheusAlertRule
304308
if err := sigsyaml.Unmarshal(data, &rule); err != nil {
305309
return "", "", "", fmt.Errorf("failed to decode document: %w", err)
306310
}
307-
name = asset.ExtractCheckRuleName(&rule)
308-
id = asset.ExtractCheckRuleID(&rule)
311+
name = dash0api.GetCheckRuleName(&rule)
312+
id = dash0api.GetCheckRuleID(&rule)
309313

310314
case "view":
311315
var view dash0api.ViewDefinition
312316
if err := sigsyaml.Unmarshal(data, &view); err != nil {
313317
return "", "", "", fmt.Errorf("failed to decode document: %w", err)
314318
}
315-
name = asset.ExtractViewName(&view)
316-
id = asset.ExtractViewID(&view)
319+
name = dash0api.GetViewName(&view)
320+
id = dash0api.GetViewID(&view)
317321

318322
case "syntheticcheck":
319323
var check dash0api.SyntheticCheckDefinition
320324
if err := sigsyaml.Unmarshal(data, &check); err != nil {
321325
return "", "", "", fmt.Errorf("failed to decode document: %w", err)
322326
}
323-
name = asset.ExtractSyntheticCheckName(&check)
324-
id = asset.ExtractSyntheticCheckID(&check)
327+
name = dash0api.GetSyntheticCheckName(&check)
328+
id = dash0api.GetSyntheticCheckID(&check)
325329

326330
case "prometheusrule":
327-
var promRule asset.PrometheusRule
328-
if err := sigsyaml.Unmarshal(data, &promRule); err != nil {
331+
// We only need metadata (name + ID) here; the Metadata struct has no
332+
// time.Duration fields, so a partial unmarshal via sigsyaml is safe.
333+
var partial struct {
334+
Metadata dash0api.PrometheusRulesMetadata `json:"metadata"`
335+
}
336+
if err := sigsyaml.Unmarshal(data, &partial); err != nil {
329337
return "", "", "", fmt.Errorf("failed to decode document: %w", err)
330338
}
331-
name = asset.ExtractPrometheusRuleName(&promRule)
332-
id = asset.ExtractPrometheusRuleID(&promRule)
339+
name = partial.Metadata.Name
340+
id = partial.Metadata.Labels[dash0api.LabelID]
333341

334342
case "persesdashboard":
335-
var perses asset.PersesDashboard
343+
var perses dash0api.PersesDashboard
336344
if err := sigsyaml.Unmarshal(data, &perses); err != nil {
337345
return "", "", "", fmt.Errorf("failed to decode document: %w", err)
338346
}
339-
name = asset.ExtractPersesDashboardName(&perses)
340-
id = asset.ExtractPersesDashboardID(&perses)
347+
name = dash0api.GetPersesDashboardName(&perses)
348+
id = dash0api.GetPersesDashboardID(&perses)
341349

342350
default:
343351
var raw map[string]any
@@ -546,21 +554,21 @@ func normalizeKind(kind string) string {
546554
func applyDocument(ctx context.Context, apiClient dash0api.Client, doc assetDocument, dataset *string) ([]applyResult, error) {
547555
switch normalizeKind(doc.kind) {
548556
case "dashboard", "persesdashboard":
549-
dashboard, err := asset.ParseDashboardInput(doc.raw)
557+
dashboard, err := dash0yaml.ParseAsDashboard(doc.raw)
550558
if err != nil {
551559
return nil, err
552560
}
553561
result, err := asset.ImportDashboard(ctx, apiClient, dashboard, dataset)
554562
if err != nil {
555563
return nil, client.HandleAPIError(err, client.ErrorContext{
556564
AssetType: "dashboard",
557-
AssetName: asset.ExtractDashboardDisplayName(dashboard),
565+
AssetName: dash0api.GetDashboardName(dashboard),
558566
})
559567
}
560568
return []applyResult{{kind: "Dashboard", name: result.Name, id: result.ID, action: applyAction(result.Action), before: result.Before, after: result.After}}, nil
561569

562570
case "checkrule", "prometheusrule":
563-
rules, err := asset.ParseCheckRuleInputs(doc.raw)
571+
rules, err := dash0yaml.ParseAsPrometheusAlertRules(doc.raw)
564572
if err != nil {
565573
return nil, err
566574
}

internal/apply/apply_test.go

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"strings"
88
"testing"
99

10-
"github.com/dash0hq/dash0-cli/internal/asset"
1110
"github.com/stretchr/testify/assert"
1211
"github.com/stretchr/testify/require"
1312
)
@@ -189,54 +188,6 @@ func TestApplyAction_String(t *testing.T) {
189188
assert.Equal(t, "updated", string(actionUpdated))
190189
}
191190

192-
func TestConvertToCheckRule(t *testing.T) {
193-
rule := &asset.PrometheusAlertingRule{
194-
Alert: "HighErrorRate",
195-
Expr: "sum(rate(errors[5m])) > 0.1",
196-
For: "5m",
197-
Labels: map[string]string{
198-
"severity": "critical",
199-
},
200-
Annotations: map[string]string{
201-
"summary": "High error rate detected",
202-
"description": "Error rate exceeds threshold",
203-
},
204-
}
205-
206-
checkRule := asset.ConvertToCheckRule(rule, "1m", "test-id")
207-
208-
assert.Equal(t, "HighErrorRate", checkRule.Name)
209-
assert.Equal(t, "sum(rate(errors[5m])) > 0.1", checkRule.Expression)
210-
assert.NotNil(t, checkRule.For)
211-
assert.Equal(t, "5m", string(*checkRule.For))
212-
assert.NotNil(t, checkRule.Interval)
213-
assert.Equal(t, "1m", string(*checkRule.Interval))
214-
assert.NotNil(t, checkRule.Id)
215-
assert.Equal(t, "test-id", *checkRule.Id)
216-
assert.NotNil(t, checkRule.Summary)
217-
assert.Equal(t, "High error rate detected", *checkRule.Summary)
218-
assert.NotNil(t, checkRule.Description)
219-
assert.Equal(t, "Error rate exceeds threshold", *checkRule.Description)
220-
require.NotNil(t, checkRule.Labels)
221-
assert.Equal(t, "critical", (*checkRule.Labels)["severity"])
222-
}
223-
224-
func TestConvertToCheckRule_MinimalInput(t *testing.T) {
225-
rule := &asset.PrometheusAlertingRule{
226-
Alert: "SimpleAlert",
227-
Expr: "up == 0",
228-
}
229-
230-
checkRule := asset.ConvertToCheckRule(rule, "", "")
231-
232-
assert.Equal(t, "SimpleAlert", checkRule.Name)
233-
assert.Equal(t, "up == 0", checkRule.Expression)
234-
assert.Nil(t, checkRule.For)
235-
assert.Nil(t, checkRule.Interval)
236-
assert.Nil(t, checkRule.Id)
237-
assert.Nil(t, checkRule.Labels)
238-
}
239-
240191
func TestPrometheusRuleParsing(t *testing.T) {
241192
yaml := `apiVersion: monitoring.coreos.com/v1
242193
kind: PrometheusRule

internal/apply/integration_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ spec:
268268
require.NotNil(t, dashboard.Metadata.Annotations)
269269
assert.Equal(t, "/test/foo/bar", *dashboard.Metadata.Annotations.Dash0ComfolderPath)
270270
assert.Equal(t, "team-123", *dashboard.Metadata.Annotations.Dash0Comsharing)
271-
assert.Equal(t, dash0api.Ui, *dashboard.Metadata.Annotations.Dash0Comsource)
271+
assert.Equal(t, dash0api.DashboardSourceUi, *dashboard.Metadata.Annotations.Dash0Comsource)
272272
// Server-generated fields are stripped
273273
assert.Nil(t, dashboard.Metadata.Annotations.Dash0ComdeletedAt)
274274
assert.Nil(t, dashboard.Metadata.CreatedAt)

internal/asset/checkrule.go

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,13 @@ import (
66
dash0api "github.com/dash0hq/dash0-api-client-go"
77
)
88

9-
// StripCheckRuleServerFields removes server-generated fields from a check rule
10-
// definition. Used by both Import (to avoid sending rejected fields to the API)
11-
// and diff rendering (to suppress noise).
12-
func StripCheckRuleServerFields(r *dash0api.PrometheusAlertRule) {
13-
r.Dataset = nil
14-
if r.Labels != nil {
15-
delete(*r.Labels, "dash0.com/origin")
16-
}
17-
}
18-
199
// ImportCheckRule creates or updates a check rule via the standard CRUD APIs.
2010
// When the input has a user-defined ID, UPDATE is always used — PUT has
2111
// create-or-replace semantics, so this is idempotent regardless of whether the
2212
// rule already exists.
2313
// When the input has no ID, CREATE is used and the server assigns an ID.
2414
func ImportCheckRule(ctx context.Context, apiClient dash0api.Client, rule *dash0api.PrometheusAlertRule, dataset *string) (ImportResult, error) {
25-
StripCheckRuleServerFields(rule)
15+
dash0api.StripCheckRuleServerFields(rule)
2616

2717
action := ActionCreated
2818
var before any
@@ -53,15 +43,3 @@ func ImportCheckRule(ctx context.Context, apiClient dash0api.Client, rule *dash0
5343
return ImportResult{Name: result.Name, ID: id, Action: action, Before: before, After: result}, nil
5444
}
5545

56-
// ExtractCheckRuleID extracts the ID from a check rule definition.
57-
func ExtractCheckRuleID(rule *dash0api.PrometheusAlertRule) string {
58-
if rule.Id != nil {
59-
return *rule.Id
60-
}
61-
return ""
62-
}
63-
64-
// ExtractCheckRuleName extracts the name from a check rule definition.
65-
func ExtractCheckRuleName(rule *dash0api.PrometheusAlertRule) string {
66-
return rule.Name
67-
}

internal/asset/checkrule_test.go

Lines changed: 0 additions & 55 deletions
This file was deleted.

0 commit comments

Comments
 (0)