-
Notifications
You must be signed in to change notification settings - Fork 9.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement data aws_pricing_product #5057
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package aws | ||
|
||
import ( | ||
"log" | ||
|
||
"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" | ||
) | ||
|
||
func dataSourceAwsPricingProduct() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceAwsPricingProductRead, | ||
Schema: map[string]*schema.Schema{ | ||
"service_code": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"filters": { | ||
Type: schema.TypeList, | ||
Required: true, | ||
MinItems: 1, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"field": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"value": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
"result": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceAwsPricingProductRead(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).pricingconn | ||
|
||
params := &pricing.GetProductsInput{ | ||
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{ | ||
Field: aws.String(m["field"].(string)), | ||
Value: aws.String(m["value"].(string)), | ||
Type: aws.String(pricing.FilterTypeTermMatch), | ||
}) | ||
} | ||
|
||
log.Printf("[DEBUG] Reading pricing of products: %s", params) | ||
resp, err := conn.GetProducts(params) | ||
if err != nil { | ||
return fmt.Errorf("Error reading pricing of products: %s", err) | ||
} | ||
|
||
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(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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
package aws | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform/helper/resource" | ||
"github.com/hashicorp/terraform/terraform" | ||
) | ||
|
||
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, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccDataSourceAwsPricingProductConfigEc2("test", "c5.large"), | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
resource.TestCheckResourceAttrSet("data.aws_pricing_product.test", "result"), | ||
testAccPricingCheckValueIsJSON("data.aws_pricing_product.test"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
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, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccDataSourceAwsPricingProductConfigRedshift(), | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
resource.TestCheckResourceAttrSet("data.aws_pricing_product.test", "result"), | ||
testAccPricingCheckValueIsJSON("data.aws_pricing_product.test"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccDataSourceAwsPricingProductConfigEc2(dataName string, instanceType string) string { | ||
return fmt.Sprintf(`data "aws_pricing_product" "%s" { | ||
service_code = "AmazonEC2" | ||
|
||
filters = [ | ||
{ | ||
field = "instanceType" | ||
value = "%s" | ||
}, | ||
{ | ||
field = "operatingSystem" | ||
value = "Linux" | ||
}, | ||
{ | ||
field = "location" | ||
value = "US East (N. Virginia)" | ||
}, | ||
{ | ||
field = "preInstalledSw" | ||
value = "NA" | ||
}, | ||
{ | ||
field = "licenseModel" | ||
value = "No License required" | ||
}, | ||
{ | ||
field = "tenancy" | ||
value = "Shared" | ||
}, | ||
] | ||
} | ||
`, dataName, instanceType) | ||
} | ||
|
||
func testAccDataSourceAwsPricingProductConfigRedshift() string { | ||
return fmt.Sprintf(`data "aws_pricing_product" "test" { | ||
service_code = "AmazonRedshift" | ||
|
||
filters = [ | ||
{ | ||
field = "instanceType" | ||
value = "ds1.xlarge" | ||
}, | ||
{ | ||
field = "location" | ||
value = "US East (N. Virginia)" | ||
}, | ||
] | ||
} | ||
`) | ||
} | ||
|
||
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) | ||
} | ||
|
||
result := rs.Primary.Attributes["result"] | ||
var objmap map[string]*json.RawMessage | ||
|
||
if err := json.Unmarshal([]byte(result), &objmap); err != nil { | ||
return fmt.Errorf("%s result value (%s) is not JSON: %s", data, result, err) | ||
} | ||
|
||
if len(objmap) == 0 { | ||
return fmt.Errorf("%s result value (%s) unmarshalling resulted in an empty map", data, result) | ||
} | ||
|
||
return nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
--- | ||
layout: "aws" | ||
page_title: "AWS: aws_pricing_product" | ||
sidebar_current: "docs-aws-datasource-pricing-product" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This page needs a sidebar link added to |
||
description: |- | ||
Get information regarding the pricing of an Amazon product | ||
--- | ||
|
||
# Data Source: aws_pricing_product | ||
|
||
Use this data source to get the pricing information of all products in AWS. | ||
This data source is only available in a us-east-1 or ap-south-1 provider. | ||
|
||
## Example Usage | ||
|
||
```hcl | ||
data "aws_pricing_product" "example" { | ||
service_code = "AmazonEC2" | ||
|
||
filters = [ | ||
{ | ||
field = "instanceType" | ||
value = "c5.xlarge" | ||
}, | ||
{ | ||
field = "operatingSystem" | ||
value = "Linux" | ||
}, | ||
{ | ||
field = "location" | ||
value = "US East (N. Virginia)" | ||
}, | ||
{ | ||
field = "preInstalledSw" | ||
value = "NA" | ||
}, | ||
{ | ||
field = "licenseModel" | ||
value = "No License required" | ||
}, | ||
{ | ||
field = "tenancy" | ||
value = "Shared" | ||
}, | ||
] | ||
} | ||
``` | ||
|
||
```hcl | ||
data "aws_pricing_product" "example" { | ||
service_code = "AmazonRedshift" | ||
|
||
filters = [ | ||
{ | ||
field = "instanceType" | ||
value = "ds1.xlarge" | ||
}, | ||
{ | ||
field = "location" | ||
value = "US East (N. Virginia)" | ||
}, | ||
] | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
* `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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need to describe the
|
||
|
||
### `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. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor nitpick: The Go imports seem to be a little off formatting wise. The stdlib imports should be grouped and alphabetically sorted at the top, a blank line, then external imports grouped and alphabetically sorted.
We tend to prefer using
goimports
for managing these consistently (and automatically) everywhere 👍