/
datasource_pipeline.go
61 lines (51 loc) · 1.39 KB
/
datasource_pipeline.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package grafanadatasource
import (
"github.com/ghodss/yaml"
"github.com/integr8ly/grafana-operator/v3/pkg/apis/integreatly/v1alpha1"
"github.com/pkg/errors"
v1 "k8s.io/api/core/v1"
)
type DatasourcePipeline interface {
ProcessDatasource(known *v1.ConfigMap) error
}
type DatasourcePipelineImpl struct {
datasource *v1alpha1.GrafanaDataSource
contents string
hash string
}
func NewDatasourcePipeline(ds *v1alpha1.GrafanaDataSource) DatasourcePipeline {
return &DatasourcePipelineImpl{
datasource: ds,
}
}
func (i *DatasourcePipelineImpl) ProcessDatasource(known *v1.ConfigMap) error {
// parse the datasource to make sure it is value
err := i.parse()
if err != nil {
return errors.Wrap(err, "error parsing datasource")
}
// append the datasource to the configmap
i.append(known)
return nil
}
func (i *DatasourcePipelineImpl) parse() error {
datasources := struct {
ApiVersion int `json:"apiVersion"`
Datasources []v1alpha1.GrafanaDataSourceFields `json:"datasources"`
}{
ApiVersion: DatasourcesApiVersion,
Datasources: i.datasource.Spec.Datasources,
}
bytes, err := yaml.Marshal(datasources)
if err != nil {
return err
}
i.contents = string(bytes)
return nil
}
func (i *DatasourcePipelineImpl) append(known *v1.ConfigMap) {
if known.Data == nil {
known.Data = map[string]string{}
}
known.Data[i.datasource.Filename()] = i.contents
}