Skip to content

Commit 12ef62a

Browse files
committed
Support importing by team token ID
1 parent dd52671 commit 12ef62a

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed

internal/provider/resource_tfe_team_token.go

+34-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
2222
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
2323
"github.com/hashicorp/terraform-plugin-framework/types"
24+
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
2425
"github.com/hashicorp/terraform-plugin-log/tflog"
2526
)
2627

@@ -268,7 +269,39 @@ func (r *resourceTFETeamToken) Delete(ctx context.Context, req resource.DeleteRe
268269
}
269270

270271
func (r *resourceTFETeamToken) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
271-
resource.ImportStatePassthroughID(ctx, path.Root("team_id"), req, resp)
272+
if !isTokenID(req.ID) {
273+
// Set the team ID field
274+
resource.ImportStatePassthroughID(ctx, path.Root("team_id"), req, resp)
275+
return
276+
}
277+
278+
// Fetch token by ID to set attributes
279+
token, err := r.config.Client.TeamTokens.ReadByID(ctx, req.ID)
280+
if err != nil {
281+
resp.Diagnostics.AddError("Error importing team token", err.Error())
282+
return
283+
}
284+
if token.Team == nil {
285+
resp.Diagnostics.AddError("Error importing team token", "token did not return associated team")
286+
return
287+
}
288+
289+
var expiredAt types.String
290+
if !token.ExpiredAt.IsZero() {
291+
expiredAt = types.StringValue(token.ExpiredAt.Format(time.RFC3339))
292+
} else {
293+
expiredAt = types.StringNull()
294+
}
295+
296+
var description types.String
297+
if token.Description != nil {
298+
description = types.StringValue(*token.Description)
299+
} else {
300+
description = types.StringNull()
301+
}
302+
303+
result := modelFromTFEToken(types.StringValue(token.Team.ID), types.StringValue(token.ID), types.StringValue(token.Token), basetypes.NewBoolNull(), expiredAt, description)
304+
resp.Diagnostics.Append(resp.State.Set(ctx, &result)...)
272305
}
273306

274307
// Determines whether the ID of the resource is the ID of the authentication token

internal/provider/resource_tfe_team_token_test.go

+34
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,40 @@ func TestAccTFETeamToken_import(t *testing.T) {
214214
})
215215
}
216216

217+
func TestAccTFETeamToken_importByTokenID(t *testing.T) {
218+
skipUnlessBeta(t)
219+
rInt := rand.New(rand.NewSource(time.Now().UnixNano())).Int()
220+
221+
resource.Test(t, resource.TestCase{
222+
PreCheck: func() { testAccPreCheck(t) },
223+
ProtoV5ProviderFactories: testAccMuxedProviders,
224+
CheckDestroy: testAccCheckTFETeamTokenDestroy,
225+
Steps: []resource.TestStep{
226+
{
227+
Config: testAccTFETeamToken_withMultipleTokens(rInt),
228+
},
229+
{
230+
ResourceName: "tfe_team_token.multi_token_1",
231+
ImportState: true,
232+
ImportStateVerify: true,
233+
ImportStateVerifyIgnore: []string{"token"},
234+
},
235+
{
236+
ResourceName: "tfe_team_token.multi_token_2",
237+
ImportState: true,
238+
ImportStateVerify: true,
239+
ImportStateVerifyIgnore: []string{"token"},
240+
},
241+
{
242+
ResourceName: "tfe_team_token.legacy",
243+
ImportState: true,
244+
ImportStateVerify: true,
245+
ImportStateVerifyIgnore: []string{"token"},
246+
},
247+
},
248+
})
249+
}
250+
217251
func testAccCheckTFETeamTokenExists(
218252
n string, token *tfe.TeamToken) resource.TestCheckFunc {
219253
return func(s *terraform.State) error {

0 commit comments

Comments
 (0)