From a00a7624fc0798356fbc75474f7619c4138ac3f6 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Thu, 30 Apr 2026 20:12:08 +0100 Subject: [PATCH] fix: accept numeric broker machine ids --- internal/cli/coordinator.go | 18 +++++++++++++++++- internal/cli/coordinator_test.go | 23 +++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 internal/cli/coordinator_test.go diff --git a/internal/cli/coordinator.go b/internal/cli/coordinator.go index 4dda5a0..b233ec1 100644 --- a/internal/cli/coordinator.go +++ b/internal/cli/coordinator.go @@ -37,7 +37,7 @@ type CoordinatorLease struct { } type CoordinatorMachine struct { - ID string `json:"id"` + ID CoordinatorID `json:"id"` Provider string `json:"provider"` CloudID string `json:"cloudID"` Name string `json:"name"` @@ -47,6 +47,22 @@ type CoordinatorMachine struct { Labels map[string]string `json:"labels"` } +type CoordinatorID string + +func (id *CoordinatorID) UnmarshalJSON(data []byte) error { + var s string + if err := json.Unmarshal(data, &s); err == nil { + *id = CoordinatorID(s) + return nil + } + var n int64 + if err := json.Unmarshal(data, &n); err == nil { + *id = CoordinatorID(fmt.Sprint(n)) + return nil + } + return fmt.Errorf("invalid coordinator id: %s", string(data)) +} + func newCoordinatorClient(cfg Config) (*CoordinatorClient, bool, error) { if cfg.Coordinator == "" { return nil, false, nil diff --git a/internal/cli/coordinator_test.go b/internal/cli/coordinator_test.go new file mode 100644 index 0000000..7e6aa7a --- /dev/null +++ b/internal/cli/coordinator_test.go @@ -0,0 +1,23 @@ +package cli + +import ( + "encoding/json" + "testing" +) + +func TestCoordinatorMachineIDAcceptsStringOrNumber(t *testing.T) { + for name, input := range map[string]string{ + "string": `{"id":"i-123","labels":{}}`, + "number": `{"id":128694755,"labels":{}}`, + } { + t.Run(name, func(t *testing.T) { + var machine CoordinatorMachine + if err := json.Unmarshal([]byte(input), &machine); err != nil { + t.Fatal(err) + } + if machine.ID == "" { + t.Fatalf("machine ID was empty") + } + }) + } +}