Skip to content

Commit 51b9b49

Browse files
authored
access-token: another ExpiresAt bugfix (#124)
Signed-off-by: Nick Santos <[email protected]>
1 parent a12d594 commit 51b9b49

2 files changed

Lines changed: 43 additions & 6 deletions

File tree

internal/provider/resource_access_token.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ func (r *AccessTokenResource) toModel(ctx context.Context, at hubclient.AccessTo
269269
}
270270

271271
// If the current state is null, keep it as null instead of changing to empty string.
272-
if at.ExpiresAt == "" && currentState != nil && currentState.ExpiresAt.IsNull() {
272+
if at.ExpiresAt == "" && (currentState == nil || currentState.ExpiresAt.IsNull()) {
273273
result.ExpiresAt = types.StringNull()
274274
}
275275

internal/provider/resource_access_token_test.go

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,20 @@ import (
2323
"github.com/hashicorp/terraform-plugin-testing/terraform"
2424
)
2525

26-
func TestAccessTokenResource(t *testing.T) {
26+
func TestAccessTokenResource_WithExpires(t *testing.T) {
27+
config := `
28+
resource "docker_access_token" "test" {
29+
token_label = "test-label"
30+
scopes = ["repo:read", "repo:write"]
31+
expires_at = "2029-12-31T23:59:59Z"
32+
}
33+
`
2734
resource.Test(t, resource.TestCase{
2835
PreCheck: func() { testAccPreCheck(t) },
2936
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
3037
Steps: []resource.TestStep{
3138
{
32-
Config: testAccessTokenResourceConfig,
39+
Config: config,
3340
Check: resource.ComposeAggregateTestCheckFunc(
3441
resource.TestCheckResourceAttrSet("docker_access_token.test", "uuid"),
3542
resource.TestCheckResourceAttr("docker_access_token.test", "is_active", "true"),
@@ -55,13 +62,43 @@ func TestAccessTokenResource(t *testing.T) {
5562
})
5663
}
5764

58-
const testAccessTokenResourceConfig = `
65+
func TestAccessTokenResource_NoExpires(t *testing.T) {
66+
config := `
5967
resource "docker_access_token" "test" {
6068
token_label = "test-label"
61-
scopes = ["repo:read", "repo:write"]
62-
expires_at = "2029-12-31T23:59:59Z"
69+
scopes = ["repo:read"]
6370
}
6471
`
72+
resource.Test(t, resource.TestCase{
73+
PreCheck: func() { testAccPreCheck(t) },
74+
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
75+
Steps: []resource.TestStep{
76+
{
77+
Config: config,
78+
Check: resource.ComposeAggregateTestCheckFunc(
79+
resource.TestCheckResourceAttrSet("docker_access_token.test", "uuid"),
80+
resource.TestCheckResourceAttr("docker_access_token.test", "is_active", "true"),
81+
resource.TestCheckResourceAttr("docker_access_token.test", "token_label", "test-label"),
82+
resource.TestCheckResourceAttr("docker_access_token.test", "scopes.#", "1"),
83+
resource.TestCheckResourceAttrSet("docker_access_token.test", "token"), // Check if the token is set
84+
resource.TestCheckNoResourceAttr("docker_access_token.test", "expires_at"),
85+
),
86+
},
87+
{
88+
ResourceName: "docker_access_token.test",
89+
ImportState: true,
90+
ImportStateVerify: true,
91+
ImportStateVerifyIdentifierAttribute: "uuid",
92+
ImportStateVerifyIgnore: []string{
93+
"token", // Ignore the token attribute during import state verification
94+
},
95+
ImportStateIdFunc: func(state *terraform.State) (string, error) {
96+
return state.RootModule().Resources["docker_access_token.test"].Primary.Attributes["uuid"], nil
97+
},
98+
},
99+
},
100+
})
101+
}
65102

66103
func TestAccessTokenResource_Upgrade(t *testing.T) {
67104
config := `

0 commit comments

Comments
 (0)