Skip to content

Commit 5745cb7

Browse files
committed
Add support for nested k/v pairs to datasource search_key
1 parent 9cd1f44 commit 5745cb7

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

restapi/datasource_api_object.go

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"errors"
77
"log"
88
"encoding/json"
9+
"reflect"
910
)
1011

1112
func dataSourceRestApi() *schema.Resource {
@@ -25,7 +26,7 @@ func dataSourceRestApi() *schema.Resource {
2526
},
2627
"search_key": &schema.Schema{
2728
Type: schema.TypeString,
28-
Description: "When reading search results from the API, this key is used to identify the specific record to read. This should be a unique record such as 'name'.",
29+
Description: "When reading search results from the API, this key is used to identify the specific record to read. This should be a unique record such as 'name'. Similar to results_key, the value may be in the format of 'field/field/field' to search for data deeper in the returned object.",
2930
Required: true,
3031
},
3132
"search_value": &schema.Schema{
@@ -107,6 +108,7 @@ func dataSourceRestApiRead(d *schema.ResourceData, meta interface{}) error {
107108
var tmp interface{}
108109

109110
if debug { log.Printf("datasource_api_object.go: Locating '%s' in the results", results_key) }
111+
110112
/* First verify the data we got back is a hash */
111113
if _, ok = result.(map[string]interface{}); !ok {
112114
return fmt.Errorf("datasource_api_object.go: The results of a GET to '%s' did not return a hash. Cannot search within for results_key '%s'", search_path, results_key)
@@ -117,25 +119,35 @@ func dataSourceRestApiRead(d *schema.ResourceData, meta interface{}) error {
117119
return fmt.Errorf("datasource_api_object.go: Error finding results_key: %s", err)
118120
}
119121
if data_array, ok = tmp.([]interface{}); !ok {
120-
return fmt.Errorf("datasource_api_object.go: The data at results_key location '%s' is not an array.", results_key)
122+
return fmt.Errorf("datasource_api_object.go: The data at results_key location '%s' is not an array. It is a '%s'", results_key, reflect.TypeOf(tmp))
121123
}
122124
} else {
123125
if debug { log.Printf("datasource_api_object.go: results_key is not set - coaxing data to array of interfaces") }
124126
if data_array, ok = result.([]interface{}); !ok {
125-
return fmt.Errorf("datasource_api_object.go: The results of a GET to '%s' did not return an array. Perhaps you meant to add a results_key?", search_path)
127+
return fmt.Errorf("datasource_api_object.go: The results of a GET to '%s' did not return an array. It is a '%s'. Perhaps you meant to add a results_key?", search_path, reflect.TypeOf(result))
126128
}
127129
}
128130

129131
/* Loop through all of the results seeking the specific record */
130132
for _, item := range data_array {
131-
hash := item.(map[string]interface{})
133+
var hash map[string]interface{}
134+
135+
if hash, ok = item.(map[string]interface{}); !ok {
136+
return fmt.Errorf("datasource_api_object.go: The elements being searched for data are not a map of key value pairs.")
137+
}
138+
132139
if debug {
133140
log.Printf("datasource_api_object.go: Examining %v", hash)
134-
log.Printf("datasource_api_object.go: Comparing '%s' to '%s'", search_value, hash[search_key])
141+
log.Printf("datasource_api_object.go: Comparing '%s' to the value in '%s'", search_value, search_key)
142+
}
143+
144+
tmp, err := GetStringAtKey(hash, search_key, debug)
145+
if err != nil {
146+
return(fmt.Errorf("Failed to get the value of '%s' in the results array at '%s': %s", search_key, results_key, err))
135147
}
136148

137149
/* We found our record */
138-
if hash[search_key] == search_value {
150+
if tmp == search_value {
139151
id = fmt.Sprintf("%v", hash[id_attribute])
140152
if debug { log.Printf("datasource_api_object.go: Found ID %s", id) }
141153

0 commit comments

Comments
 (0)