Skip to content

Commit

Permalink
Remove JSON dependency according to PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien Duchesne committed Jul 3, 2018
1 parent 68d64c1 commit d403191
Show file tree
Hide file tree
Showing 13 changed files with 17 additions and 2,903 deletions.
14 changes: 3 additions & 11 deletions aws/data_source_aws_pricing_product.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/aws/aws-sdk-go/service/pricing"
"github.com/hashicorp/terraform/helper/hashcode"
"github.com/hashicorp/terraform/helper/schema"
"github.com/tidwall/gjson"
)

const (
Expand Down Expand Up @@ -41,11 +40,7 @@ func dataSourceAwsPricingProduct() *schema.Resource {
},
},
},
"json_query": {
Type: schema.TypeString,
Required: true,
},
"query_result": {
"result": {
Type: schema.TypeString,
Computed: true,
},
Expand Down Expand Up @@ -86,11 +81,8 @@ func dataSourceAwsPricingProductRead(d *schema.ResourceData, meta interface{}) e
return fmt.Errorf("Invalid JSON value returned by AWS: %s", err)
}

jsonQuery := d.Get("json_query").(string)
queryResult := gjson.Get(string(pricingResult), jsonQuery)

d.SetId(fmt.Sprintf("%d-%d", hashcode.String(params.String()), hashcode.String(jsonQuery)))
d.Set("query_result", queryResult.String())
d.SetId(fmt.Sprintf("%d", hashcode.String(params.String())))
d.Set("result", string(pricingResult))
return nil
}

Expand Down
50 changes: 13 additions & 37 deletions aws/data_source_aws_pricing_product_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package aws

import (
"encoding/json"
"fmt"
"os"
"strconv"
"testing"

"github.com/hashicorp/terraform/helper/resource"
Expand All @@ -17,13 +17,10 @@ func TestAccDataSourceAwsPricingProduct_ec2(t *testing.T) {
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAwsPricingProductConfigEc2("test1", "c5.large") + testAccDataSourceAwsPricingProductConfigEc2("test2", "c5.xlarge"),
Config: testAccDataSourceAwsPricingProductConfigEc2("test", "c5.large"),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrSet("data.aws_pricing_product.test1", "query_result"),
resource.TestCheckResourceAttrSet("data.aws_pricing_product.test2", "query_result"),
testAccPricingCheckValueIsFloat("data.aws_pricing_product.test1"),
testAccPricingCheckValueIsFloat("data.aws_pricing_product.test2"),
testAccPricingCheckGreaterValue("data.aws_pricing_product.test2", "data.aws_pricing_product.test1"),
resource.TestCheckResourceAttrSet("data.aws_pricing_product.test", "result"),
testAccPricingCheckValueIsJSON("data.aws_pricing_product.test"),
),
},
},
Expand All @@ -39,8 +36,8 @@ func TestAccDataSourceAwsPricingProduct_redshift(t *testing.T) {
{
Config: testAccDataSourceAwsPricingProductConfigRedshift(),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrSet("data.aws_pricing_product.test", "query_result"),
testAccPricingCheckValueIsFloat("data.aws_pricing_product.test"),
resource.TestCheckResourceAttrSet("data.aws_pricing_product.test", "result"),
testAccPricingCheckValueIsJSON("data.aws_pricing_product.test"),
),
},
},
Expand Down Expand Up @@ -77,8 +74,6 @@ func testAccDataSourceAwsPricingProductConfigEc2(dataName string, instanceType s
value = "Shared"
},
]
json_query = "terms.OnDemand.*.priceDimensions.*.pricePerUnit.USD"
}
`, dataName, instanceType)
}
Expand All @@ -97,46 +92,27 @@ func testAccDataSourceAwsPricingProductConfigRedshift() string {
value = "US East (N. Virginia)"
},
]
json_query = "terms.OnDemand.*.priceDimensions.*.pricePerUnit.USD"
}
`)
}

func testAccPricingCheckValueIsFloat(data string) resource.TestCheckFunc {
func testAccPricingCheckValueIsJSON(data string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[data]

if !ok {
return fmt.Errorf("Can't find resource: %s", data)
}

queryResult := rs.Primary.Attributes["query_result"]
if _, err := strconv.ParseFloat(queryResult, 32); err != nil {
return fmt.Errorf("%s query_result value (%s) is not a float: %s", data, queryResult, err)
}

return nil
}
}
result := rs.Primary.Attributes["result"]
var objmap map[string]*json.RawMessage

func testAccPricingCheckGreaterValue(dataWithGreaterValue string, otherData string) resource.TestCheckFunc {
return func(s *terraform.State) error {
greaterResource, ok := s.RootModule().Resources[dataWithGreaterValue]
if !ok {
return fmt.Errorf("Can't find resource: %s", dataWithGreaterValue)
}

lesserResource, ok := s.RootModule().Resources[otherData]
if !ok {
return fmt.Errorf("Can't find resource: %s", otherData)
if err := json.Unmarshal([]byte(result), &objmap); err != nil {
return fmt.Errorf("%s result value (%s) is not JSON: %s", data, result, err)
}

greaterValue := greaterResource.Primary.Attributes["query_result"]
lesserValue := lesserResource.Primary.Attributes["query_result"]

if greaterValue <= lesserValue {
return fmt.Errorf("%s (%s) has a greater value than %s (%s). Should have been the opposite", otherData, lesserValue, dataWithGreaterValue, greaterValue)
if len(objmap) == 0 {
return fmt.Errorf("%s result value (%s) unmarshalling resulted in an empty map", data, result)
}

return nil
Expand Down
20 changes: 0 additions & 20 deletions vendor/github.com/tidwall/gjson/LICENSE

This file was deleted.

Loading

0 comments on commit d403191

Please sign in to comment.