Skip to content

Commit ffea585

Browse files
committed
resource/gitlab_project_hook: Add importer
Refs: #36
1 parent f717f4b commit ffea585

File tree

4 files changed

+61
-3
lines changed

4 files changed

+61
-3
lines changed

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: 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_project_hook.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ import (
55
"fmt"
66
"log"
77
"strconv"
8+
"strings"
89

910
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1011
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1112
gitlab "github.com/xanzy/go-gitlab"
1213
)
1314

1415
var _ = registerResource("gitlab_project_hook", func() *schema.Resource {
15-
// lintignore: XR002 // TODO: Resolve this tfproviderlint issue
1616
return &schema.Resource{
1717
Description: `The ` + "`" + `gitlab_project_hook` + "`" + ` resource allows to manage the lifecycle of a project hook.
1818
@@ -22,6 +22,9 @@ var _ = registerResource("gitlab_project_hook", func() *schema.Resource {
2222
ReadContext: resourceGitlabProjectHookRead,
2323
UpdateContext: resourceGitlabProjectHookUpdate,
2424
DeleteContext: resourceGitlabProjectHookDelete,
25+
Importer: &schema.ResourceImporter{
26+
StateContext: resourceGitlabProjectHookStateImporter,
27+
},
2528

2629
Schema: map[string]*schema.Schema{
2730
"project": {
@@ -35,7 +38,7 @@ var _ = registerResource("gitlab_project_hook", func() *schema.Resource {
3538
Required: true,
3639
},
3740
"token": {
38-
Description: "A token to present when invoking the hook.",
41+
Description: "A token to present when invoking the hook. The token is not available for imported resources.",
3942
Type: schema.TypeString,
4043
Optional: true,
4144
Sensitive: true,
@@ -160,6 +163,7 @@ func resourceGitlabProjectHookCreate(ctx context.Context, d *schema.ResourceData
160163
}
161164

162165
d.SetId(fmt.Sprintf("%d", hook.ID))
166+
d.Set("token", options.Token)
163167

164168
return resourceGitlabProjectHookRead(ctx, d, meta)
165169
}
@@ -256,3 +260,17 @@ func resourceGitlabProjectHookDelete(ctx context.Context, d *schema.ResourceData
256260

257261
return nil
258262
}
263+
264+
func resourceGitlabProjectHookStateImporter(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
265+
s := strings.Split(d.Id(), ":")
266+
if len(s) != 2 {
267+
d.SetId("")
268+
return nil, fmt.Errorf("Invalid Project Hook import format; expected '{project_id}:{hook_id}'")
269+
}
270+
project, id := s[0], s[1]
271+
272+
d.SetId(id)
273+
d.Set("project", project)
274+
275+
return []*schema.ResourceData{d}, nil
276+
}

internal/provider/resource_gitlab_project_hook_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,14 @@ func TestAccGitlabProjectHook_basic(t *testing.T) {
6868
}),
6969
),
7070
},
71+
// Verify import
72+
{
73+
ResourceName: "gitlab_project_hook.foo",
74+
ImportStateIdFunc: getProjectHookImportID("gitlab_project_hook.foo"),
75+
ImportState: true,
76+
ImportStateVerify: true,
77+
ImportStateVerifyIgnore: []string{"token"},
78+
},
7179
},
7280
})
7381
}
@@ -203,6 +211,25 @@ func testAccCheckGitlabProjectHookDestroy(s *terraform.State) error {
203211
return nil
204212
}
205213

214+
func getProjectHookImportID(n string) resource.ImportStateIdFunc {
215+
return func(s *terraform.State) (string, error) {
216+
rs, ok := s.RootModule().Resources[n]
217+
if !ok {
218+
return "", fmt.Errorf("Not Found: %s", n)
219+
}
220+
221+
hookID := rs.Primary.ID
222+
if hookID == "" {
223+
return "", fmt.Errorf("No hook ID is set")
224+
}
225+
projectID := rs.Primary.Attributes["project"]
226+
if projectID == "" {
227+
return "", fmt.Errorf("No project ID is set")
228+
}
229+
return fmt.Sprintf("%s:%s", projectID, hookID), nil
230+
}
231+
}
232+
206233
func testAccGitlabProjectHookConfig(rInt int) string {
207234
return fmt.Sprintf(`
208235
resource "gitlab_project" "foo" {

0 commit comments

Comments
 (0)