Skip to content

Commit

Permalink
fix: Return error when aws_cur_report_definition is not associated wi…
Browse files Browse the repository at this point in the history
…th us-east-1 region
  • Loading branch information
acwwat committed Mar 23, 2024
1 parent 691f4c1 commit 52bbcfc
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .changelog/36540.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
```release-note:bug
resource/aws_cur_report_definition: Add region validation before operations to prevent hangs as CUR service is only available in `us-east-1`
```

```release-note:bug
data-source/aws_cur_report_definition: Add region validation before read operation to prevent hangs as CUR service is only available in `us-east-1`
```
21 changes: 21 additions & 0 deletions internal/service/cur/report_definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/YakDriver/regexache"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/arn"
"github.com/aws/aws-sdk-go/aws/endpoints"
cur "github.com/aws/aws-sdk-go/service/costandusagereportservice"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand All @@ -20,6 +21,10 @@ import (
"github.com/hashicorp/terraform-provider-aws/internal/flex"
)

const (
availRegion = endpoints.UsEast1RegionID
)

// @SDKResource("aws_cur_report_definition")
func ResourceReportDefinition() *schema.Resource {
return &schema.Resource{
Expand Down Expand Up @@ -111,6 +116,10 @@ func resourceReportDefinitionCreate(ctx context.Context, d *schema.ResourceData,
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).CURConn(ctx)

if aws.StringValue(conn.Config.Region) != availRegion {
return sdkdiag.AppendErrorf(diags, "The AWS Cost And Usage Report service is only available in the %s region.", availRegion)
}

reportName := d.Get("report_name").(string)
additionalArtifacts := flex.ExpandStringSet(d.Get("additional_artifacts").(*schema.Set))
compression := d.Get("compression").(string)
Expand Down Expand Up @@ -168,6 +177,10 @@ func resourceReportDefinitionRead(ctx context.Context, d *schema.ResourceData, m
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).CURConn(ctx)

if aws.StringValue(conn.Config.Region) != availRegion {
return sdkdiag.AppendErrorf(diags, "The AWS Cost And Usage Report service is only available in the %s region.", availRegion)
}

reportDefinition, err := FindReportDefinitionByName(ctx, conn, d.Id())

if err != nil {
Expand Down Expand Up @@ -214,6 +227,10 @@ func resourceReportDefinitionUpdate(ctx context.Context, d *schema.ResourceData,
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).CURConn(ctx)

if aws.StringValue(conn.Config.Region) != availRegion {
return sdkdiag.AppendErrorf(diags, "The AWS Cost And Usage Report service is only available in the %s region.", availRegion)
}

additionalArtifacts := flex.ExpandStringSet(d.Get("additional_artifacts").(*schema.Set))
compression := d.Get("compression").(string)
format := d.Get("format").(string)
Expand Down Expand Up @@ -271,6 +288,10 @@ func resourceReportDefinitionDelete(ctx context.Context, d *schema.ResourceData,
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).CURConn(ctx)

if aws.StringValue(conn.Config.Region) != availRegion {
return sdkdiag.AppendErrorf(diags, "The AWS Cost And Usage Report service is only available in the %s region.", availRegion)
}

log.Printf("[DEBUG] Deleting Cost And Usage Report Definition: %s", d.Id())
_, err := conn.DeleteReportDefinitionWithContext(ctx, &cur.DeleteReportDefinitionInput{
ReportName: aws.String(d.Id()),
Expand Down
4 changes: 4 additions & 0 deletions internal/service/cur/report_definition_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ func dataSourceReportDefinitionRead(ctx context.Context, d *schema.ResourceData,
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).CURConn(ctx)

if aws.StringValue(conn.Config.Region) != availRegion {
return sdkdiag.AppendErrorf(diags, "The AWS Cost And Usage Report service is only available in the %s region.", availRegion)
}

reportName := d.Get("report_name").(string)

reportDefinition, err := FindReportDefinitionByName(ctx, conn, reportName)
Expand Down

0 comments on commit 52bbcfc

Please sign in to comment.