@@ -3,6 +3,7 @@ package gapi
3
3
import (
4
4
"encoding/json"
5
5
"fmt"
6
+ "strings"
6
7
)
7
8
8
9
// DataSource represents a Grafana data source.
@@ -19,16 +20,12 @@ type DataSource struct {
19
20
20
21
Database string `json:"database,omitempty"`
21
22
User string `json:"user,omitempty"`
22
- // Deprecated: Use secureJsonData.password instead.
23
- Password string `json:"password,omitempty"`
24
23
25
24
OrgID int64 `json:"orgId,omitempty"`
26
25
IsDefault bool `json:"isDefault"`
27
26
28
27
BasicAuth bool `json:"basicAuth"`
29
28
BasicAuthUser string `json:"basicAuthUser,omitempty"`
30
- // Deprecated: Use secureJsonData.basicAuthPassword instead.
31
- BasicAuthPassword string `json:"basicAuthPassword,omitempty"`
32
29
33
30
JSONData map [string ]interface {} `json:"jsonData,omitempty"`
34
31
SecureJSONData map [string ]interface {} `json:"secureJsonData,omitempty"`
@@ -138,3 +135,49 @@ func (c *Client) DeleteDataSourceByName(name string) error {
138
135
139
136
return c .request ("DELETE" , path , nil , nil , nil )
140
137
}
138
+
139
+ func cloneMap (m map [string ]interface {}) map [string ]interface {} {
140
+ clone := make (map [string ]interface {})
141
+ for k , v := range m {
142
+ clone [k ] = v
143
+ }
144
+ return clone
145
+ }
146
+
147
+ func JSONDataWithHeaders (jsonData , secureJSONData map [string ]interface {}, headers map [string ]string ) (map [string ]interface {}, map [string ]interface {}) {
148
+ // Clone the maps so we don't modify the original
149
+ jsonData = cloneMap (jsonData )
150
+ secureJSONData = cloneMap (secureJSONData )
151
+
152
+ idx := 1
153
+ for name , value := range headers {
154
+ jsonData [fmt .Sprintf ("httpHeaderName%d" , idx )] = name
155
+ secureJSONData [fmt .Sprintf ("httpHeaderValue%d" , idx )] = value
156
+ idx += 1
157
+ }
158
+
159
+ return jsonData , secureJSONData
160
+ }
161
+
162
+ func ExtractHeadersFromJSONData (jsonData , secureJSONData map [string ]interface {}) (map [string ]interface {}, map [string ]interface {}, map [string ]string ) {
163
+ // Clone the maps so we don't modify the original
164
+ jsonData = cloneMap (jsonData )
165
+ secureJSONData = cloneMap (secureJSONData )
166
+ headers := make (map [string ]string )
167
+
168
+ for dataName , dataValue := range jsonData {
169
+ if strings .HasPrefix (dataName , "httpHeaderName" ) {
170
+ // Remove the header name from JSON data
171
+ delete (jsonData , dataName )
172
+
173
+ // Remove the header value from secure JSON data
174
+ secureDataName := strings .Replace (dataName , "httpHeaderName" , "httpHeaderValue" , 1 )
175
+ delete (secureJSONData , secureDataName )
176
+
177
+ headerName := dataValue .(string )
178
+ headers [headerName ] = "true" // We can't retrieve the headers, so we just set a dummy value
179
+ }
180
+ }
181
+
182
+ return jsonData , secureJSONData , headers
183
+ }
0 commit comments