Skip to content

Commit

Permalink
Include RequestTimeout in marshal/unmarshal of ServiceResolverConfigE…
Browse files Browse the repository at this point in the history
…ntry
  • Loading branch information
cthain committed Sep 29, 2023
1 parent 7ce6eba commit 4ad1a87
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 9 deletions.
12 changes: 7 additions & 5 deletions agent/structs/config_entry_discoverychain.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"time"

"github.com/hashicorp/go-bexpr"
"github.com/hashicorp/go-multierror"
"github.com/mitchellh/copystructure"
"github.com/mitchellh/hashstructure"

Expand Down Expand Up @@ -929,21 +930,22 @@ func (e *ServiceResolverConfigEntry) UnmarshalJSON(data []byte) error {
}{
Alias: (*Alias)(e),
}
if err := lib.UnmarshalJSON(data, &aux); err != nil {
var err error
if err = lib.UnmarshalJSON(data, &aux); err != nil {
return err
}
var err error
var merr *multierror.Error
if aux.ConnectTimeout != "" {
if e.ConnectTimeout, err = time.ParseDuration(aux.ConnectTimeout); err != nil {
return err
merr = multierror.Append(merr, err)
}
}
if aux.RequestTimeout != "" {
if e.RequestTimeout, err = time.ParseDuration(aux.RequestTimeout); err != nil {
return err
merr = multierror.Append(merr, err)
}
}
return nil
return merr.ErrorOrNil()
}

func (e *ServiceResolverConfigEntry) SubsetExists(name string) bool {
Expand Down
9 changes: 9 additions & 0 deletions agent/structs/config_entry_discoverychain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1571,6 +1571,15 @@ func TestServiceResolverConfigEntry(t *testing.T) {
},
validateErr: "Bad ConnectTimeout",
},
{
name: "bad request timeout",
entry: &ServiceResolverConfigEntry{
Kind: ServiceResolver,
Name: "test",
RequestTimeout: -1 * time.Second,
},
validateErr: "Bad RequestTimeout",
},
}

// Bulk add a bunch of similar validation cases.
Expand Down
22 changes: 18 additions & 4 deletions api/config_entry_discoverychain.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ package api
import (
"encoding/json"
"time"

"github.com/hashicorp/go-multierror"
)

type ServiceRouterConfigEntry struct {
Expand Down Expand Up @@ -189,14 +191,19 @@ func (e *ServiceResolverConfigEntry) MarshalJSON() ([]byte, error) {
type Alias ServiceResolverConfigEntry
exported := &struct {
ConnectTimeout string `json:",omitempty"`
RequestTimeout string `json:",omitempty"`
*Alias
}{
ConnectTimeout: e.ConnectTimeout.String(),
RequestTimeout: e.RequestTimeout.String(),
Alias: (*Alias)(e),
}
if e.ConnectTimeout == 0 {
exported.ConnectTimeout = ""
}
if e.RequestTimeout == 0 {
exported.RequestTimeout = ""
}

return json.Marshal(exported)
}
Expand All @@ -205,20 +212,27 @@ func (e *ServiceResolverConfigEntry) UnmarshalJSON(data []byte) error {
type Alias ServiceResolverConfigEntry
aux := &struct {
ConnectTimeout string
RequestTimeout string
*Alias
}{
Alias: (*Alias)(e),
}
if err := json.Unmarshal(data, &aux); err != nil {
var err error
if err = json.Unmarshal(data, &aux); err != nil {
return err
}
var err error
var merr *multierror.Error
if aux.ConnectTimeout != "" {
if e.ConnectTimeout, err = time.ParseDuration(aux.ConnectTimeout); err != nil {
return err
merr = multierror.Append(merr, err)
}
}
return nil
if aux.RequestTimeout != "" {
if e.RequestTimeout, err = time.ParseDuration(aux.RequestTimeout); err != nil {
merr = multierror.Append(merr, err)
}
}
return merr.ErrorOrNil()
}

func (e *ServiceResolverConfigEntry) GetKind() string { return e.Kind }
Expand Down
1 change: 1 addition & 0 deletions api/config_entry_discoverychain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ func TestAPI_ConfigEntry_DiscoveryChain(t *testing.T) {
},
},
ConnectTimeout: 5 * time.Second,
RequestTimeout: 10 * time.Second,
Meta: map[string]string{
"foo": "bar",
"gir": "zim",
Expand Down

0 comments on commit 4ad1a87

Please sign in to comment.