Skip to content

Commit 81dbd48

Browse files
committed
datasource/gitlab_projects: Allow to get archived and unarchived repositories. Closes #452
This change makes use of the deprecated `d.GetOkExists()` function to check if a boolean attribute has been set by the user or not. AFAIK, it's the only way to achieve that in the SDK v2. It doesn't seem to go anywhere without a proper replacement. See: * hashicorp/terraform-plugin-sdk#817 * hashicorp/terraform-plugin-sdk#350 (comment) Closes #452
1 parent f818824 commit 81dbd48

File tree

2 files changed

+146
-12
lines changed

2 files changed

+146
-12
lines changed

internal/provider/data_source_gitlab_projects.go

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -811,15 +811,24 @@ func dataSourceGitlabProjectsRead(ctx context.Context, d *schema.ResourceData, m
811811
var withProgrammingLanguagePtr *string
812812
var withSharedPtr *bool
813813

814-
if data, ok := d.GetOk("archived"); ok {
814+
// NOTE: `GetOkExists()` is deprecated, but until there is a replacement we need to use it.
815+
// see https://github.com/hashicorp/terraform-plugin-sdk/pull/350#issuecomment-597888969
816+
817+
// nolint:staticcheck // SA1019 ignore deprecated GetOkExists
818+
// lintignore: XR001 // TODO: replace with alternative for GetOkExists
819+
if data, ok := d.GetOkExists("archived"); ok {
815820
d := data.(bool)
816821
archivedPtr = &d
817822
}
818-
if data, ok := d.GetOk("include_subgroups"); ok {
823+
// nolint:staticcheck // SA1019 ignore deprecated GetOkExists
824+
// lintignore: XR001 // TODO: replace with alternative for GetOkExists
825+
if data, ok := d.GetOkExists("include_subgroups"); ok {
819826
d := data.(bool)
820827
includeSubGroupsPtr = &d
821828
}
822-
if data, ok := d.GetOk("membership"); ok {
829+
// nolint:staticcheck // SA1019 ignore deprecated GetOkExists
830+
// lintignore: XR001 // TODO: replace with alternative for GetOkExists
831+
if data, ok := d.GetOkExists("membership"); ok {
823832
d := data.(bool)
824833
membershipPtr = &d
825834
}
@@ -830,50 +839,66 @@ func dataSourceGitlabProjectsRead(ctx context.Context, d *schema.ResourceData, m
830839
d := data.(string)
831840
orderByPtr = &d
832841
}
833-
if data, ok := d.GetOk("owned"); ok {
842+
// nolint:staticcheck // SA1019 ignore deprecated GetOkExists
843+
// lintignore: XR001 // TODO: replace with alternative for GetOkExists
844+
if data, ok := d.GetOkExists("owned"); ok {
834845
d := data.(bool)
835846
ownedPtr = &d
836847
}
837848
if data, ok := d.GetOk("search"); ok {
838849
d := data.(string)
839850
searchPtr = &d
840851
}
841-
if data, ok := d.GetOk("simple"); ok {
852+
// nolint:staticcheck // SA1019 ignore deprecated GetOkExists
853+
// lintignore: XR001 // TODO: replace with alternative for GetOkExists
854+
if data, ok := d.GetOkExists("simple"); ok {
842855
d := data.(bool)
843856
simplePtr = &d
844857
}
845858
if data, ok := d.GetOk("sort"); ok {
846859
d := data.(string)
847860
sortPtr = &d
848861
}
849-
if data, ok := d.GetOk("starred"); ok {
862+
// nolint:staticcheck // SA1019 ignore deprecated GetOkExists
863+
// lintignore: XR001 // TODO: replace with alternative for GetOkExists
864+
if data, ok := d.GetOkExists("starred"); ok {
850865
d := data.(bool)
851866
starredPtr = &d
852867
}
853-
if data, ok := d.GetOk("statistics"); ok {
868+
// nolint:staticcheck // SA1019 ignore deprecated GetOkExists
869+
// lintignore: XR001 // TODO: replace with alternative for GetOkExists
870+
if data, ok := d.GetOkExists("statistics"); ok {
854871
d := data.(bool)
855872
statisticsPtr = &d
856873
}
857874
if data, ok := d.GetOk("visibility"); ok {
858875
visibilityPtr = gitlab.Visibility(gitlab.VisibilityValue(data.(string)))
859876
}
860-
if data, ok := d.GetOk("with_custom_attributes"); ok {
877+
// nolint:staticcheck // SA1019 ignore deprecated GetOkExists
878+
// lintignore: XR001 // TODO: replace with alternative for GetOkExists
879+
if data, ok := d.GetOkExists("with_custom_attributes"); ok {
861880
d := data.(bool)
862881
withCustomAttributesPtr = &d
863882
}
864-
if data, ok := d.GetOk("with_issues_enabled"); ok {
883+
// nolint:staticcheck // SA1019 ignore deprecated GetOkExists
884+
// lintignore: XR001 // TODO: replace with alternative for GetOkExists
885+
if data, ok := d.GetOkExists("with_issues_enabled"); ok {
865886
d := data.(bool)
866887
withIssuesEnabledPtr = &d
867888
}
868-
if data, ok := d.GetOk("with_merge_requests_enabled"); ok {
889+
// nolint:staticcheck // SA1019 ignore deprecated GetOkExists
890+
// lintignore: XR001 // TODO: replace with alternative for GetOkExists
891+
if data, ok := d.GetOkExists("with_merge_requests_enabled"); ok {
869892
d := data.(bool)
870893
withMergeRequestsEnabledPtr = &d
871894
}
872895
if data, ok := d.GetOk("with_programming_language"); ok {
873896
d := data.(string)
874897
withProgrammingLanguagePtr = &d
875898
}
876-
if data, ok := d.GetOk("with_shared"); ok {
899+
// nolint:staticcheck // SA1019 ignore deprecated GetOkExists
900+
// lintignore: XR001 // TODO: replace with alternative for GetOkExists
901+
if data, ok := d.GetOkExists("with_shared"); ok {
877902
d := data.(bool)
878903
withSharedPtr = &d
879904
}

internal/provider/data_source_gitlab_projects_test.go

Lines changed: 110 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ package provider
22

33
import (
44
"fmt"
5-
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
65
"strconv"
76
"strings"
87
"testing"
98

9+
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
10+
1011
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
1112
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
1213
)
@@ -96,6 +97,52 @@ func TestAccDataGitlabProjectsGroups(t *testing.T) {
9697
})
9798
}
9899

100+
func TestAccDataGitlabProjects_searchArchivedRepository(t *testing.T) {
101+
rInt := acctest.RandInt()
102+
103+
resource.Test(t, resource.TestCase{
104+
PreCheck: func() { testAccPreCheck(t) },
105+
ProviderFactories: providerFactories,
106+
Steps: []resource.TestStep{
107+
{
108+
Config: testAccDataGitlabProjectsConfigGetProjectArchivedRepositoryAll(rInt),
109+
Check: resource.ComposeAggregateTestCheckFunc(
110+
resource.TestCheckResourceAttr(
111+
"data.gitlab_projects.search",
112+
"projects.0.name",
113+
fmt.Sprintf("archived-%d", rInt),
114+
),
115+
resource.TestCheckResourceAttr(
116+
"data.gitlab_projects.search",
117+
"projects.1.name",
118+
fmt.Sprintf("not-archived-%d", rInt),
119+
),
120+
),
121+
},
122+
{
123+
Config: testAccDataGitlabProjectsConfigGetProjectArchivedRepository(rInt, "true"),
124+
Check: resource.ComposeAggregateTestCheckFunc(
125+
resource.TestCheckResourceAttr(
126+
"data.gitlab_projects.search",
127+
"projects.0.name",
128+
fmt.Sprintf("archived-%d", rInt),
129+
),
130+
),
131+
},
132+
{
133+
Config: testAccDataGitlabProjectsConfigGetProjectArchivedRepository(rInt, "false"),
134+
Check: resource.ComposeAggregateTestCheckFunc(
135+
resource.TestCheckResourceAttr(
136+
"data.gitlab_projects.search",
137+
"projects.0.name",
138+
fmt.Sprintf("not-archived-%d", rInt),
139+
),
140+
),
141+
},
142+
},
143+
})
144+
}
145+
99146
func testAccDataSourceGitlabProjects(src string, n string) resource.TestCheckFunc {
100147
return func(s *terraform.State) error {
101148

@@ -167,6 +214,68 @@ data "gitlab_projects" "search" {
167214
`, projectName, projectName)
168215
}
169216

217+
func testAccDataGitlabProjectsConfigGetProjectArchivedRepositoryAll(rInt int) string {
218+
return fmt.Sprintf(`
219+
resource "gitlab_group" "test" {
220+
name = "test-%d"
221+
path = "test-%d"
222+
}
223+
224+
resource "gitlab_project" "archived_repo" {
225+
name = "archived-%d"
226+
namespace_id = gitlab_group.test.id
227+
archived = true
228+
}
229+
230+
resource "gitlab_project" "not_archived_repo" {
231+
name = "not-archived-%d"
232+
namespace_id = gitlab_group.test.id
233+
archived = false
234+
}
235+
236+
data "gitlab_projects" "search" {
237+
group_id = gitlab_group.test.id
238+
// NOTE: is required to have deterministic results
239+
order_by = "name"
240+
sort = "asc"
241+
242+
depends_on = [gitlab_project.archived_repo, gitlab_project.not_archived_repo]
243+
}
244+
`, rInt, rInt, rInt, rInt)
245+
}
246+
247+
func testAccDataGitlabProjectsConfigGetProjectArchivedRepository(rInt int, archived string) string {
248+
return fmt.Sprintf(`
249+
resource "gitlab_group" "test" {
250+
name = "test-%d"
251+
path = "test-%d"
252+
}
253+
254+
resource "gitlab_project" "archived_repo" {
255+
name = "archived-%d"
256+
namespace_id = gitlab_group.test.id
257+
archived = true
258+
}
259+
260+
resource "gitlab_project" "not_archived_repo" {
261+
name = "not-archived-%d"
262+
namespace_id = gitlab_group.test.id
263+
archived = false
264+
}
265+
266+
data "gitlab_projects" "search" {
267+
group_id = gitlab_group.test.id
268+
// NOTE: is required to have deterministic results
269+
order_by = "name"
270+
sort = "asc"
271+
272+
archived = %s
273+
274+
depends_on = [gitlab_project.archived_repo, gitlab_project.not_archived_repo]
275+
}
276+
`, rInt, rInt, rInt, rInt, archived)
277+
}
278+
170279
func testAccDataGitlabProjectsConfigGetGroupProjectsByGroupId(groupName string, projectName string) string {
171280
return fmt.Sprintf(`
172281
resource "gitlab_group" "testGroup" {

0 commit comments

Comments
 (0)