fix: accept numeric broker machine ids
Some checks are pending
CI / Go (push) Waiting to run
CI / Worker (push) Waiting to run
CI / Release Check (push) Waiting to run

This commit is contained in:
Peter Steinberger 2026-04-30 20:12:08 +01:00
parent 484675a7ec
commit a00a7624fc
No known key found for this signature in database
2 changed files with 40 additions and 1 deletions

View File

@ -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

View File

@ -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")
}
})
}
}