-
Notifications
You must be signed in to change notification settings - Fork 11.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature toggles management: Define get feature toggles api #72106
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package api | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/grafana/grafana/pkg/api/response" | ||
contextmodel "github.com/grafana/grafana/pkg/services/contexthandler/model" | ||
) | ||
|
||
func (hs *HTTPServer) GetFeatureToggles(ctx *contextmodel.ReqContext) response.Response { | ||
featureMgmtCfg := hs.Cfg.FeatureManagement | ||
|
||
features := hs.Features.GetFlags() | ||
enabledFeatures := hs.Features.GetEnabled(ctx.Req.Context()) | ||
|
||
for i := 0; i < len(features); { | ||
ft := features[i] | ||
if _, ok := featureMgmtCfg.HiddenToggles[ft.Name]; ok { | ||
features = append(features[:i], features[i+1:]...) // remove feature | ||
continue | ||
} | ||
if _, ok := featureMgmtCfg.ReadOnlyToggles[ft.Name]; ok { | ||
features[i].ReadOnly = true | ||
} | ||
features[i].Enabled = enabledFeatures[ft.Name] | ||
i++ | ||
} | ||
|
||
return response.JSON(http.StatusOK, features) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package api | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
|
||
"net/http" | ||
|
||
"github.com/grafana/grafana/pkg/services/accesscontrol" | ||
"github.com/grafana/grafana/pkg/services/featuremgmt" | ||
"github.com/grafana/grafana/pkg/services/org/orgtest" | ||
"github.com/grafana/grafana/pkg/services/user" | ||
"github.com/grafana/grafana/pkg/services/user/usertest" | ||
"github.com/grafana/grafana/pkg/setting" | ||
"github.com/grafana/grafana/pkg/web/webtest" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestGetFeatureToggles(t *testing.T) { | ||
type testCase struct { | ||
desc string | ||
permissions []accesscontrol.Permission | ||
features []interface{} | ||
expectedCode int | ||
hiddenTogles map[string]struct{} | ||
readOnlyToggles map[string]struct{} | ||
} | ||
|
||
tests := []testCase{ | ||
{ | ||
desc: "should not be able to get feature toggles without permissions", | ||
permissions: []accesscontrol.Permission{}, | ||
features: []interface{}{}, | ||
expectedCode: http.StatusForbidden, | ||
}, | ||
{ | ||
desc: "should be able to get feature toggles with correct permissions", | ||
permissions: []accesscontrol.Permission{{Action: accesscontrol.ActionFeatureManagementRead}}, | ||
features: []interface{}{"toggle1", true, "toggle2", false}, | ||
expectedCode: http.StatusOK, | ||
}, | ||
{ | ||
desc: "hidden toggles are not present in the response", | ||
permissions: []accesscontrol.Permission{{Action: accesscontrol.ActionFeatureManagementRead}}, | ||
features: []interface{}{"toggle1", true, "toggle2", false}, | ||
expectedCode: http.StatusOK, | ||
hiddenTogles: map[string]struct{}{"toggle1": {}}, | ||
}, | ||
{ | ||
desc: "read only toggles have the readOnly field set", | ||
permissions: []accesscontrol.Permission{{Action: accesscontrol.ActionFeatureManagementRead}}, | ||
features: []interface{}{"toggle1", true, "toggle2", false}, | ||
expectedCode: http.StatusOK, | ||
hiddenTogles: map[string]struct{}{"toggle1": {}}, | ||
readOnlyToggles: map[string]struct{}{"toggle2": {}}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.desc, func(t *testing.T) { | ||
cfg := setting.NewCfg() | ||
cfg.FeatureManagement.HiddenToggles = tt.hiddenTogles | ||
cfg.FeatureManagement.ReadOnlyToggles = tt.readOnlyToggles | ||
server := SetupAPITestServer(t, func(hs *HTTPServer) { | ||
hs.Cfg = cfg | ||
hs.Features = featuremgmt.WithFeatures(append([]interface{}{"featureToggleAdminPage", true}, tt.features...)...) | ||
hs.orgService = orgtest.NewOrgServiceFake() | ||
hs.userService = &usertest.FakeUserService{ | ||
ExpectedUser: &user.User{ID: 1}, | ||
} | ||
}) | ||
|
||
req := webtest.RequestWithSignedInUser(server.NewGetRequest("/api/featuremgmt"), userWithPermissions(1, tt.permissions)) | ||
res, err := server.SendJSON(req) | ||
require.NoError(t, err) | ||
defer func() { require.NoError(t, res.Body.Close()) }() | ||
assert.Equal(t, tt.expectedCode, res.StatusCode) | ||
|
||
if tt.expectedCode == http.StatusOK { | ||
var result []featuremgmt.FeatureFlag | ||
err := json.NewDecoder(res.Body).Decode(&result) | ||
require.NoError(t, err) | ||
|
||
for _, ft := range result { | ||
if _, ok := tt.hiddenTogles[ft.Name]; ok { | ||
t.Fail() | ||
} | ||
if _, ok := tt.readOnlyToggles[ft.Name]; ok { | ||
assert.True(t, ft.ReadOnly, tt.desc) | ||
} | ||
} | ||
assert.Equal(t, 3-len(tt.hiddenTogles), len(result), tt.desc) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package setting | ||
|
||
import ( | ||
"github.com/grafana/grafana/pkg/util" | ||
) | ||
|
||
type FeatureMgmtSettings struct { | ||
HiddenToggles map[string]struct{} | ||
ReadOnlyToggles map[string]struct{} | ||
} | ||
|
||
func (cfg *Cfg) readFeatureManagementConfig() { | ||
section := cfg.Raw.Section("feature_management") | ||
|
||
hiddenToggles := make(map[string]struct{}) | ||
readOnlyToggles := make(map[string]struct{}) | ||
|
||
// parse the comma separated list in `hidden_toggles`. | ||
hiddenTogglesStr := valueAsString(section, "hidden_toggles", "") | ||
for _, feature := range util.SplitString(hiddenTogglesStr) { | ||
hiddenToggles[feature] = struct{}{} | ||
} | ||
|
||
// parse the comma separated list in `read_only_toggles`. | ||
readOnlyTogglesStr := valueAsString(section, "read_only_toggles", "") | ||
for _, feature := range util.SplitString(readOnlyTogglesStr) { | ||
readOnlyToggles[feature] = struct{}{} | ||
} | ||
|
||
cfg.FeatureManagement.HiddenToggles = hiddenToggles | ||
cfg.FeatureManagement.ReadOnlyToggles = readOnlyToggles | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need these as instance level config? (Vs just options in each feature)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These configs were proposed during our DNA discussion, as they could be useful for operators (HG or others) to decide which features their grafana administrators would be able to tune in.