mirror of https://github.com/grafana/grafana
Scopes: Add Handler for returning dashboards related to a list of scopes. (#87758)
- Adds a find endpoint to return dashboard bindings that match any of the scopes. For example /apis/scope.grafana.app/v0alpha1/namespaces/default/find/scope_dashboard_bindings?scope=s1&scope=s2 - Updates the ScopeNode find endpoint to a new path, /find/scope_node_children , makes the key "items" for all find endpoints (instead of mix of "found" and "items"), and makes the list item type a ScopeNode instead of its own type. - Updates kubectl get commands to return more information about scopes, scopenodes, and scopedashboard bindings to display more fields in table output --------- Signed-off-by: bergquist <carl.bergquist@gmail.com> Co-authored-by: Ryan McKinley <ryantxu@gmail.com> Co-authored-by: Kyle Brandt <kyle@grafana.com> Co-authored-by: Todd Treece <todd.treece@grafana.com>pull/88803/head
parent
023857625a
commit
16cc75b02c
@ -0,0 +1,96 @@ |
||||
package scope |
||||
|
||||
import ( |
||||
"context" |
||||
"fmt" |
||||
"net/http" |
||||
|
||||
"k8s.io/apimachinery/pkg/api/errors" |
||||
"k8s.io/apimachinery/pkg/apis/meta/internalversion" |
||||
"k8s.io/apimachinery/pkg/runtime" |
||||
"k8s.io/apimachinery/pkg/runtime/schema" |
||||
"k8s.io/apiserver/pkg/registry/rest" |
||||
|
||||
scope "github.com/grafana/grafana/pkg/apis/scope/v0alpha1" |
||||
) |
||||
|
||||
type findScopeDashboardsREST struct { |
||||
scopeDashboardStorage *storage |
||||
} |
||||
|
||||
var ( |
||||
_ rest.Storage = (*findScopeDashboardsREST)(nil) |
||||
_ rest.SingularNameProvider = (*findScopeDashboardsREST)(nil) |
||||
_ rest.Connecter = (*findScopeDashboardsREST)(nil) |
||||
_ rest.Scoper = (*findScopeDashboardsREST)(nil) |
||||
_ rest.StorageMetadata = (*findScopeDashboardsREST)(nil) |
||||
) |
||||
|
||||
func (f *findScopeDashboardsREST) New() runtime.Object { |
||||
return &scope.FindScopeDashboardBindingsResults{} |
||||
} |
||||
|
||||
func (f *findScopeDashboardsREST) Destroy() {} |
||||
|
||||
func (f *findScopeDashboardsREST) NamespaceScoped() bool { |
||||
return true |
||||
} |
||||
|
||||
func (f *findScopeDashboardsREST) GetSingularName() string { |
||||
return "FindScopeDashboardsResult" // not sure if this is actually used, but it is required to exist
|
||||
} |
||||
|
||||
func (f *findScopeDashboardsREST) ProducesMIMETypes(verb string) []string { |
||||
return []string{"application/json"} // and parquet!
|
||||
} |
||||
|
||||
func (f *findScopeDashboardsREST) ProducesObject(verb string) interface{} { |
||||
return &scope.FindScopeDashboardBindingsResults{} |
||||
} |
||||
|
||||
func (f *findScopeDashboardsREST) ConnectMethods() []string { |
||||
return []string{"GET"} |
||||
} |
||||
|
||||
func (f *findScopeDashboardsREST) NewConnectOptions() (runtime.Object, bool, string) { |
||||
return nil, false, "" // true means you can use the trailing path as a variable
|
||||
} |
||||
|
||||
func (f *findScopeDashboardsREST) Connect(ctx context.Context, name string, opts runtime.Object, responder rest.Responder) (http.Handler, error) { |
||||
// See: /pkg/apiserver/builder/helper.go#L34
|
||||
// The name is set with a rewriter hack
|
||||
if name != "name" { |
||||
return nil, errors.NewNotFound(schema.GroupResource{}, name) |
||||
} |
||||
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { |
||||
raw, err := f.scopeDashboardStorage.List(ctx, &internalversion.ListOptions{}) |
||||
if err != nil { |
||||
w.WriteHeader(500) |
||||
return |
||||
} |
||||
all, ok := raw.(*scope.ScopeDashboardBindingList) |
||||
if !ok { |
||||
w.WriteHeader(500) |
||||
return |
||||
} |
||||
|
||||
scopes := req.URL.Query()["scope"] |
||||
results := &scope.FindScopeDashboardBindingsResults{ |
||||
Message: fmt.Sprintf("Find: %s", scopes), |
||||
Items: make([]scope.ScopeDashboardBinding, 0), |
||||
} |
||||
|
||||
// we can improve the performance by calling .List once per scope if they are index by labels.
|
||||
// The API stays the same thou.
|
||||
for _, item := range all.Items { |
||||
for _, s := range scopes { |
||||
if item.Spec.Scope == s { |
||||
results.Items = append(results.Items, item) |
||||
} |
||||
} |
||||
} |
||||
|
||||
responder.Object(200, results) |
||||
}), nil |
||||
} |
||||
Loading…
Reference in new issue