Skip to content

Commit 8b5d627

Browse files
committed
Still some code review changes
1 parent eae09e3 commit 8b5d627

File tree

2 files changed

+47
-8
lines changed

2 files changed

+47
-8
lines changed

gitlab/resource_gitlab_project_access_token.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,7 @@ func resourceGitlabProjectAccessToken() *schema.Resource {
4242
Type: schema.TypeString,
4343
Optional: true,
4444
ValidateFunc: func(i interface{}, k string) (warnings []string, errors []error) {
45-
v, ok := i.(string)
46-
if !ok {
47-
errors = append(errors, fmt.Errorf("expected type of %q to be string", k))
48-
return warnings, errors
49-
}
45+
v := i.(string)
5046

5147
if _, err := time.Parse("2006-01-02", v); err != nil {
5248
errors = append(errors, fmt.Errorf("expected %q to be a valid YYYY-MM-DD date, got %q: %+v", k, i, err))
@@ -89,7 +85,7 @@ func resourceGitlabProjectAccessTokenCreate(d *schema.ResourceData, meta interfa
8985
Scopes: *stringSetToStringSlice(d.Get("scopes").(*schema.Set)),
9086
}
9187

92-
log.Printf("[DEBUG] create gitlab ProjectAccessToken %s %s", *options.Name, options.Scopes)
88+
log.Printf("[DEBUG] create gitlab ProjectAccessToken %s %s for project ID %d", *options.Name, options.Scopes, project)
9389

9490
if v, ok := d.GetOk("expires_at"); ok {
9591
parsedExpiresAt, err := time.Parse("2006-01-02", v.(string))
@@ -98,14 +94,16 @@ func resourceGitlabProjectAccessTokenCreate(d *schema.ResourceData, meta interfa
9894
}
9995
parsedExpiresAtISOTime := gitlab.ISOTime(parsedExpiresAt)
10096
options.ExpiresAt = &parsedExpiresAtISOTime
101-
log.Printf("[DEBUG] create gitlab ProjectAccessToken with expires_at %s", *options.ExpiresAt)
97+
log.Printf("[DEBUG] create gitlab ProjectAccessToken %s with expires_at %s for project ID %d", *options.Name, *options.ExpiresAt, project)
10298
}
10399

104100
projectAccessToken, _, err := client.ProjectAccessTokens.CreateProjectAccessToken(project, options)
105101
if err != nil {
106102
return err
107103
}
108104

105+
log.Printf("[DEBUG] created gitlab ProjectAccessToken %d - %s for project ID %d", projectAccessToken.ID, *options.Name, project)
106+
109107
d.SetId(strconv.Itoa(projectAccessToken.ID))
110108
d.Set("token", projectAccessToken.Token)
111109

gitlab/resource_gitlab_project_access_token_test.go

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"strconv"
66
"testing"
7+
"time"
78

89
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
910
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
@@ -72,10 +73,37 @@ func TestAccGitlabProjectAccessToken_basic(t *testing.T) {
7273
}),
7374
),
7475
},
76+
//Destroy Project Access Token
77+
{
78+
Config: testAccGitlabProjectAccessTokenDestroyToken(rInt),
79+
Check: testAccCheckGitlabProjectAccessTokenDoesNotExist(&pat),
80+
},
7581
},
7682
})
7783
}
7884

85+
func testAccCheckGitlabProjectAccessTokenDoesNotExist(pat *testAccGitlabProjectAccessTokenWrapper) resource.TestCheckFunc {
86+
return func(s *terraform.State) error {
87+
conn := testAccProvider.Meta().(*gitlab.Client)
88+
89+
//Unfortunately we need to wait a bit, since the API call doesn't wait to destroy the PAT and returns immediately
90+
time.Sleep(3 * time.Second) // lintignore: R018 // TODO: Resolve this tfproviderlint issue
91+
92+
tokens, _, err := conn.ProjectAccessTokens.ListProjectAccessTokens(pat.project, nil)
93+
if err != nil {
94+
return err
95+
}
96+
97+
for _, token := range tokens {
98+
if token.ID == pat.pat.ID {
99+
return fmt.Errorf("Found token %d for project %s (tokens found: %d)", token.ID, pat.project, len(tokens))
100+
}
101+
}
102+
103+
return nil
104+
}
105+
}
106+
79107
func testAccCheckGitlabProjectAccessTokenExists(n string, pat *testAccGitlabProjectAccessTokenWrapper) resource.TestCheckFunc {
80108
return func(s *terraform.State) error {
81109
rs, ok := s.RootModule().Resources[n]
@@ -103,7 +131,7 @@ func testAccCheckGitlabProjectAccessTokenExists(n string, pat *testAccGitlabProj
103131
return nil
104132
}
105133
}
106-
return fmt.Errorf("Project Acces Token does not exist")
134+
return fmt.Errorf("Project Access Token does not exist")
107135
}
108136
}
109137

@@ -199,6 +227,19 @@ resource "gitlab_project_access_token" "bar" {
199227
`, rInt)
200228
}
201229

230+
func testAccGitlabProjectAccessTokenDestroyToken(rInt int) string {
231+
return fmt.Sprintf(`
232+
resource "gitlab_project" "foo" {
233+
name = "foo-%d"
234+
description = "Terraform acceptance tests"
235+
236+
# So that acceptance tests can be run in a gitlab organization
237+
# with no billing
238+
visibility_level = "public"
239+
}
240+
`, rInt)
241+
}
242+
202243
func testAccGitlabProjectAccessTokenUpdateConfig(rInt int) string {
203244
return fmt.Sprintf(`
204245
resource "gitlab_project" "foo" {

0 commit comments

Comments
 (0)