Skip to content

Commit

Permalink
Merge pull request hashicorp#38743 from carlosrodgut/f-grafana-new-li…
Browse files Browse the repository at this point in the history
…censing-model

Amazon Managed Grafana new licensing model
  • Loading branch information
ewbankkit authored Aug 8, 2024
2 parents 0aba279 + bdbc7e4 commit 5c2b9db
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .changelog/38743.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/aws_grafana_license_association: Add `grafana_token` argument
```
3 changes: 2 additions & 1 deletion internal/service/grafana/grafana_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ func TestAccGrafana_serial(t *testing.T) {
acctest.CtBasic: testAccWorkspaceDataSource_basic,
},
"LicenseAssociation": {
"enterpriseFreeTrial": testAccLicenseAssociation_freeTrial,
"enterpriseFreeTrial": testAccLicenseAssociation_freeTrial,
"enterpriseGrafanaToken": testAccLicenseAssociation_enterpriseToken,
},
"SamlConfiguration": {
acctest.CtBasic: testAccWorkspaceSAMLConfiguration_basic,
Expand Down
12 changes: 12 additions & 0 deletions internal/service/grafana/license_association.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"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"
Expand Down Expand Up @@ -42,6 +43,12 @@ func resourceLicenseAssociation() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"grafana_token": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: validation.IsUUID,
},
"license_expiration": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -71,6 +78,10 @@ func resourceLicenseAssociationCreate(ctx context.Context, d *schema.ResourceDat
WorkspaceId: aws.String(workspaceID),
}

if v, ok := d.GetOk("grafana_token"); ok {
input.GrafanaToken = aws.String(v.(string))
}

output, err := conn.AssociateLicense(ctx, input)

if err != nil {
Expand Down Expand Up @@ -107,6 +118,7 @@ func resourceLicenseAssociationRead(ctx context.Context, d *schema.ResourceData,
} else {
d.Set("free_trial_expiration", nil)
}
d.Set("grafana_token", workspace.GrafanaToken)
if workspace.LicenseExpiration != nil {
d.Set("license_expiration", workspace.LicenseExpiration.Format(time.RFC3339))
} else {
Expand Down
46 changes: 46 additions & 0 deletions internal/service/grafana/license_association_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@ import (
"fmt"
"testing"

"github.com/YakDriver/regexache"
awstypes "github.com/aws/aws-sdk-go-v2/service/grafana/types"
uuid "github.com/hashicorp/go-uuid"
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"
tfgrafana "github.com/hashicorp/terraform-provider-aws/internal/service/grafana"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
"github.com/hashicorp/terraform-provider-aws/internal/verify"
"github.com/hashicorp/terraform-provider-aws/names"
)

Expand Down Expand Up @@ -60,6 +63,49 @@ resource "aws_grafana_license_association" "test" {
`, licenseType))
}

func testAccLicenseAssociation_enterpriseToken(t *testing.T) {
acctest.Skip(t, "Grafana token is invalid")

ctx := acctest.Context(t)
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_grafana_license_association.test"
workspaceResourceName := "aws_grafana_workspace.test"
uuidGrafanaToken, _ := uuid.GenerateUUID()

resource.Test(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t); acctest.PreCheckPartitionHasService(t, names.GrafanaEndpointID) },
ErrorCheck: acctest.ErrorCheck(t, names.GrafanaServiceID),
CheckDestroy: testAccCheckLicenseAssociationDestroy(ctx),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccLicenseAssociationConfig_enterpriseToken(rName, string(awstypes.LicenseTypeEnterprise), uuidGrafanaToken),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckLicenseAssociationExists(ctx, resourceName),
resource.TestMatchResourceAttr(resourceName, "grafana_token", regexache.MustCompile(fmt.Sprintf(`^%s$`, verify.UUIDRegexPattern))),
resource.TestCheckResourceAttr(resourceName, "license_type", string(awstypes.LicenseTypeEnterprise)),
resource.TestCheckResourceAttrPair(resourceName, "workspace_id", workspaceResourceName, names.AttrID),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccLicenseAssociationConfig_enterpriseToken(rName string, licenseType string, grafanaToken string) string {
return acctest.ConfigCompose(testAccWorkspaceConfig_authenticationProvider(rName, "SAML"), fmt.Sprintf(`
resource "aws_grafana_license_association" "test" {
workspace_id = aws_grafana_workspace.test.id
license_type = %[1]q
grafana_token = %[2]q
}
`, licenseType, grafanaToken))
}

func testAccCheckLicenseAssociationExists(ctx context.Context, name string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[name]
Expand Down
3 changes: 2 additions & 1 deletion website/docs/r/grafana_license_association.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ resource "aws_iam_role" "assume" {

## Argument Reference

The following arguments are required:
This resource supports the following arguments:

* `grafana_token` - (Optional) A token from Grafana Labs that ties your AWS account with a Grafana Labs account.
* `license_type` - (Required) The type of license for the workspace license association. Valid values are `ENTERPRISE` and `ENTERPRISE_FREE_TRIAL`.
* `workspace_id` - (Required) The workspace id.

Expand Down

0 comments on commit 5c2b9db

Please sign in to comment.