Skip to content

Commit 412cae7

Browse files
committed
datasource/gitlab_group_hooks: New Data Source
Refs: #680
1 parent bce0b77 commit 412cae7

File tree

4 files changed

+178
-0
lines changed

4 files changed

+178
-0
lines changed

docs/data-sources/group_hooks.md

+66
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+
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+
}
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+
}
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+
}

0 commit comments

Comments
 (0)