Skip to content

Commit

Permalink
Merge pull request #32135 from hashicorp/td-migrate-pricing-to-aws-sd…
Browse files Browse the repository at this point in the history
…k-v2

Tech debt: Migrate `pricing` resources to AWS SDK for Go v2
  • Loading branch information
ewbankkit authored Jun 21, 2023
2 parents f41c3ee + 125111e commit 5108afe
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 65 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ require (
github.com/aws/aws-sdk-go-v2/service/oam v1.1.13
github.com/aws/aws-sdk-go-v2/service/opensearchserverless v1.2.6
github.com/aws/aws-sdk-go-v2/service/pipes v1.2.8
github.com/aws/aws-sdk-go-v2/service/pricing v1.20.0
github.com/aws/aws-sdk-go-v2/service/rbin v1.8.14
github.com/aws/aws-sdk-go-v2/service/rds v1.45.2
github.com/aws/aws-sdk-go-v2/service/resourceexplorer2 v1.2.15
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ github.com/aws/aws-sdk-go-v2/service/opensearchserverless v1.2.6 h1:tADh6fIpsh0X
github.com/aws/aws-sdk-go-v2/service/opensearchserverless v1.2.6/go.mod h1:Ju/rPQaOhCUQAFIZlf+drYB+YJQSuwbCYlAzfCGQVQc=
github.com/aws/aws-sdk-go-v2/service/pipes v1.2.8 h1:TPr7uAucpAvgE0ac9zMs/LTx9O0dmLWHWr2mkZjL4iY=
github.com/aws/aws-sdk-go-v2/service/pipes v1.2.8/go.mod h1:T8pM2eiirH5Ld58Ek+t7tK3SqQNUwp9zkqkslW1v8sM=
github.com/aws/aws-sdk-go-v2/service/pricing v1.20.0 h1:x5gKeerbKIQ/tdhmaAGNpivSfmb+p2rdt0wyjCGz+4Q=
github.com/aws/aws-sdk-go-v2/service/pricing v1.20.0/go.mod h1:JjpnqJdEW/5An429Ou+5Kb3UkwjXv16gRD2ZdGA2Gw8=
github.com/aws/aws-sdk-go-v2/service/rbin v1.8.14 h1:oEeNqSze4JYbhsDg7udtz+AtLwrIXlvjItAd09Z2zTw=
github.com/aws/aws-sdk-go-v2/service/rbin v1.8.14/go.mod h1:JehbzDgwXYQi1wqKKFLstaNqsRPgPu9Kd6DiwhtFxoA=
github.com/aws/aws-sdk-go-v2/service/rds v1.45.2 h1:tKZ+n9/BTnNYXZo80nIoFes7QEbOd/lnbp0EkPbHfUU=
Expand Down
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
6 changes: 3 additions & 3 deletions internal/conns/awsclient_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 21 additions & 31 deletions internal/service/pricing/product_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ package pricing

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

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/pricing"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/pricing"
"github.com/aws/aws-sdk-go-v2/service/pricing/types"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
Expand All @@ -16,14 +15,11 @@ import (
)

// @SDKDataSource("aws_pricing_product")
func DataSourceProduct() *schema.Resource {
func dataSourceProduct() *schema.Resource {
return &schema.Resource{
ReadWithoutTimeout: dataSourceProductRead,

Schema: map[string]*schema.Schema{
"service_code": {
Type: schema.TypeString,
Required: true,
},
"filters": {
Type: schema.TypeList,
Required: true,
Expand All @@ -45,53 +41,47 @@ func DataSourceProduct() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"service_code": {
Type: schema.TypeString,
Required: true,
},
},
}
}

func dataSourceProductRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).PricingConn(ctx)
conn := meta.(*conns.AWSClient).PricingClient(ctx)

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

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

log.Printf("[DEBUG] Reading pricing of products: %s", params)
resp, err := conn.GetProductsWithContext(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(params.String())))
d.Set("result", string(pricingResult))
return diags
}
27 changes: 5 additions & 22 deletions internal/service/pricing/product_data_source_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package pricing_test

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

"github.com/aws/aws-sdk-go/aws/endpoints"
"github.com/aws/aws-sdk-go/service/pricing"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-provider-aws/internal/acctest"
"github.com/hashicorp/terraform-provider-aws/names"
)

func TestAccPricingProductDataSource_ec2(t *testing.T) {
Expand All @@ -21,13 +18,13 @@ func TestAccPricingProductDataSource_ec2(t *testing.T) {
acctest.PreCheck(ctx, t)
acctest.PreCheckRegion(t, endpoints.UsEast1RegionID, endpoints.ApSouth1RegionID)
},
ErrorCheck: acctest.ErrorCheck(t, pricing.EndpointsID),
ErrorCheck: acctest.ErrorCheck(t, names.PricingEndpointID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccProductDataSourceConfig_ec2,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrWith(dataSourceName, "result", testAccCheckValueIsJSON),
acctest.CheckResourceAttrIsJSONString(dataSourceName, "result"),
),
},
},
Expand All @@ -43,13 +40,13 @@ func TestAccPricingProductDataSource_redshift(t *testing.T) {
acctest.PreCheck(ctx, t)
acctest.PreCheckRegion(t, endpoints.UsEast1RegionID, endpoints.ApSouth1RegionID)
},
ErrorCheck: acctest.ErrorCheck(t, pricing.EndpointsID),
ErrorCheck: acctest.ErrorCheck(t, names.PricingEndpointID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
Steps: []resource.TestStep{
{
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
}
19 changes: 11 additions & 8 deletions internal/service/pricing/service_package_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions names/names.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const (
ObservabilityAccessManagerEndpointID = "oam"
OpenSearchServerlessEndpointID = "aoss"
PipesEndpointID = "pipes"
PricingEndpointID = "pricing"
ResourceExplorer2EndpointID = "resource-explorer-2"
RolesAnywhereEndpointID = "rolesanywhere"
Route53DomainsEndpointID = "route53domains"
Expand Down
2 changes: 1 addition & 1 deletion names/names_data.csv
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ pinpoint-sms-voice,pinpointsmsvoice,pinpointsmsvoice,pinpointsmsvoice,,pinpoints
pipes,pipes,pipes,pipes,,pipes,,,Pipes,Pipes,,,2,,aws_pipes_,,pipes_,EventBridge Pipes,Amazon,,,,,
polly,polly,polly,polly,,polly,,,Polly,Polly,,1,,,aws_polly_,,polly_,Polly,Amazon,,,,,
,,,,,,,,,,,,,,,,,Porting Assistant for .NET,,x,,,,No SDK support
pricing,pricing,pricing,pricing,,pricing,,,Pricing,Pricing,,1,,,aws_pricing_,,pricing_,Pricing Calculator,AWS,,,,,
pricing,pricing,pricing,pricing,,pricing,,,Pricing,Pricing,,,2,,aws_pricing_,,pricing_,Pricing Calculator,AWS,,,,,
proton,proton,proton,proton,,proton,,,Proton,Proton,,1,,,aws_proton_,,proton_,Proton,AWS,,,,,
qldb,qldb,qldb,qldb,,qldb,,,QLDB,QLDB,,1,,,aws_qldb_,,qldb_,QLDB (Quantum Ledger Database),Amazon,,,,,
qldb-session,qldbsession,qldbsession,qldbsession,,qldbsession,,,QLDBSession,QLDBSession,,1,,,aws_qldbsession_,,qldbsession_,QLDB Session,Amazon,,,,,
Expand Down

0 comments on commit 5108afe

Please sign in to comment.