Skip to content

Commit

Permalink
add filterMatch logic
Browse files Browse the repository at this point in the history
- filterMatch is responsible for finding matches based on filters from the user
against the response payload received from the API
  • Loading branch information
dikhan committed Sep 13, 2019
1 parent 636e1ff commit 1ad586d
Showing 1 changed file with 47 additions and 10 deletions.
57 changes: 47 additions & 10 deletions openapi/data_source_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package openapi
import (
"fmt"
"net/http"
"strconv"

"github.com/hashicorp/terraform/helper/schema"
)
Expand Down Expand Up @@ -85,20 +86,56 @@ func (d dataSourceFactory) read(data *schema.ResourceData, i interface{}) error
return fmt.Errorf("[data source='%s'] GET %s failed: %s", d.openAPIResource.getResourceName(), resourcePath, err)
}

fmt.Println(filters)
//for _, payloadItem := range responsePayload {
//
//}

// TODO: make use of responsePayload to filter out results
var filteredResults []map[string]interface{}
for _, payloadItem := range responsePayload {
match, err := d.filterMatch(filters, payloadItem)
if err != nil {
return err
}
if match {
filteredResults = append(filteredResults, payloadItem)
}
}

// TODO: If there are multiple matches after applying the filters return an error
// TODO: If there are no matches after applying the filters return an error
if len(filteredResults) == 0 { // TODO: untested
return fmt.Errorf("your query returned no results. Please change your search criteria and try again")
}

// TODO: update the state data object with the filtered result data
// d.updateStateWithPayloadData(remoteData, data)
if len(filteredResults) > 1 { // TODO: untested
return fmt.Errorf("your query returned contains more than one result. Please change your search criteria to make it more specific")
}

return nil
//return updateStateWithPayloadData(d.openAPIResource, filteredResults[0], data)
}

// TODO: add tests
func (d dataSourceFactory) filterMatch(filters filters, payloadItem map[string]interface{}) (bool, error) {
specSchemaDefinition, err := d.openAPIResource.getResourceSchema()
if err != nil {
return false, err
}
for _, filter := range filters {
if val, exists := payloadItem[filter.name]; exists {
schemaProperty, _ := specSchemaDefinition.getProperty(filter.name)
var value string
switch schemaProperty.Type {
case typeInt:
value = strconv.Itoa(val.(int))
case typeFloat:
value = fmt.Sprintf("%g", val.(float64))
case typeBool:
value = strconv.FormatBool(val.(bool))
default:
value = val.(string)
}
if value == filter.value {
continue
}
}
return false, nil
}
return true, nil
}

func (d dataSourceFactory) validateInput(data *schema.ResourceData) (filters, error) {
Expand Down

0 comments on commit 1ad586d

Please sign in to comment.