fix: accept numeric broker machine ids
This commit is contained in:
parent
484675a7ec
commit
a00a7624fc
@ -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
|
||||
|
||||
23
internal/cli/coordinator_test.go
Normal file
23
internal/cli/coordinator_test.go
Normal 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")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user