Skip to content

Commit 375586b

Browse files
authored
Merge pull request #960 from timofurrer/feature/importer-36
Implement all missing importers
2 parents 733e3f9 + 3d30a46 commit 375586b

13 files changed

+194
-43
lines changed

docs/resources/deploy_token.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,16 @@ resource "gitlab_deploy_token" "example" {
5959

6060
### Read-Only
6161

62-
- **token** (String, Sensitive) The secret token. This is only populated when creating a new deploy token.
62+
- **token** (String, Sensitive) The secret token. This is only populated when creating a new deploy token. **Note**: The token is not available for imported resources.
6363

64+
## Import
6465

66+
Import is supported using the following syntax:
67+
68+
```shell
69+
# GitLab deploy tokens can be imported using an id made up of `{type}:{type_id}:{deploy_token_id}`, where type is one of: project, group.
70+
terraform import gitlab_deploy_token.group_token group:1:3
71+
terraform import gitlab_deploy_token.project_token project:1:4
72+
73+
# Note: the `token` resource attribute is not available for imported resources as this information cannot be read from the GitLab API.
74+
```

docs/resources/project_access_token.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,16 @@ resource "gitlab_project_variable" "example" {
5050
- **active** (Boolean) True if the token is active.
5151
- **created_at** (String) Time the token has been created, RFC3339 format.
5252
- **revoked** (Boolean) True if the token is revoked.
53-
- **token** (String, Sensitive) The secret token. This is only populated when creating a new project access token.
53+
- **token** (String, Sensitive) The secret token. **Note**: the token is not available for imported resources.
5454
- **user_id** (Number) The user_id associated to the token.
5555

56+
## Import
5657

58+
Import is supported using the following syntax:
59+
60+
```shell
61+
# A GitLab Project Access Token can be imported using a key composed of `<project-id>:<token-id>`, e.g.
62+
terraform import gitlab_project_access_token.example "12345:1"
63+
64+
# NOTE: the `token` resource attribute is not available for imported resources as this information cannot be read from the GitLab API.
65+
```

docs/resources/project_hook.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,16 @@ resource "gitlab_project_hook" "example" {
4747
- **push_events_branch_filter** (String) Invoke the hook for push events on matching branches only.
4848
- **releases_events** (Boolean) Invoke the hook for releases events.
4949
- **tag_push_events** (Boolean) Invoke the hook for tag push events.
50-
- **token** (String, Sensitive) A token to present when invoking the hook.
50+
- **token** (String, Sensitive) A token to present when invoking the hook. The token is not available for imported resources.
5151
- **wiki_page_events** (Boolean) Invoke the hook for wiki page events.
5252

53+
## Import
5354

55+
Import is supported using the following syntax:
56+
57+
```shell
58+
# A GitLab Project Hook can be imported using a key composed of `<project-id>:<hook-id>`, e.g.
59+
terraform import gitlab_project_hook.example "12345:1"
60+
61+
# NOTE: the `token` resource attribute is not available for imported resources as this information cannot be read from the GitLab API.
62+
```
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# GitLab deploy tokens can be imported using an id made up of `{type}:{type_id}:{deploy_token_id}`, where type is one of: project, group.
2+
terraform import gitlab_deploy_token.group_token group:1:3
3+
terraform import gitlab_deploy_token.project_token project:1:4
4+
5+
# Note: the `token` resource attribute is not available for imported resources as this information cannot be read from the GitLab API.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# A GitLab Project Access Token can be imported using a key composed of `<project-id>:<token-id>`, e.g.
2+
terraform import gitlab_project_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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# A GitLab Project Hook can be imported using a key composed of `<project-id>:<hook-id>`, e.g.
2+
terraform import gitlab_project_hook.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.

internal/provider/resource_gitlab_deploy_token.go

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"log"
77
"net/http"
88
"strconv"
9+
"strings"
910
"time"
1011

1112
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
@@ -15,7 +16,6 @@ import (
1516
)
1617

1718
var _ = registerResource("gitlab_deploy_token", func() *schema.Resource {
18-
// lintignore: XR002 // TODO: Resolve this tfproviderlint issue
1919
return &schema.Resource{
2020
Description: `The ` + "`gitlab_deploy_token`" + ` resource allows to manage the lifecycle of group and project deploy tokens.
2121
@@ -24,6 +24,9 @@ var _ = registerResource("gitlab_deploy_token", func() *schema.Resource {
2424
CreateContext: resourceGitlabDeployTokenCreate,
2525
ReadContext: resourceGitlabDeployTokenRead,
2626
DeleteContext: resourceGitlabDeployTokenDelete,
27+
Importer: &schema.ResourceImporter{
28+
StateContext: resourceGitlabDeployTokenStateImporter,
29+
},
2730

2831
Schema: map[string]*schema.Schema{
2932
"project": {
@@ -80,7 +83,7 @@ var _ = registerResource("gitlab_deploy_token", func() *schema.Resource {
8083
},
8184

8285
"token": {
83-
Description: "The secret token. This is only populated when creating a new deploy token.",
86+
Description: "The secret token. This is only populated when creating a new deploy token. **Note**: The token is not available for imported resources.",
8487
Type: schema.TypeString,
8588
Computed: true,
8689
Sensitive: true,
@@ -257,3 +260,25 @@ func resourceGitlabDeployTokenDelete(ctx context.Context, d *schema.ResourceData
257260

258261
return nil
259262
}
263+
264+
func resourceGitlabDeployTokenStateImporter(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
265+
s := strings.Split(d.Id(), ":")
266+
if len(s) != 3 {
267+
d.SetId("")
268+
return nil, fmt.Errorf("Invalid Deploy Token import format; expected '{type}:{type_id}:{deploy_token_id}' where the type can either be a group or project")
269+
}
270+
271+
tokenType, typeID, id := s[0], s[1], s[2]
272+
273+
d.SetId(id)
274+
switch tokenType {
275+
case "project":
276+
d.Set("project", typeID)
277+
case "group":
278+
d.Set("group", typeID)
279+
default:
280+
d.SetId("")
281+
return nil, fmt.Errorf("Invalid Deploy Token import format; expected '{type}:{type_id}:{deploy_token_id}' where the type can either be a group or project")
282+
}
283+
return []*schema.ResourceData{d}, nil
284+
}

internal/provider/resource_gitlab_deploy_token_test.go

Lines changed: 62 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,49 @@ import (
55
"strconv"
66
"testing"
77

8-
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
98
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
109
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
1110
"github.com/xanzy/go-gitlab"
1211
)
1312

1413
func TestAccGitlabDeployToken_basic(t *testing.T) {
15-
var deployToken gitlab.DeployToken
16-
rInt := acctest.RandInt()
14+
testAccCheck(t)
15+
16+
var projectDeployToken gitlab.DeployToken
17+
var groupDeployToken gitlab.DeployToken
18+
19+
testProject := testAccCreateProject(t)
20+
testGroup := testAccCreateGroups(t, 1)[0]
1721

1822
resource.Test(t, resource.TestCase{
1923
PreCheck: func() { testAccPreCheck(t) },
2024
ProviderFactories: providerFactories,
2125
CheckDestroy: testAccCheckGitlabDeployTokenDestroy,
2226
Steps: []resource.TestStep{
2327
{
24-
Config: testAccGitlabDeployTokenConfig(rInt),
28+
Config: testAccGitlabDeployTokenConfig(testProject.ID, testGroup.ID),
2529
Check: resource.ComposeTestCheckFunc(
26-
testAccCheckGitlabDeployTokenExists("gitlab_deploy_token.foo", &deployToken),
27-
testAccCheckGitlabDeployTokenAttributes(&deployToken, &testAccCheckGitlabDeployTokenExpectedAttributes{
28-
Name: fmt.Sprintf("deployToken-%d", rInt),
29-
Username: "my-username",
30-
}),
30+
testAccCheckGitlabDeployTokenExists("gitlab_deploy_token.project_token", &projectDeployToken),
31+
resource.TestCheckResourceAttrSet("gitlab_deploy_token.project_token", "token"),
32+
testAccCheckGitlabDeployTokenExists("gitlab_deploy_token.group_token", &groupDeployToken),
33+
resource.TestCheckResourceAttrSet("gitlab_deploy_token.group_token", "token"),
3134
),
3235
},
36+
// Verify import
37+
{
38+
ResourceName: "gitlab_deploy_token.project_token",
39+
ImportStateIdFunc: getDeployTokenImportID("gitlab_deploy_token.project_token"),
40+
ImportState: true,
41+
ImportStateVerify: true,
42+
ImportStateVerifyIgnore: []string{"token"},
43+
},
44+
{
45+
ResourceName: "gitlab_deploy_token.group_token",
46+
ImportStateIdFunc: getDeployTokenImportID("gitlab_deploy_token.group_token"),
47+
ImportState: true,
48+
ImportStateVerify: true,
49+
ImportStateVerifyIgnore: []string{"token"},
50+
},
3351
},
3452
})
3553
}
@@ -98,23 +116,27 @@ func testAccCheckGitlabDeployTokenExists(n string, deployToken *gitlab.DeployTok
98116
}
99117
}
100118

101-
type testAccCheckGitlabDeployTokenExpectedAttributes struct {
102-
Name string
103-
Username string
104-
}
105-
106-
func testAccCheckGitlabDeployTokenAttributes(deployToken *gitlab.DeployToken, want *testAccCheckGitlabDeployTokenExpectedAttributes) resource.TestCheckFunc {
107-
return func(s *terraform.State) error {
108-
109-
if deployToken.Name != want.Name {
110-
return fmt.Errorf("got name %q; want %q", deployToken.Name, want.Name)
119+
func getDeployTokenImportID(n string) resource.ImportStateIdFunc {
120+
return func(s *terraform.State) (string, error) {
121+
rs, ok := s.RootModule().Resources[n]
122+
if !ok {
123+
return "", fmt.Errorf("Not Found: %s", n)
111124
}
112125

113-
if deployToken.Username != want.Username {
114-
return fmt.Errorf("got username %q; want %q", deployToken.Username, want.Username)
126+
deployTokenID := rs.Primary.ID
127+
if deployTokenID == "" {
128+
return "", fmt.Errorf("No deploy token ID is set")
129+
}
130+
projectID := rs.Primary.Attributes["project"]
131+
if projectID != "" {
132+
return fmt.Sprintf("project:%s:%s", projectID, deployTokenID), nil
133+
}
134+
groupID := rs.Primary.Attributes["group"]
135+
if groupID != "" {
136+
return fmt.Sprintf("group:%s:%s", groupID, deployTokenID), nil
115137
}
116138

117-
return nil
139+
return "", fmt.Errorf("No project or group ID is set")
118140
}
119141
}
120142

@@ -158,20 +180,27 @@ func testAccCheckGitlabDeployTokenDestroy(s *terraform.State) error {
158180
return nil
159181
}
160182

161-
func testAccGitlabDeployTokenConfig(rInt int) string {
183+
func testAccGitlabDeployTokenConfig(projectID int, groupID int) string {
162184
return fmt.Sprintf(`
163-
resource "gitlab_project" "foo" {
164-
name = "foo-%d"
165-
description = "Terraform acceptance test"
185+
resource "gitlab_deploy_token" "project_token" {
186+
project = "%d"
187+
name = "project-deploy-token"
188+
username = "my-username"
166189
167-
# So that acceptance tests can be run in a gitlab organization
168-
# with no billing
169-
visibility_level = "public"
190+
expires_at = "2021-03-14T07:20:50.000Z"
191+
192+
scopes = [
193+
"read_registry",
194+
"read_repository",
195+
"read_package_registry",
196+
"write_registry",
197+
"write_package_registry",
198+
]
170199
}
171200
172-
resource "gitlab_deploy_token" "foo" {
173-
project = "${gitlab_project.foo.id}"
174-
name = "deployToken-%d"
201+
resource "gitlab_deploy_token" "group_token" {
202+
group = "%d"
203+
name = "group-deploy-token"
175204
username = "my-username"
176205
177206
expires_at = "2021-03-14T07:20:50.000Z"
@@ -184,7 +213,7 @@ resource "gitlab_deploy_token" "foo" {
184213
"write_package_registry",
185214
]
186215
}
187-
`, rInt, rInt)
216+
`, projectID, groupID)
188217
}
189218

190219
func testAccGitlabDeployTokenPaginationConfig(numberOfTokens int, groupID int, projectID int) string {

internal/provider/resource_gitlab_group_ldap_link.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
)
1414

1515
var _ = registerResource("gitlab_group_ldap_link", func() *schema.Resource {
16-
// lintignore: XR002 // TODO: Resolve this tfproviderlint issue
1716
return &schema.Resource{
1817
Description: `The ` + "`gitlab_group_ldap_link`" + ` resource allows to manage the lifecycle of an LDAP integration with a group.
1918

internal/provider/resource_gitlab_project_access_token.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
)
1515

1616
var _ = registerResource("gitlab_project_access_token", func() *schema.Resource {
17-
// lintignore: XR002 // TODO: Resolve this tfproviderlint issue
1817
return &schema.Resource{
1918
Description: `The ` + "`" + `gitlab_project_access_token` + "`" + ` resource allows to manage the lifecycle of a project access token.
2019
@@ -23,6 +22,9 @@ var _ = registerResource("gitlab_project_access_token", func() *schema.Resource
2322
CreateContext: resourceGitlabProjectAccessTokenCreate,
2423
ReadContext: resourceGitlabProjectAccessTokenRead,
2524
DeleteContext: resourceGitlabProjectAccessTokenDelete,
25+
Importer: &schema.ResourceImporter{
26+
StateContext: schema.ImportStatePassthroughContext,
27+
},
2628

2729
Schema: map[string]*schema.Schema{
2830
"project": {
@@ -63,7 +65,7 @@ var _ = registerResource("gitlab_project_access_token", func() *schema.Resource
6365
ForceNew: true,
6466
},
6567
"token": {
66-
Description: "The secret token. This is only populated when creating a new project access token.",
68+
Description: "The secret token. **Note**: the token is not available for imported resources.",
6769
Type: schema.TypeString,
6870
Computed: true,
6971
Sensitive: true,

0 commit comments

Comments
 (0)