Skip to content
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

feat: added billing view and initial test #41075

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
567 changes: 567 additions & 0 deletions internal/service/billing/billing_view.go

Large diffs are not rendered by default.

228 changes: 228 additions & 0 deletions internal/service/billing/billing_view_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package billing_test

import (
"context"
"errors"
"fmt"
"testing"

"github.com/aws/aws-sdk-go-v2/service/billing"
awstypes "github.com/aws/aws-sdk-go-v2/service/billing/types"
sdkacctest "github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"
"github.com/hashicorp/terraform-provider-aws/internal/acctest"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
"github.com/hashicorp/terraform-provider-aws/internal/create"
tfbilling "github.com/hashicorp/terraform-provider-aws/internal/service/billing"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
"github.com/hashicorp/terraform-provider-aws/names"
)

func TestAccBillingView_basic(t *testing.T) {
ctx := acctest.Context(t)

var view awstypes.BillingViewElement
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_billing_view.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
acctest.PreCheck(ctx, t)
testAccPreCheck(ctx, t)
},
ErrorCheck: acctest.ErrorCheck(t, names.BillingServiceID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckViewDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccViewConfig_basic(rName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckViewExists(ctx, resourceName, &view),
resource.TestCheckResourceAttr(resourceName, names.AttrName, rName),
resource.TestCheckResourceAttr(resourceName, names.AttrDescription, "Test description"),
acctest.CheckResourceAttrContains(resourceName, names.AttrARN, "billingview/custom-"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateIdFunc: acctest.AttrImportStateIdFunc(resourceName, names.AttrARN),
ImportStateVerify: true,
ImportStateVerifyIdentifierAttribute: names.AttrARN,
},
},
})
}

func TestAccBillingView_update(t *testing.T) {
ctx := acctest.Context(t)

var view awstypes.BillingViewElement
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_billing_view.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
acctest.PreCheck(ctx, t)
testAccPreCheck(ctx, t)
},
ErrorCheck: acctest.ErrorCheck(t, names.BillingServiceID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckViewDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccViewConfig_update(rName, "Test description"),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckViewExists(ctx, resourceName, &view),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateIdFunc: acctest.AttrImportStateIdFunc(resourceName, names.AttrARN),
ImportStateVerifyIdentifierAttribute: names.AttrARN,
},
{
Config: testAccViewConfig_update(fmt.Sprintf("%s-updated", rName), "Test description updated"),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckViewExists(ctx, resourceName, &view),
resource.TestCheckResourceAttr(resourceName, names.AttrName, fmt.Sprintf("%s-updated", rName)),
resource.TestCheckResourceAttr(resourceName, names.AttrDescription, "Test description updated"),
acctest.CheckResourceAttrContains(resourceName, names.AttrARN, "billingview/custom-"),
),
},
},
})
}

func TestAccBillingView_disappears(t *testing.T) {
ctx := acctest.Context(t)
if testing.Short() {
t.Skip("skipping long-running test in short mode")
}

var view awstypes.BillingViewElement
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_billing_view.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
acctest.PreCheck(ctx, t)
testAccPreCheck(ctx, t)
},
ErrorCheck: acctest.ErrorCheck(t, names.BillingServiceID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckViewDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccViewConfig_basic(rName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckViewExists(ctx, resourceName, &view),
acctest.CheckFrameworkResourceDisappears(ctx, acctest.Provider, tfbilling.ResourceView, resourceName),
),
ExpectNonEmptyPlan: true,
},
},
})
}

func testAccCheckViewDestroy(ctx context.Context) resource.TestCheckFunc {
return func(s *terraform.State) error {
conn := acctest.Provider.Meta().(*conns.AWSClient).BillingClient(ctx)

for _, rs := range s.RootModule().Resources {
if rs.Type != "aws_billing_view" {
continue
}

arn := rs.Primary.Attributes[names.AttrARN]
if arn == "" {
return create.Error(names.Billing, create.ErrActionCheckingExistence, tfbilling.ResNameView, rs.Primary.ID, errors.New("no ARN is set"))
}

_, err := tfbilling.FindViewByARN(ctx, conn, arn)
if tfresource.NotFound(err) {
return nil
}
if err != nil {
return create.Error(names.Billing, create.ErrActionCheckingDestroyed, tfbilling.ResNameView, rs.Primary.ID, err)
}

return create.Error(names.Billing, create.ErrActionCheckingDestroyed, tfbilling.ResNameView, rs.Primary.ID, errors.New("not destroyed"))
}

return nil
}
}

func testAccCheckViewExists(ctx context.Context, name string, view *awstypes.BillingViewElement) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[name]
if !ok {
return create.Error(names.Billing, create.ErrActionCheckingExistence, tfbilling.ResNameView, name, errors.New("not found"))
}

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

arn := rs.Primary.Attributes[names.AttrARN]
if arn == "" {
return create.Error(names.Billing, create.ErrActionCheckingExistence, tfbilling.ResNameView, rs.Primary.ID, errors.New("no ARN is set"))
}

resp, err := tfbilling.FindViewByARN(ctx, conn, arn)
if err != nil {
return create.Error(names.Billing, create.ErrActionCheckingExistence, tfbilling.ResNameView, rs.Primary.ID, err)
}

*view = *resp

return nil
}
}

func testAccPreCheck(ctx context.Context, t *testing.T) {
conn := acctest.Provider.Meta().(*conns.AWSClient).BillingClient(ctx)

input := &billing.ListBillingViewsInput{}

_, err := conn.ListBillingViews(ctx, input)

if acctest.PreCheckSkipError(err) {
t.Skipf("skipping acceptance testing: %s", err)
}
if err != nil {
t.Fatalf("unexpected PreCheck error: %s", err)
}
}

func testAccViewConfig_base() string {
return `
data "aws_caller_identity" "current" {}
data "aws_partition" "current" {}
`
}

func testAccViewConfig_basic(rName string) string {
return acctest.ConfigCompose(testAccViewConfig_base(), fmt.Sprintf(`
resource "aws_billing_view" "test" {
name = "%s"
description = "Test description"
source_views = ["arn:${data.aws_partition.current.partition}:billing::${data.aws_caller_identity.current.account_id}:billingview/primary"]
}
`, rName))
}

func testAccViewConfig_update(rName, description string) string {
return acctest.ConfigCompose(testAccViewConfig_base(), fmt.Sprintf(`
resource "aws_billing_view" "test" {
name = %[1]q
description = %[2]q
source_views = ["arn:${data.aws_partition.current.partition}:billing::${data.aws_caller_identity.current.account_id}:billingview/primary"]
}
`, rName, description))
}
9 changes: 9 additions & 0 deletions internal/service/billing/exports_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package billing

var (
FindViewByARN = findViewByARN
ResourceView = newResourceView
)
1 change: 1 addition & 0 deletions internal/service/billing/generate.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

//go:generate go run ../../generate/tags/main.go -TagType=ResourceTag -GetTag -ListTags -ListTagsOutTagsElem=ResourceTags -ServiceTagsSlice -TagOp=TagResource -TagInTagsElem=ResourceTags -UpdateTags -UntagInTagsElem=ResourceTagKeys -CreateTags -ParentNotFoundErrCode=InvalidParameterException "-ParentNotFoundErrMsg=The specified cluster is inactive. Specify an active cluster and try again."
//go:generate go run ../../generate/servicepackage/main.go
// ONLY generate directives and package declaration! Do not add anything else to this file.

Expand Down
8 changes: 7 additions & 1 deletion internal/service/billing/service_package_gen.go

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

Loading
Loading