Skip to content

Commit 67c7783

Browse files
Shocktroopertimofurrerpdecat
authored
Initial Addition of Personal Access Token Resource + some refactors (#1007)
* Initial Addition of Personal Access Token Resource + some refactors + bumping go-gitlab version + fixes from resulting go-gitlab upgrade * Merge from main * Update go-gitlab dependencies + Adding reworked setup files for registry enablement + added cleanup to volumes on testacc-down * Merge from main * Fixing more squash conflicts * Merge from main * Implemented review comments Co-authored-by: Timo Furrer <[email protected]> Co-authored-by: Patrick Decat <[email protected]>
1 parent 0ec0886 commit 67c7783

File tree

11 files changed

+473
-43
lines changed

11 files changed

+473
-43
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "gitlab_personal_access_token Resource - terraform-provider-gitlab"
4+
subcategory: ""
5+
description: |-
6+
The gitlab_personal_access_token resource allows to manage the lifecycle of a personal access token for a specified user.
7+
-> This resource requires administration privileges.
8+
Upstream API: GitLab REST API docs https://docs.gitlab.com/ee/api/personal_access_tokens.html
9+
---
10+
11+
# gitlab_personal_access_token (Resource)
12+
13+
The `gitlab_personal_access_token` resource allows to manage the lifecycle of a personal access token for a specified user.
14+
15+
-> This resource requires administration privileges.
16+
17+
**Upstream API**: [GitLab REST API docs](https://docs.gitlab.com/ee/api/personal_access_tokens.html)
18+
19+
## Example Usage
20+
21+
```terraform
22+
resource "gitlab_personal_access_token" "example" {
23+
user_id = "25"
24+
name = "Example personal access token"
25+
expires_at = "2020-03-14"
26+
27+
scopes = ["api"]
28+
}
29+
30+
resource "gitlab_project_variable" "example" {
31+
project = gitlab_project.example.id
32+
key = "pat"
33+
value = gitlab_personal_access_token.example.token
34+
}
35+
```
36+
37+
<!-- schema generated by tfplugindocs -->
38+
## Schema
39+
40+
### Required
41+
42+
- `name` (String) The name of the personal access token.
43+
- `scopes` (Set of String) The scope for the personal access token. It determines the actions which can be performed when authenticating with this token. Valid values are: `api`, `read_user`, `read_api`, `read_repository`, `write_repository`, `read_registry`, `write_registry`, `sudo`.
44+
- `user_id` (Number) The id of the user.
45+
46+
### Optional
47+
48+
- `expires_at` (String) The token expires at midnight UTC on that date. The date must be in the format YYYY-MM-DD. Default is never.
49+
- `id` (String) The ID of this resource.
50+
51+
### Read-Only
52+
53+
- `active` (Boolean) True if the token is active.
54+
- `created_at` (String) Time the token has been created, RFC3339 format.
55+
- `revoked` (Boolean) True if the token is revoked.
56+
- `token` (String, Sensitive) The personal access token. This is only populated when creating a new personal access token. This attribute is not available for imported resources.
57+
58+
## Import
59+
60+
Import is supported using the following syntax:
61+
62+
```shell
63+
# A GitLab Personal Access Token can be imported using a key composed of `<user-id>:<token-id>`, e.g.
64+
terraform import gitlab_personal_access_token.example "12345:1"
65+
66+
# NOTE: the `token` resource attribute is not available for imported resources as this information cannot be read from the GitLab API.
67+
```
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# A GitLab Personal Access Token can be imported using a key composed of `<user-id>:<token-id>`, e.g.
2+
terraform import gitlab_personal_access_token.example "12345:1"
3+
4+
# NOTE: the `token` resource attribute is not available for imported resources as this information cannot be read from the GitLab API.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
resource "gitlab_personal_access_token" "example" {
2+
user_id = "25"
3+
name = "Example personal access token"
4+
expires_at = "2020-03-14"
5+
6+
scopes = ["api"]
7+
}
8+
9+
resource "gitlab_project_variable" "example" {
10+
project = gitlab_project.example.id
11+
key = "pat"
12+
value = gitlab_personal_access_token.example.token
13+
}

go.mod

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ require (
88
github.com/hashicorp/terraform-plugin-sdk/v2 v2.14.0
99
github.com/mitchellh/hashstructure v1.1.0
1010
github.com/onsi/gomega v1.19.0
11-
github.com/xanzy/go-gitlab v0.60.0
11+
github.com/xanzy/go-gitlab v0.63.0
1212
golang.org/x/crypto v0.0.0-20220214200702-86341886e292 // indirect
13-
golang.org/x/oauth2 v0.0.0-20220309155454-6242fa91716a // indirect
14-
golang.org/x/time v0.0.0-20220224211638-0e9765cccd65 // indirect
13+
golang.org/x/net v0.0.0-20220412020605-290c469a71a5 // indirect
14+
golang.org/x/oauth2 v0.0.0-20220411215720-9780585627b5 // indirect
15+
golang.org/x/time v0.0.0-20220411224347-583f2d630306 // indirect
1516
google.golang.org/appengine v1.6.7 // indirect
1617
google.golang.org/genproto v0.0.0-20200904004341-0bd0a958aa1d // indirect
1718
)

go.sum

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -309,8 +309,8 @@ github.com/vmihailenco/msgpack/v4 v4.3.12 h1:07s4sz9IReOgdikxLTKNbBdqDMLsjPKXwvC
309309
github.com/vmihailenco/msgpack/v4 v4.3.12/go.mod h1:gborTTJjAo/GWTqqRjrLCn9pgNN+NXzzngzBKDPIqw4=
310310
github.com/vmihailenco/tagparser v0.1.1 h1:quXMXlA39OCbd2wAdTsGDlK9RkOk6Wuw+x37wVyIuWY=
311311
github.com/vmihailenco/tagparser v0.1.1/go.mod h1:OeAg3pn3UbLjkWt+rN9oFYB6u/cQgqMEUPoW2WPyhdI=
312-
github.com/xanzy/go-gitlab v0.60.0 h1:HaIlc14k4t9eJjAhY0Gmq2fBHgKd1MthBn3+vzDtsbA=
313-
github.com/xanzy/go-gitlab v0.60.0/go.mod h1:F0QEXwmqiBUxCgJm8fE9S+1veX4XC9Z4cfaAbqwk4YM=
312+
github.com/xanzy/go-gitlab v0.63.0 h1:a9fXpKWykUS6dowapFej/2Wjf4aOAEFC1q2ZIcz4IpI=
313+
github.com/xanzy/go-gitlab v0.63.0/go.mod h1:F0QEXwmqiBUxCgJm8fE9S+1veX4XC9Z4cfaAbqwk4YM=
314314
github.com/xanzy/ssh-agent v0.3.0 h1:wUMzuKtKilRgBAD1sUb8gOwwRr2FGoBVumcjoOACClI=
315315
github.com/xanzy/ssh-agent v0.3.0/go.mod h1:3s9xbODqPuuhK9JV1R321M/FlMZSBvE5aY6eAcqrDh0=
316316
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
@@ -410,16 +410,17 @@ golang.org/x/net v0.0.0-20210326060303-6b1517762897/go.mod h1:uSPa2vr4CLtc/ILN5o
410410
golang.org/x/net v0.0.0-20210428140749-89ef3d95e781/go.mod h1:OJAsFXCWl8Ukc7SiCT/9KSuxbyM7479/AVlXFRxuMCk=
411411
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
412412
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
413-
golang.org/x/net v0.0.0-20220225172249-27dd8689420f h1:oA4XRj0qtSt8Yo1Zms0CUlsT3KG69V2UGQWPBxujDmc=
414413
golang.org/x/net v0.0.0-20220225172249-27dd8689420f/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
414+
golang.org/x/net v0.0.0-20220412020605-290c469a71a5 h1:bRb386wvrE+oBNdF1d/Xh9mQrfQ4ecYhW5qJ5GvTGT4=
415+
golang.org/x/net v0.0.0-20220412020605-290c469a71a5/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
415416
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
416417
golang.org/x/oauth2 v0.0.0-20181106182150-f42d05182288/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
417418
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
418419
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
419420
golang.org/x/oauth2 v0.0.0-20191202225959-858c2ad4c8b6/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
420421
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
421-
golang.org/x/oauth2 v0.0.0-20220309155454-6242fa91716a h1:qfl7ob3DIEs3Ml9oLuPwY2N04gymzAW04WsUQHIClgM=
422-
golang.org/x/oauth2 v0.0.0-20220309155454-6242fa91716a/go.mod h1:DAh4E804XQdzx2j+YRIaUnCqCV2RuMz24cGBJ5QYIrc=
422+
golang.org/x/oauth2 v0.0.0-20220411215720-9780585627b5 h1:OSnWWcOd/CtWQC2cYSBgbTSJv3ciqd8r54ySIW2y3RE=
423+
golang.org/x/oauth2 v0.0.0-20220411215720-9780585627b5/go.mod h1:DAh4E804XQdzx2j+YRIaUnCqCV2RuMz24cGBJ5QYIrc=
423424
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
424425
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
425426
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
@@ -488,8 +489,8 @@ golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
488489
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
489490
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
490491
golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
491-
golang.org/x/time v0.0.0-20220224211638-0e9765cccd65 h1:M73Iuj3xbbb9Uk1DYhzydthsj6oOd6l9bpuFcNoUvTs=
492-
golang.org/x/time v0.0.0-20220224211638-0e9765cccd65/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
492+
golang.org/x/time v0.0.0-20220411224347-583f2d630306 h1:+gHMid33q6pen7kv9xvT+JRinntgeXO2AeZVd0AWD3w=
493+
golang.org/x/time v0.0.0-20220411224347-583f2d630306/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
493494
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
494495
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
495496
golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY=

internal/provider/data_source_gitlab_projects.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1099,7 +1099,7 @@ func dataSourceGitlabProjectsRead(ctx context.Context, d *schema.ResourceData, m
10991099
WithIssuesEnabled: withIssuesEnabledPtr,
11001100
WithMergeRequestsEnabled: withMergeRequestsEnabledPtr,
11011101
WithShared: withSharedPtr,
1102-
IncludeSubgroups: includeSubGroupsPtr,
1102+
IncludeSubGroups: includeSubGroupsPtr,
11031103
WithCustomAttributes: withCustomAttributesPtr,
11041104
}
11051105

internal/provider/resource_gitlab_group_access_token.go

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"strconv"
88
"time"
99

10-
"github.com/hashicorp/go-cty/cty"
1110
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1211
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1312
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
@@ -77,19 +76,11 @@ var _ = registerResource("gitlab_group_access_token", func() *schema.Resource {
7776
ValidateDiagFunc: validation.ToDiagFunc(validation.StringInSlice(validAccessLevels, false)),
7877
},
7978
"expires_at": {
80-
Description: "The token expires at midnight UTC on that date. The date must be in the format YYYY-MM-DD. Default is never.",
81-
Type: schema.TypeString,
82-
Optional: true,
83-
ForceNew: true,
84-
ValidateDiagFunc: func(i interface{}, p cty.Path) diag.Diagnostics {
85-
v := i.(string)
86-
87-
if _, err := time.Parse("2006-01-02", v); err != nil {
88-
return diag.Errorf("expected %q to be a valid YYYY-MM-DD date, got %q: %+v", p, i, err)
89-
}
90-
91-
return nil
92-
},
79+
Description: "The token expires at midnight UTC on that date. The date must be in the format YYYY-MM-DD. Default is never.",
80+
Type: schema.TypeString,
81+
Optional: true,
82+
ForceNew: true,
83+
ValidateDiagFunc: isISO6801Date,
9384
},
9485
"token": {
9586
Description: "The group access token. This is only populated when creating a new group access token. This attribute is not available for imported resources.",
@@ -237,6 +228,6 @@ func resourceGitlabGroupAccessTokenDelete(ctx context.Context, d *schema.Resourc
237228
}
238229

239230
log.Printf("[DEBUG] Delete gitlab GroupAccessToken %s", d.Id())
240-
_, err = client.GroupAccessTokens.DeleteGroupAccessToken(group, groupAccessTokenId, gitlab.WithContext(ctx))
231+
_, err = client.GroupAccessTokens.RevokeGroupAccessToken(group, groupAccessTokenId, gitlab.WithContext(ctx))
241232
return diag.FromErr(err)
242233
}

0 commit comments

Comments
 (0)