Skip to content

Commit

Permalink
Fix comments from PR #5057 (pricing_product)
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien Duchesne committed Jul 4, 2018
1 parent d403191 commit 1d729c1
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 24 deletions.
34 changes: 12 additions & 22 deletions aws/data_source_aws_pricing_product.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,13 @@ import (

"encoding/json"
"fmt"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/pricing"
"github.com/hashicorp/terraform/helper/hashcode"
"github.com/hashicorp/terraform/helper/schema"
)

const (
awsPricingTermMatch = "TERM_MATCH"
)

func dataSourceAwsPricingProduct() *schema.Resource {
return &schema.Resource{
Read: dataSourceAwsPricingProductRead,
Expand Down Expand Up @@ -62,7 +59,7 @@ func dataSourceAwsPricingProductRead(d *schema.ResourceData, meta interface{}) e
params.Filters = append(params.Filters, &pricing.Filter{
Field: aws.String(m["field"].(string)),
Value: aws.String(m["value"].(string)),
Type: aws.String(awsPricingTermMatch),
Type: aws.String(pricing.FilterTypeTermMatch),
})
}

Expand All @@ -72,31 +69,24 @@ func dataSourceAwsPricingProductRead(d *schema.ResourceData, meta interface{}) e
return fmt.Errorf("Error reading pricing of products: %s", err)
}

if err = verifyProductsPriceListLength(resp.PriceList); err != nil {
return err
}

pricingResult, err := json.Marshal(resp.PriceList[0])
if err != nil {
return fmt.Errorf("Invalid JSON value returned by AWS: %s", err)
}

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

func verifyProductsPriceListLength(priceList []aws.JSONValue) error {
numberOfElements := len(priceList)
numberOfElements := len(resp.PriceList)
if numberOfElements == 0 {
return fmt.Errorf("Pricing product query did not return any elements")
} else if numberOfElements > 1 {
priceListBytes, err := json.Marshal(priceList)
priceListBytes, err := json.Marshal(resp.PriceList)
priceListString := string(priceListBytes)
if err != nil {
priceListString = err.Error()
}
return fmt.Errorf("Pricing product query not precise enough. Returned more than one element: %s", priceListString)
}

pricingResult, err := json.Marshal(resp.PriceList[0])
if err != nil {
return fmt.Errorf("Invalid JSON value returned by AWS: %s", err)
}

d.SetId(fmt.Sprintf("%d", hashcode.String(params.String())))
d.Set("result", string(pricingResult))
return nil
}
4 changes: 4 additions & 0 deletions aws/data_source_aws_pricing_product_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import (
)

func TestAccDataSourceAwsPricingProduct_ec2(t *testing.T) {
oldRegion := os.Getenv("AWS_DEFAULT_REGION")
os.Setenv("AWS_DEFAULT_REGION", "us-east-1")
defer os.Setenv("AWS_DEFAULT_REGION", oldRegion)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Expand All @@ -28,7 +30,9 @@ func TestAccDataSourceAwsPricingProduct_ec2(t *testing.T) {
}

func TestAccDataSourceAwsPricingProduct_redshift(t *testing.T) {
oldRegion := os.Getenv("AWS_DEFAULT_REGION")
os.Setenv("AWS_DEFAULT_REGION", "us-east-1")
defer os.Setenv("AWS_DEFAULT_REGION", oldRegion)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Expand Down
3 changes: 3 additions & 0 deletions website/aws.erb
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,9 @@
<li<%= sidebar_current("docs-aws-datasource-prefix-list") %>>
<a href="/docs/providers/aws/d/prefix_list.html">aws_prefix_list</a>
</li>
<li<%= sidebar_current("docs-aws-datasource-pricing-product") %>>
<a href="/docs/providers/aws/d/pricing_product.html">aws_pricing_product</a>
</li>
<li<%= sidebar_current("docs-aws-datasource-rds-cluster") %>>
<a href="/docs/providers/aws/d/rds_cluster.html">aws_rds_cluster</a>
</li>
Expand Down
11 changes: 9 additions & 2 deletions website/docs/d/pricing_product.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ This data source is only available in a us-east-1 or ap-south-1 provider.
## Example Usage

```hcl
data "aws_pricing_product" "test1" {
data "aws_pricing_product" "example" {
service_code = "AmazonEC2"
filters = [
Expand Down Expand Up @@ -44,8 +44,10 @@ data "aws_pricing_product" "test1" {
},
]
}
```

data "aws_pricing_product" "test2" {
```hcl
data "aws_pricing_product" "example" {
service_code = "AmazonRedshift"
filters = [
Expand All @@ -66,6 +68,11 @@ data "aws_pricing_product" "test2" {
* `service_code` - (Required) The code of the service. Available service codes can be fetched using the DescribeServices pricing API call.
* `filters` - (Required) A list of filters. Passed directly to the API (see GetProducts API reference). These filters must describe a single product, this resource will fail if more than one product is returned by the API.

### `filters`

* `field` (Required) The product attribute name that you want to filter on.
* `value` (Required) The product attribute value that you want to filter on.

## Attributes Reference

* `result` - Set to the product returned from the API.

0 comments on commit 1d729c1

Please sign in to comment.