Skip to content

Commit e183e8a

Browse files
authored
Merge pull request #1152 from timofurrer/feature/issue-1151
resource/gitlab_project: Fix passing `false` to API for explicitly set optional attributes
2 parents d88d528 + 62e7313 commit e183e8a

File tree

4 files changed

+113
-18
lines changed

4 files changed

+113
-18
lines changed

internal/provider/resource_gitlab_group.go

+15-5
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,9 @@ func resourceGitlabGroupCreate(ctx context.Context, d *schema.ResourceData, meta
183183
options.ShareWithGroupLock = gitlab.Bool(v.(bool))
184184
}
185185

186-
if v, ok := d.GetOk("require_two_factor_authentication"); ok {
186+
// nolint:staticcheck // SA1019 ignore deprecated GetOkExists
187+
// lintignore: XR001 // TODO: replace with alternative for GetOkExists
188+
if v, ok := d.GetOkExists("require_two_factor_authentication"); ok {
187189
options.RequireTwoFactorAuth = gitlab.Bool(v.(bool))
188190
}
189191

@@ -195,19 +197,25 @@ func resourceGitlabGroupCreate(ctx context.Context, d *schema.ResourceData, meta
195197
options.ProjectCreationLevel = stringToProjectCreationLevel(v.(string))
196198
}
197199

198-
if v, ok := d.GetOk("auto_devops_enabled"); ok {
200+
// nolint:staticcheck // SA1019 ignore deprecated GetOkExists
201+
// lintignore: XR001 // TODO: replace with alternative for GetOkExists
202+
if v, ok := d.GetOkExists("auto_devops_enabled"); ok {
199203
options.AutoDevopsEnabled = gitlab.Bool(v.(bool))
200204
}
201205

202206
if v, ok := d.GetOk("subgroup_creation_level"); ok {
203207
options.SubGroupCreationLevel = stringToSubGroupCreationLevel(v.(string))
204208
}
205209

206-
if v, ok := d.GetOk("emails_disabled"); ok {
210+
// nolint:staticcheck // SA1019 ignore deprecated GetOkExists
211+
// lintignore: XR001 // TODO: replace with alternative for GetOkExists
212+
if v, ok := d.GetOkExists("emails_disabled"); ok {
207213
options.EmailsDisabled = gitlab.Bool(v.(bool))
208214
}
209215

210-
if v, ok := d.GetOk("mentions_disabled"); ok {
216+
// nolint:staticcheck // SA1019 ignore deprecated GetOkExists
217+
// lintignore: XR001 // TODO: replace with alternative for GetOkExists
218+
if v, ok := d.GetOkExists("mentions_disabled"); ok {
211219
options.MentionsDisabled = gitlab.Bool(v.(bool))
212220
}
213221

@@ -232,7 +240,9 @@ func resourceGitlabGroupCreate(ctx context.Context, d *schema.ResourceData, meta
232240

233241
var updateOptions gitlab.UpdateGroupOptions
234242

235-
if v, ok := d.GetOk("prevent_forking_outside_group"); ok {
243+
// nolint:staticcheck // SA1019 ignore deprecated GetOkExists
244+
// lintignore: XR001 // TODO: replace with alternative for GetOkExists
245+
if v, ok := d.GetOkExists("prevent_forking_outside_group"); ok {
236246
updateOptions.PreventForkingOutsideGroup = gitlab.Bool(v.(bool))
237247
}
238248

internal/provider/resource_gitlab_group_test.go

+30
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,36 @@ func testAccCheckGitlabGroupDisappears(group *gitlab.Group) resource.TestCheckFu
313313
}
314314
}
315315

316+
func TestAccGitlabGroup_SetDefaultFalseBooleansOnCreate(t *testing.T) {
317+
rInt := acctest.RandInt()
318+
319+
resource.ParallelTest(t, resource.TestCase{
320+
ProviderFactories: providerFactories,
321+
CheckDestroy: testAccCheckGitlabProjectDestroy,
322+
Steps: []resource.TestStep{
323+
{
324+
Config: fmt.Sprintf(`
325+
resource "gitlab_group" "this" {
326+
name = "foo-%d"
327+
path = "path-%d"
328+
visibility_level = "public"
329+
330+
require_two_factor_authentication = false
331+
auto_devops_enabled = false
332+
emails_disabled = false
333+
mentions_disabled = false
334+
prevent_forking_outside_group = false
335+
}`, rInt, rInt),
336+
},
337+
{
338+
ResourceName: "gitlab_group.this",
339+
ImportState: true,
340+
ImportStateVerify: true,
341+
},
342+
},
343+
})
344+
}
345+
316346
func testAccCheckGitlabGroupExists(n string, group *gitlab.Group) resource.TestCheckFunc {
317347
return func(s *terraform.State) error {
318348
rs, ok := s.RootModule().Resources[n]

internal/provider/resource_gitlab_project.go

+33-11
Original file line numberDiff line numberDiff line change
@@ -820,7 +820,9 @@ func resourceGitlabProjectCreate(ctx context.Context, d *schema.ResourceData, me
820820
options.TagList = stringSetToStringSlice(v.(*schema.Set))
821821
}
822822

823-
if v, ok := d.GetOk("initialize_with_readme"); ok {
823+
// nolint:staticcheck // SA1019 ignore deprecated GetOkExists
824+
// lintignore: XR001 // TODO: replace with alternative for GetOkExists
825+
if v, ok := d.GetOkExists("initialize_with_readme"); ok {
824826
options.InitializeWithReadme = gitlab.Bool(v.(bool))
825827
}
826828

@@ -836,7 +838,9 @@ func resourceGitlabProjectCreate(ctx context.Context, d *schema.ResourceData, me
836838
options.TemplateProjectID = gitlab.Int(v.(int))
837839
}
838840

839-
if v, ok := d.GetOk("use_custom_template"); ok {
841+
// nolint:staticcheck // SA1019 ignore deprecated GetOkExists
842+
// lintignore: XR001 // TODO: replace with alternative for GetOkExists
843+
if v, ok := d.GetOkExists("use_custom_template"); ok {
840844
options.UseCustomTemplate = gitlab.Bool(v.(bool))
841845
}
842846

@@ -852,7 +856,9 @@ func resourceGitlabProjectCreate(ctx context.Context, d *schema.ResourceData, me
852856
options.CIConfigPath = gitlab.String(v.(string))
853857
}
854858

855-
if v, ok := d.GetOk("resolve_outdated_diff_discussions"); ok {
859+
// nolint:staticcheck // SA1019 ignore deprecated GetOkExists
860+
// lintignore: XR001 // TODO: replace with alternative for GetOkExists
861+
if v, ok := d.GetOkExists("resolve_outdated_diff_discussions"); ok {
856862
options.ResolveOutdatedDiffDiscussions = gitlab.Bool(v.(bool))
857863
}
858864

@@ -868,11 +874,15 @@ func resourceGitlabProjectCreate(ctx context.Context, d *schema.ResourceData, me
868874
options.AutoDevopsDeployStrategy = gitlab.String(v.(string))
869875
}
870876

871-
if v, ok := d.GetOk("auto_devops_enabled"); ok {
877+
// nolint:staticcheck // SA1019 ignore deprecated GetOkExists
878+
// lintignore: XR001 // TODO: replace with alternative for GetOkExists
879+
if v, ok := d.GetOkExists("auto_devops_enabled"); ok {
872880
options.AutoDevopsEnabled = gitlab.Bool(v.(bool))
873881
}
874882

875-
if v, ok := d.GetOk("autoclose_referenced_issues"); ok {
883+
// nolint:staticcheck // SA1019 ignore deprecated GetOkExists
884+
// lintignore: XR001 // TODO: replace with alternative for GetOkExists
885+
if v, ok := d.GetOkExists("autoclose_referenced_issues"); ok {
876886
options.AutocloseReferencedIssues = gitlab.Bool(v.(bool))
877887
}
878888

@@ -896,7 +906,9 @@ func resourceGitlabProjectCreate(ctx context.Context, d *schema.ResourceData, me
896906
options.ContainerRegistryAccessLevel = stringToAccessControlValue(v.(string))
897907
}
898908

899-
if v, ok := d.GetOk("emails_disabled"); ok {
909+
// nolint:staticcheck // SA1019 ignore deprecated GetOkExists
910+
// lintignore: XR001 // TODO: replace with alternative for GetOkExists
911+
if v, ok := d.GetOkExists("emails_disabled"); ok {
900912
options.EmailsDisabled = gitlab.Bool(v.(bool))
901913
}
902914

@@ -920,7 +932,9 @@ func resourceGitlabProjectCreate(ctx context.Context, d *schema.ResourceData, me
920932
options.OperationsAccessLevel = stringToAccessControlValue(v.(string))
921933
}
922934

923-
if v, ok := d.GetOk("public_builds"); ok {
935+
// nolint:staticcheck // SA1019 ignore deprecated GetOkExists
936+
// lintignore: XR001 // TODO: replace with alternative for GetOkExists
937+
if v, ok := d.GetOkExists("public_builds"); ok {
924938
options.PublicBuilds = gitlab.Bool(v.(bool))
925939
}
926940

@@ -1128,12 +1142,16 @@ func resourceGitlabProjectCreate(ctx context.Context, d *schema.ResourceData, me
11281142

11291143
var editProjectOptions gitlab.EditProjectOptions
11301144

1131-
if v, ok := d.GetOk("mirror_overwrites_diverged_branches"); ok {
1145+
// nolint:staticcheck // SA1019 ignore deprecated GetOkExists
1146+
// lintignore: XR001 // TODO: replace with alternative for GetOkExists
1147+
if v, ok := d.GetOkExists("mirror_overwrites_diverged_branches"); ok {
11321148
editProjectOptions.MirrorOverwritesDivergedBranches = gitlab.Bool(v.(bool))
11331149
editProjectOptions.ImportURL = gitlab.String(d.Get("import_url").(string))
11341150
}
11351151

1136-
if v, ok := d.GetOk("only_mirror_protected_branches"); ok {
1152+
// nolint:staticcheck // SA1019 ignore deprecated GetOkExists
1153+
// lintignore: XR001 // TODO: replace with alternative for GetOkExists
1154+
if v, ok := d.GetOkExists("only_mirror_protected_branches"); ok {
11371155
editProjectOptions.OnlyMirrorProtectedBranches = gitlab.Bool(v.(bool))
11381156
editProjectOptions.ImportURL = gitlab.String(d.Get("import_url").(string))
11391157
}
@@ -1146,11 +1164,15 @@ func resourceGitlabProjectCreate(ctx context.Context, d *schema.ResourceData, me
11461164
editProjectOptions.MergeRequestsTemplate = gitlab.String(v.(string))
11471165
}
11481166

1149-
if v, ok := d.GetOk("merge_pipelines_enabled"); ok {
1167+
// nolint:staticcheck // SA1019 ignore deprecated GetOkExists
1168+
// lintignore: XR001 // TODO: replace with alternative for GetOkExists
1169+
if v, ok := d.GetOkExists("merge_pipelines_enabled"); ok {
11501170
editProjectOptions.MergePipelinesEnabled = gitlab.Bool(v.(bool))
11511171
}
11521172

1153-
if v, ok := d.GetOk("merge_trains_enabled"); ok {
1173+
// nolint:staticcheck // SA1019 ignore deprecated GetOkExists
1174+
// lintignore: XR001 // TODO: replace with alternative for GetOkExists
1175+
if v, ok := d.GetOkExists("merge_trains_enabled"); ok {
11541176
editProjectOptions.MergeTrainsEnabled = gitlab.Bool(v.(bool))
11551177
}
11561178

internal/provider/resource_gitlab_project_test.go

+35-2
Original file line numberDiff line numberDiff line change
@@ -1126,7 +1126,7 @@ func TestAccGitlabProject_ImportURLMirrored(t *testing.T) {
11261126
func TestAccGitlabProject_templateMutualExclusiveNameAndID(t *testing.T) {
11271127
rInt := acctest.RandInt()
11281128

1129-
resource.Test(t, resource.TestCase{
1129+
resource.ParallelTest(t, resource.TestCase{
11301130
ProviderFactories: providerFactories,
11311131
CheckDestroy: testAccCheckGitlabProjectDestroy,
11321132
Steps: []resource.TestStep{
@@ -1225,7 +1225,7 @@ func TestAccGitlabProject_DeprecatedBuildCoverageRegex(t *testing.T) {
12251225
var received gitlab.Project
12261226
rInt := acctest.RandInt()
12271227

1228-
resource.Test(t, resource.TestCase{
1228+
resource.ParallelTest(t, resource.TestCase{
12291229
ProviderFactories: providerFactories,
12301230
CheckDestroy: testAccCheckGitlabProjectDestroy,
12311231
Steps: []resource.TestStep{
@@ -1252,6 +1252,39 @@ func TestAccGitlabProject_DeprecatedBuildCoverageRegex(t *testing.T) {
12521252
})
12531253
}
12541254

1255+
func TestAccGitlabProject_SetDefaultFalseBooleansOnCreate(t *testing.T) {
1256+
rInt := acctest.RandInt()
1257+
1258+
resource.ParallelTest(t, resource.TestCase{
1259+
ProviderFactories: providerFactories,
1260+
CheckDestroy: testAccCheckGitlabProjectDestroy,
1261+
Steps: []resource.TestStep{
1262+
{
1263+
Config: fmt.Sprintf(`
1264+
resource "gitlab_project" "this" {
1265+
name = "foo-%d"
1266+
visibility_level = "public"
1267+
1268+
initialize_with_readme = false
1269+
resolve_outdated_diff_discussions = false
1270+
auto_devops_enabled = false
1271+
autoclose_referenced_issues = false
1272+
emails_disabled = false
1273+
public_builds = false
1274+
merge_pipelines_enabled = false
1275+
merge_trains_enabled = false
1276+
}`, rInt),
1277+
},
1278+
{
1279+
ResourceName: "gitlab_project.this",
1280+
ImportState: true,
1281+
ImportStateVerify: true,
1282+
ImportStateVerifyIgnore: []string{"initialize_with_readme"},
1283+
},
1284+
},
1285+
})
1286+
}
1287+
12551288
func testAccCheckGitlabProjectExists(n string, project *gitlab.Project) resource.TestCheckFunc {
12561289
return func(s *terraform.State) error {
12571290
var err error

0 commit comments

Comments
 (0)