Skip to content

Commit 5d3ec85

Browse files
committed
feat(gitlab_project_access_token): add access_level
1 parent 13f1c1a commit 5d3ec85

File tree

2 files changed

+93
-9
lines changed

2 files changed

+93
-9
lines changed

internal/provider/resource_gitlab_project_access_token.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,17 +90,27 @@ var _ = registerResource("gitlab_project_access_token", func() *schema.Resource
9090
Type: schema.TypeInt,
9191
Computed: true,
9292
},
93+
"access_level": {
94+
Description: fmt.Sprintf("The access level of the associated user_id membership (cf resource_gitlab_project_membership). Valid values are: %s", renderValueListForDocs(validProjectAccessLevelNames)),
95+
Type: schema.TypeString,
96+
ValidateDiagFunc: validation.ToDiagFunc(validation.StringInSlice(validProjectAccessLevelNames, false)),
97+
Optional: true,
98+
Default: accessLevelValueToName[gitlab.MaintainerPermissions],
99+
ForceNew: true,
100+
},
93101
},
94102
}
95103
})
96104

97105
func resourceGitlabProjectAccessTokenCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
98106
client := meta.(*gitlab.Client)
99-
107+
accessLevelId := accessLevelNameToValue[d.Get("access_level").(string)]
100108
project := d.Get("project").(string)
109+
101110
options := &gitlab.CreateProjectAccessTokenOptions{
102-
Name: gitlab.String(d.Get("name").(string)),
103-
Scopes: stringSetToStringSlice(d.Get("scopes").(*schema.Set)),
111+
Name: gitlab.String(d.Get("name").(string)),
112+
Scopes: stringSetToStringSlice(d.Get("scopes").(*schema.Set)),
113+
AccessLevel: &accessLevelId,
104114
}
105115

106116
log.Printf("[DEBUG] create gitlab ProjectAccessToken %s %s for project ID %s", *options.Name, options.Scopes, project)
@@ -172,6 +182,7 @@ func resourceGitlabProjectAccessTokenRead(ctx context.Context, d *schema.Resourc
172182
d.Set("created_at", projectAccessToken.CreatedAt.String())
173183
d.Set("revoked", projectAccessToken.Revoked)
174184
d.Set("user_id", projectAccessToken.UserID)
185+
d.Set("access_level", accessLevelValueToName[projectAccessToken.AccessLevel])
175186

176187
err = d.Set("scopes", projectAccessToken.Scopes)
177188
if err != nil {

internal/provider/resource_gitlab_project_access_token_test.go

Lines changed: 79 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@ func TestAccGitlabProjectAccessToken_basic(t *testing.T) {
3030
Check: resource.ComposeTestCheckFunc(
3131
testAccCheckGitlabProjectAccessTokenExists("gitlab_project_access_token.bar", &pat),
3232
testAccCheckGitlabProjectAccessTokenAttributes(&pat, &testAccGitlabProjectAccessTokenExpectedAttributes{
33-
name: "my project token",
34-
scopes: map[string]bool{"read_repository": true, "api": true, "write_repository": true, "read_api": true},
35-
expiresAt: "2022-04-01",
33+
name: "my project token",
34+
scopes: map[string]bool{"read_repository": true, "api": true, "write_repository": true, "read_api": true},
35+
expiresAt: "2022-04-01",
36+
accessLevel: accessLevelValueToName[gitlab.MaintainerPermissions], // default permission on gitlab side when unspecified
3637
}),
3738
),
3839
},
@@ -92,6 +93,56 @@ func TestAccGitlabProjectAccessToken_basic(t *testing.T) {
9293
})
9394
}
9495

96+
func TestAccGitlabProjectAccessToken_accessLevel(t *testing.T) {
97+
var pat testAccGitlabProjectAccessTokenWrapper
98+
rInt := acctest.RandInt()
99+
100+
testAccGitlabProjectStart(t)
101+
102+
resource.Test(t, resource.TestCase{
103+
PreCheck: func() { testAccPreCheck(t) },
104+
ProviderFactories: providerFactories,
105+
CheckDestroy: testAccCheckGitlabProjectAccessTokenDestroy,
106+
Steps: []resource.TestStep{
107+
// Create a project and a Project Access Token
108+
{
109+
Config: testAccGitlabProjectAccessTokenConfigWithAccessLevel(rInt, accessLevelValueToName[gitlab.MaintainerPermissions]),
110+
Check: resource.ComposeTestCheckFunc(
111+
testAccCheckGitlabProjectAccessTokenExists("gitlab_project_access_token.bar", &pat),
112+
testAccCheckGitlabProjectAccessTokenAttributes(&pat, &testAccGitlabProjectAccessTokenExpectedAttributes{
113+
name: "my project token",
114+
scopes: map[string]bool{"read_repository": true, "api": true, "write_repository": true, "read_api": true},
115+
expiresAt: "2022-04-01",
116+
accessLevel: accessLevelValueToName[gitlab.MaintainerPermissions],
117+
}),
118+
),
119+
},
120+
// Update the Project Access Token to change the parameters
121+
{
122+
Config: testAccGitlabProjectAccessTokenConfigWithAccessLevel(rInt, accessLevelValueToName[gitlab.DeveloperPermissions]),
123+
Check: resource.ComposeTestCheckFunc(
124+
testAccCheckGitlabProjectAccessTokenExists("gitlab_project_access_token.bar", &pat),
125+
testAccCheckGitlabProjectAccessTokenAttributes(&pat, &testAccGitlabProjectAccessTokenExpectedAttributes{
126+
name: "my project token",
127+
scopes: map[string]bool{"read_repository": true, "api": true, "write_repository": true, "read_api": true},
128+
expiresAt: "2022-04-01",
129+
accessLevel: accessLevelValueToName[gitlab.DeveloperPermissions],
130+
}),
131+
),
132+
},
133+
// Verify import
134+
{
135+
ResourceName: "gitlab_project_access_token.bar",
136+
ImportState: true,
137+
ImportStateVerify: true,
138+
ImportStateVerifyIgnore: []string{
139+
// the token is only known during creating. We explicitly mention this limitation in the docs.
140+
"token",
141+
},
142+
},
143+
}})
144+
}
145+
95146
func testAccCheckGitlabProjectAccessTokenDoesNotExist(pat *testAccGitlabProjectAccessTokenWrapper) resource.TestCheckFunc {
96147
return func(s *terraform.State) error {
97148
return gomega.InterceptGomegaFailure(func() {
@@ -155,9 +206,10 @@ func testAccCheckGitlabProjectAccessTokenExists(n string, pat *testAccGitlabProj
155206
}
156207

157208
type testAccGitlabProjectAccessTokenExpectedAttributes struct {
158-
name string
159-
scopes map[string]bool
160-
expiresAt string
209+
name string
210+
scopes map[string]bool
211+
expiresAt string
212+
accessLevel string
161213
}
162214

163215
type testAccGitlabProjectAccessTokenWrapper struct {
@@ -304,3 +356,24 @@ resource "gitlab_project_variable" "var" {
304356
305357
`, rInt)
306358
}
359+
360+
func testAccGitlabProjectAccessTokenConfigWithAccessLevel(rInt int, level string) string {
361+
return fmt.Sprintf(`
362+
resource "gitlab_project" "foo" {
363+
name = "foo-%d"
364+
description = "Terraform acceptance tests"
365+
366+
# So that acceptance tests can be run in a gitlab organization
367+
# with no billing
368+
visibility_level = "public"
369+
}
370+
371+
resource "gitlab_project_access_token" "bar" {
372+
name = "my project token"
373+
project = gitlab_project.foo.id
374+
expires_at = "2022-04-01"
375+
scopes = ["read_repository" , "api", "write_repository", "read_api"]
376+
access_level = "%s"
377+
}
378+
`, rInt, level)
379+
}

0 commit comments

Comments
 (0)