/
gdg_api.go
77 lines (65 loc) · 1.54 KB
/
gdg_api.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package service
import (
"context"
"github.com/esnet/gdg/internal/api"
"github.com/esnet/gdg/internal/config"
"github.com/spf13/viper"
"log/slog"
"sync"
)
var (
instance *DashNGoImpl
initServiceOnce sync.Once
)
type DashNGoImpl struct {
extended *api.ExtendedApi
grafanaConf *config.GrafanaConfig
configRef *viper.Viper
debug bool
storage Storage
}
func NewDashNGoImpl() *DashNGoImpl {
initServiceOnce.Do(func() {
instance = newInstance()
})
return instance
}
func newInstance() *DashNGoImpl {
obj := &DashNGoImpl{}
obj.grafanaConf = config.Config().GetDefaultGrafanaConfig()
obj.configRef = config.Config().GetViperConfig(config.ViperGdgConfig)
obj.Login()
obj.debug = config.Config().IsDebug()
configureStorage(obj)
return obj
}
// Testing Only
func (s *DashNGoImpl) SetStorage(v Storage) {
s.storage = v
}
func configureStorage(obj *DashNGoImpl) {
//config
storageType, appData := config.Config().GetCloudConfiguration(config.Config().GetDefaultGrafanaConfig().Storage)
var err error
ctx := context.Background()
ctx = context.WithValue(ctx, StorageContext, appData)
switch storageType {
case "cloud":
{
obj.storage, err = NewCloudStorage(ctx)
if err != nil {
slog.Warn("falling back on Local Storage, Cloud storage configuration error")
obj.storage = NewLocalStorage(ctx)
}
}
default:
obj.storage = NewLocalStorage(ctx)
}
}
func NewApiService(override ...string) GrafanaService {
//Used for Testing purposes
if len(override) > 0 {
return newInstance()
}
return NewDashNGoImpl()
}