Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit cdab8f5

Browse files
authoredAug 24, 2022
Merge pull request #1221 from timofurrer/feature/group-hooks
Group Hook Resource and Data Sources
2 parents 00a121b + 412cae7 commit cdab8f5

15 files changed

+888
-0
lines changed
 

‎docs/data-sources/group_hook.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "gitlab_group_hook Data Source - terraform-provider-gitlab"
4+
subcategory: ""
5+
description: |-
6+
The gitlab_group_hook data source allows to retrieve details about a hook in a group.
7+
Upstream API: GitLab REST API docs https://docs.gitlab.com/ee/api/groups.html#get-group-hook
8+
---
9+
10+
# gitlab_group_hook (Data Source)
11+
12+
The `gitlab_group_hook` data source allows to retrieve details about a hook in a group.
13+
14+
**Upstream API**: [GitLab REST API docs](https://docs.gitlab.com/ee/api/groups.html#get-group-hook)
15+
16+
## Example Usage
17+
18+
```terraform
19+
data "gitlab_group" "example" {
20+
id = "foo/bar/baz"
21+
}
22+
23+
data "gitlab_group_hook" "example" {
24+
group = data.gitlab_group.example.id
25+
hook_id = 1
26+
}
27+
```
28+
29+
<!-- schema generated by tfplugindocs -->
30+
## Schema
31+
32+
### Required
33+
34+
- `group` (String) The ID or full path of the group.
35+
- `hook_id` (Number) The id of the group hook.
36+
37+
### Read-Only
38+
39+
- `confidential_issues_events` (Boolean) Invoke the hook for confidential issues events.
40+
- `confidential_note_events` (Boolean) Invoke the hook for confidential notes events.
41+
- `deployment_events` (Boolean) Invoke the hook for deployment events.
42+
- `enable_ssl_verification` (Boolean) Enable ssl verification when invoking the hook.
43+
- `group_id` (Number) The id of the group for the hook.
44+
- `id` (String) The ID of this resource.
45+
- `issues_events` (Boolean) Invoke the hook for issues events.
46+
- `job_events` (Boolean) Invoke the hook for job events.
47+
- `merge_requests_events` (Boolean) Invoke the hook for merge requests.
48+
- `note_events` (Boolean) Invoke the hook for notes events.
49+
- `pipeline_events` (Boolean) Invoke the hook for pipeline events.
50+
- `push_events` (Boolean) Invoke the hook for push events.
51+
- `push_events_branch_filter` (String) Invoke the hook for push events on matching branches only.
52+
- `releases_events` (Boolean) Invoke the hook for releases events.
53+
- `subgroup_events` (Boolean) Invoke the hook for subgroup events.
54+
- `tag_push_events` (Boolean) Invoke the hook for tag push events.
55+
- `token` (String) A token to present when invoking the hook. The token is not available for imported resources.
56+
- `url` (String) The url of the hook to invoke.
57+
- `wiki_page_events` (Boolean) Invoke the hook for wiki page events.
58+
59+

‎docs/data-sources/group_hooks.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "gitlab_group_hooks Data Source - terraform-provider-gitlab"
4+
subcategory: ""
5+
description: |-
6+
The gitlab_group_hooks data source allows to retrieve details about hooks in a group.
7+
Upstream API: GitLab REST API docs https://docs.gitlab.com/ee/api/groups.html#list-group-hooks
8+
---
9+
10+
# gitlab_group_hooks (Data Source)
11+
12+
The `gitlab_group_hooks` data source allows to retrieve details about hooks in a group.
13+
14+
**Upstream API**: [GitLab REST API docs](https://docs.gitlab.com/ee/api/groups.html#list-group-hooks)
15+
16+
## Example Usage
17+
18+
```terraform
19+
data "gitlab_group" "example" {
20+
id = "foo/bar/baz"
21+
}
22+
23+
data "gitlab_group_hooks" "examples" {
24+
group = data.gitlab_group.example.id
25+
}
26+
```
27+
28+
<!-- schema generated by tfplugindocs -->
29+
## Schema
30+
31+
### Required
32+
33+
- `group` (String) The ID or full path of the group.
34+
35+
### Read-Only
36+
37+
- `hooks` (List of Object) The list of hooks. (see [below for nested schema](#nestedatt--hooks))
38+
- `id` (String) The ID of this resource.
39+
40+
<a id="nestedatt--hooks"></a>
41+
### Nested Schema for `hooks`
42+
43+
Read-Only:
44+
45+
- `confidential_issues_events` (Boolean)
46+
- `confidential_note_events` (Boolean)
47+
- `deployment_events` (Boolean)
48+
- `enable_ssl_verification` (Boolean)
49+
- `group` (String)
50+
- `group_id` (Number)
51+
- `hook_id` (Number)
52+
- `issues_events` (Boolean)
53+
- `job_events` (Boolean)
54+
- `merge_requests_events` (Boolean)
55+
- `note_events` (Boolean)
56+
- `pipeline_events` (Boolean)
57+
- `push_events` (Boolean)
58+
- `push_events_branch_filter` (String)
59+
- `releases_events` (Boolean)
60+
- `subgroup_events` (Boolean)
61+
- `tag_push_events` (Boolean)
62+
- `token` (String)
63+
- `url` (String)
64+
- `wiki_page_events` (Boolean)
65+
66+

‎docs/resources/group_hook.md

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "gitlab_group_hook Resource - terraform-provider-gitlab"
4+
subcategory: ""
5+
description: |-
6+
The gitlab_group_hook resource allows to manage the lifecycle of a group hook.
7+
Upstream API: GitLab REST API docs https://docs.gitlab.com/ee/api/groups.html#hooks
8+
---
9+
10+
# gitlab_group_hook (Resource)
11+
12+
The `gitlab_group_hook` resource allows to manage the lifecycle of a group hook.
13+
14+
**Upstream API**: [GitLab REST API docs](https://docs.gitlab.com/ee/api/groups.html#hooks)
15+
16+
## Example Usage
17+
18+
```terraform
19+
resource "gitlab_group_hook" "example" {
20+
group = "example/hooked"
21+
url = "https://example.com/hook/example"
22+
merge_requests_events = true
23+
}
24+
25+
# Setting all attributes
26+
resource "gitlab_group_hook" "all_attributes" {
27+
group = 1
28+
url = "http://example.com"
29+
token = "supersecret"
30+
enable_ssl_verification = false
31+
push_events = true
32+
push_events_branch_filter = "devel"
33+
issues_events = false
34+
confidential_issues_events = false
35+
merge_requests_events = true
36+
tag_push_events = true
37+
note_events = true
38+
confidential_note_events = true
39+
job_events = true
40+
pipeline_events = true
41+
wiki_page_events = true
42+
deployment_events = true
43+
releases_events = true
44+
subgroup_events = true
45+
}
46+
```
47+
48+
<!-- schema generated by tfplugindocs -->
49+
## Schema
50+
51+
### Required
52+
53+
- `group` (String) The ID or full path of the group.
54+
- `url` (String) The url of the hook to invoke.
55+
56+
### Optional
57+
58+
- `confidential_issues_events` (Boolean) Invoke the hook for confidential issues events.
59+
- `confidential_note_events` (Boolean) Invoke the hook for confidential notes events.
60+
- `deployment_events` (Boolean) Invoke the hook for deployment events.
61+
- `enable_ssl_verification` (Boolean) Enable ssl verification when invoking the hook.
62+
- `issues_events` (Boolean) Invoke the hook for issues events.
63+
- `job_events` (Boolean) Invoke the hook for job events.
64+
- `merge_requests_events` (Boolean) Invoke the hook for merge requests.
65+
- `note_events` (Boolean) Invoke the hook for notes events.
66+
- `pipeline_events` (Boolean) Invoke the hook for pipeline events.
67+
- `push_events` (Boolean) Invoke the hook for push events.
68+
- `push_events_branch_filter` (String) Invoke the hook for push events on matching branches only.
69+
- `releases_events` (Boolean) Invoke the hook for releases events.
70+
- `subgroup_events` (Boolean) Invoke the hook for subgroup events.
71+
- `tag_push_events` (Boolean) Invoke the hook for tag push events.
72+
- `token` (String, Sensitive) A token to present when invoking the hook. The token is not available for imported resources.
73+
- `wiki_page_events` (Boolean) Invoke the hook for wiki page events.
74+
75+
### Read-Only
76+
77+
- `group_id` (Number) The id of the group for the hook.
78+
- `hook_id` (Number) The id of the group hook.
79+
- `id` (String) The ID of this resource.
80+
81+
## Import
82+
83+
Import is supported using the following syntax:
84+
85+
```shell
86+
# A GitLab Group Hook can be imported using a key composed of `<group-id>:<hook-id>`, e.g.
87+
terraform import gitlab_group_hook.example "12345:1"
88+
89+
# NOTE: the `token` resource attribute is not available for imported resources as this information cannot be read from the GitLab API.
90+
```
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
data "gitlab_group" "example" {
2+
id = "foo/bar/baz"
3+
}
4+
5+
data "gitlab_group_hook" "example" {
6+
group = data.gitlab_group.example.id
7+
hook_id = 1
8+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
data "gitlab_group" "example" {
2+
id = "foo/bar/baz"
3+
}
4+
5+
data "gitlab_group_hooks" "examples" {
6+
group = data.gitlab_group.example.id
7+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# A GitLab Group Hook can be imported using a key composed of `<group-id>:<hook-id>`, e.g.
2+
terraform import gitlab_group_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.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
resource "gitlab_group_hook" "example" {
2+
group = "example/hooked"
3+
url = "https://example.com/hook/example"
4+
merge_requests_events = true
5+
}
6+
7+
# Setting all attributes
8+
resource "gitlab_group_hook" "all_attributes" {
9+
group = 1
10+
url = "http://example.com"
11+
token = "supersecret"
12+
enable_ssl_verification = false
13+
push_events = true
14+
push_events_branch_filter = "devel"
15+
issues_events = false
16+
confidential_issues_events = false
17+
merge_requests_events = true
18+
tag_push_events = true
19+
note_events = true
20+
confidential_note_events = true
21+
job_events = true
22+
pipeline_events = true
23+
wiki_page_events = true
24+
deployment_events = true
25+
releases_events = true
26+
subgroup_events = true
27+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package provider
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
9+
"github.com/xanzy/go-gitlab"
10+
)
11+
12+
var _ = registerDataSource("gitlab_group_hook", func() *schema.Resource {
13+
return &schema.Resource{
14+
Description: `The ` + "`gitlab_group_hook`" + ` data source allows to retrieve details about a hook in a group.
15+
16+
**Upstream API**: [GitLab REST API docs](https://docs.gitlab.com/ee/api/groups.html#get-group-hook)`,
17+
18+
ReadContext: dataSourceGitlabGroupHookRead,
19+
Schema: datasourceSchemaFromResourceSchema(gitlabGroupHookSchema(), []string{"group", "hook_id"}, nil),
20+
}
21+
})
22+
23+
func dataSourceGitlabGroupHookRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
24+
client := meta.(*gitlab.Client)
25+
group := d.Get("group").(string)
26+
hookID := d.Get("hook_id").(int)
27+
28+
hook, _, err := client.Groups.GetGroupHook(group, hookID, gitlab.WithContext(ctx))
29+
if err != nil {
30+
return diag.FromErr(err)
31+
}
32+
33+
d.SetId(fmt.Sprintf("%s:%d", group, hookID))
34+
stateMap := gitlabGroupHookToStateMap(group, hook)
35+
if err := setStateMapInResourceData(stateMap, d); err != nil {
36+
return diag.FromErr(err)
37+
}
38+
return nil
39+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//go:build acceptance
2+
// +build acceptance
3+
4+
package provider
5+
6+
import (
7+
"fmt"
8+
"testing"
9+
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
11+
)
12+
13+
func TestAccDataSourceGitlabGroupHook_basic(t *testing.T) {
14+
testAccCheckEE(t)
15+
16+
testGroup := testAccCreateGroups(t, 1)[0]
17+
testHook := testAccCreateGroupHooks(t, testGroup.ID, 1)[0]
18+
19+
resource.ParallelTest(t, resource.TestCase{
20+
ProviderFactories: providerFactories,
21+
Steps: []resource.TestStep{
22+
{
23+
Config: fmt.Sprintf(`
24+
data "gitlab_group_hook" "this" {
25+
group = "%s"
26+
hook_id = %d
27+
}
28+
`, testGroup.FullPath, testHook.ID),
29+
Check: resource.ComposeTestCheckFunc(
30+
resource.TestCheckResourceAttr("data.gitlab_group_hook.this", "hook_id", fmt.Sprintf("%d", testHook.ID)),
31+
resource.TestCheckResourceAttr("data.gitlab_group_hook.this", "group_id", fmt.Sprintf("%d", testGroup.ID)),
32+
resource.TestCheckResourceAttr("data.gitlab_group_hook.this", "url", testHook.URL),
33+
),
34+
},
35+
},
36+
})
37+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package provider
2+
3+
import (
4+
"context"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
8+
"github.com/xanzy/go-gitlab"
9+
)
10+
11+
var _ = registerDataSource("gitlab_group_hooks", func() *schema.Resource {
12+
return &schema.Resource{
13+
Description: `The ` + "`gitlab_group_hooks`" + ` data source allows to retrieve details about hooks in a group.
14+
15+
**Upstream API**: [GitLab REST API docs](https://docs.gitlab.com/ee/api/groups.html#list-group-hooks)`,
16+
17+
ReadContext: dataSourceGitlabGroupHooksRead,
18+
Schema: map[string]*schema.Schema{
19+
"group": {
20+
Description: "The ID or full path of the group.",
21+
Type: schema.TypeString,
22+
Required: true,
23+
},
24+
"hooks": {
25+
Description: "The list of hooks.",
26+
Type: schema.TypeList,
27+
Computed: true,
28+
Elem: &schema.Resource{
29+
Schema: datasourceSchemaFromResourceSchema(gitlabGroupHookSchema(), nil, nil),
30+
},
31+
},
32+
},
33+
}
34+
})
35+
36+
func dataSourceGitlabGroupHooksRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
37+
client := meta.(*gitlab.Client)
38+
39+
group := d.Get("group").(string)
40+
options := gitlab.ListGroupHooksOptions{
41+
PerPage: 20,
42+
Page: 1,
43+
}
44+
45+
var hooks []*gitlab.GroupHook
46+
for options.Page != 0 {
47+
paginatedHooks, resp, err := client.Groups.ListGroupHooks(group, &options, gitlab.WithContext(ctx))
48+
if err != nil {
49+
return diag.FromErr(err)
50+
}
51+
52+
hooks = append(hooks, paginatedHooks...)
53+
options.Page = resp.NextPage
54+
}
55+
56+
d.SetId(group)
57+
if err := d.Set("hooks", flattenGitlabGroupHooks(group, hooks)); err != nil {
58+
return diag.Errorf("failed to set hooks to state: %v", err)
59+
}
60+
61+
return nil
62+
}
63+
64+
func flattenGitlabGroupHooks(group string, hooks []*gitlab.GroupHook) (values []map[string]interface{}) {
65+
for _, hook := range hooks {
66+
values = append(values, gitlabGroupHookToStateMap(group, hook))
67+
}
68+
return values
69+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//go:build acceptance
2+
// +build acceptance
3+
4+
package provider
5+
6+
import (
7+
"fmt"
8+
"testing"
9+
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
11+
)
12+
13+
func TestAccDataSourceGitlabGroupHooks_basic(t *testing.T) {
14+
testAccCheckEE(t)
15+
16+
testGroup := testAccCreateGroups(t, 1)[0]
17+
testHooks := testAccCreateGroupHooks(t, testGroup.ID, 25)
18+
19+
resource.ParallelTest(t, resource.TestCase{
20+
ProviderFactories: providerFactories,
21+
Steps: []resource.TestStep{
22+
{
23+
Config: fmt.Sprintf(`
24+
data "gitlab_group_hooks" "this" {
25+
group = "%s"
26+
}
27+
`, testGroup.FullPath),
28+
Check: resource.ComposeTestCheckFunc(
29+
resource.TestCheckResourceAttr("data.gitlab_group_hooks.this", "hooks.#", fmt.Sprintf("%d", len(testHooks))),
30+
resource.TestCheckResourceAttr("data.gitlab_group_hooks.this", "hooks.0.url", testHooks[0].URL),
31+
resource.TestCheckResourceAttr("data.gitlab_group_hooks.this", "hooks.1.url", testHooks[1].URL),
32+
),
33+
},
34+
},
35+
})
36+
}

‎internal/provider/helper_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,22 @@ func testAccCreateSubGroups(t *testing.T, parentGroup *gitlab.Group, n int) []*g
249249
return groups
250250
}
251251

252+
func testAccCreateGroupHooks(t *testing.T, gid interface{}, n int) []*gitlab.GroupHook {
253+
t.Helper()
254+
255+
var hooks []*gitlab.GroupHook
256+
for i := 0; i < n; i++ {
257+
hook, _, err := testGitlabClient.Groups.AddGroupHook(gid, &gitlab.AddGroupHookOptions{
258+
URL: gitlab.String(fmt.Sprintf("https://%s.com", acctest.RandomWithPrefix("acctest"))),
259+
})
260+
if err != nil {
261+
t.Fatalf("could not create group hook: %v", err)
262+
}
263+
hooks = append(hooks, hook)
264+
}
265+
return hooks
266+
}
267+
252268
// testAccCreateBranches is a test helper for creating a specified number of branches.
253269
// It assumes the project will be destroyed at the end of the test and will not cleanup created branches.
254270
func testAccCreateBranches(t *testing.T, project *gitlab.Project, n int) []*gitlab.Branch {
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
package provider
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"log"
7+
"strconv"
8+
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
11+
gitlab "github.com/xanzy/go-gitlab"
12+
)
13+
14+
var _ = registerResource("gitlab_group_hook", func() *schema.Resource {
15+
return &schema.Resource{
16+
Description: `The ` + "`" + `gitlab_group_hook` + "`" + ` resource allows to manage the lifecycle of a group hook.
17+
18+
**Upstream API**: [GitLab REST API docs](https://docs.gitlab.com/ee/api/groups.html#hooks)`,
19+
20+
CreateContext: resourceGitlabGroupHookCreate,
21+
ReadContext: resourceGitlabGroupHookRead,
22+
UpdateContext: resourceGitlabGroupHookUpdate,
23+
DeleteContext: resourceGitlabGroupHookDelete,
24+
Importer: &schema.ResourceImporter{
25+
StateContext: schema.ImportStatePassthroughContext,
26+
},
27+
Schema: gitlabGroupHookSchema(),
28+
}
29+
})
30+
31+
func resourceGitlabGroupHookCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
32+
client := meta.(*gitlab.Client)
33+
group := d.Get("group").(string)
34+
options := &gitlab.AddGroupHookOptions{
35+
URL: gitlab.String(d.Get("url").(string)),
36+
PushEvents: gitlab.Bool(d.Get("push_events").(bool)),
37+
PushEventsBranchFilter: gitlab.String(d.Get("push_events_branch_filter").(string)),
38+
IssuesEvents: gitlab.Bool(d.Get("issues_events").(bool)),
39+
ConfidentialIssuesEvents: gitlab.Bool(d.Get("confidential_issues_events").(bool)),
40+
MergeRequestsEvents: gitlab.Bool(d.Get("merge_requests_events").(bool)),
41+
TagPushEvents: gitlab.Bool(d.Get("tag_push_events").(bool)),
42+
NoteEvents: gitlab.Bool(d.Get("note_events").(bool)),
43+
ConfidentialNoteEvents: gitlab.Bool(d.Get("confidential_note_events").(bool)),
44+
JobEvents: gitlab.Bool(d.Get("job_events").(bool)),
45+
PipelineEvents: gitlab.Bool(d.Get("pipeline_events").(bool)),
46+
WikiPageEvents: gitlab.Bool(d.Get("wiki_page_events").(bool)),
47+
DeploymentEvents: gitlab.Bool(d.Get("deployment_events").(bool)),
48+
ReleasesEvents: gitlab.Bool(d.Get("releases_events").(bool)),
49+
SubGroupEvents: gitlab.Bool(d.Get("subgroup_events").(bool)),
50+
EnableSSLVerification: gitlab.Bool(d.Get("enable_ssl_verification").(bool)),
51+
}
52+
53+
if v, ok := d.GetOk("token"); ok {
54+
options.Token = gitlab.String(v.(string))
55+
}
56+
57+
log.Printf("[DEBUG] create gitlab group hook %q", *options.URL)
58+
59+
hook, _, err := client.Groups.AddGroupHook(group, options, gitlab.WithContext(ctx))
60+
if err != nil {
61+
return diag.FromErr(err)
62+
}
63+
64+
d.SetId(resourceGitlabGroupHookBuildID(group, hook.ID))
65+
d.Set("token", options.Token)
66+
67+
return resourceGitlabGroupHookRead(ctx, d, meta)
68+
}
69+
70+
func resourceGitlabGroupHookRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
71+
group, hookID, err := resourceGitlabGroupHookParseID(d.Id())
72+
if err != nil {
73+
return diag.FromErr(err)
74+
}
75+
log.Printf("[DEBUG] read gitlab group hook %s/%d", group, hookID)
76+
77+
client := meta.(*gitlab.Client)
78+
hook, _, err := client.Groups.GetGroupHook(group, hookID, gitlab.WithContext(ctx))
79+
if err != nil {
80+
if is404(err) {
81+
log.Printf("[DEBUG] gitlab group hook not found %s/%d, removing from state", group, hookID)
82+
d.SetId("")
83+
return nil
84+
}
85+
return diag.FromErr(err)
86+
}
87+
88+
stateMap := gitlabGroupHookToStateMap(group, hook)
89+
if err = setStateMapInResourceData(stateMap, d); err != nil {
90+
return diag.FromErr(err)
91+
}
92+
return nil
93+
}
94+
95+
func resourceGitlabGroupHookUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
96+
group, hookID, err := resourceGitlabGroupHookParseID(d.Id())
97+
if err != nil {
98+
return diag.FromErr(err)
99+
}
100+
101+
client := meta.(*gitlab.Client)
102+
options := &gitlab.EditGroupHookOptions{
103+
URL: gitlab.String(d.Get("url").(string)),
104+
PushEvents: gitlab.Bool(d.Get("push_events").(bool)),
105+
PushEventsBranchFilter: gitlab.String(d.Get("push_events_branch_filter").(string)),
106+
IssuesEvents: gitlab.Bool(d.Get("issues_events").(bool)),
107+
ConfidentialIssuesEvents: gitlab.Bool(d.Get("confidential_issues_events").(bool)),
108+
MergeRequestsEvents: gitlab.Bool(d.Get("merge_requests_events").(bool)),
109+
TagPushEvents: gitlab.Bool(d.Get("tag_push_events").(bool)),
110+
NoteEvents: gitlab.Bool(d.Get("note_events").(bool)),
111+
ConfidentialNoteEvents: gitlab.Bool(d.Get("confidential_note_events").(bool)),
112+
JobEvents: gitlab.Bool(d.Get("job_events").(bool)),
113+
PipelineEvents: gitlab.Bool(d.Get("pipeline_events").(bool)),
114+
WikiPageEvents: gitlab.Bool(d.Get("wiki_page_events").(bool)),
115+
DeploymentEvents: gitlab.Bool(d.Get("deployment_events").(bool)),
116+
ReleasesEvents: gitlab.Bool(d.Get("releases_events").(bool)),
117+
SubGroupEvents: gitlab.Bool(d.Get("subgroup_events").(bool)),
118+
EnableSSLVerification: gitlab.Bool(d.Get("enable_ssl_verification").(bool)),
119+
}
120+
121+
if d.HasChange("token") {
122+
options.Token = gitlab.String(d.Get("token").(string))
123+
}
124+
125+
log.Printf("[DEBUG] update gitlab group hook %s", d.Id())
126+
127+
_, _, err = client.Groups.EditGroupHook(group, hookID, options, gitlab.WithContext(ctx))
128+
if err != nil {
129+
return diag.FromErr(err)
130+
}
131+
132+
return resourceGitlabGroupHookRead(ctx, d, meta)
133+
}
134+
135+
func resourceGitlabGroupHookDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
136+
group, hookID, err := resourceGitlabGroupHookParseID(d.Id())
137+
if err != nil {
138+
return diag.FromErr(err)
139+
}
140+
log.Printf("[DEBUG] Delete gitlab group hook %s/%d", group, hookID)
141+
142+
client := meta.(*gitlab.Client)
143+
_, err = client.Groups.DeleteGroupHook(group, hookID, gitlab.WithContext(ctx))
144+
if err != nil {
145+
return diag.FromErr(err)
146+
}
147+
148+
return nil
149+
}
150+
151+
func resourceGitlabGroupHookBuildID(group string, agentID int) string {
152+
return fmt.Sprintf("%s:%d", group, agentID)
153+
}
154+
155+
func resourceGitlabGroupHookParseID(id string) (string, int, error) {
156+
groupID, rawHookID, err := parseTwoPartID(id)
157+
if err != nil {
158+
return "", 0, err
159+
}
160+
161+
hookID, err := strconv.Atoi(rawHookID)
162+
if err != nil {
163+
return "", 0, err
164+
}
165+
166+
return groupID, hookID, nil
167+
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
//go:build acceptance
2+
// +build acceptance
3+
4+
package provider
5+
6+
import (
7+
"fmt"
8+
"testing"
9+
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
11+
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
12+
)
13+
14+
func TestAccGitlabGroupHook_basic(t *testing.T) {
15+
testAccCheckEE(t)
16+
17+
testGroup := testAccCreateGroups(t, 1)[0]
18+
19+
resource.ParallelTest(t, resource.TestCase{
20+
ProviderFactories: providerFactories,
21+
CheckDestroy: testAccCheckGitlabGroupHookDestroy,
22+
Steps: []resource.TestStep{
23+
// Create a Group Hook with required attributes only
24+
{
25+
Config: fmt.Sprintf(`
26+
resource "gitlab_group_hook" "this" {
27+
group = "%s"
28+
url = "http://example.com"
29+
}
30+
`, testGroup.FullPath),
31+
},
32+
// Verify Import
33+
{
34+
ResourceName: "gitlab_group_hook.this",
35+
ImportState: true,
36+
ImportStateVerify: true,
37+
ImportStateVerifyIgnore: []string{"token"},
38+
},
39+
// Update Group Hook to set all attributes
40+
{
41+
Config: fmt.Sprintf(`
42+
resource "gitlab_group_hook" "this" {
43+
group = "%s"
44+
url = "http://example.com"
45+
46+
token = "supersecret"
47+
enable_ssl_verification = false
48+
push_events = true
49+
push_events_branch_filter = "devel"
50+
issues_events = false
51+
confidential_issues_events = false
52+
merge_requests_events = true
53+
tag_push_events = true
54+
note_events = true
55+
confidential_note_events = true
56+
job_events = true
57+
pipeline_events = true
58+
wiki_page_events = true
59+
deployment_events = true
60+
releases_events = true
61+
subgroup_events = true
62+
}
63+
`, testGroup.FullPath),
64+
},
65+
// Verify Import
66+
{
67+
ResourceName: "gitlab_group_hook.this",
68+
ImportState: true,
69+
ImportStateVerify: true,
70+
ImportStateVerifyIgnore: []string{"token"},
71+
},
72+
// Update Group Hook to defaults again
73+
{
74+
Config: fmt.Sprintf(`
75+
resource "gitlab_group_hook" "this" {
76+
group = "%s"
77+
url = "http://example.com"
78+
}
79+
`, testGroup.FullPath),
80+
},
81+
// Verify Import
82+
{
83+
ResourceName: "gitlab_group_hook.this",
84+
ImportState: true,
85+
ImportStateVerify: true,
86+
ImportStateVerifyIgnore: []string{"token"},
87+
},
88+
},
89+
})
90+
}
91+
92+
func testAccCheckGitlabGroupHookDestroy(s *terraform.State) error {
93+
for _, rs := range s.RootModule().Resources {
94+
if rs.Type != "gitlab_group_hook" {
95+
continue
96+
}
97+
98+
group, hookID, err := resourceGitlabGroupHookParseID(rs.Primary.ID)
99+
if err != nil {
100+
return err
101+
}
102+
103+
_, _, err = testGitlabClient.Groups.GetGroupHook(group, hookID)
104+
if err == nil {
105+
return fmt.Errorf("Group Hook %d in group %s still exists", hookID, group)
106+
}
107+
if !is404(err) {
108+
return err
109+
}
110+
return nil
111+
}
112+
return nil
113+
}
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
package provider
2+
3+
import (
4+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
5+
"github.com/xanzy/go-gitlab"
6+
)
7+
8+
func gitlabGroupHookSchema() map[string]*schema.Schema {
9+
return map[string]*schema.Schema{
10+
"group": {
11+
Description: "The ID or full path of the group.",
12+
Type: schema.TypeString,
13+
Required: true,
14+
},
15+
"group_id": {
16+
Description: "The id of the group for the hook.",
17+
Type: schema.TypeInt,
18+
Computed: true,
19+
},
20+
"hook_id": {
21+
Description: "The id of the group hook.",
22+
Type: schema.TypeInt,
23+
Computed: true,
24+
},
25+
"url": {
26+
Description: "The url of the hook to invoke.",
27+
Type: schema.TypeString,
28+
Required: true,
29+
},
30+
"token": {
31+
Description: "A token to present when invoking the hook. The token is not available for imported resources.",
32+
Type: schema.TypeString,
33+
Optional: true,
34+
Sensitive: true,
35+
},
36+
"push_events": {
37+
Description: "Invoke the hook for push events.",
38+
Type: schema.TypeBool,
39+
Optional: true,
40+
Default: true,
41+
},
42+
"push_events_branch_filter": {
43+
Description: "Invoke the hook for push events on matching branches only.",
44+
Type: schema.TypeString,
45+
Optional: true,
46+
},
47+
"issues_events": {
48+
Description: "Invoke the hook for issues events.",
49+
Type: schema.TypeBool,
50+
Optional: true,
51+
Default: false,
52+
},
53+
"confidential_issues_events": {
54+
Description: "Invoke the hook for confidential issues events.",
55+
Type: schema.TypeBool,
56+
Optional: true,
57+
Default: false,
58+
},
59+
"merge_requests_events": {
60+
Description: "Invoke the hook for merge requests.",
61+
Type: schema.TypeBool,
62+
Optional: true,
63+
Default: false,
64+
},
65+
"tag_push_events": {
66+
Description: "Invoke the hook for tag push events.",
67+
Type: schema.TypeBool,
68+
Optional: true,
69+
Default: false,
70+
},
71+
"note_events": {
72+
Description: "Invoke the hook for notes events.",
73+
Type: schema.TypeBool,
74+
Optional: true,
75+
Default: false,
76+
},
77+
"confidential_note_events": {
78+
Description: "Invoke the hook for confidential notes events.",
79+
Type: schema.TypeBool,
80+
Optional: true,
81+
Default: false,
82+
},
83+
"job_events": {
84+
Description: "Invoke the hook for job events.",
85+
Type: schema.TypeBool,
86+
Optional: true,
87+
Default: false,
88+
},
89+
"pipeline_events": {
90+
Description: "Invoke the hook for pipeline events.",
91+
Type: schema.TypeBool,
92+
Optional: true,
93+
Default: false,
94+
},
95+
"wiki_page_events": {
96+
Description: "Invoke the hook for wiki page events.",
97+
Type: schema.TypeBool,
98+
Optional: true,
99+
Default: false,
100+
},
101+
"deployment_events": {
102+
Description: "Invoke the hook for deployment events.",
103+
Type: schema.TypeBool,
104+
Optional: true,
105+
Default: false,
106+
},
107+
"releases_events": {
108+
Description: "Invoke the hook for releases events.",
109+
Type: schema.TypeBool,
110+
Optional: true,
111+
Default: false,
112+
},
113+
"subgroup_events": {
114+
Description: "Invoke the hook for subgroup events.",
115+
Type: schema.TypeBool,
116+
Optional: true,
117+
Default: false,
118+
},
119+
"enable_ssl_verification": {
120+
Description: "Enable ssl verification when invoking the hook.",
121+
Type: schema.TypeBool,
122+
Optional: true,
123+
Default: true,
124+
},
125+
}
126+
}
127+
128+
func gitlabGroupHookToStateMap(group string, hook *gitlab.GroupHook) map[string]interface{} {
129+
stateMap := make(map[string]interface{})
130+
stateMap["group"] = group
131+
stateMap["group_id"] = hook.GroupID
132+
stateMap["hook_id"] = hook.ID
133+
stateMap["url"] = hook.URL
134+
stateMap["push_events"] = hook.PushEvents
135+
stateMap["push_events_branch_filter"] = hook.PushEventsBranchFilter
136+
stateMap["issues_events"] = hook.IssuesEvents
137+
stateMap["confidential_issues_events"] = hook.ConfidentialIssuesEvents
138+
stateMap["merge_requests_events"] = hook.MergeRequestsEvents
139+
stateMap["tag_push_events"] = hook.TagPushEvents
140+
stateMap["note_events"] = hook.NoteEvents
141+
stateMap["confidential_note_events"] = hook.ConfidentialNoteEvents
142+
stateMap["job_events"] = hook.JobEvents
143+
stateMap["pipeline_events"] = hook.PipelineEvents
144+
stateMap["wiki_page_events"] = hook.WikiPageEvents
145+
stateMap["deployment_events"] = hook.DeploymentEvents
146+
stateMap["releases_events"] = hook.ReleasesEvents
147+
stateMap["subgroup_events"] = hook.SubGroupEvents
148+
stateMap["enable_ssl_verification"] = hook.EnableSSLVerification
149+
return stateMap
150+
}

0 commit comments

Comments
 (0)
Please sign in to comment.