Skip to content

Commit 34ef2f9

Browse files
authored
Merge pull request #575 from RomanNess/522_deploy_token_expires_at_suppress_func
add diffSuppressFunc for 'expires_at' attribute in 'deploy_token'
2 parents 5283e6f + 7447992 commit 34ef2f9

File tree

2 files changed

+58
-5
lines changed

2 files changed

+58
-5
lines changed

gitlab/resource_gitlab_deploy_token.go

+14-4
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,11 @@ func resourceGitlabDeployToken() *schema.Resource {
4343
ForceNew: true,
4444
},
4545
"expires_at": {
46-
Type: schema.TypeString,
47-
Optional: true,
48-
ValidateFunc: validation.IsRFC3339Time,
49-
ForceNew: true,
46+
Type: schema.TypeString,
47+
Optional: true,
48+
ValidateFunc: validation.IsRFC3339Time,
49+
DiffSuppressFunc: expiresAtSuppressFunc,
50+
ForceNew: true,
5051
},
5152
"scopes": {
5253
Type: schema.TypeSet,
@@ -67,6 +68,15 @@ func resourceGitlabDeployToken() *schema.Resource {
6768
}
6869
}
6970

71+
func expiresAtSuppressFunc(k, old, new string, d *schema.ResourceData) bool {
72+
oldDate, oldDateErr := time.Parse(time.RFC3339, old)
73+
newDate, newDateErr := time.Parse(time.RFC3339, new)
74+
if oldDateErr != nil || newDateErr != nil {
75+
return false
76+
}
77+
return oldDate == newDate
78+
}
79+
7080
func resourceGitlabDeployTokenCreate(d *schema.ResourceData, meta interface{}) error {
7181
client := meta.(*gitlab.Client)
7282
project, isProject := d.GetOk("project")

gitlab/resource_gitlab_deploy_token_test.go

+44-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ resource "gitlab_deploy_token" "foo" {
156156
name = "deployToken-%d"
157157
username = "my-username"
158158
159-
expires_at = "2021-03-14T07:20:50Z"
159+
expires_at = "2021-03-14T07:20:50.000Z"
160160
161161
scopes = [
162162
"read_registry",
@@ -165,3 +165,46 @@ resource "gitlab_deploy_token" "foo" {
165165
}
166166
`, rInt, rInt)
167167
}
168+
169+
type expiresAtSuppressFuncTest struct {
170+
description string
171+
old string
172+
new string
173+
expected bool
174+
}
175+
176+
func TestExpiresAtSuppressFunc(t *testing.T) {
177+
testcases := []expiresAtSuppressFuncTest{
178+
{
179+
description: "same dates without millis",
180+
old: "2025-03-14T00:00:00Z",
181+
new: "2025-03-14T00:00:00Z",
182+
expected: true,
183+
}, {
184+
description: "different date without millis",
185+
old: "2025-03-14T00:00:00Z",
186+
new: "2025-03-14T11:11:11Z",
187+
expected: false,
188+
}, {
189+
description: "same date with and without millis",
190+
old: "2025-03-14T00:00:00Z",
191+
new: "2025-03-14T00:00:00.000Z",
192+
expected: true,
193+
}, {
194+
description: "cannot parse new date",
195+
old: "2025-03-14T00:00:00Z",
196+
new: "invalid-date",
197+
expected: false,
198+
},
199+
}
200+
201+
for _, test := range testcases {
202+
t.Run(test.description, func(t *testing.T) {
203+
actual := expiresAtSuppressFunc("", test.old, test.new, nil)
204+
if actual != test.expected {
205+
t.Fatalf("FAIL\n\told: %s, new: %s\n\texpected: %t\n\tactual: %t",
206+
test.old, test.new, test.expected, actual)
207+
}
208+
})
209+
}
210+
}

0 commit comments

Comments
 (0)