From acc3dda2dfda22ded3a56d8a17813d27b7486a88 Mon Sep 17 00:00:00 2001 From: Jim Date: Wed, 4 Aug 2021 17:57:36 -0400 Subject: [PATCH] parse the duration string specified in an event file config into a time.Duration (#1446) --- internal/cmd/config/config.go | 9 +++++++++ internal/cmd/config/config_test.go | 11 +++++++++-- internal/observability/event/sink_config.go | 11 ++++++----- 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/internal/cmd/config/config.go b/internal/cmd/config/config.go index 72f587644f..638e6d8438 100644 --- a/internal/cmd/config/config.go +++ b/internal/cmd/config/config.go @@ -473,6 +473,15 @@ func parseEventing(eventObj *ast.ObjectItem) (*event.EventerConfig, error) { s.StderrConfig = new(event.StderrSinkTypeConfig) } + // parse the duration string specified in a file config into a time.Duration + if s.FileConfig != nil && s.FileConfig.RotateDurationHCL != "" { + var err error + s.FileConfig.RotateDuration, err = time.ParseDuration(s.FileConfig.RotateDurationHCL) + if err != nil { + return nil, fmt.Errorf("can't parse rotation duration %s", s.FileConfig.RotateDurationHCL) + } + } + if err := s.Validate(); err != nil { return nil, err } diff --git a/internal/cmd/config/config_test.go b/internal/cmd/config/config_test.go index 406128dd6b..384c189c18 100644 --- a/internal/cmd/config/config_test.go +++ b/internal/cmd/config/config_test.go @@ -4,6 +4,7 @@ import ( "fmt" "os" "testing" + "time" "github.com/hashicorp/boundary/internal/observability/event" "github.com/hashicorp/go-secure-stdlib/configutil" @@ -394,6 +395,7 @@ func TestController_EventingConfig(t *testing.T) { event_types = [ "audit", "observation" ] file { file_name = "file-name" + rotate_duration = "2m" } } sink { @@ -412,6 +414,7 @@ func TestController_EventingConfig(t *testing.T) { event_types = [ "audit", "observation" ] file { file_name = "file-name" + rotate_duration = "2m" } } sink "stderr" { @@ -429,6 +432,7 @@ func TestController_EventingConfig(t *testing.T) { event_types = [ "audit", "observation" ] file { file_name = "file-name" + rotate_duration = "2m" } } sink { @@ -449,7 +453,8 @@ func TestController_EventingConfig(t *testing.T) { "name": "configured-sink", "event_types": ["audit", "observation"], "file": { - "file_name": "file-name" + "file_name": "file-name", + "rotate_duration": "2m" } }, { @@ -472,7 +477,9 @@ func TestController_EventingConfig(t *testing.T) { Format: "cloudevents-json", EventTypes: []event.Type{"audit", "observation"}, FileConfig: &event.FileSinkTypeConfig{ - FileName: "file-name", + FileName: "file-name", + RotateDurationHCL: "2m", + RotateDuration: 2 * time.Minute, }, }, { diff --git a/internal/observability/event/sink_config.go b/internal/observability/event/sink_config.go index 63dfacdd68..942bb84aa2 100644 --- a/internal/observability/event/sink_config.go +++ b/internal/observability/event/sink_config.go @@ -75,11 +75,12 @@ type StderrSinkTypeConfig struct{} // FileSinkTypeConfig contains configuration structures for file sink types type FileSinkTypeConfig struct { - Path string `hcl:"path" mapstructure:"path"` // Path defines the file path for the sink - FileName string `hcl:"file_name" mapstructure:"file_name"` // FileName defines the file name for the sink - RotateBytes int `hcl:"rotate_bytes" mapstructure:"rotate_bytes"` // RotateByes defines the number of bytes that should trigger rotation of a FileSink - RotateDuration time.Duration `hcl:"rotate_duration" mapstructure:"rotate_duration"` // RotateDuration defines how often a FileSink should be rotated - RotateMaxFiles int `hcl:"rotate_max_files" mapstructure:"rotate_max_files"` // RotateMaxFiles defines how may historical rotated files should be kept for a FileSink + Path string `hcl:"path" mapstructure:"path"` // Path defines the file path for the sink + FileName string `hcl:"file_name" mapstructure:"file_name"` // FileName defines the file name for the sink + RotateBytes int `hcl:"rotate_bytes" mapstructure:"rotate_bytes"` // RotateBytes defines the number of bytes that should trigger rotation of a FileSink + RotateDuration time.Duration `mapstructure:"rotate_duration"` // RotateDuration defines how often a FileSink should be rotated + RotateDurationHCL string `hcl:"rotate_duration" json:"-"` // RotateDurationHCL defines hcl string version of RotateDuration + RotateMaxFiles int `hcl:"rotate_max_files" mapstructure:"rotate_max_files"` // RotateMaxFiles defines how may historical rotated files should be kept for a FileSink } // FilterType defines a type for filters (allow or deny)