Skip to content

Commit

Permalink
datasource/gitlab_group_hooks: New Data Source
Browse files Browse the repository at this point in the history
Refs: #680
  • Loading branch information
timofurrer committed Aug 19, 2022
1 parent 76100ed commit 1d5acb1
Show file tree
Hide file tree
Showing 4 changed files with 176 additions and 0 deletions.
66 changes: 66 additions & 0 deletions docs/data-sources/group_hooks.md
@@ -0,0 +1,66 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "gitlab_group_hooks Data Source - terraform-provider-gitlab"
subcategory: ""
description: |-
The gitlab_group_hooks data source allows to retrieve details about hooks in a group.
Upstream API: GitLab REST API docs https://docs.gitlab.com/ee/api/groups.html#list-group-hooks
---

# gitlab_group_hooks (Data Source)

The `gitlab_group_hooks` data source allows to retrieve details about hooks in a group.

**Upstream API**: [GitLab REST API docs](https://docs.gitlab.com/ee/api/groups.html#list-group-hooks)

## Example Usage

```terraform
data "gitlab_group" "example" {
id = "foo/bar/baz"
}
data "gitlab_group_hooks" "examples" {
group = data.gitlab_group.example.id
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `group` (String) The ID or full path of the group.

### Read-Only

- `hooks` (List of Object) The list of hooks. (see [below for nested schema](#nestedatt--hooks))
- `id` (String) The ID of this resource.

<a id="nestedatt--hooks"></a>
### Nested Schema for `hooks`

Read-Only:

- `confidential_issues_events` (Boolean)
- `confidential_note_events` (Boolean)
- `deployment_events` (Boolean)
- `enable_ssl_verification` (Boolean)
- `group` (String)
- `group_id` (Number)
- `hook_id` (Number)
- `issues_events` (Boolean)
- `job_events` (Boolean)
- `merge_requests_events` (Boolean)
- `note_events` (Boolean)
- `pipeline_events` (Boolean)
- `push_events` (Boolean)
- `push_events_branch_filter` (String)
- `releases_events` (Boolean)
- `subgroup_events` (Boolean)
- `tag_push_events` (Boolean)
- `token` (String)
- `url` (String)
- `wiki_page_events` (Boolean)


7 changes: 7 additions & 0 deletions examples/data-sources/gitlab_group_hooks/data-source.tf
@@ -0,0 +1,7 @@
data "gitlab_group" "example" {
id = "foo/bar/baz"
}

data "gitlab_group_hooks" "examples" {
group = data.gitlab_group.example.id
}
69 changes: 69 additions & 0 deletions internal/provider/data_source_gitlab_group_hooks.go
@@ -0,0 +1,69 @@
package provider

import (
"context"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/xanzy/go-gitlab"
)

var _ = registerDataSource("gitlab_group_hooks", func() *schema.Resource {
return &schema.Resource{
Description: `The ` + "`gitlab_group_hooks`" + ` data source allows to retrieve details about hooks in a group.
**Upstream API**: [GitLab REST API docs](https://docs.gitlab.com/ee/api/groups.html#list-group-hooks)`,

ReadContext: dataSourceGitlabGroupHooksRead,
Schema: map[string]*schema.Schema{
"group": {
Description: "The ID or full path of the group.",
Type: schema.TypeString,
Required: true,
},
"hooks": {
Description: "The list of hooks.",
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: datasourceSchemaFromResourceSchema(gitlabGroupHookSchema(), nil, nil),
},
},
},
}
})

func dataSourceGitlabGroupHooksRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*gitlab.Client)

group := d.Get("group").(string)
options := gitlab.ListGroupHooksOptions{
PerPage: 20,
Page: 1,
}

var hooks []*gitlab.GroupHook
for options.Page != 0 {
paginatedHooks, resp, err := client.Groups.ListGroupHooks(group, &options, gitlab.WithContext(ctx))
if err != nil {
return diag.FromErr(err)
}

hooks = append(hooks, paginatedHooks...)
options.Page = resp.NextPage
}

d.SetId(group)
if err := d.Set("hooks", flattenGitlabGroupHooks(group, hooks)); err != nil {
return diag.Errorf("failed to set hooks to state: %v", err)
}

return nil
}

func flattenGitlabGroupHooks(group string, hooks []*gitlab.GroupHook) (values []map[string]interface{}) {
for _, hook := range hooks {
values = append(values, gitlabGroupHookToStateMap(group, hook))
}
return values
}
34 changes: 34 additions & 0 deletions internal/provider/data_source_gitlab_group_hooks_test.go
@@ -0,0 +1,34 @@
//go:build acceptance
// +build acceptance

package provider

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccDataSourceGitlabGroupHooks_basic(t *testing.T) {
testGroup := testAccCreateGroups(t, 1)[0]
testHooks := testAccCreateGroupHooks(t, testGroup.ID, 25)

resource.ParallelTest(t, resource.TestCase{
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(`
data "gitlab_group_hooks" "this" {
group = "%s"
}
`, testGroup.FullPath),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.gitlab_group_hooks.this", "hooks.#", fmt.Sprintf("%d", len(testHooks))),
resource.TestCheckResourceAttr("data.gitlab_group_hooks.this", "hooks.0.url", testHooks[0].URL),
resource.TestCheckResourceAttr("data.gitlab_group_hooks.this", "hooks.1.url", testHooks[1].URL),
),
},
},
})
}

0 comments on commit 1d5acb1

Please sign in to comment.