Skip to content

Commit eb30a44

Browse files
committed
New data source gitlab_project_variables
Refs: #386
1 parent e6d191d commit eb30a44

File tree

4 files changed

+182
-0
lines changed

4 files changed

+182
-0
lines changed
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_project_variables Data Source - terraform-provider-gitlab"
4+
subcategory: ""
5+
description: |-
6+
The gitlab_project_variables data source allows to retrieve all project-level CI/CD variables.
7+
Upstream API: GitLab REST API docs https://docs.gitlab.com/ee/api/project_level_variables.html
8+
---
9+
10+
# gitlab_project_variables (Data Source)
11+
12+
The `gitlab_project_variables` data source allows to retrieve all project-level CI/CD variables.
13+
14+
**Upstream API**: [GitLab REST API docs](https://docs.gitlab.com/ee/api/project_level_variables.html)
15+
16+
17+
18+
<!-- schema generated by tfplugindocs -->
19+
## Schema
20+
21+
### Required
22+
23+
- `project` (String) The name or id of the project.
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+
- `key` (String)
41+
- `masked` (Boolean)
42+
- `project` (String)
43+
- `protected` (Boolean)
44+
- `value` (String)
45+
- `variable_type` (String)
46+
47+
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package provider
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
9+
"github.com/xanzy/go-gitlab"
10+
)
11+
12+
var _ = registerDataSource("gitlab_project_variables", func() *schema.Resource {
13+
return &schema.Resource{
14+
Description: `The ` + "`gitlab_project_variables`" + ` data source allows to retrieve all project-level CI/CD variables.
15+
16+
**Upstream API**: [GitLab REST API docs](https://docs.gitlab.com/ee/api/project_level_variables.html)`,
17+
18+
ReadContext: dataSourceGitlabProjectVariablesRead,
19+
Schema: map[string]*schema.Schema{
20+
"project": {
21+
Description: "The name or id of the project.",
22+
Type: schema.TypeString,
23+
ForceNew: true,
24+
Required: true,
25+
},
26+
"environment_scope": {
27+
Description: "The environment scope of the variable. Defaults to all environment (`*`).",
28+
Type: schema.TypeString,
29+
Optional: true,
30+
Default: "*",
31+
},
32+
"variables": {
33+
Description: "The list of variables returned by the search",
34+
Type: schema.TypeList,
35+
Computed: true,
36+
Elem: &schema.Resource{
37+
Schema: datasourceSchemaFromResourceSchema(gitlabProjectVariableGetSchema(), nil, nil),
38+
},
39+
},
40+
},
41+
}
42+
})
43+
44+
func dataSourceGitlabProjectVariablesRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
45+
client := meta.(*gitlab.Client)
46+
project := d.Get("project").(string)
47+
environmentScope := d.Get("environment_scope").(string)
48+
49+
options := &gitlab.ListProjectVariablesOptions{
50+
Page: 1,
51+
PerPage: 20,
52+
}
53+
54+
var variables []*gitlab.ProjectVariable
55+
for options.Page != 0 {
56+
paginatedVariables, resp, err := client.ProjectVariables.ListVariables(project, options, gitlab.WithContext(ctx), withEnvironmentScopeFilter(ctx, environmentScope))
57+
if err != nil {
58+
return diag.FromErr(err)
59+
}
60+
61+
variables = append(variables, paginatedVariables...)
62+
options.Page = resp.NextPage
63+
}
64+
65+
d.SetId(fmt.Sprintf("%s:%s", project, environmentScope))
66+
if err := d.Set("variables", flattenGitlabProjectVariables(project, variables)); err != nil {
67+
return diag.Errorf("failed to set variables to state: %v", err)
68+
}
69+
return nil
70+
}
71+
72+
func flattenGitlabProjectVariables(project string, variables []*gitlab.ProjectVariable) (values []map[string]interface{}) {
73+
for _, variable := range variables {
74+
values = append(values, gitlabProjectVariableToStateMap(project, variable))
75+
}
76+
return values
77+
}
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 TestAccDataSourceGitlabProjectVariables_basic(t *testing.T) {
12+
testAccCheck(t)
13+
14+
testProject := testAccCreateProject(t)
15+
testVariables := make([]*gitlab.ProjectVariable, 0)
16+
for i := 0; i < 25; i++ {
17+
testVariables = append(testVariables, testAccCreateProjectVariable(t, testProject.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_project_variables" "this" {
27+
project = %d
28+
}
29+
`, testProject.ID),
30+
Check: resource.ComposeTestCheckFunc(
31+
resource.TestCheckResourceAttr("data.gitlab_project_variables.this", "variables.#", fmt.Sprintf("%d", len(testVariables))),
32+
resource.TestCheckResourceAttr("data.gitlab_project_variables.this", "variables.0.key", testVariables[0].Key),
33+
resource.TestCheckResourceAttr("data.gitlab_project_variables.this", "variables.0.value", testVariables[0].Value),
34+
resource.TestCheckResourceAttr("data.gitlab_project_variables.this", "variables.24.key", testVariables[24].Key),
35+
resource.TestCheckResourceAttr("data.gitlab_project_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
@@ -351,6 +351,24 @@ func testAccCreateProjectEnvironment(t *testing.T, projectID int, options *gitla
351351
return projectEnvironment
352352
}
353353

354+
func testAccCreateProjectVariable(t *testing.T, projectID int) *gitlab.ProjectVariable {
355+
variable, _, err := testGitlabClient.ProjectVariables.CreateVariable(projectID, &gitlab.CreateProjectVariableOptions{
356+
Key: gitlab.String(fmt.Sprintf("test_key_%d", acctest.RandInt())),
357+
Value: gitlab.String("test_value"),
358+
})
359+
if err != nil {
360+
t.Fatal(err)
361+
}
362+
363+
t.Cleanup(func() {
364+
if _, err := testGitlabClient.ProjectVariables.RemoveVariable(projectID, variable.Key, nil); err != nil {
365+
t.Fatal(err)
366+
}
367+
})
368+
369+
return variable
370+
}
371+
354372
// testAccGitlabProjectContext encapsulates a GitLab client and test project to be used during an
355373
// acceptance test.
356374
type testAccGitlabProjectContext struct {

0 commit comments

Comments
 (0)