From 1d5acb120c2c915705f8332c94451749a8f9c546 Mon Sep 17 00:00:00 2001 From: Timo Furrer Date: Fri, 19 Aug 2022 17:17:38 +0200 Subject: [PATCH] datasource/gitlab_group_hooks: New Data Source Refs: #680 --- docs/data-sources/group_hooks.md | 66 ++++++++++++++++++ .../gitlab_group_hooks/data-source.tf | 7 ++ .../data_source_gitlab_group_hooks.go | 69 +++++++++++++++++++ .../data_source_gitlab_group_hooks_test.go | 34 +++++++++ 4 files changed, 176 insertions(+) create mode 100644 docs/data-sources/group_hooks.md create mode 100644 examples/data-sources/gitlab_group_hooks/data-source.tf create mode 100644 internal/provider/data_source_gitlab_group_hooks.go create mode 100644 internal/provider/data_source_gitlab_group_hooks_test.go diff --git a/docs/data-sources/group_hooks.md b/docs/data-sources/group_hooks.md new file mode 100644 index 000000000..b79b11693 --- /dev/null +++ b/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 + +### 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. + + +### 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) + + diff --git a/examples/data-sources/gitlab_group_hooks/data-source.tf b/examples/data-sources/gitlab_group_hooks/data-source.tf new file mode 100644 index 000000000..2e1cf9d7b --- /dev/null +++ b/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 +} diff --git a/internal/provider/data_source_gitlab_group_hooks.go b/internal/provider/data_source_gitlab_group_hooks.go new file mode 100644 index 000000000..486bb13ce --- /dev/null +++ b/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 +} diff --git a/internal/provider/data_source_gitlab_group_hooks_test.go b/internal/provider/data_source_gitlab_group_hooks_test.go new file mode 100644 index 000000000..391da132d --- /dev/null +++ b/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), + ), + }, + }, + }) +}