Skip to content

Support multiple team tokens for a single team with tfe_team_token #1698

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

mkam
Copy link
Contributor

@mkam mkam commented Apr 18, 2025

Description

This PR adds support to create multiple team tokens for a single team using tfe_team_token by adding the description attribute. If no description is provided, it follows the previous behavior of tfe_team_token, which uses the old team token API that assumes there is only one token per team.

Some other notable differences:

  • The new API for team tokens that supports multiple tokens does not support regenerating tokens with a single API request (i.e., they must be rotated via separate delete and create requests). That means force_regenerate cannot be used when description is set.
  • The ID of the resource was previously the team ID since there was only ever one token per team. For tokens without descriptions, this will continue to be the team ID to preserve backwards compatibility. For tokens with descriptions, the ID is the ID of the authentication token itself. This is distinguished by inspecting the the prefix of the ID (team- vs. at-)

Also, adding support for creating multiple ephemeral team tokens is out of scope for this PR. This will be added in a separate, follow-up PR.

This feature is not GA yet, so I've labeled this PR as "do not merge" for now.

Testing plan

  1. Create a team.
  2. Create multiple tokens for the team with unique descriptions, including one without a description.
  3. Modify the description of a token and validate that the token is recreated.
  4. Import a team token by team ID and a team token by token ID.
resource "tfe_team" "this" {
  organization = data.tfe_organization.test.name
  name         = "test-team"
}

resource "time_rotating" "example" {
  rotation_days = 30
}

resource "tfe_team_token" "multi_token_1" {
  team_id     = tfe_team.this.id
  description = "test1"
}


resource "tfe_team_token" "multi_token_2" {
  team_id     = tfe_team.this.id
  description = "test2"
  expired_at  = time_rotating.example.rotation_rfc3339
}


resource "tfe_team_token" "legacy" {
  team_id     = tfe_team.this.id
}

terraform import tfe_team_token.import_by_team_id team-1234
terraform import tfe_team_token.import_by_token_id at-1234

resource "tfe_team_token" "import_by_team_id" {
  team_id = "team-1234"
  expired_at = time_rotating.example.rotation_rfc3339
}

resource "tfe_team_token" "import_by_token_id" {
  team_id = "team-1234"
  description = "test-updated"
}

Output from acceptance tests

Test results with beta enabled
-> % ENABLE_BETA=1 go test ./... -v -run "TestAccTFETeamToken"
?   	github.com/hashicorp/terraform-provider-tfe	[no test files]
testing: warning: no tests to run
PASS
ok  	github.com/hashicorp/terraform-provider-tfe/internal/client	(cached) [no tests to run]
testing: warning: no tests to run
PASS
ok  	github.com/hashicorp/terraform-provider-tfe/internal/logging	(cached) [no tests to run]
=== RUN   TestAccTFETeamToken_basic
--- PASS: TestAccTFETeamToken_basic (5.95s)
=== RUN   TestAccTFETeamToken_multiple_team_tokens
--- PASS: TestAccTFETeamToken_multiple_team_tokens (7.97s)
=== RUN   TestAccTFETeamToken_existsWithoutForce
--- PASS: TestAccTFETeamToken_existsWithoutForce (6.32s)
=== RUN   TestAccTFETeamToken_existsWithForce
--- PASS: TestAccTFETeamToken_existsWithForce (8.84s)
=== RUN   TestAccTFETeamToken_invalidWithForceGenerateAndDescription
--- PASS: TestAccTFETeamToken_invalidWithForceGenerateAndDescription (0.36s)
=== RUN   TestAccTFETeamToken_withBlankExpiry
--- PASS: TestAccTFETeamToken_withBlankExpiry (4.95s)
=== RUN   TestAccTFETeamToken_withValidExpiry
--- PASS: TestAccTFETeamToken_withValidExpiry (5.37s)
=== RUN   TestAccTFETeamToken_withInvalidExpiry
--- PASS: TestAccTFETeamToken_withInvalidExpiry (3.01s)
=== RUN   TestAccTFETeamToken_import
--- PASS: TestAccTFETeamToken_import (7.04s)
=== RUN   TestAccTFETeamToken_importByTokenID
--- PASS: TestAccTFETeamToken_importByTokenID (9.84s)
PASS
ok  	github.com/hashicorp/terraform-provider-tfe/internal/provider	60.387s
?   	github.com/hashicorp/terraform-provider-tfe/internal/provider/helpers	[no test files]
?   	github.com/hashicorp/terraform-provider-tfe/internal/provider/planmodifiers	[no test files]
?   	github.com/hashicorp/terraform-provider-tfe/internal/provider/validators	[no test files]
?   	github.com/hashicorp/terraform-provider-tfe/version	[no test files]

Output from documentation preview

Docs changes

Screenshot 2025-04-18 at 4 26 49 PM

Screenshot 2025-04-18 at 4 27 02 PM

Screenshot 2025-04-18 at 4 27 14 PM

@mkam mkam changed the title Mkam/tf 24515/multiple team tokens Support multiple team tokens Apr 18, 2025
@mkam mkam changed the title Support multiple team tokens Support multiple team tokens for a single team with tfe_team_token Apr 18, 2025
@mkam mkam force-pushed the mkam/TF-24515/multiple-team-tokens branch 2 times, most recently from e2bb093 to ccfd721 Compare April 21, 2025 16:52
@mkam mkam requested review from a team, tylerwolf and juliannatetreault April 21, 2025 19:24
@mkam mkam marked this pull request as ready for review April 21, 2025 19:24
@mkam mkam requested a review from a team as a code owner April 21, 2025 19:24
@mkam mkam force-pushed the mkam/TF-24515/multiple-team-tokens branch 2 times, most recently from 30d2ada to 00d0b73 Compare April 22, 2025 15:53
mkam added 6 commits April 24, 2025 13:39
By setting the description, this allows for creation of multiple team tokens.
Previously, when a team only had a single token, it was sufficient to have
the ID of the token be set to the team ID. Now that we support multiple
team tokens, we should use the token ID instead. We will continue to
use the team ID for descriptionless tokens so that it is backwards
compatible, though.
@mkam mkam force-pushed the mkam/TF-24515/multiple-team-tokens branch from 00d0b73 to bdc8aec Compare April 24, 2025 18:40
go.mod Outdated
@@ -14,7 +14,7 @@ require (
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/go-retryablehttp v0.7.7 // indirect
github.com/hashicorp/go-slug v0.16.5
github.com/hashicorp/go-tfe v1.78.0
github.com/hashicorp/go-tfe v1.78.1-0.20250418170002-da71abb96c5a
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume this is just to get nil Description support?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's correct!

Copy link
Collaborator

@brandonc brandonc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is looking and behaving very well. I smoke-tested import and different combinations of adding/removing descriptions and force_regenerate. It works as documented.

When does your feature reach GA? That would probably be the time to merge this PR.

@mkam
Copy link
Contributor Author

mkam commented Apr 24, 2025

@brandonc Thanks for taking a look! The plan is to GA for HCP Terraform this upcoming Monday, and then hopefully have it included in next TFE release (v202505-1).

@mkam mkam requested a review from brandonc May 7, 2025 22:03
@mkam mkam removed the DO NOT MERGE label May 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants