Skip to content

Commit

Permalink
costexplorer: Migrate to AWS SDK v2
Browse files Browse the repository at this point in the history
  • Loading branch information
mattburgess committed Apr 7, 2024
1 parent ab1378f commit 3f5e993
Show file tree
Hide file tree
Showing 23 changed files with 358 additions and 368 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ require (
github.com/aws/aws-sdk-go-v2/service/connectcases v1.15.4
github.com/aws/aws-sdk-go-v2/service/controltower v1.13.4
github.com/aws/aws-sdk-go-v2/service/costandusagereportservice v1.23.4
github.com/aws/aws-sdk-go-v2/service/costexplorer v1.37.1
github.com/aws/aws-sdk-go-v2/service/costoptimizationhub v1.4.4
github.com/aws/aws-sdk-go-v2/service/customerprofiles v1.36.4
github.com/aws/aws-sdk-go-v2/service/datazone v1.8.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ github.com/aws/aws-sdk-go-v2/service/controltower v1.13.4 h1:vUHPzud4ZEbPNCBxRsr
github.com/aws/aws-sdk-go-v2/service/controltower v1.13.4/go.mod h1:qwJIgEG0ASp7utTqwiagEW0LOE6AFsNzQL1oOWRsydU=
github.com/aws/aws-sdk-go-v2/service/costandusagereportservice v1.23.4 h1:MDEvMVWZZetTacemmH+Z7ScvQkOKxqkEQfFROo//G18=
github.com/aws/aws-sdk-go-v2/service/costandusagereportservice v1.23.4/go.mod h1:DSbQUgLN9rHicCWE2Wuu7yRNHT3oQvnOiPk3LAGZe9I=
github.com/aws/aws-sdk-go-v2/service/costexplorer v1.37.1 h1:xjhk+io+kPtDOG5RizvHlkGKET3dxRBzorLdPPkpZQc=
github.com/aws/aws-sdk-go-v2/service/costexplorer v1.37.1/go.mod h1:uLOg0o57AyQQhZGtUKIlcBJOKE53mO9bXKyrM9dFhy4=
github.com/aws/aws-sdk-go-v2/service/costoptimizationhub v1.4.4 h1:gSO6kMlH4cXxBmZwTA1qngTVxt8Och7irFtNGrxIUEg=
github.com/aws/aws-sdk-go-v2/service/costoptimizationhub v1.4.4/go.mod h1:UkyRWEyu3iT7oPmPri8xwPnKXqJQzSUDK9MOKq7xyZE=
github.com/aws/aws-sdk-go-v2/service/customerprofiles v1.36.4 h1:UBo3t3uliQIP3f8duZhmJ1Z62bz/j5o7LH8f/BTt1mU=
Expand Down
6 changes: 3 additions & 3 deletions internal/conns/awsclient_gen.go

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

60 changes: 31 additions & 29 deletions internal/service/ce/anomaly_monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@ import (
"encoding/json"

"github.com/YakDriver/regexache"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/costexplorer"
"github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/costexplorer"
awstypes "github.com/aws/aws-sdk-go-v2/service/costexplorer/types"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/structure"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
"github.com/hashicorp/terraform-provider-aws/internal/create"
"github.com/hashicorp/terraform-provider-aws/internal/enum"
"github.com/hashicorp/terraform-provider-aws/internal/errs"
"github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag"
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
Expand All @@ -41,10 +43,10 @@ func ResourceAnomalyMonitor() *schema.Resource {
Computed: true,
},
"monitor_dimension": {
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"monitor_specification"},
ValidateFunc: validation.StringInSlice(costexplorer.MonitorDimension_Values(), false),
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"monitor_specification"},
ValidateDiagFunc: enum.Validate[awstypes.MonitorDimension](),
},
"name": {
Type: schema.TypeString,
Expand All @@ -62,10 +64,10 @@ func ResourceAnomalyMonitor() *schema.Resource {
ConflictsWith: []string{"monitor_dimension"},
},
"monitor_type": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice(costexplorer.MonitorType_Values(), false),
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateDiagFunc: enum.Validate[awstypes.MonitorType](),
},
names.AttrTags: tftags.TagsSchema(),
names.AttrTagsAll: tftags.TagsSchemaComputed(),
Expand All @@ -78,37 +80,37 @@ func ResourceAnomalyMonitor() *schema.Resource {
func resourceAnomalyMonitorCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics

conn := meta.(*conns.AWSClient).CEConn(ctx)
conn := meta.(*conns.AWSClient).CEClient(ctx)

input := &costexplorer.CreateAnomalyMonitorInput{
AnomalyMonitor: &costexplorer.AnomalyMonitor{
AnomalyMonitor: &awstypes.AnomalyMonitor{
MonitorName: aws.String(d.Get("name").(string)),
MonitorType: aws.String(d.Get("monitor_type").(string)),
MonitorType: awstypes.MonitorType(d.Get("monitor_type").(string)),
},
ResourceTags: getTagsIn(ctx),
}
switch d.Get("monitor_type").(string) {
case costexplorer.MonitorTypeDimensional:
switch awstypes.MonitorType(d.Get("monitor_type").(string)) {
case awstypes.MonitorTypeDimensional:
if v, ok := d.GetOk("monitor_dimension"); ok {
input.AnomalyMonitor.MonitorDimension = aws.String(v.(string))
input.AnomalyMonitor.MonitorDimension = awstypes.MonitorDimension(v.(string))
} else {
return sdkdiag.AppendErrorf(diags, "If Monitor Type is %s, dimension attrribute is required", costexplorer.MonitorTypeDimensional)
return sdkdiag.AppendErrorf(diags, "If Monitor Type is %s, dimension attrribute is required", awstypes.MonitorTypeDimensional)
}
case costexplorer.MonitorTypeCustom:
case awstypes.MonitorTypeCustom:
if v, ok := d.GetOk("monitor_specification"); ok {
expression := costexplorer.Expression{}
expression := awstypes.Expression{}

if err := json.Unmarshal([]byte(v.(string)), &expression); err != nil {
return sdkdiag.AppendErrorf(diags, "parsing specification: %s", err)
}

input.AnomalyMonitor.MonitorSpecification = &expression
} else {
return sdkdiag.AppendErrorf(diags, "If Monitor Type is %s, dimension attrribute is required", costexplorer.MonitorTypeCustom)
return sdkdiag.AppendErrorf(diags, "If Monitor Type is %s, dimension attrribute is required", awstypes.MonitorTypeCustom)
}
}

resp, err := conn.CreateAnomalyMonitorWithContext(ctx, input)
resp, err := conn.CreateAnomalyMonitor(ctx, input)

if err != nil {
return sdkdiag.AppendErrorf(diags, "creating Anomaly Monitor: %s", err)
Expand All @@ -118,15 +120,15 @@ func resourceAnomalyMonitorCreate(ctx context.Context, d *schema.ResourceData, m
return sdkdiag.AppendErrorf(diags, "creating Cost Explorer Anomaly Monitor resource (%s): empty output", d.Get("name").(string))
}

d.SetId(aws.StringValue(resp.MonitorArn))
d.SetId(aws.ToString(resp.MonitorArn))

return append(diags, resourceAnomalyMonitorRead(ctx, d, meta)...)
}

func resourceAnomalyMonitorRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics

conn := meta.(*conns.AWSClient).CEConn(ctx)
conn := meta.(*conns.AWSClient).CEClient(ctx)

monitor, err := FindAnomalyMonitorByARN(ctx, conn, d.Id())

Expand Down Expand Up @@ -165,7 +167,7 @@ func resourceAnomalyMonitorRead(ctx context.Context, d *schema.ResourceData, met
func resourceAnomalyMonitorUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics

conn := meta.(*conns.AWSClient).CEConn(ctx)
conn := meta.(*conns.AWSClient).CEClient(ctx)
requestUpdate := false

input := &costexplorer.UpdateAnomalyMonitorInput{
Expand All @@ -178,7 +180,7 @@ func resourceAnomalyMonitorUpdate(ctx context.Context, d *schema.ResourceData, m
}

if requestUpdate {
_, err := conn.UpdateAnomalyMonitorWithContext(ctx, input)
_, err := conn.UpdateAnomalyMonitor(ctx, input)

if err != nil {
return create.AppendDiagError(diags, names.CE, create.ErrActionUpdating, ResNameAnomalyMonitor, d.Id(), err)
Expand All @@ -191,11 +193,11 @@ func resourceAnomalyMonitorUpdate(ctx context.Context, d *schema.ResourceData, m
func resourceAnomalyMonitorDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics

conn := meta.(*conns.AWSClient).CEConn(ctx)
conn := meta.(*conns.AWSClient).CEClient(ctx)

_, err := conn.DeleteAnomalyMonitorWithContext(ctx, &costexplorer.DeleteAnomalyMonitorInput{MonitorArn: aws.String(d.Id())})
_, err := conn.DeleteAnomalyMonitor(ctx, &costexplorer.DeleteAnomalyMonitorInput{MonitorArn: aws.String(d.Id())})

if err != nil && tfawserr.ErrCodeEquals(err, costexplorer.ErrCodeUnknownMonitorException) {
if err != nil && errs.IsA[*awstypes.UnknownMonitorException](err) {
return diags
}

Expand Down
18 changes: 9 additions & 9 deletions internal/service/ce/anomaly_monitor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"testing"

"github.com/YakDriver/regexache"
"github.com/aws/aws-sdk-go/service/costexplorer"
awstypes "github.com/aws/aws-sdk-go-v2/service/costexplorer/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"
Expand All @@ -24,7 +24,7 @@ import (

func TestAccCEAnomalyMonitor_basic(t *testing.T) {
ctx := acctest.Context(t)
var monitor costexplorer.AnomalyMonitor
var monitor awstypes.AnomalyMonitor
resourceName := "aws_ce_anomaly_monitor.test"
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)

Expand Down Expand Up @@ -55,7 +55,7 @@ func TestAccCEAnomalyMonitor_basic(t *testing.T) {

func TestAccCEAnomalyMonitor_disappears(t *testing.T) {
ctx := acctest.Context(t)
var monitor costexplorer.AnomalyMonitor
var monitor awstypes.AnomalyMonitor
resourceName := "aws_ce_anomaly_monitor.test"
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)

Expand All @@ -79,7 +79,7 @@ func TestAccCEAnomalyMonitor_disappears(t *testing.T) {

func TestAccCEAnomalyMonitor_update(t *testing.T) {
ctx := acctest.Context(t)
var monitor costexplorer.AnomalyMonitor
var monitor awstypes.AnomalyMonitor
resourceName := "aws_ce_anomaly_monitor.test"
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
rName2 := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
Expand Down Expand Up @@ -115,7 +115,7 @@ func TestAccCEAnomalyMonitor_update(t *testing.T) {

func TestAccCEAnomalyMonitor_tags(t *testing.T) {
ctx := acctest.Context(t)
var monitor costexplorer.AnomalyMonitor
var monitor awstypes.AnomalyMonitor
resourceName := "aws_ce_anomaly_monitor.test"
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)

Expand Down Expand Up @@ -164,7 +164,7 @@ func TestAccCEAnomalyMonitor_tags(t *testing.T) {
// following test in a serial test
func TestAccCEAnomalyMonitor_Dimensional(t *testing.T) {
ctx := acctest.Context(t)
var monitor costexplorer.AnomalyMonitor
var monitor awstypes.AnomalyMonitor
resourceName := "aws_ce_anomaly_monitor.test"
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)

Expand Down Expand Up @@ -192,9 +192,9 @@ func TestAccCEAnomalyMonitor_Dimensional(t *testing.T) {
})
}

func testAccCheckAnomalyMonitorExists(ctx context.Context, n string, anomalyMonitor *costexplorer.AnomalyMonitor) resource.TestCheckFunc {
func testAccCheckAnomalyMonitorExists(ctx context.Context, n string, anomalyMonitor *awstypes.AnomalyMonitor) resource.TestCheckFunc {
return func(s *terraform.State) error {
conn := acctest.Provider.Meta().(*conns.AWSClient).CEConn(ctx)
conn := acctest.Provider.Meta().(*conns.AWSClient).CEClient(ctx)

rs, ok := s.RootModule().Resources[n]
if !ok {
Expand Down Expand Up @@ -223,7 +223,7 @@ func testAccCheckAnomalyMonitorExists(ctx context.Context, n string, anomalyMoni

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

for _, rs := range s.RootModule().Resources {
if rs.Type != "aws_ce_anomaly_monitor" {
Expand Down
Loading

0 comments on commit 3f5e993

Please sign in to comment.