Skip to content

Commit 25f70e8

Browse files
committed
New data source gitlab_group_variables
Refs: #386
1 parent ff569d3 commit 25f70e8

File tree

3 files changed

+105
-0
lines changed

3 files changed

+105
-0
lines changed

docs/data-sources/group_variables.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "gitlab_group_variables Data Source - terraform-provider-gitlab"
4+
subcategory: ""
5+
description: |-
6+
The gitlab_group_variables data source allows to retrieve all group-level CI/CD variables.
7+
Upstream API: GitLab REST API docs https://docs.gitlab.com/ee/api/group_level_variables.html
8+
---
9+
10+
# gitlab_group_variables (Data Source)
11+
12+
The `gitlab_group_variables` data source allows to retrieve all group-level CI/CD variables.
13+
14+
**Upstream API**: [GitLab REST API docs](https://docs.gitlab.com/ee/api/group_level_variables.html)
15+
16+
17+
18+
<!-- schema generated by tfplugindocs -->
19+
## Schema
20+
21+
### Required
22+
23+
- `group` (String) The name or id of the group.
24+
25+
### Optional
26+
27+
- `environment_scope` (String) The environment scope of the variable. Defaults to all environment (`*`).
28+
- `id` (String) The ID of this resource.
29+
30+
### Read-Only
31+
32+
- `variables` (List of Object) The list of variables returned by the search (see [below for nested schema](#nestedatt--variables))
33+
34+
<a id="nestedatt--variables"></a>
35+
### Nested Schema for `variables`
36+
37+
Read-Only:
38+
39+
- `environment_scope` (String)
40+
- `group` (String)
41+
- `key` (String)
42+
- `masked` (Boolean)
43+
- `protected` (Boolean)
44+
- `value` (String)
45+
- `variable_type` (String)
46+
47+
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package provider
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
8+
"github.com/xanzy/go-gitlab"
9+
)
10+
11+
func TestAccDataSourceGitlabGroupVariables_basic(t *testing.T) {
12+
testAccCheck(t)
13+
14+
testGroup := testAccCreateGroups(t, 1)[0]
15+
testVariables := make([]*gitlab.GroupVariable, 0)
16+
for i := 0; i < 25; i++ {
17+
testVariables = append(testVariables, testAccCreateGroupVariable(t, testGroup.ID))
18+
}
19+
20+
resource.Test(t, resource.TestCase{
21+
PreCheck: func() { testAccPreCheck(t) },
22+
ProviderFactories: providerFactories,
23+
Steps: []resource.TestStep{
24+
{
25+
Config: fmt.Sprintf(`
26+
data "gitlab_group_variables" "this" {
27+
group = %d
28+
}
29+
`, testGroup.ID),
30+
Check: resource.ComposeTestCheckFunc(
31+
resource.TestCheckResourceAttr("data.gitlab_group_variables.this", "variables.#", fmt.Sprintf("%d", len(testVariables))),
32+
resource.TestCheckResourceAttr("data.gitlab_group_variables.this", "variables.0.key", testVariables[0].Key),
33+
resource.TestCheckResourceAttr("data.gitlab_group_variables.this", "variables.0.value", testVariables[0].Value),
34+
resource.TestCheckResourceAttr("data.gitlab_group_variables.this", "variables.24.key", testVariables[24].Key),
35+
resource.TestCheckResourceAttr("data.gitlab_group_variables.this", "variables.24.value", testVariables[24].Value),
36+
),
37+
},
38+
},
39+
})
40+
}

internal/provider/helper_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,24 @@ func testAccCreateProjectVariable(t *testing.T, projectID int) *gitlab.ProjectVa
369369
return variable
370370
}
371371

372+
func testAccCreateGroupVariable(t *testing.T, groupID int) *gitlab.GroupVariable {
373+
variable, _, err := testGitlabClient.GroupVariables.CreateVariable(groupID, &gitlab.CreateGroupVariableOptions{
374+
Key: gitlab.String(fmt.Sprintf("test_key_%d", acctest.RandInt())),
375+
Value: gitlab.String("test_value"),
376+
})
377+
if err != nil {
378+
t.Fatal(err)
379+
}
380+
381+
t.Cleanup(func() {
382+
if _, err := testGitlabClient.GroupVariables.RemoveVariable(groupID, variable.Key, nil); err != nil {
383+
t.Fatal(err)
384+
}
385+
})
386+
387+
return variable
388+
}
389+
372390
// testAccGitlabProjectContext encapsulates a GitLab client and test project to be used during an
373391
// acceptance test.
374392
type testAccGitlabProjectContext struct {

0 commit comments

Comments
 (0)