Skip to content

Commit

Permalink
Merge pull request hashicorp#40521 from drewtul/bug-ce-rule-order
Browse files Browse the repository at this point in the history
[Bug] Fix cost category rule ordering
  • Loading branch information
ewbankkit authored Dec 11, 2024
2 parents f528b5f + 4d5ebc7 commit 75720e8
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 50 deletions.
3 changes: 3 additions & 0 deletions .changelog/40521.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
resource/aws_ce_cost_category: Change `rule` from `TypeSet` to `TypeList` as order is significant
```
10 changes: 5 additions & 5 deletions internal/service/ce/anomaly_monitor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestAccCEAnomalyMonitor_basic(t *testing.T) {
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t) },
PreCheck: func() { acctest.PreCheck(ctx, t); testAccPreCheckPayerAccount(ctx, t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckAnomalyMonitorDestroy(ctx),
ErrorCheck: acctest.ErrorCheck(t, names.CEServiceID),
Expand Down Expand Up @@ -58,7 +58,7 @@ func TestAccCEAnomalyMonitor_disappears(t *testing.T) {
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t) },
PreCheck: func() { acctest.PreCheck(ctx, t); testAccPreCheckPayerAccount(ctx, t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckAnomalyMonitorDestroy(ctx),
ErrorCheck: acctest.ErrorCheck(t, names.CEServiceID),
Expand All @@ -83,7 +83,7 @@ func TestAccCEAnomalyMonitor_update(t *testing.T) {
rName2 := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t) },
PreCheck: func() { acctest.PreCheck(ctx, t); testAccPreCheckPayerAccount(ctx, t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckAnomalyMonitorDestroy(ctx),
ErrorCheck: acctest.ErrorCheck(t, names.CEServiceID),
Expand Down Expand Up @@ -118,7 +118,7 @@ func TestAccCEAnomalyMonitor_tags(t *testing.T) {
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t) },
PreCheck: func() { acctest.PreCheck(ctx, t); testAccPreCheckPayerAccount(ctx, t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckAnomalyMonitorDestroy(ctx),
ErrorCheck: acctest.ErrorCheck(t, names.CEServiceID),
Expand Down Expand Up @@ -167,7 +167,7 @@ func TestAccCEAnomalyMonitor_Dimensional(t *testing.T) {
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t) },
PreCheck: func() { acctest.PreCheck(ctx, t); testAccPreCheckPayerAccount(ctx, t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckAnomalyMonitorDestroy(ctx),
ErrorCheck: acctest.ErrorCheck(t, names.CEServiceID),
Expand Down
14 changes: 7 additions & 7 deletions internal/service/ce/anomaly_subscription_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestAccCEAnomalySubscription_basic(t *testing.T) {
address := acctest.RandomEmailAddress(domain)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t) },
PreCheck: func() { acctest.PreCheck(ctx, t); testAccPreCheckPayerAccount(ctx, t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckAnomalySubscriptionDestroy(ctx),
ErrorCheck: acctest.ErrorCheck(t, names.CEServiceID),
Expand Down Expand Up @@ -65,7 +65,7 @@ func TestAccCEAnomalySubscription_disappears(t *testing.T) {
address := acctest.RandomEmailAddress(domain)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t) },
PreCheck: func() { acctest.PreCheck(ctx, t); testAccPreCheckPayerAccount(ctx, t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckAnomalySubscriptionDestroy(ctx),
ErrorCheck: acctest.ErrorCheck(t, names.CEServiceID),
Expand All @@ -91,7 +91,7 @@ func TestAccCEAnomalySubscription_Frequency(t *testing.T) {
address := acctest.RandomEmailAddress(domain)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t) },
PreCheck: func() { acctest.PreCheck(ctx, t); testAccPreCheckPayerAccount(ctx, t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckAnomalySubscriptionDestroy(ctx),
ErrorCheck: acctest.ErrorCheck(t, names.CEServiceID),
Expand Down Expand Up @@ -129,7 +129,7 @@ func TestAccCEAnomalySubscription_MonitorARNList(t *testing.T) {
address := acctest.RandomEmailAddress(domain)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t) },
PreCheck: func() { acctest.PreCheck(ctx, t); testAccPreCheckPayerAccount(ctx, t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckAnomalySubscriptionDestroy(ctx),
ErrorCheck: acctest.ErrorCheck(t, names.CEServiceID),
Expand Down Expand Up @@ -168,7 +168,7 @@ func TestAccCEAnomalySubscription_Subscriber(t *testing.T) {
address2 := acctest.RandomEmailAddress(domain)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t) },
PreCheck: func() { acctest.PreCheck(ctx, t); testAccPreCheckPayerAccount(ctx, t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckAnomalySubscriptionDestroy(ctx),
ErrorCheck: acctest.ErrorCheck(t, names.CEServiceID),
Expand Down Expand Up @@ -226,7 +226,7 @@ func TestAccCEAnomalySubscription_Subscriber(t *testing.T) {
})
}

func TestAccCEAnomalySubscription_Tags(t *testing.T) {
func TestAccCEAnomalySubscription_tags(t *testing.T) {
ctx := acctest.Context(t)
var subscription awstypes.AnomalySubscription
resourceName := "aws_ce_anomaly_subscription.test"
Expand All @@ -235,7 +235,7 @@ func TestAccCEAnomalySubscription_Tags(t *testing.T) {
address := acctest.RandomEmailAddress(domain)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t) },
PreCheck: func() { acctest.PreCheck(ctx, t); testAccPreCheckPayerAccount(ctx, t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckAnomalySubscriptionDestroy(ctx),
ErrorCheck: acctest.ErrorCheck(t, names.CEServiceID),
Expand Down
22 changes: 2 additions & 20 deletions internal/service/ce/cost_allocation_tag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ import (
"fmt"
"testing"

"github.com/aws/aws-sdk-go-v2/service/costexplorer"
awstypes "github.com/aws/aws-sdk-go-v2/service/costexplorer/types"
"github.com/hashicorp/aws-sdk-go-base/v2/tfawserr"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"
"github.com/hashicorp/terraform-provider-aws/internal/acctest"
Expand All @@ -26,7 +24,7 @@ func TestAccCECostAllocationTag_basic(t *testing.T) {
rName := "Tag01"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t); testAccPreCheckCostAllocationTagPayerAccount(ctx, t) },
PreCheck: func() { acctest.PreCheck(ctx, t); testAccPreCheckPayerAccount(ctx, t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckCostAllocationTagDestroy(ctx),
ErrorCheck: acctest.ErrorCheck(t, names.CEServiceID),
Expand Down Expand Up @@ -73,7 +71,7 @@ func TestAccCECostAllocationTag_disappears(t *testing.T) {
rName := "Tag02"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t); testAccPreCheckCostAllocationTagPayerAccount(ctx, t) },
PreCheck: func() { acctest.PreCheck(ctx, t); testAccPreCheckPayerAccount(ctx, t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckCostAllocationTagDestroy(ctx),
ErrorCheck: acctest.ErrorCheck(t, names.CEServiceID),
Expand All @@ -90,22 +88,6 @@ func TestAccCECostAllocationTag_disappears(t *testing.T) {
})
}

func testAccPreCheckCostAllocationTagPayerAccount(ctx context.Context, t *testing.T) {
t.Helper()

conn := acctest.Provider.Meta().(*conns.AWSClient).CEClient(ctx)

_, err := conn.ListCostAllocationTags(ctx, &costexplorer.ListCostAllocationTagsInput{})

if tfawserr.ErrMessageContains(err, "AccessDeniedException", "Linked account doesn't have access to") {
t.Skip("skipping tests; this AWS account must be a payer account")
}

if err != nil {
t.Fatalf("listing Cost Explorer Cost Allocation Tags: %s", err)
}
}

func testAccCheckCostAllocationTagExists(ctx context.Context, n string, v *awstypes.CostAllocationTag) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
Expand Down
6 changes: 3 additions & 3 deletions internal/service/ce/cost_category.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func resourceCostCategory() *schema.Resource {
ValidateFunc: validation.StringLenBetween(1, 50),
},
names.AttrRule: {
Type: schema.TypeSet,
Type: schema.TypeList,
Required: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
Expand Down Expand Up @@ -305,7 +305,7 @@ func resourceCostCategoryCreate(ctx context.Context, d *schema.ResourceData, met
input := &costexplorer.CreateCostCategoryDefinitionInput{
Name: aws.String(name),
ResourceTags: getTagsIn(ctx),
Rules: expandCostCategoryRules(d.Get(names.AttrRule).(*schema.Set).List()),
Rules: expandCostCategoryRules(d.Get(names.AttrRule).([]interface{})),
RuleVersion: awstypes.CostCategoryRuleVersion(d.Get("rule_version").(string)),
}

Expand Down Expand Up @@ -375,7 +375,7 @@ func resourceCostCategoryUpdate(ctx context.Context, d *schema.ResourceData, met
input := &costexplorer.UpdateCostCategoryDefinitionInput{
CostCategoryArn: aws.String(d.Id()),
EffectiveStart: aws.String(d.Get("effective_start").(string)),
Rules: expandCostCategoryRules(d.Get(names.AttrRule).(*schema.Set).List()),
Rules: expandCostCategoryRules(d.Get(names.AttrRule).([]interface{})),
RuleVersion: awstypes.CostCategoryRuleVersion(d.Get("rule_version").(string)),
}

Expand Down
2 changes: 1 addition & 1 deletion internal/service/ce/cost_category_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestAccCECostCategoryDataSource_basic(t *testing.T) {
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t); testAccPreCheckCostCategoryPayerAccount(ctx, t) },
PreCheck: func() { acctest.PreCheck(ctx, t); testAccPreCheckPayerAccount(ctx, t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
ErrorCheck: acctest.ErrorCheck(t, names.CEServiceID),
Steps: []resource.TestStep{
Expand Down
29 changes: 17 additions & 12 deletions internal/service/ce/cost_category_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,22 @@ func TestAccCECostCategory_basic(t *testing.T) {
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t); testAccPreCheckCostCategoryPayerAccount(ctx, t) },
PreCheck: func() { acctest.PreCheck(ctx, t); testAccPreCheckPayerAccount(ctx, t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckCostCategoryDestroy(ctx),
ErrorCheck: acctest.ErrorCheck(t, names.CEServiceID),
Steps: []resource.TestStep{
{
Config: testAccCostCategoryConfig_basic(rName),
Check: resource.ComposeTestCheckFunc(
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckCostCategoryExists(ctx, resourceName, &output),
resource.TestCheckResourceAttr(resourceName, names.AttrName, rName),
resource.TestCheckResourceAttrSet(resourceName, "effective_start"),
acctest.MatchResourceAttrGlobalARN(ctx, resourceName, names.AttrARN, "ce", regexache.MustCompile(`costcategory/.+$`)),
resource.TestCheckResourceAttrSet(resourceName, "effective_start"),
resource.TestCheckResourceAttr(resourceName, names.AttrName, rName),
resource.TestCheckResourceAttr(resourceName, acctest.CtRulePound, "3"),
resource.TestCheckResourceAttr(resourceName, "rule.0.value", "production"),
resource.TestCheckResourceAttr(resourceName, "rule.1.value", "staging"),
resource.TestCheckResourceAttr(resourceName, "rule.2.value", "testing"),
),
},
{
Expand All @@ -66,7 +70,7 @@ func TestAccCECostCategory_effectiveStart(t *testing.T) {
firstOfLastMonth := firstDayOfMonth(now.AddDate(0, -1, 0))

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t); testAccPreCheckCostCategoryPayerAccount(ctx, t) },
PreCheck: func() { acctest.PreCheck(ctx, t); testAccPreCheckPayerAccount(ctx, t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckCostCategoryDestroy(ctx),
ErrorCheck: acctest.ErrorCheck(t, names.CEServiceID),
Expand Down Expand Up @@ -103,7 +107,7 @@ func TestAccCECostCategory_disappears(t *testing.T) {
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t); testAccPreCheckCostCategoryPayerAccount(ctx, t) },
PreCheck: func() { acctest.PreCheck(ctx, t); testAccPreCheckPayerAccount(ctx, t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckCostCategoryDestroy(ctx),
ErrorCheck: acctest.ErrorCheck(t, names.CEServiceID),
Expand All @@ -127,7 +131,7 @@ func TestAccCECostCategory_complete(t *testing.T) {
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t); testAccPreCheckCostCategoryPayerAccount(ctx, t) },
PreCheck: func() { acctest.PreCheck(ctx, t); testAccPreCheckPayerAccount(ctx, t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckCostCategoryDestroy(ctx),
ErrorCheck: acctest.ErrorCheck(t, names.CEServiceID),
Expand Down Expand Up @@ -162,7 +166,7 @@ func TestAccCECostCategory_notWithAnd(t *testing.T) {
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t); testAccPreCheckCostCategoryPayerAccount(ctx, t) },
PreCheck: func() { acctest.PreCheck(ctx, t); testAccPreCheckPayerAccount(ctx, t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckCostCategoryDestroy(ctx),
ErrorCheck: acctest.ErrorCheck(t, names.CEServiceID),
Expand Down Expand Up @@ -190,7 +194,7 @@ func TestAccCECostCategory_splitCharge(t *testing.T) {
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t); testAccPreCheckCostCategoryPayerAccount(ctx, t) },
PreCheck: func() { acctest.PreCheck(ctx, t); testAccPreCheckPayerAccount(ctx, t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckCostCategoryDestroy(ctx),
ErrorCheck: acctest.ErrorCheck(t, names.CEServiceID),
Expand Down Expand Up @@ -225,7 +229,7 @@ func TestAccCECostCategory_tags(t *testing.T) {
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t); testAccPreCheckCostCategoryPayerAccount(ctx, t) },
PreCheck: func() { acctest.PreCheck(ctx, t); testAccPreCheckPayerAccount(ctx, t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckCostCategoryDestroy(ctx),
ErrorCheck: acctest.ErrorCheck(t, names.CEServiceID),
Expand Down Expand Up @@ -264,14 +268,15 @@ func TestAccCECostCategory_tags(t *testing.T) {
})
}

func testAccPreCheckCostCategoryPayerAccount(ctx context.Context, t *testing.T) {
func testAccPreCheckPayerAccount(ctx context.Context, t *testing.T) {
t.Helper()

conn := acctest.Provider.Meta().(*conns.AWSClient).CEClient(ctx)

_, err := conn.ListCostCategoryDefinitions(ctx, &costexplorer.ListCostCategoryDefinitionsInput{})

if tfawserr.ErrMessageContains(err, "AccessDeniedException", "Linked account doesn't have access to") {
if tfawserr.ErrMessageContains(err, "AccessDeniedException", "Linked account doesn't have access to") ||
tfawserr.ErrMessageContains(err, "ValidationException", "Linked accounts can only create") {
t.Skip("skipping tests; this AWS account must be a payer account")
}

Expand Down
4 changes: 2 additions & 2 deletions internal/service/ce/tags_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestAccCETagsDataSource_basic(t *testing.T) {
endDate := currentTime.Format(formatDate)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t) },
PreCheck: func() { acctest.PreCheck(ctx, t); testAccPreCheckPayerAccount(ctx, t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
ErrorCheck: acctest.ErrorCheck(t, names.CEServiceID),
Steps: []resource.TestStep{
Expand Down Expand Up @@ -58,7 +58,7 @@ func TestAccCETagsDataSource_filter(t *testing.T) {
endDate := currentTime.Format(formatDate)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t) },
PreCheck: func() { acctest.PreCheck(ctx, t); testAccPreCheckPayerAccount(ctx, t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
ErrorCheck: acctest.ErrorCheck(t, names.CEServiceID),
Steps: []resource.TestStep{
Expand Down

0 comments on commit 75720e8

Please sign in to comment.