Skip to content

Commit de7b382

Browse files
authored
cleanup: typos, unused methods & parameters, docstrings, etc. (#138)
1 parent 1795df6 commit de7b382

File tree

3 files changed

+45
-66
lines changed

3 files changed

+45
-66
lines changed

pkg/awsds/sessions.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ func NewSessionCache() *SessionCache {
3939
}
4040

4141
const (
42-
// path to the shared credentials file in the instance for the aws/aws-sdk
42+
// CredentialsPath is the path to the shared credentials file in the instance for the aws/aws-sdk
4343
// if empty string, the path is ~/.aws/credentials
4444
CredentialsPath = ""
4545

46-
// the profile containing credentials for GrafanaAssueRole auth type in the shared credentials file
46+
// ProfileName is the profile containing credentials for GrafanaAssumeRole auth type in the shared credentials file
4747
ProfileName = "assume_role_credentials"
4848
)
4949

@@ -130,17 +130,17 @@ func (sc *SessionCache) GetSession(c SessionConfig) (*session.Session, error) {
130130
}
131131

132132
// Hash the settings to use as a cache key
133-
bldr := strings.Builder{}
133+
b := strings.Builder{}
134134
for i, s := range []string{
135135
c.Settings.AuthType.String(), c.Settings.AccessKey, c.Settings.SecretKey, c.Settings.Profile, c.Settings.AssumeRoleARN, c.Settings.Region, c.Settings.Endpoint,
136136
} {
137137
if i != 0 {
138-
bldr.WriteString(":")
138+
b.WriteString(":")
139139
}
140-
bldr.WriteString(strings.ReplaceAll(s, ":", `\:`))
140+
b.WriteString(strings.ReplaceAll(s, ":", `\:`))
141141
}
142142

143-
hashedSettings := sha256.Sum256([]byte(bldr.String()))
143+
hashedSettings := sha256.Sum256([]byte(b.String()))
144144
cacheKey := fmt.Sprintf("%v", hashedSettings)
145145

146146
// Check if we have a valid session in the cache, if so return it

pkg/sql/datasource/datasource.go

Lines changed: 36 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -16,96 +16,86 @@ import (
1616

1717
// AWSDatasource stores a cache of several instances.
1818
// Each Map will depend on the datasource ID (and connection options):
19+
// - sessionCache: AWS cache. This is not a Map since it does not depend on the datasource.
1920
// - config: Base configuration. It will be used as base to populate datasource settings.
2021
// It does not depend on connection options (only one per datasource)
21-
// - api: API instace with the common methods to contact the data source API.
22-
// - driver: Abstraction on top the upstream sql.Driver. Defined to handle multiple connections.
23-
// - db: Upstream database (sql.DB).
24-
// - sessionCache: AWS cache. This is not a Map since it does not depend on the datasource.
22+
// - api: API instance with the common methods to contact the data source API.
2523
type AWSDatasource struct {
2624
sessionCache *awsds.SessionCache
2725
config sync.Map
2826
api sync.Map
29-
driver sync.Map
3027
}
3128

3229
func New() *AWSDatasource {
3330
ds := &AWSDatasource{sessionCache: awsds.NewSessionCache()}
3431
return ds
3532
}
3633

37-
func (d *AWSDatasource) storeConfig(config backend.DataSourceInstanceSettings) {
38-
d.config.Store(config.ID, config)
34+
func (ds *AWSDatasource) storeConfig(config backend.DataSourceInstanceSettings) {
35+
ds.config.Store(config.ID, config)
3936
}
4037

41-
func (s *AWSDatasource) createDB(id int64, args sqlds.Options, settings models.Settings, dr driver.Driver) (*sql.DB, error) {
38+
func (ds *AWSDatasource) createDB(dr driver.Driver) (*sql.DB, error) {
4239
db, err := dr.OpenDB()
4340
if err != nil {
44-
return nil, fmt.Errorf("%w: Failed to connect to database. Is the hostname and port correct?", err)
41+
return nil, fmt.Errorf("%w: failed to connect to database (check hostname and port?)", err)
4542
}
4643

4744
return db, nil
4845
}
4946

50-
func (s *AWSDatasource) createAsyncDB(id int64, args sqlds.Options, settings models.Settings, dr asyncDriver.Driver) (awsds.AsyncDB, error) {
47+
func (ds *AWSDatasource) createAsyncDB(dr asyncDriver.Driver) (awsds.AsyncDB, error) {
5148
db, err := dr.GetAsyncDB()
5249
if err != nil {
53-
return nil, fmt.Errorf("%w: Failed to connect to database. Is the hostname and port correct?", err)
50+
return nil, fmt.Errorf("%w: failed to connect to database (check hostname and port)", err)
5451
}
5552

5653
return db, nil
5754
}
5855

59-
func (d *AWSDatasource) storeAPI(id int64, args sqlds.Options, dsAPI api.AWSAPI) {
56+
func (ds *AWSDatasource) storeAPI(id int64, args sqlds.Options, dsAPI api.AWSAPI) {
6057
key := connectionKey(id, args)
61-
d.api.Store(key, dsAPI)
58+
ds.api.Store(key, dsAPI)
6259
}
6360

64-
func (d *AWSDatasource) loadAPI(id int64, args sqlds.Options) (api.AWSAPI, bool) {
61+
func (ds *AWSDatasource) loadAPI(id int64, args sqlds.Options) (api.AWSAPI, bool) {
6562
key := connectionKey(id, args)
66-
dsAPI, exists := d.api.Load(key)
63+
dsAPI, exists := ds.api.Load(key)
6764
if exists {
6865
return dsAPI.(api.AWSAPI), true
6966
}
7067
return nil, false
7168
}
7269

73-
func (s *AWSDatasource) createAPI(id int64, args sqlds.Options, settings models.Settings, loader api.Loader) (api.AWSAPI, error) {
74-
api, err := loader(s.sessionCache, settings)
70+
func (ds *AWSDatasource) createAPI(id int64, args sqlds.Options, settings models.Settings, loader api.Loader) (api.AWSAPI, error) {
71+
dsAPI, err := loader(ds.sessionCache, settings)
7572
if err != nil {
7673
return nil, fmt.Errorf("%w: Failed to create client", err)
7774
}
78-
s.storeAPI(id, args, api)
79-
return api, err
80-
}
81-
82-
func (d *AWSDatasource) storeDriver(id int64, args sqlds.Options, dr interface{}) {
83-
key := connectionKey(id, args)
84-
d.driver.Store(key, dr)
75+
ds.storeAPI(id, args, dsAPI)
76+
return dsAPI, err
8577
}
8678

87-
func (s *AWSDatasource) createDriver(id int64, args sqlds.Options, settings models.Settings, dsAPI api.AWSAPI, loader driver.Loader) (driver.Driver, error) {
79+
func (ds *AWSDatasource) createDriver(dsAPI api.AWSAPI, loader driver.Loader) (driver.Driver, error) {
8880
dr, err := loader(dsAPI)
8981
if err != nil {
9082
return nil, fmt.Errorf("%w: Failed to create client", err)
9183
}
92-
s.storeDriver(id, args, dr)
9384

9485
return dr, nil
9586
}
9687

97-
func (s *AWSDatasource) createAsyncDriver(id int64, args sqlds.Options, settings models.Settings, dsAPI api.AWSAPI, loader asyncDriver.Loader) (asyncDriver.Driver, error) {
88+
func (ds *AWSDatasource) createAsyncDriver(dsAPI api.AWSAPI, loader asyncDriver.Loader) (asyncDriver.Driver, error) {
9889
dr, err := loader(dsAPI)
9990
if err != nil {
10091
return nil, fmt.Errorf("%w: Failed to create client", err)
10192
}
102-
s.storeDriver(id, args, dr)
10393

10494
return dr, nil
10595
}
10696

107-
func (d *AWSDatasource) parseSettings(id int64, args sqlds.Options, settings models.Settings) error {
108-
config, ok := d.config.Load(id)
97+
func (ds *AWSDatasource) parseSettings(id int64, args sqlds.Options, settings models.Settings) error {
98+
config, ok := ds.config.Load(id)
10999
if !ok {
110100
return fmt.Errorf("unable to find stored configuration for datasource %d. Initialize it first", id)
111101
}
@@ -118,85 +108,85 @@ func (d *AWSDatasource) parseSettings(id int64, args sqlds.Options, settings mod
118108
}
119109

120110
// Init stores the data source configuration. It's needed for the GetDB and GetAPI functions
121-
func (s *AWSDatasource) Init(config backend.DataSourceInstanceSettings) {
122-
s.storeConfig(config)
111+
func (ds *AWSDatasource) Init(config backend.DataSourceInstanceSettings) {
112+
ds.storeConfig(config)
123113
}
124114

125115
// GetDB returns a *sql.DB. It will use the loader functions to initialize the required
126116
// settings, API and driver and finally create a DB.
127-
func (s *AWSDatasource) GetDB(
117+
func (ds *AWSDatasource) GetDB(
128118
id int64,
129119
options sqlds.Options,
130120
settingsLoader models.Loader,
131121
apiLoader api.Loader,
132122
driverLoader driver.Loader,
133123
) (*sql.DB, error) {
134124
settings := settingsLoader()
135-
err := s.parseSettings(id, options, settings)
125+
err := ds.parseSettings(id, options, settings)
136126
if err != nil {
137127
return nil, err
138128
}
139129

140-
dsAPI, err := s.createAPI(id, options, settings, apiLoader)
130+
dsAPI, err := ds.createAPI(id, options, settings, apiLoader)
141131
if err != nil {
142132
return nil, err
143133
}
144134

145-
dr, err := s.createDriver(id, options, settings, dsAPI, driverLoader)
135+
dr, err := ds.createDriver(dsAPI, driverLoader)
146136
if err != nil {
147137
return nil, err
148138
}
149139

150-
return s.createDB(id, options, settings, dr)
140+
return ds.createDB(dr)
151141
}
152142

153143
// GetAsyncDB returns a sqlds.AsyncDB. It will use the loader functions to initialize the required
154144
// settings, API and driver and finally create a DB.
155-
func (s *AWSDatasource) GetAsyncDB(
145+
func (ds *AWSDatasource) GetAsyncDB(
156146
id int64,
157147
options sqlds.Options,
158148
settingsLoader models.Loader,
159149
apiLoader api.Loader,
160150
driverLoader asyncDriver.Loader,
161151
) (awsds.AsyncDB, error) {
162152
settings := settingsLoader()
163-
err := s.parseSettings(id, options, settings)
153+
err := ds.parseSettings(id, options, settings)
164154
if err != nil {
165155
return nil, err
166156
}
167157

168-
dsAPI, err := s.createAPI(id, options, settings, apiLoader)
158+
dsAPI, err := ds.createAPI(id, options, settings, apiLoader)
169159
if err != nil {
170160
return nil, err
171161
}
172162

173-
dr, err := s.createAsyncDriver(id, options, settings, dsAPI, driverLoader)
163+
dr, err := ds.createAsyncDriver(dsAPI, driverLoader)
174164
if err != nil {
175165
return nil, err
176166
}
177167

178-
return s.createAsyncDB(id, options, settings, dr)
168+
return ds.createAsyncDB(dr)
179169
}
180170

181171
// GetAPI returns an API interface. When called multiple times with the same id and options, it
182172
// will return a cached version of the API. The first time, it will use the loader
183173
// functions to initialize the required settings and API.
184-
func (s *AWSDatasource) GetAPI(
174+
func (ds *AWSDatasource) GetAPI(
185175
id int64,
186176
options sqlds.Options,
187177
settingsLoader models.Loader,
188178
apiLoader api.Loader,
189179
) (api.AWSAPI, error) {
190-
cachedAPI, exists := s.loadAPI(id, options)
180+
cachedAPI, exists := ds.loadAPI(id, options)
191181
if exists {
192182
return cachedAPI, nil
193183
}
194184

195185
// create new api
196186
settings := settingsLoader()
197-
err := s.parseSettings(id, options, settings)
187+
err := ds.parseSettings(id, options, settings)
198188
if err != nil {
199189
return nil, err
200190
}
201-
return s.createAPI(id, options, settings, apiLoader)
191+
return ds.createAPI(id, options, settings, apiLoader)
202192
}

pkg/sql/datasource/datasource_test.go

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ func TestParseSettings(t *testing.T) {
136136
}
137137
}
138138

139-
func fakeAPILoader(cache *awsds.SessionCache, settings models.Settings) (sqlApi.AWSAPI, error) {
139+
func fakeAPILoader(_ *awsds.SessionCache, _ models.Settings) (sqlApi.AWSAPI, error) {
140140
return fakeAPI{}, nil
141141
}
142142

@@ -165,35 +165,24 @@ func fakeDriverLoader(sqlApi.AWSAPI) (sqlDriver.Driver, error) {
165165
}
166166

167167
func TestCreateDriver(t *testing.T) {
168-
id := int64(1)
169-
args := sqlds.Options{"foo": "bar"}
170168
ds := &AWSDatasource{}
171-
key := connectionKey(id, args)
172169
api := fakeAPI{}
173-
settings := &fakeSettings{}
174170

175-
dr, err := ds.createDriver(id, args, settings, api, fakeDriverLoader)
171+
dr, err := ds.createDriver(api, fakeDriverLoader)
176172
if err != nil {
177173
t.Errorf("unexpected error %v", err)
178174
}
179175
if dr == nil {
180176
t.Errorf("unexpected result driver %v", dr)
181177
}
182-
cachedDriver, ok := ds.driver.Load(key)
183-
if !ok || cachedDriver == nil {
184-
t.Errorf("unexpected cached driver %v", cachedDriver)
185-
}
186178
}
187179

188180
func TestCreateDB(t *testing.T) {
189-
id := int64(1)
190-
args := sqlds.Options{"foo": "bar"}
191181
ds := &AWSDatasource{}
192182
db := &sql.DB{}
193183
dr := &fakeDriver{db: db}
194-
settings := &fakeSettings{}
195184

196-
res, err := ds.createDB(id, args, settings, dr)
185+
res, err := ds.createDB(dr)
197186
if err != nil {
198187
t.Errorf("unexpected error %v", err)
199188
}

0 commit comments

Comments
 (0)