Skip to content

Commit

Permalink
d/aws_pricing_product: Get acceptance tests passing.
Browse files Browse the repository at this point in the history
  • Loading branch information
ewbankkit committed Jun 21, 2023
1 parent 3800ee5 commit 125111e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 40 deletions.
17 changes: 17 additions & 0 deletions internal/acctest/acctest.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package acctest
import (
"context"
"encoding/json"
"errors"
"fmt"
"log"
"os"
Expand Down Expand Up @@ -2296,6 +2297,22 @@ func CheckResourceAttrGreaterThanOrEqualValue(n, key string, val int) resource.T
})
}

func CheckResourceAttrIsJSONString(n, key string) resource.TestCheckFunc {
return resource.TestCheckResourceAttrWith(n, key, func(value string) error {
var m map[string]*json.RawMessage

if err := json.Unmarshal([]byte(value), &m); err != nil {
return err
}

if len(m) == 0 {
return errors.New(`empty JSON string`)
}

return nil
})
}

// RunSerialTests1Level runs test cases in parallel, optionally sleeping between each.
func RunSerialTests1Level(t *testing.T, testCases map[string]func(t *testing.T), d time.Duration) {
t.Helper()
Expand Down
32 changes: 11 additions & 21 deletions internal/service/pricing/product_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package pricing

import (
"context"
"encoding/json"
"fmt"

"github.com/aws/aws-sdk-go-v2/aws"
Expand Down Expand Up @@ -54,44 +53,35 @@ func dataSourceProductRead(ctx context.Context, d *schema.ResourceData, meta int
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).PricingClient(ctx)

params := &pricing.GetProductsInput{
ServiceCode: aws.String(d.Get("service_code").(string)),
input := &pricing.GetProductsInput{
Filters: []types.Filter{},
ServiceCode: aws.String(d.Get("service_code").(string)),
}

filters := d.Get("filters")
for _, v := range filters.([]interface{}) {
m := v.(map[string]interface{})
params.Filters = append(params.Filters, types.Filter{
input.Filters = append(input.Filters, types.Filter{
Field: aws.String(m["field"].(string)),
Value: aws.String(m["value"].(string)),
Type: types.FilterTypeTermMatch,
Value: aws.String(m["value"].(string)),
})
}

resp, err := conn.GetProducts(ctx, params)
output, err := conn.GetProducts(ctx, input)

if err != nil {
return sdkdiag.AppendErrorf(diags, "reading pricing of products: %s", err)
return sdkdiag.AppendErrorf(diags, "reading Pricing Products: %s", err)
}

numberOfElements := len(resp.PriceList)
if numberOfElements == 0 {
if numberOfElements := len(output.PriceList); numberOfElements == 0 {
return sdkdiag.AppendErrorf(diags, "Pricing product query did not return any elements")
} else if numberOfElements > 1 {
priceListBytes, err := json.Marshal(resp.PriceList)
priceListString := string(priceListBytes)
if err != nil {
priceListString = err.Error()
}
return sdkdiag.AppendErrorf(diags, "Pricing product query not precise enough. Returned more than one element: %s", priceListString)
return sdkdiag.AppendErrorf(diags, "Pricing product query not precise enough. Returned %d elements", numberOfElements)
}

pricingResult, err := json.Marshal(resp.PriceList[0])
if err != nil {
return sdkdiag.AppendErrorf(diags, "Invalid JSON value returned by AWS: %s", err)
}
d.SetId(fmt.Sprintf("%d", create.StringHashcode(fmt.Sprintf("%#v", input))))
d.Set("result", output.PriceList[0])

d.SetId(fmt.Sprintf("%d", create.StringHashcode(fmt.Sprintf("%#v", params))))
d.Set("result", string(pricingResult))
return diags
}
21 changes: 2 additions & 19 deletions internal/service/pricing/product_data_source_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package pricing_test

import (
"encoding/json"
"errors"
"fmt"
"testing"

"github.com/aws/aws-sdk-go/aws/endpoints"
Expand All @@ -27,7 +24,7 @@ func TestAccPricingProductDataSource_ec2(t *testing.T) {
{
Config: testAccProductDataSourceConfig_ec2,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrWith(dataSourceName, "result", testAccCheckValueIsJSON),
acctest.CheckResourceAttrIsJSONString(dataSourceName, "result"),
),
},
},
Expand All @@ -49,7 +46,7 @@ func TestAccPricingProductDataSource_redshift(t *testing.T) {
{
Config: testAccProductDataSourceConfig_redshift,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrWith(dataSourceName, "result", testAccCheckValueIsJSON),
acctest.CheckResourceAttrIsJSONString(dataSourceName, "result"),
),
},
},
Expand Down Expand Up @@ -129,17 +126,3 @@ data "aws_pricing_product" "test" {
}
}
`

func testAccCheckValueIsJSON(v string) error {
var m map[string]*json.RawMessage

if err := json.Unmarshal([]byte(v), &m); err != nil {
return fmt.Errorf("parsing JSON: %s", err)
}

if len(m) == 0 {
return errors.New(`empty JSON`)
}

return nil
}

0 comments on commit 125111e

Please sign in to comment.