@@ -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.
2523type AWSDatasource struct {
2624 sessionCache * awsds.SessionCache
2725 config sync.Map
2826 api sync.Map
29- driver sync.Map
3027}
3128
3229func 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}
0 commit comments