Skip to content

Commit

Permalink
Grafana: migrate to AWS SDK v2
Browse files Browse the repository at this point in the history
  • Loading branch information
aristosvo committed Jun 4, 2024
1 parent 0eac25e commit 27dbf54
Show file tree
Hide file tree
Showing 22 changed files with 515 additions and 453 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ require (
github.com/aws/aws-sdk-go-v2/service/fms v1.33.5
github.com/aws/aws-sdk-go-v2/service/glacier v1.22.8
github.com/aws/aws-sdk-go-v2/service/globalaccelerator v1.23.5
github.com/aws/aws-sdk-go-v2/service/grafana v1.22.2
github.com/aws/aws-sdk-go-v2/service/groundstation v1.27.4
github.com/aws/aws-sdk-go-v2/service/healthlake v1.24.4
github.com/aws/aws-sdk-go-v2/service/iam v1.32.4
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,8 @@ github.com/aws/aws-sdk-go-v2/service/glacier v1.22.8 h1:BT2IFtBxqoxVk7XeE2/HISi4
github.com/aws/aws-sdk-go-v2/service/glacier v1.22.8/go.mod h1:h7fH8k8flhqe6S0QiQgknLUhdodEGoF2u5bO+7l6vQ4=
github.com/aws/aws-sdk-go-v2/service/globalaccelerator v1.23.5 h1:eewvXGWeFiyVPX45CQyFlj+EgCbHOIptUdk5Ilb0Ios=
github.com/aws/aws-sdk-go-v2/service/globalaccelerator v1.23.5/go.mod h1:641tgeMVfmvuXFc3PVh4I8+Tsag3TzaE/7ojMAjSeoI=
github.com/aws/aws-sdk-go-v2/service/grafana v1.22.2 h1:HhifXEWSEsVYH5UtLDYXFHd851sthOx/xTLG3TwmIzg=
github.com/aws/aws-sdk-go-v2/service/grafana v1.22.2/go.mod h1:KeCVByYsc5xGOkgC2BI7zYgnAHHNYXDHFd2v2SLK/iw=
github.com/aws/aws-sdk-go-v2/service/groundstation v1.27.4 h1:g69GdsU6UvtS+T8C28iXoXb/WbMHDfcpYY/IcGEIKuw=
github.com/aws/aws-sdk-go-v2/service/groundstation v1.27.4/go.mod h1:HAClmwin3MTbLdiUJxhQUc7ZQFi8CQvucP2UzZf5tgw=
github.com/aws/aws-sdk-go-v2/service/healthlake v1.24.4 h1:yAlxjSKPdh/+XKzTXFEsPNnuMK581J9NJvivzeUTp4s=
Expand Down
70 changes: 32 additions & 38 deletions internal/service/grafana/find.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,32 @@ package grafana
import (
"context"

"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/helper/retry"
"github.com/hashicorp/terraform-provider-aws/internal/errs"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
)

func FindLicensedWorkspaceByID(ctx context.Context, conn *managedgrafana.ManagedGrafana, id string) (*managedgrafana.WorkspaceDescription, error) {
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 == nil {
return nil, &retry.NotFoundError{}
}

return output, nil
}

func FindWorkspaceByID(ctx context.Context, conn *managedgrafana.ManagedGrafana, id string) (*managedgrafana.WorkspaceDescription, error) {
input := &managedgrafana.DescribeWorkspaceInput{
func FindWorkspaceByID(ctx context.Context, conn *grafana.Client, id string) (*awstypes.WorkspaceDescription, error) {
input := &grafana.DescribeWorkspaceInput{
WorkspaceId: aws.String(id),
}

output, err := conn.DescribeWorkspaceWithContext(ctx, input)
output, err := conn.DescribeWorkspace(ctx, input)

if tfawserr.ErrCodeEquals(err, managedgrafana.ErrCodeResourceNotFoundException) {
if errs.IsA[*awstypes.ResourceNotFoundException](err) {
return nil, &retry.NotFoundError{
LastError: err,
LastRequest: input,
Expand All @@ -52,14 +49,14 @@ func FindWorkspaceByID(ctx context.Context, conn *managedgrafana.ManagedGrafana,
return output.Workspace, nil
}

func FindSamlConfigurationByID(ctx context.Context, conn *managedgrafana.ManagedGrafana, id string) (*managedgrafana.SamlAuthentication, error) {
input := &managedgrafana.DescribeWorkspaceAuthenticationInput{
func FindSamlConfigurationByID(ctx context.Context, conn *grafana.Client, id string) (*awstypes.SamlAuthentication, error) {
input := &grafana.DescribeWorkspaceAuthenticationInput{
WorkspaceId: aws.String(id),
}

output, err := conn.DescribeWorkspaceAuthenticationWithContext(ctx, input)
output, err := conn.DescribeWorkspaceAuthentication(ctx, input)

if tfawserr.ErrCodeEquals(err, managedgrafana.ErrCodeResourceNotFoundException) {
if errs.IsA[*awstypes.ResourceNotFoundException](err) {
return nil, &retry.NotFoundError{
LastError: err,
LastRequest: input,
Expand All @@ -74,46 +71,43 @@ func FindSamlConfigurationByID(ctx context.Context, conn *managedgrafana.Managed
return nil, tfresource.NewEmptyResultError(input)
}

if status := aws.StringValue(output.Authentication.Saml.Status); status == managedgrafana.SamlConfigurationStatusNotConfigured {
if status := output.Authentication.Saml.Status; status == awstypes.SamlConfigurationStatusNotConfigured {
return nil, &retry.NotFoundError{
Message: status,
Message: string(status),
LastRequest: input,
}
}

return output.Authentication.Saml, nil
}

func FindRoleAssociationsByRoleAndWorkspaceID(ctx context.Context, conn *managedgrafana.ManagedGrafana, role string, workspaceID string) (map[string][]string, error) {
input := &managedgrafana.ListPermissionsInput{
func FindRoleAssociationsByRoleAndWorkspaceID(ctx context.Context, conn *grafana.Client, role string, workspaceID string) (map[string][]string, error) {
input := &grafana.ListPermissionsInput{
WorkspaceId: aws.String(workspaceID),
}
output := make(map[string][]string, 0)

err := conn.ListPermissionsPagesWithContext(ctx, input, func(page *managedgrafana.ListPermissionsOutput, lastPage bool) bool {
if page == nil {
return !lastPage
}
pages := grafana.NewListPermissionsPaginator(conn, input)
for pages.HasMorePages() {
page, err := pages.NextPage(ctx)

for _, v := range page.Permissions {
if aws.StringValue(v.Role) == role {
userType := aws.StringValue(v.User.Type)
output[userType] = append(output[userType], aws.StringValue(v.User.Id))
if errs.IsA[*awstypes.ResourceNotFoundException](err) {
return nil, &retry.NotFoundError{
LastError: err,
LastRequest: input,
}
}

return !lastPage
})

if tfawserr.ErrCodeEquals(err, managedgrafana.ErrCodeResourceNotFoundException) {
return nil, &retry.NotFoundError{
LastError: err,
LastRequest: input,
if err != nil {
return output, err
}
}

if err != nil {
return nil, err
for _, v := range page.Permissions {
if string(v.Role) == role {
userType := string(v.User.Type)
output[userType] = append(output[userType], aws.ToString(v.User.Id))
}
}
}

if len(output) == 0 {
Expand Down
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 -ListTags -ServiceTagsMap -UpdateTags -AWSSDKVersion=2 -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
55 changes: 37 additions & 18 deletions internal/service/grafana/license_association.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ 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"
)
Expand Down Expand Up @@ -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)),
input := &grafana.AssociateLicenseInput{
LicenseType: awstypes.LicenseType(d.Get("license_type").(string)),
WorkspaceId: aws.String(d.Get("workspace_id").(string)),
}

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)
}

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,7 +88,7 @@ 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())

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,20 @@ func resourceLicenseAssociationDelete(ctx context.Context, d *schema.ResourceDat

return diags
}

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: statusWorkspaceStatus(ctx, conn, id),
Timeout: timeout,
}

outputRaw, err := stateConf.WaitForStateContext(ctx)

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

return nil, err
}
15 changes: 9 additions & 6 deletions internal/service/grafana/license_association_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"fmt"
"testing"

"github.com/aws/aws-sdk-go/service/managedgrafana"
awstypes "github.com/aws/aws-sdk-go-v2/service/grafana/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 @@ -26,17 +26,20 @@ func testAccLicenseAssociation_freeTrial(t *testing.T) {
workspaceResourceName := "aws_grafana_workspace.test"

resource.Test(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t); acctest.PreCheckPartitionHasService(t, managedgrafana.EndpointsID) },
PreCheck: func() {
acctest.PreCheck(ctx, t)
acctest.PreCheckPartitionHasService(t, names.ManagedGrafanaEndpointID)
},
ErrorCheck: acctest.ErrorCheck(t, names.GrafanaServiceID),
CheckDestroy: testAccCheckLicenseAssociationDestroy(ctx),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccLicenseAssociationConfig_basic(rName, managedgrafana.LicenseTypeEnterpriseFreeTrial),
Config: testAccLicenseAssociationConfig_basic(rName, string(awstypes.LicenseTypeEnterpriseFreeTrial)),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckLicenseAssociationExists(ctx, resourceName),
resource.TestCheckResourceAttrSet(resourceName, "free_trial_expiration"),
resource.TestCheckResourceAttr(resourceName, "license_type", managedgrafana.LicenseTypeEnterpriseFreeTrial),
resource.TestCheckResourceAttr(resourceName, "license_type", string(awstypes.LicenseTypeEnterpriseFreeTrial)),
resource.TestCheckResourceAttrPair(resourceName, "workspace_id", workspaceResourceName, names.AttrID),
),
},
Expand Down Expand Up @@ -69,7 +72,7 @@ func testAccCheckLicenseAssociationExists(ctx context.Context, name string) reso
return fmt.Errorf("No Grafana Workspace ID is set")
}

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

_, err := tfgrafana.FindLicensedWorkspaceByID(ctx, conn, rs.Primary.ID)

Expand All @@ -79,7 +82,7 @@ func testAccCheckLicenseAssociationExists(ctx context.Context, name string) reso

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

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

0 comments on commit 27dbf54

Please sign in to comment.