Skip to content

Commit daea743

Browse files
committed
Add query_string parameter for datasource
1 parent 436d730 commit daea743

File tree

3 files changed

+46
-7
lines changed

3 files changed

+46
-7
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ Note that the `*_path` elements are for very specific use cases where one might
5353

5454
## `restapi` datasource configuration
5555
- `path` (string, required): The API path on top of the base URL set in the provider that represents objects of this type on the API server.
56+
- `query_string` (string, optional): An optional query string to send when performing the search.
5657
- `search_key` (string, required): 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'.
5758
- `search_value` (string, required): The value of 'search_key' will be compared to this value to determine if the correct object was found. Example: if 'search_key' is 'name' and 'search_value' is 'foo', the record in the array returned by the API with name=foo will be used.
5859
- `results_key` (string, required): When issuing a GET to the path, this JSON key is used to locate the results array. The format is 'field/field/field'. Example: 'results/values'. If omitted, it is assumed the results coming back are already an array and are to be used exactly as-is

restapi/datasource_api_object.go

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ func dataSourceRestApi() *schema.Resource {
1818
Description: "The API path on top of the base URL set in the provider that represents objects of this type on the API server.",
1919
Required: true,
2020
},
21+
"query_string": &schema.Schema{
22+
Type: schema.TypeString,
23+
Description: "An optional query string to send when performing the search.",
24+
Optional: true,
25+
},
2126
"search_key": &schema.Schema{
2227
Type: schema.TypeString,
2328
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'.",
@@ -57,14 +62,15 @@ func dataSourceRestApi() *schema.Resource {
5762

5863
func dataSourceRestApiRead(d *schema.ResourceData, meta interface{}) error {
5964
path := d.Get("path").(string)
65+
query_string := d.Get("query_string").(string)
6066
debug := d.Get("debug").(bool)
6167
client := meta.(*api_client)
62-
log.Printf("datasource_api_object.go: Data routine called.")
68+
if debug { log.Printf("datasource_api_object.go: Data routine called.") }
6369

6470
search_key := d.Get("search_key").(string)
6571
search_value := d.Get("search_value").(string)
6672
results_key := d.Get("results_key").(string)
67-
if debug { log.Printf("datasource_api_object.go:\npath: %s\nsearch_key: %s\nsearch_value: %s\nresults_key: %s", path, search_key, search_value, results_key) }
73+
if debug { log.Printf("datasource_api_object.go:\npath: %s\nquery_string: %s\nsearch_key: %s\nsearch_value: %s\nresults_key: %s", path, query_string, search_key, search_value, results_key) }
6874

6975
/* Allow user to override provider-level id_attribute */
7076
id_attribute := client.id_attribute
@@ -79,8 +85,14 @@ func dataSourceRestApiRead(d *schema.ResourceData, meta interface{}) error {
7985
/*
8086
Issue a GET to the base path and expect results to come back
8187
*/
82-
if debug { log.Printf("datasource_api_object.go: Calling API on path '%s'", path) }
83-
res_str, err := client.send_request("GET", path, "")
88+
search_path := path
89+
if "" != query_string {
90+
if debug { log.Printf("datasource_api_object.go: Adding query string '%s'", query_string) }
91+
search_path = fmt.Sprintf("%s?%s", search_path, query_string)
92+
}
93+
94+
if debug { log.Printf("datasource_api_object.go: Calling API on path '%s'", search_path) }
95+
res_str, err := client.send_request("GET", search_path, "")
8496
if err != nil { return err }
8597

8698
/*
@@ -97,7 +109,7 @@ func dataSourceRestApiRead(d *schema.ResourceData, meta interface{}) error {
97109
if debug { log.Printf("datasource_api_object.go: Locating '%s' in the results", results_key) }
98110
/* First verify the data we got back is a hash */
99111
if _, ok = result.(map[string]interface{}); !ok {
100-
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'", path, results_key)
112+
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)
101113
}
102114

103115
tmp, err = GetObjectAtKey(result.(map[string]interface{}), results_key, debug)
@@ -110,7 +122,7 @@ func dataSourceRestApiRead(d *schema.ResourceData, meta interface{}) error {
110122
} else {
111123
if debug { log.Printf("datasource_api_object.go: results_key is not set - coaxing data to array of interfaces") }
112124
if data_array, ok = result.([]interface{}); !ok {
113-
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?", path)
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)
114126
}
115127
}
116128

restapi/datasource_api_object_test.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,15 @@ func TestAccRestapiobject_Basic(t *testing.T) {
3333
"last": "Bar"
3434
}
3535
`)
36+
client.send_request("POST", "/api/objects", `
37+
{
38+
"id": "4321",
39+
"first": "Foo",
40+
"last": "Baz"
41+
}
42+
`)
3643

37-
/* Send a complex object that we will pretend is the results of a search */
44+
/* Send a complex object that we will pretend is the results of a search
3845
client.send_request("POST", "/api/objects", `
3946
{
4047
"id": "people",
@@ -47,6 +54,7 @@ func TestAccRestapiobject_Basic(t *testing.T) {
4754
}
4855
}
4956
`)
57+
*/
5058

5159
resource.UnitTest(t, resource.TestCase{
5260
Providers: testAccProviders,
@@ -68,6 +76,24 @@ func TestAccRestapiobject_Basic(t *testing.T) {
6876
resource.TestCheckResourceAttr("data.restapi_object.Foo", "api_data.last", "Bar"),
6977
),
7078
},
79+
{
80+
/* Similar to the first, but also with a query string */
81+
Config: fmt.Sprintf(`
82+
data "restapi_object" "Baz" {
83+
path = "/api/objects"
84+
query_string = "someArg=foo&anotherArg=bar"
85+
search_key = "last"
86+
search_value = "Baz"
87+
debug = %t
88+
}
89+
`, debug),
90+
Check: resource.ComposeTestCheckFunc(
91+
testAccCheckRestapiObjectExists("data.restapi_object.Baz", "4321", client),
92+
resource.TestCheckResourceAttr("data.restapi_object.Baz", "id", "4321"),
93+
resource.TestCheckResourceAttr("data.restapi_object.Baz", "api_data.first", "Foo"),
94+
resource.TestCheckResourceAttr("data.restapi_object.Baz", "api_data.last", "Baz"),
95+
),
96+
},
7197
/* TODO: Fails with fakeserver because a request for /api/objects/people/4321 is unexpected (400 error)
7298
Find a way to test this effectively
7399
{

0 commit comments

Comments
 (0)