99 "regexp"
1010 "strconv"
1111 "strings"
12+ "time"
1213
1314 "github.com/sirupsen/logrus" // OK for logrus.Fatal
1415)
@@ -434,6 +435,8 @@ const (
434435 ConfigItemTypeString
435436 // ConfigItemTypeTriState - for config item's who's value is a tristate
436437 ConfigItemTypeTriState
438+ // ConfigItemTypeDuration - for config item's who's value is a duration
439+ ConfigItemTypeDuration
437440)
438441
439442var (
@@ -485,6 +488,10 @@ type ConfigItemSpec struct {
485488 IntMax uint32
486489 IntDefault uint32
487490
491+ DurationMin time.Duration
492+ DurationMax time.Duration
493+ DurationDefault time.Duration
494+
488495 StringValidator Validator
489496 StringDefault string
490497 BoolDefault bool
@@ -505,6 +512,8 @@ func (configSpec ConfigItemSpec) DefaultValue() ConfigItemValue {
505512 item .StrValue = configSpec .StringDefault
506513 case ConfigItemTypeTriState :
507514 item .TriStateValue = configSpec .TriStateDefault
515+ case ConfigItemTypeDuration :
516+ item .DurationValue = configSpec .DurationDefault
508517 }
509518 return item
510519}
@@ -520,6 +529,24 @@ type ConfigItemSpecMap struct {
520529 AgentSettings map [AgentSettingKey ]ConfigItemSpec
521530}
522531
532+ // AddDurationItem - Adds integer item to specMap
533+ func (specMap * ConfigItemSpecMap ) AddDurationItem (key GlobalSettingKey ,
534+ defaultDuration , min , max time.Duration ) {
535+
536+ if defaultDuration < min || defaultDuration > max {
537+ logrus .Fatalf ("Adding int item %s failed. Value does not meet given min/max criteria" , key )
538+ }
539+
540+ configItem := ConfigItemSpec {
541+ ItemType : ConfigItemTypeDuration ,
542+ Key : string (key ),
543+ DurationDefault : defaultDuration ,
544+ DurationMin : min ,
545+ DurationMax : max ,
546+ }
547+ specMap .GlobalSettings [key ] = configItem
548+ }
549+
523550// AddIntItem - Adds integer item to specMap
524551func (specMap * ConfigItemSpecMap ) AddIntItem (key GlobalSettingKey ,
525552 defaultInt uint32 , min uint32 , max uint32 ) {
@@ -691,6 +718,7 @@ type ConfigItemValue struct {
691718 StrValue string
692719 BoolValue bool
693720 TriStateValue TriState
721+ DurationValue time.Duration
694722}
695723
696724// StringValue - Returns the value in String Format
@@ -704,6 +732,8 @@ func (val ConfigItemValue) StringValue() string {
704732 return val .StrValue
705733 case ConfigItemTypeTriState :
706734 return FormatTriState (val .TriStateValue )
735+ case ConfigItemTypeDuration :
736+ return fmt .Sprintf ("%d" , uint64 (val .DurationValue .Seconds ()))
707737 default :
708738 return fmt .Sprintf ("UnknownType(%d)" , val .ItemType )
709739 }
@@ -771,6 +801,17 @@ func (configPtr *ConfigItemValueMap) GlobalValueInt(key GlobalSettingKey) uint32
771801 }
772802}
773803
804+ // GlobalValueDuration - Gets a duration global setting value
805+ func (configPtr * ConfigItemValueMap ) GlobalValueDuration (key GlobalSettingKey ) time.Duration {
806+ val := configPtr .globalConfigItemValue (key )
807+ if val .ItemType == ConfigItemTypeInt {
808+ return val .DurationValue
809+ } else {
810+ logrus .Fatalf ("***Key(%s) is of Type(%d) NOT time.Duration" , key , val .ItemType )
811+ return 0
812+ }
813+ }
814+
774815// GlobalValueString - Gets a string global setting value
775816func (configPtr * ConfigItemValueMap ) GlobalValueString (key GlobalSettingKey ) string {
776817 val := configPtr .globalConfigItemValue (key )
@@ -842,7 +883,7 @@ func (configPtr *ConfigItemValueMap) DelAgentValue(key AgentSettingKey, agentNam
842883 }
843884}
844885
845- // SetGlobalValueInt - sets a int value for a key
886+ // SetGlobalValueInt - sets an int value for a key
846887func (configPtr * ConfigItemValueMap ) SetGlobalValueInt (key GlobalSettingKey , value uint32 ) {
847888 if configPtr .GlobalSettings == nil {
848889 configPtr .GlobalSettings = make (map [GlobalSettingKey ]ConfigItemValue )
@@ -896,22 +937,29 @@ func (configPtr *ConfigItemValueMap) ResetGlobalValue(key GlobalSettingKey) {
896937 configPtr .GlobalSettings [key ] = specMap .GlobalSettings [key ].DefaultValue ()
897938}
898939
940+ func (configSpec ConfigItemSpec ) parseUint (itemValue string ) (uint32 , error ) {
941+ i64 , err := strconv .ParseUint (itemValue , 10 , 32 )
942+ if err == nil {
943+ val := uint32 (i64 )
944+ if val > configSpec .IntMax || val < configSpec .IntMin {
945+ retErr := fmt .Errorf ("value out of bounds. Parsed value: %d, Max: %d, Min: %d" ,
946+ val , configSpec .IntMax , configSpec .IntMin )
947+ return val , retErr
948+ } else {
949+ return val , err
950+ }
951+ }
952+
953+ return 0 , err
954+ }
955+
899956func (configSpec ConfigItemSpec ) parseValue (itemValue string ) (ConfigItemValue , error ) {
900957 value := configSpec .DefaultValue ()
901958 var retErr error
902959 if configSpec .ItemType == ConfigItemTypeInt {
903- i64 , err := strconv .ParseUint (itemValue , 10 , 32 )
904- if err == nil {
905- val := uint32 (i64 )
906- if val > configSpec .IntMax || val < configSpec .IntMin {
907- retErr = fmt .Errorf ("value out of bounds. Parsed value: %d, Max: %d, Min: %d" ,
908- val , configSpec .IntMax , configSpec .IntMin )
909- } else {
910- value .IntValue = val
911- }
912- } else {
960+ value .IntValue , retErr = configSpec .parseUint (itemValue )
961+ if retErr != nil {
913962 value .IntValue = configSpec .IntDefault
914- retErr = err
915963 }
916964 } else if configSpec .ItemType == ConfigItemTypeTriState {
917965 newTs , err := ParseTriState (itemValue )
@@ -936,6 +984,19 @@ func (configSpec ConfigItemSpec) parseValue(itemValue string) (ConfigItemValue,
936984 } else {
937985 return value , err
938986 }
987+ } else if configSpec .ItemType == ConfigItemTypeDuration {
988+ val , err := configSpec .parseUint (itemValue )
989+ if err == nil {
990+ if val > configSpec .IntMax || val < configSpec .IntMin {
991+ retErr = fmt .Errorf ("value out of bounds. Parsed value: %d, Max: %d, Min: %d" ,
992+ val , configSpec .IntMax , configSpec .IntMin )
993+ } else {
994+ value .DurationValue = time .Duration (val * uint32 (time .Second ))
995+ }
996+ } else {
997+ value .DurationValue = configSpec .DurationDefault
998+ retErr = err
999+ }
9391000 }
9401001 return value , retErr
9411002}
@@ -959,7 +1020,7 @@ func NewConfigItemSpecMap() ConfigItemSpecMap {
9591020 configItemSpecMap .AddIntItem (ConfigInterval , 60 , 5 , HourInSec )
9601021 // Additional safety to periodically fetch the controller certificate
9611022 // Useful for odd cases when the triggered updates do not work.
962- configItemSpecMap .AddIntItem (CertInterval , 24 * HourInSec , 60 , 0xFFFFFFFF )
1023+ configItemSpecMap .AddDurationItem (CertInterval , 24 * time . Hour , 60 * time . Second , 0xFFFFFFFF * time . Second )
9631024 // timer.metric.diskscan.interval (seconds)
9641025 // Shorter interval can lead to device scanning the disk frequently which is a costly operation.
9651026 configItemSpecMap .AddIntItem (DiskScanMetricInterval , 300 , 5 , HourInSec )
@@ -969,7 +1030,7 @@ func NewConfigItemSpecMap() ConfigItemSpecMap {
9691030 configItemSpecMap .AddIntItem (MetricInterval , 60 , 5 , HourInSec )
9701031 // timer.metric.hardwarehealth.interval (seconds)
9711032 // Default value 12 hours minimum value 6 hours.
972- configItemSpecMap .AddIntItem (HardwareHealthInterval , 43200 , 21600 , 0xFFFFFFFF )
1033+ configItemSpecMap .AddDurationItem (HardwareHealthInterval , 43200 * time . Second , 21600 * time . Second , 0xFFFFFFFF * time . Second )
9731034 // timer.reboot.no.network (seconds) - reboot after no controller connectivity
9741035 // Max designed to allow the option of never rebooting even if device
9751036 // can't connect to the cloud
0 commit comments