FeatureToggles: register all enterprise feature toggles (#44336)

This commit is contained in:
Ryan McKinley 2022-01-21 11:36:28 -08:00 committed by GitHub
parent 9fc0aee02b
commit f53b3fb007
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 82 additions and 0 deletions

View File

@ -14,6 +14,9 @@ type Licensing interface {
StateInfo() string
// List the enabled features
EnabledFeatures() map[string]bool
FeatureEnabled(feature string) bool
}

View File

@ -51,6 +51,23 @@ func ProvideManagerService(cfg *setting.Cfg, licensing models.Licensing) (*Featu
flag.Expression = fmt.Sprintf("%t", val) // true | false
}
// Make sure enterprise features are registered
for key, val := range licensing.EnabledFeatures() {
f, ok := mgmt.flags[key]
if ok {
f.RequiresLicense = true
if f.Expression == "" {
f.Expression = "true"
}
} else if val {
mgmt.registerFlags(FeatureFlag{
Name: key,
RequiresLicense: true,
Expression: "true",
})
}
}
// Load config settings
configfile := filepath.Join(cfg.HomePath, "conf", "features.yaml")
if _, err := os.Stat(configfile); err == nil {

View File

@ -0,0 +1,62 @@
package featuremgmt
import (
"testing"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/setting"
"github.com/stretchr/testify/require"
)
func TestFeatureService(t *testing.T) {
license := stubLicenseServier{
flags: map[string]bool{
"some.feature": true,
"another": true,
},
}
cfg := setting.NewCfg()
mgmt, err := ProvideManagerService(cfg, license)
require.NoError(t, err)
require.NotNil(t, mgmt)
require.False(t, license.FeatureEnabled("test"))
require.True(t, license.FeatureEnabled("some.feature"))
require.True(t, mgmt.IsEnabled("some.feature"))
}
var (
_ models.Licensing = (*stubLicenseServier)(nil)
)
type stubLicenseServier struct {
flags map[string]bool
}
func (s stubLicenseServier) Expiry() int64 {
return 100
}
func (s stubLicenseServier) Edition() string {
return "test"
}
func (s stubLicenseServier) ContentDeliveryPrefix() string {
return ""
}
func (s stubLicenseServier) LicenseURL(showAdminLicensingPage bool) string {
return "http://??"
}
func (s stubLicenseServier) StateInfo() string {
return "ok"
}
func (s stubLicenseServier) EnabledFeatures() map[string]bool {
return s.flags
}
func (s stubLicenseServier) FeatureEnabled(feature string) bool {
return s.flags[feature]
}