@@ -3,7 +3,6 @@ package plugin
3
3
import (
4
4
"context"
5
5
"encoding/json"
6
- "errors"
7
6
"fmt"
8
7
"strings"
9
8
"time"
@@ -30,27 +29,15 @@ var (
30
29
// NewDatasource creates a new MongoDB datasource instance.
31
30
func NewDatasource (ctx context.Context , source backend.DataSourceInstanceSettings ) (instancemgmt.Instance , error ) {
32
31
33
- var uri string
34
-
35
32
config , err := models .LoadPluginSettings (source )
36
33
if err != nil {
37
34
backend .Logger .Error (fmt .Sprintf ("Failed to load plugin settings: %s" , err .Error ()))
38
35
return nil , err
39
36
}
40
37
41
- if config .Database == "" {
42
- return nil , errors .New ("missing MongoDB database" )
43
- }
44
-
45
- if config .AuthMethod == "auth-none" {
46
- uri = fmt .Sprintf ("mongodb://%s:%d" , config .Host , config .Port )
47
- } else if config .AuthMethod == "auth-username-password" {
48
- if config .Username == "" || config .Secrets .Password == "" {
49
- return nil , errors .New ("missing MongoDB username or password" )
50
- }
51
- uri = fmt .Sprintf ("mongodb://%s:%s@%s:%d" , config .Username , config .Secrets .Password , config .Host , config .Port )
52
- } else {
53
- return nil , errors .New ("authentication method not supported" )
38
+ uri , err := MongoUri (config )
39
+ if err != nil {
40
+ return nil , err
54
41
}
55
42
56
43
opts := options .Client ().ApplyURI (uri )
@@ -64,8 +51,7 @@ func NewDatasource(ctx context.Context, source backend.DataSourceInstanceSetting
64
51
return & Datasource {
65
52
client : client ,
66
53
database : config .Database ,
67
- host : config .Host ,
68
- port : config .Port }, nil
54
+ }, nil
69
55
}
70
56
71
57
// Dispose here tells plugin SDK that plugin wants to clean up resources when a new instance
@@ -83,7 +69,7 @@ func (d *Datasource) QueryData(ctx context.Context, req *backend.QueryDataReques
83
69
// create response struct
84
70
response := backend .NewQueryDataResponse ()
85
71
// loop over queries and execute them individually.
86
- backend . Logger . Debug ( "New queries" , d . host , d . port , d . database )
72
+
87
73
for _ , q := range req .Queries {
88
74
89
75
res := d .query (ctx , req .PluginContext , q )
@@ -157,49 +143,18 @@ func (d *Datasource) query(ctx context.Context, _ backend.PluginContext, query b
157
143
func (d * Datasource ) CheckHealth (ctx context.Context , req * backend.CheckHealthRequest ) (* backend.CheckHealthResult , error ) {
158
144
res := & backend.CheckHealthResult {}
159
145
backend .Logger .Debug ("Checking health" )
160
- config , err := models .LoadPluginSettings (* req .PluginContext .DataSourceInstanceSettings )
161
146
147
+ config , err := models .LoadPluginSettings (* req .PluginContext .DataSourceInstanceSettings )
162
148
if err != nil {
163
149
res .Status = backend .HealthStatusError
164
150
res .Message = "Unable to load settings"
165
151
return res , nil
166
152
}
167
153
168
- backend .Logger .Debug (fmt .Sprintf ("Config: %v" , config ))
169
-
170
- if config .AuthMethod == "" {
171
- res .Status = backend .HealthStatusError
172
- res .Message = "Please specify the authentication type"
173
- return res , nil
174
- }
175
-
176
- if config .Host == "" {
177
- res .Status = backend .HealthStatusError
178
- res .Message = "Please specify the host address"
179
- return res , nil
180
- }
181
-
182
- if config .Database == "" {
183
- res .Status = backend .HealthStatusError
184
- res .Message = "Please specify the database"
185
- return res , nil
186
- }
187
-
188
- var uri string
189
-
190
- if config .AuthMethod == "auth-none" {
191
- uri = fmt .Sprintf ("mongodb://%s:%d" , config .Host , config .Port )
192
- } else if config .AuthMethod == "auth-username-password" {
193
- if config .Username == "" || config .Secrets .Password == "" {
194
- res .Status = backend .HealthStatusError
195
- res .Message = "Please specify the username and password"
196
- return res , nil
197
- }
198
- uri = fmt .Sprintf ("mongodb://%s:%s@%s:%d" , config .Username , config .Secrets .Password , config .Host , config .Port )
199
- } else {
154
+ uri , err := MongoUri (config )
155
+ if err != nil {
200
156
res .Status = backend .HealthStatusError
201
- res .Message = "Please specify the authentication type"
202
- return res , nil
157
+ res .Message = err .Error ()
203
158
}
204
159
205
160
opts := options .Client ().ApplyURI (uri ).SetTimeout (5 * time .Second )
0 commit comments