From 125111e9e489764ff3c748eda9a4e84798cd83bc Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Wed, 21 Jun 2023 11:06:32 -0400 Subject: [PATCH] d/aws_pricing_product: Get acceptance tests passing. --- internal/acctest/acctest.go | 17 ++++++++++ .../service/pricing/product_data_source.go | 32 +++++++------------ .../pricing/product_data_source_test.go | 21 ++---------- 3 files changed, 30 insertions(+), 40 deletions(-) diff --git a/internal/acctest/acctest.go b/internal/acctest/acctest.go index 76943f7bdca9..dd25dd86b004 100644 --- a/internal/acctest/acctest.go +++ b/internal/acctest/acctest.go @@ -3,6 +3,7 @@ package acctest import ( "context" "encoding/json" + "errors" "fmt" "log" "os" @@ -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() diff --git a/internal/service/pricing/product_data_source.go b/internal/service/pricing/product_data_source.go index 461aa3787ae2..5699bbbdac58 100644 --- a/internal/service/pricing/product_data_source.go +++ b/internal/service/pricing/product_data_source.go @@ -2,7 +2,6 @@ package pricing import ( "context" - "encoding/json" "fmt" "github.com/aws/aws-sdk-go-v2/aws" @@ -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 } diff --git a/internal/service/pricing/product_data_source_test.go b/internal/service/pricing/product_data_source_test.go index 659e5b970a97..a405bac1923e 100644 --- a/internal/service/pricing/product_data_source_test.go +++ b/internal/service/pricing/product_data_source_test.go @@ -1,9 +1,6 @@ package pricing_test import ( - "encoding/json" - "errors" - "fmt" "testing" "github.com/aws/aws-sdk-go/aws/endpoints" @@ -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"), ), }, }, @@ -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"), ), }, }, @@ -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 -}