github.com/turbot/steampipe@v1.7.0-rc.0.0.20240517123944-7cef272d4458/pkg/db/db_common/cache_settings.go (about)

     1  package db_common
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/spf13/viper"
     7  	"github.com/turbot/steampipe/pkg/constants"
     8  	"github.com/turbot/steampipe/pkg/error_helpers"
     9  )
    10  
    11  func ValidateClientCacheSettings(c Client) error_helpers.ErrorAndWarnings {
    12  	cacheEnabledResult := ValidateClientCacheEnabled(c)
    13  	cacheTtlResult := ValidateClientCacheTtl(c)
    14  
    15  	return cacheEnabledResult.Merge(cacheTtlResult)
    16  }
    17  
    18  func ValidateClientCacheEnabled(c Client) error_helpers.ErrorAndWarnings {
    19  	errorsAndWarnings := error_helpers.EmptyErrorsAndWarning()
    20  	if c.ServerSettings() == nil || !viper.IsSet(constants.ArgClientCacheEnabled) {
    21  		// if there's no serverSettings, then this is a pre-21 server
    22  		// behave as if there's no problem
    23  		return errorsAndWarnings
    24  	}
    25  
    26  	if !c.ServerSettings().CacheEnabled && viper.GetBool(constants.ArgClientCacheEnabled) {
    27  		errorsAndWarnings.AddWarning("Caching is disabled on the server.")
    28  	}
    29  	return errorsAndWarnings
    30  }
    31  
    32  func ValidateClientCacheTtl(c Client) error_helpers.ErrorAndWarnings {
    33  	errorsAndWarnings := error_helpers.EmptyErrorsAndWarning()
    34  
    35  	if c.ServerSettings() == nil || !viper.IsSet(constants.ArgCacheTtl) {
    36  		// if there's no serverSettings, then this is a pre-21 server
    37  		// behave as if there's no problem
    38  		return errorsAndWarnings
    39  	}
    40  
    41  	clientTtl := viper.GetInt(constants.ArgCacheTtl)
    42  	if can, whyCannotSet := CanSetCacheTtl(c.ServerSettings(), clientTtl); !can {
    43  		errorsAndWarnings.AddWarning(whyCannotSet)
    44  	}
    45  	return errorsAndWarnings
    46  }
    47  
    48  func CanSetCacheTtl(ss *ServerSettings, newTtl int) (bool, string) {
    49  	if ss == nil {
    50  		// nothing to enforce
    51  		return true, ""
    52  	}
    53  	serverMaxTtl := ss.CacheMaxTtl
    54  	if newTtl > serverMaxTtl {
    55  		return false, fmt.Sprintf("Server enforces maximum TTL of %d seconds. Cannot set TTL to %d seconds. TTL set to %d seconds.", serverMaxTtl, newTtl, serverMaxTtl)
    56  	}
    57  	return true, ""
    58  }