From e199c37ee4a2b44afe539c58542a25459eb30fdf Mon Sep 17 00:00:00 2001 From: Kyle Havlovitz Date: Fri, 22 Mar 2019 09:25:37 -0700 Subject: [PATCH] Add some basic normalize/validation logic for config entries --- agent/structs/config_entry.go | 40 +++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/agent/structs/config_entry.go b/agent/structs/config_entry.go index 21b12ce788..58a7a325a6 100644 --- a/agent/structs/config_entry.go +++ b/agent/structs/config_entry.go @@ -1,8 +1,12 @@ package structs +import "fmt" + const ( ServiceDefaults string = "service-defaults" ProxyDefaults string = "proxy-defaults" + + ProxyConfigGlobal string = "global" ) // ConfigEntry is the @@ -34,10 +38,20 @@ func (e *ServiceConfigEntry) GetKind() string { } func (e *ServiceConfigEntry) GetName() string { + if e == nil { + return "" + } + return e.Name } func (e *ServiceConfigEntry) Normalize() error { + if e == nil { + return fmt.Errorf("config entry is nil") + } + + e.Kind = ServiceDefaults + return nil } @@ -46,6 +60,10 @@ func (e *ServiceConfigEntry) Validate() error { } func (e *ServiceConfigEntry) GetRaftIndex() *RaftIndex { + if e == nil { + return &RaftIndex{} + } + return &e.RaftIndex } @@ -96,18 +114,40 @@ func (e *ProxyConfigEntry) GetKind() string { } func (e *ProxyConfigEntry) GetName() string { + if e == nil { + return "" + } + return e.Name } func (e *ProxyConfigEntry) Normalize() error { + if e == nil { + return fmt.Errorf("config entry is nil") + } + + e.Kind = ProxyDefaults + return nil } func (e *ProxyConfigEntry) Validate() error { + if e == nil { + return fmt.Errorf("config entry is nil") + } + + if e.Name != ProxyConfigGlobal { + return fmt.Errorf("invalid name (%q), only %q is supported", e.Name, ProxyConfigGlobal) + } + return nil } func (e *ProxyConfigEntry) GetRaftIndex() *RaftIndex { + if e == nil { + return &RaftIndex{} + } + return &e.RaftIndex }