-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for grafana config to PluginContext (#726)
* first pass * fix linter * update factory func * simplify field * fix linter * tidy * fix linter * apply PR feedback * update field name * update field * refactor instance stale check * fix linter * tidy * Revert "refactor instance stale check" This reverts commit 0273225. * update proto field name * update func names and remove unused func * rename func and fix imports * add pdc to cfg * fix linter * remove newline * update field names
- Loading branch information
Showing
21 changed files
with
799 additions
and
329 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package backend | ||
|
||
import ( | ||
"context" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/grafana/grafana-plugin-sdk-go/backend/proxy" | ||
"github.com/grafana/grafana-plugin-sdk-go/experimental/featuretoggles" | ||
) | ||
|
||
type configKey struct{} | ||
|
||
// GrafanaConfigFromContext returns Grafana config from context. | ||
func GrafanaConfigFromContext(ctx context.Context) *GrafanaCfg { | ||
v := ctx.Value(configKey{}) | ||
if v == nil { | ||
return NewGrafanaCfg(nil) | ||
} | ||
|
||
return v.(*GrafanaCfg) | ||
} | ||
|
||
// withGrafanaConfig injects supplied Grafana config into context. | ||
func withGrafanaConfig(ctx context.Context, cfg *GrafanaCfg) context.Context { | ||
ctx = context.WithValue(ctx, configKey{}, cfg) | ||
return ctx | ||
} | ||
|
||
type GrafanaCfg struct { | ||
config map[string]string | ||
} | ||
|
||
func NewGrafanaCfg(cfg map[string]string) *GrafanaCfg { | ||
return &GrafanaCfg{config: cfg} | ||
} | ||
|
||
func (c *GrafanaCfg) Get(key string) string { | ||
return c.config[key] | ||
} | ||
|
||
func (c *GrafanaCfg) FeatureToggles() FeatureToggles { | ||
features, exists := c.config[featuretoggles.EnabledFeatures] | ||
if !exists { | ||
return FeatureToggles{} | ||
} | ||
|
||
fs := strings.Split(features, ",") | ||
enabledFeatures := make(map[string]struct{}, len(fs)) | ||
for _, f := range fs { | ||
enabledFeatures[f] = struct{}{} | ||
} | ||
|
||
return FeatureToggles{ | ||
enabled: enabledFeatures, | ||
} | ||
} | ||
|
||
func (c *GrafanaCfg) Equal(c2 *GrafanaCfg) bool { | ||
if c == nil && c2 == nil { | ||
return true | ||
} | ||
if c == nil || c2 == nil { | ||
return false | ||
} | ||
|
||
if len(c.config) != len(c2.config) { | ||
return false | ||
} | ||
for k, v1 := range c.config { | ||
if v2, ok := c2.config[k]; !ok || v1 != v2 { | ||
return false | ||
} | ||
} | ||
return true | ||
} | ||
|
||
type FeatureToggles struct { | ||
// enabled is a set-like map of feature flags that are enabled. | ||
enabled map[string]struct{} | ||
} | ||
|
||
// IsEnabled returns true if feature f is contained in ft.enabled. | ||
func (ft FeatureToggles) IsEnabled(f string) bool { | ||
_, exists := ft.enabled[f] | ||
return exists | ||
} | ||
|
||
type Proxy struct { | ||
clientCfg proxy.ClientCfg | ||
} | ||
|
||
func (pc Proxy) ClientConfig() proxy.ClientCfg { | ||
return pc.clientCfg | ||
} | ||
|
||
func (c *GrafanaCfg) Proxy() Proxy { | ||
if v, exists := c.config[proxy.PluginSecureSocksProxyEnabled]; exists && v == strconv.FormatBool(true) { | ||
return Proxy{ | ||
clientCfg: proxy.ClientCfg{ | ||
Enabled: true, | ||
ClientCert: c.Get(proxy.PluginSecureSocksProxyClientCert), | ||
ClientKey: c.Get(proxy.PluginSecureSocksProxyClientKey), | ||
RootCA: c.Get(proxy.PluginSecureSocksProxyRootCACert), | ||
ProxyAddress: c.Get(proxy.PluginSecureSocksProxyProxyAddress), | ||
ServerName: c.Get(proxy.PluginSecureSocksProxyServerName), | ||
}, | ||
} | ||
} | ||
return Proxy{clientCfg: proxy.ClientCfg{}} | ||
} |
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
Oops, something went wrong.