diff --git a/.changelog/38743.txt b/.changelog/38743.txt new file mode 100644 index 00000000000..405963f2034 --- /dev/null +++ b/.changelog/38743.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +resource/aws_grafana_license_association: Add `grafana_token` argument +``` \ No newline at end of file diff --git a/internal/service/grafana/grafana_test.go b/internal/service/grafana/grafana_test.go index 9023f1171bf..a2447d73fa9 100644 --- a/internal/service/grafana/grafana_test.go +++ b/internal/service/grafana/grafana_test.go @@ -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, diff --git a/internal/service/grafana/license_association.go b/internal/service/grafana/license_association.go index 1a5aabf5150..32b4871dff2 100644 --- a/internal/service/grafana/license_association.go +++ b/internal/service/grafana/license_association.go @@ -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" @@ -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, @@ -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 { @@ -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 { diff --git a/internal/service/grafana/license_association_test.go b/internal/service/grafana/license_association_test.go index 5cae0e194f6..7799c23b860 100644 --- a/internal/service/grafana/license_association_test.go +++ b/internal/service/grafana/license_association_test.go @@ -8,7 +8,9 @@ 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" @@ -16,6 +18,7 @@ import ( "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" ) @@ -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] diff --git a/website/docs/r/grafana_license_association.html.markdown b/website/docs/r/grafana_license_association.html.markdown index 07f9854114a..c49cf652789 100644 --- a/website/docs/r/grafana_license_association.html.markdown +++ b/website/docs/r/grafana_license_association.html.markdown @@ -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.