2017-11-30 10:43:28 -06:00
|
|
|
package dashboards
|
2017-11-23 04:29:06 -06:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
type DashboardProvisioner struct {
|
|
|
|
cfgReader *configReader
|
|
|
|
log log.Logger
|
|
|
|
ctx context.Context
|
|
|
|
}
|
|
|
|
|
|
|
|
func Provision(ctx context.Context, configDirectory string) (*DashboardProvisioner, error) {
|
2018-01-24 07:20:16 -06:00
|
|
|
log := log.New("provisioning.dashboard")
|
2017-11-23 04:29:06 -06:00
|
|
|
d := &DashboardProvisioner{
|
2018-01-24 07:20:16 -06:00
|
|
|
cfgReader: &configReader{path: configDirectory, log: log},
|
|
|
|
log: log,
|
2017-11-23 04:29:06 -06:00
|
|
|
ctx: ctx,
|
|
|
|
}
|
|
|
|
|
2017-11-30 10:17:49 -06:00
|
|
|
err := d.Provision(ctx)
|
|
|
|
return d, err
|
2017-11-23 04:29:06 -06:00
|
|
|
}
|
|
|
|
|
2017-11-30 10:17:49 -06:00
|
|
|
func (provider *DashboardProvisioner) Provision(ctx context.Context) error {
|
2017-11-23 04:29:06 -06:00
|
|
|
cfgs, err := provider.cfgReader.readConfig()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, cfg := range cfgs {
|
2017-11-28 07:01:10 -06:00
|
|
|
switch cfg.Type {
|
|
|
|
case "file":
|
2017-12-05 06:57:39 -06:00
|
|
|
fileReader, err := NewDashboardFileReader(cfg, provider.log.New("type", cfg.Type, "name", cfg.Name))
|
2017-11-23 04:29:06 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-11-28 07:01:10 -06:00
|
|
|
go fileReader.ReadAndListen(ctx)
|
|
|
|
default:
|
2017-11-23 04:29:06 -06:00
|
|
|
return fmt.Errorf("type %s is not supported", cfg.Type)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|