-
Notifications
You must be signed in to change notification settings - Fork 17
/
datasource.go
205 lines (174 loc) · 5.96 KB
/
datasource.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package datasource
import (
"context"
"database/sql"
"fmt"
"sync"
"github.com/grafana/grafana-aws-sdk/pkg/awsds"
"github.com/grafana/grafana-aws-sdk/pkg/sql/api"
"github.com/grafana/grafana-aws-sdk/pkg/sql/driver"
asyncDriver "github.com/grafana/grafana-aws-sdk/pkg/sql/driver/async"
"github.com/grafana/grafana-aws-sdk/pkg/sql/models"
"github.com/grafana/grafana-plugin-sdk-go/backend"
"github.com/grafana/sqlds/v4"
)
// AWSClient provides creation and caching of sessions, database connections, and API clients
type AWSClient interface {
Init(config backend.DataSourceInstanceSettings)
GetDB(ctx context.Context, id int64, options sqlds.Options) (*sql.DB, error)
GetAsyncDB(ctx context.Context, id int64, options sqlds.Options) (awsds.AsyncDB, error)
GetAPI(ctx context.Context, id int64, options sqlds.Options) (api.AWSAPI, error)
}
type Loader interface {
LoadSettings(context.Context) models.Settings
LoadAPI(context.Context, *awsds.SessionCache, models.Settings) (api.AWSAPI, error)
LoadDriver(context.Context, api.AWSAPI) (driver.Driver, error)
LoadAsyncDriver(context.Context, api.AWSAPI) (asyncDriver.Driver, error)
}
// awsClient provides creation and caching of several types of instances.
// Each Map will depend on the datasource ID (and connection options):
// - sessionCache: AWS cache. This is not a Map since it does not depend on the datasource.
// - config: Base configuration. It will be used as base to populate datasource settings.
// It does not depend on connection options (only one per datasource)
// - api: API instance with the common methods to contact the data source API.
type awsClient struct {
sessionCache *awsds.SessionCache
config sync.Map
api sync.Map
loader Loader
}
func New(loader Loader) AWSClient {
ds := &awsClient{sessionCache: awsds.NewSessionCache(), loader: loader}
return ds
}
func (ds *awsClient) storeConfig(config backend.DataSourceInstanceSettings) {
ds.config.Store(config.ID, config)
}
func (ds *awsClient) createDB(dr driver.Driver) (*sql.DB, error) {
db, err := dr.OpenDB()
if err != nil {
return nil, fmt.Errorf("%w: failed to connect to database (check hostname and port?)", err)
}
return db, nil
}
func (ds *awsClient) createAsyncDB(dr asyncDriver.Driver) (awsds.AsyncDB, error) {
db, err := dr.GetAsyncDB()
if err != nil {
return nil, fmt.Errorf("%w: failed to connect to database (check hostname and port)", err)
}
return db, nil
}
func (ds *awsClient) storeAPI(id int64, args sqlds.Options, dsAPI api.AWSAPI) {
key := connectionKey(id, args)
ds.api.Store(key, dsAPI)
}
func (ds *awsClient) loadAPI(id int64, args sqlds.Options) (api.AWSAPI, bool) {
key := connectionKey(id, args)
dsAPI, exists := ds.api.Load(key)
if exists {
return dsAPI.(api.AWSAPI), true
}
return nil, false
}
func (ds *awsClient) createAPI(ctx context.Context, id int64, args sqlds.Options, settings models.Settings) (api.AWSAPI, error) {
dsAPI, err := ds.loader.LoadAPI(ctx, ds.sessionCache, settings)
if err != nil {
return nil, fmt.Errorf("%w: Failed to create client", err)
}
ds.storeAPI(id, args, dsAPI)
return dsAPI, err
}
func (ds *awsClient) createDriver(ctx context.Context, dsAPI api.AWSAPI) (driver.Driver, error) {
dr, err := ds.loader.LoadDriver(ctx, dsAPI)
if err != nil {
return nil, fmt.Errorf("%w: Failed to create client", err)
}
return dr, nil
}
func (ds *awsClient) createAsyncDriver(ctx context.Context, dsAPI api.AWSAPI) (asyncDriver.Driver, error) {
dr, err := ds.loader.LoadAsyncDriver(ctx, dsAPI)
if err != nil {
return nil, fmt.Errorf("%w: Failed to create client", err)
}
return dr, nil
}
func (ds *awsClient) parseSettings(id int64, args sqlds.Options, settings models.Settings) error {
config, ok := ds.config.Load(id)
if !ok {
return fmt.Errorf("unable to find stored configuration for datasource %d. Initialize it first", id)
}
err := settings.Load(config.(backend.DataSourceInstanceSettings))
if err != nil {
return fmt.Errorf("error reading settings: %s", err.Error())
}
settings.Apply(args)
return nil
}
// Init stores the data source configuration. It's needed for the GetDB and GetAPI functions
func (ds *awsClient) Init(config backend.DataSourceInstanceSettings) {
ds.storeConfig(config)
}
// GetDB returns a *sql.DB. It will use the loader functions to initialize the required
// settings, API and driver and finally create a DB.
func (ds *awsClient) GetDB(
ctx context.Context,
id int64,
options sqlds.Options,
) (*sql.DB, error) {
settings := ds.loader.LoadSettings(ctx)
err := ds.parseSettings(id, options, settings)
if err != nil {
return nil, err
}
dsAPI, err := ds.createAPI(ctx, id, options, settings)
if err != nil {
return nil, err
}
dr, err := ds.createDriver(ctx, dsAPI)
if err != nil {
return nil, err
}
return ds.createDB(dr)
}
// GetAsyncDB returns a sqlds.AsyncDB. It will use the loader functions to initialize the required
// settings, API and driver and finally create a DB.
func (ds *awsClient) GetAsyncDB(
ctx context.Context,
id int64,
options sqlds.Options,
) (awsds.AsyncDB, error) {
settings := ds.loader.LoadSettings(ctx)
err := ds.parseSettings(id, options, settings)
if err != nil {
return nil, err
}
dsAPI, err := ds.createAPI(ctx, id, options, settings)
if err != nil {
return nil, err
}
dr, err := ds.createAsyncDriver(ctx, dsAPI)
if err != nil {
return nil, err
}
return ds.createAsyncDB(dr)
}
// GetAPI returns an API interface. When called multiple times with the same id and options, it
// will return a cached version of the API. The first time, it will use the loader
// functions to initialize the required settings and API.
func (ds *awsClient) GetAPI(
ctx context.Context,
id int64,
options sqlds.Options,
) (api.AWSAPI, error) {
cachedAPI, exists := ds.loadAPI(id, options)
if exists {
return cachedAPI, nil
}
// create new api
settings := ds.loader.LoadSettings(ctx)
err := ds.parseSettings(id, options, settings)
if err != nil {
return nil, err
}
return ds.createAPI(ctx, id, options, settings)
}