Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
datasource/gitlab_group_hooks: New Data Source
Refs: #680
- Loading branch information
1 parent
76100ed
commit 1d5acb1
Showing
4 changed files
with
176 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
data "gitlab_group" "example" { | ||
id = "foo/bar/baz" | ||
} | ||
|
||
data "gitlab_group_hooks" "examples" { | ||
group = data.gitlab_group.example.id | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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), | ||
), | ||
}, | ||
}, | ||
}) | ||
} |