Skip to content

Commit b291103

Browse files
DS: Remove JSONData and SecureJSONData (grafana#156)
Following grafana/terraform-provider-grafana#963 We don't need those fields anymore. We pass arbitrary maps to the API
1 parent 023cfdf commit b291103

File tree

3 files changed

+95
-308
lines changed

3 files changed

+95
-308
lines changed

datasource.go

+47-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package gapi
33
import (
44
"encoding/json"
55
"fmt"
6+
"strings"
67
)
78

89
// DataSource represents a Grafana data source.
@@ -19,16 +20,12 @@ type DataSource struct {
1920

2021
Database string `json:"database,omitempty"`
2122
User string `json:"user,omitempty"`
22-
// Deprecated: Use secureJsonData.password instead.
23-
Password string `json:"password,omitempty"`
2423

2524
OrgID int64 `json:"orgId,omitempty"`
2625
IsDefault bool `json:"isDefault"`
2726

2827
BasicAuth bool `json:"basicAuth"`
2928
BasicAuthUser string `json:"basicAuthUser,omitempty"`
30-
// Deprecated: Use secureJsonData.basicAuthPassword instead.
31-
BasicAuthPassword string `json:"basicAuthPassword,omitempty"`
3229

3330
JSONData map[string]interface{} `json:"jsonData,omitempty"`
3431
SecureJSONData map[string]interface{} `json:"secureJsonData,omitempty"`
@@ -138,3 +135,49 @@ func (c *Client) DeleteDataSourceByName(name string) error {
138135

139136
return c.request("DELETE", path, nil, nil, nil)
140137
}
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+
}

datasource_json_data.go

-224
This file was deleted.

0 commit comments

Comments
 (0)