Skip to content

Commit

Permalink
wip: passing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ctrombley committed Feb 21, 2025
1 parent 5b598bd commit 0dd4231
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 21 deletions.
30 changes: 19 additions & 11 deletions internal/provider/ephemeral_resource_organization_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package provider
import (
"context"
"fmt"
"time"

"github.com/hashicorp/go-tfe"
"github.com/hashicorp/terraform-plugin-framework/ephemeral"
Expand All @@ -27,10 +28,9 @@ type OrganizationTokenEphemeralResource struct {
}

type OrganizationTokenEphemeralResourceModel struct {
Organization types.String `tfsdk:"organization"`
Token types.String `tfsdk:"token"`
ForceRegenerate types.Bool `tfsdk:"force_generate"`
ExpiredAt types.String `tfsdk:"expired_at"`
Organization types.String `tfsdk:"organization"`
Token types.String `tfsdk:"token"`
ExpiredAt types.String `tfsdk:"expired_at"`
}

func (e *OrganizationTokenEphemeralResource) Schema(ctx context.Context, req ephemeral.SchemaRequest, resp *ephemeral.SchemaResponse) {
Expand All @@ -45,13 +45,10 @@ func (e *OrganizationTokenEphemeralResource) Schema(ctx context.Context, req eph
"token": schema.StringAttribute{
Description: `The generated token.`,
Computed: true,
},
"force_generate": schema.BoolAttribute{
Description: `If set to true, a new token will be generated even if a token already exists. This will invalidate the existing token!`,
Optional: true,
Sensitive: true,
},
"expired_at": schema.StringAttribute{
Description: `The token's expiration date. The expiration date must be a date/time string in RFC3339 format (e.g., "2024-12-31T23:59:59Z"). If no expiration date is supplied, the expiration date will default to null and never expire.`,
Description: `The token's expiration date.`,
Optional: true,
Computed: true,
},
Expand Down Expand Up @@ -97,7 +94,19 @@ func (e *OrganizationTokenEphemeralResource) Open(ctx context.Context, req ephem
return
}

result, err := e.config.Client.OrganizationTokens.Read(ctx, orgName)
// Create a new options struct
options := tfe.OrganizationTokenCreateOptions{}
if data.ExpiredAt.IsUnknown() {
expiredAt := data.ExpiredAt.ValueString()
expiredAtTime, err := time.Parse(time.RFC3339, expiredAt)
if err != nil {
resp.Diagnostics.AddError("Invalid ExpiredAt value", "The ExpiredAt value must be set to a valid date.")
return
}
options.ExpiredAt = &expiredAtTime
}

result, err := e.config.Client.OrganizationTokens.CreateWithOptions(ctx, orgName, options)
if err != nil {
resp.Diagnostics.AddError("Unable to read resource", err.Error())
return
Expand All @@ -113,7 +122,6 @@ func (e *OrganizationTokenEphemeralResource) Open(ctx context.Context, req ephem
// tfe.OrganizationToken value.
func ephemeralResourceModelFromTFEOrganizationToken(organization string, v *tfe.OrganizationToken) OrganizationTokenEphemeralResourceModel {
return OrganizationTokenEphemeralResourceModel{
ID: types.StringValue(v.ID),
Organization: types.StringValue(organization),
Token: types.StringValue(v.Token),
ExpiredAt: types.StringValue(v.ExpiredAt.String()),
Expand Down
18 changes: 8 additions & 10 deletions internal/provider/ephemeral_resource_organization_token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
package provider

import (
"context"
"fmt"
"regexp"
"testing"

"github.com/hashicorp/terraform-plugin-go/tfprotov6"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/echoprovider"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/knownvalue"
"github.com/hashicorp/terraform-plugin-testing/statecheck"
"github.com/hashicorp/terraform-plugin-testing/tfjsonpath"
)

func TestAccOrganizationTokenEphemeralResource_basic(t *testing.T) {
Expand All @@ -22,11 +25,6 @@ func TestAccOrganizationTokenEphemeralResource_basic(t *testing.T) {
org, orgCleanup := createBusinessOrganization(t, tfeClient)
t.Cleanup(orgCleanup)

result, err := tfeClient.OrganizationTokens.Create(context.Background(), org.Name)
if err != nil {
t.Fatal(err)
}

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV5ProviderFactories: testAccMuxedProviders,
Expand All @@ -36,9 +34,9 @@ func TestAccOrganizationTokenEphemeralResource_basic(t *testing.T) {
Steps: []resource.TestStep{
{
Config: testAccOrganizationTokenEphemeralResourceConfig(org.Name),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("echo.data", "token", result.Token),
),
ConfigStateChecks: []statecheck.StateCheck{
statecheck.ExpectKnownValue("echo.this", tfjsonpath.New("data"), knownvalue.StringRegexp(regexp.MustCompile(`^[a-zA-Z0-9]+\.atlasv1\.[a-zA-Z0-9]+$`))),
},
},
},
})
Expand Down

0 comments on commit 0dd4231

Please sign in to comment.