Skip to content

Commit

Permalink
Merge pull request #37831 from aristosvo/sdkv2/grafana
Browse files Browse the repository at this point in the history
Grafana: migrate to AWS SDK v2
  • Loading branch information
ewbankkit authored Jun 28, 2024
2 parents 6003b07 + 585c745 commit cdb7f5d
Show file tree
Hide file tree
Showing 28 changed files with 1,402 additions and 1,305 deletions.
381 changes: 191 additions & 190 deletions go.mod

Large diffs are not rendered by default.

762 changes: 382 additions & 380 deletions go.sum

Large diffs are not rendered by default.

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.

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

package grafana

import (
"errors"
"fmt"

"github.com/aws/aws-sdk-go-v2/aws"
awstypes "github.com/aws/aws-sdk-go-v2/service/grafana/types"
tfslices "github.com/hashicorp/terraform-provider-aws/internal/slices"
)

func updatesError(apiObjects []awstypes.UpdateError) error {
errs := tfslices.ApplyToAll(apiObjects, func(v awstypes.UpdateError) error {
return updateError(&v)
})

return errors.Join(errs...)
}

func updateError(apiObject *awstypes.UpdateError) error {
if apiObject == nil {
return nil
}

return fmt.Errorf("%d: %s", aws.ToInt32(apiObject.Code), aws.ToString(apiObject.Message))
}
18 changes: 18 additions & 0 deletions internal/service/grafana/exports_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package grafana

// Exports for use in tests only.
var (
ResourceLicenseAssociation = resourceLicenseAssociation
ResourceRoleAssociation = resourceRoleAssociation
ResourceWorkspace = resourceWorkspace
ResourceWorkspaceAPIKey = resourceWorkspaceAPIKey
ResourceWorkspaceSAMLConfiguration = resourceWorkspaceSAMLConfiguration

FindLicensedWorkspaceByID = findLicensedWorkspaceByID
FindRoleAssociationsByTwoPartKey = findRoleAssociationsByTwoPartKey
FindSAMLConfigurationByID = findSAMLConfigurationByID
FindWorkspaceByID = findWorkspaceByID
)
124 changes: 0 additions & 124 deletions internal/service/grafana/find.go

This file was deleted.

2 changes: 1 addition & 1 deletion internal/service/grafana/generate.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

//go:generate go run ../../generate/tags/main.go -ListTags -ServiceTagsMap -UpdateTags
//go:generate go run ../../generate/tags/main.go -AWSSDKVersion=2 -ListTags -ServiceTagsMap -UpdateTags -KVTValues -SkipTypesImp
//go:generate go run ../../generate/servicepackage/main.go
// ONLY generate directives and package declaration! Do not add anything else to this file.

Expand Down
81 changes: 57 additions & 24 deletions internal/service/grafana/license_association.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,21 @@ import (
"log"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/managedgrafana"
"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/grafana"
awstypes "github.com/aws/aws-sdk-go-v2/service/grafana/types"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
"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"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
)

// @SDKResource("aws_grafana_license_association")
func ResourceLicenseAssociation() *schema.Resource {
// @SDKResource("aws_grafana_license_association", name="License Association")
func resourceLicenseAssociation() *schema.Resource {
return &schema.Resource{
CreateWithoutTimeout: resourceLicenseAssociationCreate,
ReadWithoutTimeout: resourceLicenseAssociationRead,
Expand All @@ -45,10 +47,10 @@ func ResourceLicenseAssociation() *schema.Resource {
Computed: true,
},
"license_type": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice(managedgrafana.LicenseType_Values(), false),
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateDiagFunc: enum.Validate[awstypes.LicenseType](),
},
"workspace_id": {
Type: schema.TypeString,
Expand All @@ -61,21 +63,21 @@ func ResourceLicenseAssociation() *schema.Resource {

func resourceLicenseAssociationCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).GrafanaConn(ctx)
conn := meta.(*conns.AWSClient).GrafanaClient(ctx)

input := &managedgrafana.AssociateLicenseInput{
LicenseType: aws.String(d.Get("license_type").(string)),
WorkspaceId: aws.String(d.Get("workspace_id").(string)),
workspaceID := d.Get("workspace_id").(string)
input := &grafana.AssociateLicenseInput{
LicenseType: awstypes.LicenseType(d.Get("license_type").(string)),
WorkspaceId: aws.String(workspaceID),
}

log.Printf("[DEBUG] Creating Grafana License Association: %s", input)
output, err := conn.AssociateLicenseWithContext(ctx, input)
output, err := conn.AssociateLicense(ctx, input)

if err != nil {
return sdkdiag.AppendErrorf(diags, "creating Grafana License Association: %s", err)
return sdkdiag.AppendErrorf(diags, "creating Grafana License Association (%s): %s", workspaceID, err)
}

d.SetId(aws.StringValue(output.Workspace.Id))
d.SetId(aws.ToString(output.Workspace.Id))

if _, err := waitLicenseAssociationCreated(ctx, conn, d.Id(), d.Timeout(schema.TimeoutCreate)); err != nil {
return sdkdiag.AppendErrorf(diags, "waiting for Grafana License Association (%s) create: %s", d.Id(), err)
Expand All @@ -86,9 +88,9 @@ func resourceLicenseAssociationCreate(ctx context.Context, d *schema.ResourceDat

func resourceLicenseAssociationRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).GrafanaConn(ctx)
conn := meta.(*conns.AWSClient).GrafanaClient(ctx)

workspace, err := FindLicensedWorkspaceByID(ctx, conn, d.Id())
workspace, err := findLicensedWorkspaceByID(ctx, conn, d.Id())

if tfresource.NotFound(err) && !d.IsNewResource() {
log.Printf("[WARN] Grafana License Association (%s) not found, removing from state", d.Id())
Expand Down Expand Up @@ -118,15 +120,15 @@ func resourceLicenseAssociationRead(ctx context.Context, d *schema.ResourceData,

func resourceLicenseAssociationDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).GrafanaConn(ctx)
conn := meta.(*conns.AWSClient).GrafanaClient(ctx)

log.Printf("[DEBUG] Deleting Grafana License Association: %s", d.Id())
_, err := conn.DisassociateLicenseWithContext(ctx, &managedgrafana.DisassociateLicenseInput{
LicenseType: aws.String(d.Get("license_type").(string)),
_, err := conn.DisassociateLicense(ctx, &grafana.DisassociateLicenseInput{
LicenseType: awstypes.LicenseType(d.Get("license_type").(string)),
WorkspaceId: aws.String(d.Id()),
})

if tfawserr.ErrCodeEquals(err, managedgrafana.ErrCodeResourceNotFoundException) {
if errs.IsA[*awstypes.ResourceNotFoundException](err) {
return diags
}

Expand All @@ -140,3 +142,34 @@ func resourceLicenseAssociationDelete(ctx context.Context, d *schema.ResourceDat

return diags
}

func findLicensedWorkspaceByID(ctx context.Context, conn *grafana.Client, id string) (*awstypes.WorkspaceDescription, error) {
output, err := findWorkspaceByID(ctx, conn, id)

if err != nil {
return nil, err
}

if output.LicenseType == "" {
return nil, &retry.NotFoundError{}
}

return output, nil
}

func waitLicenseAssociationCreated(ctx context.Context, conn *grafana.Client, id string, timeout time.Duration) (*awstypes.WorkspaceDescription, error) {
stateConf := &retry.StateChangeConf{
Pending: enum.Slice(awstypes.WorkspaceStatusUpgrading),
Target: enum.Slice(awstypes.WorkspaceStatusActive),
Refresh: statusWorkspace(ctx, conn, id),
Timeout: timeout,
}

outputRaw, err := stateConf.WaitForStateContext(ctx)

if output, ok := outputRaw.(*awstypes.WorkspaceDescription); ok {
return output, err
}

return nil, err
}
Loading

0 comments on commit cdb7f5d

Please sign in to comment.