Skip to content
This repository was archived by the owner on Jan 15, 2024. It is now read-only.

Commit 3881f5c

Browse files
Support extracting headers from JSON Data (#108)
* Support extracting headers from JSON Data This will allow us to read back the JSON data from the API back into terraform attributes * lint
1 parent 1a99912 commit 3881f5c

File tree

1 file changed

+37
-11
lines changed

1 file changed

+37
-11
lines changed

datasource_json_data.go

Lines changed: 37 additions & 11 deletions
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
type LokiDerivedField struct {
@@ -176,23 +177,48 @@ func (d SecureJSONData) Map() (map[string]interface{}, error) {
176177
return fields, nil
177178
}
178179

180+
func cloneMap(m map[string]interface{}) map[string]interface{} {
181+
clone := make(map[string]interface{})
182+
for k, v := range m {
183+
clone[k] = v
184+
}
185+
return clone
186+
}
187+
179188
func JSONDataWithHeaders(jsonData, secureJSONData map[string]interface{}, headers map[string]string) (map[string]interface{}, map[string]interface{}) {
180189
// Clone the maps so we don't modify the original
181-
clonedJSONData := make(map[string]interface{}, len(jsonData))
182-
for k, v := range jsonData {
183-
clonedJSONData[k] = v
184-
}
185-
clonedSecureJSONData := make(map[string]interface{}, len(secureJSONData))
186-
for k, v := range secureJSONData {
187-
clonedSecureJSONData[k] = v
188-
}
190+
jsonData = cloneMap(jsonData)
191+
secureJSONData = cloneMap(secureJSONData)
189192

190193
idx := 1
191194
for name, value := range headers {
192-
clonedJSONData[fmt.Sprintf("httpHeaderName%d", idx)] = name
193-
clonedSecureJSONData[fmt.Sprintf("httpHeaderValue%d", idx)] = value
195+
jsonData[fmt.Sprintf("httpHeaderName%d", idx)] = name
196+
secureJSONData[fmt.Sprintf("httpHeaderValue%d", idx)] = value
194197
idx += 1
195198
}
196199

197-
return clonedJSONData, clonedSecureJSONData
200+
return jsonData, secureJSONData
201+
}
202+
203+
func ExtractHeadersFromJSONData(jsonData, secureJSONData map[string]interface{}) (map[string]interface{}, map[string]interface{}, map[string]string) {
204+
// Clone the maps so we don't modify the original
205+
jsonData = cloneMap(jsonData)
206+
secureJSONData = cloneMap(secureJSONData)
207+
headers := make(map[string]string)
208+
209+
for dataName, dataValue := range jsonData {
210+
if strings.HasPrefix(dataName, "httpHeaderName") {
211+
// Remove the header name from JSON data
212+
delete(jsonData, dataName)
213+
214+
// Remove the header value from secure JSON data
215+
secureDataName := strings.Replace(dataName, "httpHeaderName", "httpHeaderValue", 1)
216+
delete(secureJSONData, secureDataName)
217+
218+
headerName := dataValue.(string)
219+
headers[headerName] = "true" // We can't retrieve the headers, so we just set a dummy value
220+
}
221+
}
222+
223+
return jsonData, secureJSONData, headers
198224
}

0 commit comments

Comments
 (0)