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 6, 2024
1 parent e1a799b commit 0ad8c2b
Show file tree
Hide file tree
Showing 23 changed files with 510 additions and 458 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ require (
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.8 // indirect
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.0 // indirect
github.com/aws/aws-sdk-go-v2/internal/v4a v1.3.8 // indirect
github.com/aws/aws-sdk-go-v2/service/grafana v1.22.3 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.11.2 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.3.10 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/endpoint-discovery v1.9.9 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,8 @@ github.com/aws/aws-sdk-go-v2/service/glacier v1.22.9 h1:VEV/9IVykgeCD76wE+MRvSB8
github.com/aws/aws-sdk-go-v2/service/glacier v1.22.9/go.mod h1:YhA4aSfqiqBGagXxuT6jf8zQ79Yb3abiZpdtRiIljpY=
github.com/aws/aws-sdk-go-v2/service/globalaccelerator v1.23.6 h1:/4Ha4fN6d/onv04rPmKVxShhJj3FVKFa3sfc5ZX3bCQ=
github.com/aws/aws-sdk-go-v2/service/globalaccelerator v1.23.6/go.mod h1:45UdEiD/JbcgBVwcLZZd752sAutrndMdvNop/3js2oI=
github.com/aws/aws-sdk-go-v2/service/grafana v1.22.3 h1:MrXjRKJ8kVrEo3pK0rhcMhhPoSI/Wfa8I94tVoHGAW4=
github.com/aws/aws-sdk-go-v2/service/grafana v1.22.3/go.mod h1:p78W3TxlAFTSAWNm8ibtwBDJbtSqIovC5Ruicx8PcY8=
github.com/aws/aws-sdk-go-v2/service/groundstation v1.27.5 h1:mNgrkKH+6B1dtbkzSVA8qf9fR9lMg+mUEYUBNnWeWyU=
github.com/aws/aws-sdk-go-v2/service/groundstation v1.27.5/go.mod h1:a8x2abmJoP3bGTTsX2LYIIyj4tMKLG8ophLxHBF6utM=
github.com/aws/aws-sdk-go-v2/service/healthlake v1.24.5 h1:3BYfEzrCaDWyUOMDypGp6GyFYl3xYbAU88LQr4cTj4k=
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
57 changes: 38 additions & 19 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)
log.Printf("[DEBUG] Creating Grafana License Association: %v", 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 0ad8c2b

Please sign in to comment.