Skip to content

Commit

Permalink
resource/gitlab_project: Fix passing false to API for explicitly se…
Browse files Browse the repository at this point in the history
…t optional attributes
  • Loading branch information
timofurrer committed Jun 29, 2022
1 parent 5b3eaef commit 0bca00d
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 13 deletions.
44 changes: 33 additions & 11 deletions internal/provider/resource_gitlab_project.go
Expand Up @@ -812,7 +812,9 @@ func resourceGitlabProjectCreate(ctx context.Context, d *schema.ResourceData, me
options.TagList = stringSetToStringSlice(v.(*schema.Set))
}

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

Expand All @@ -828,7 +830,9 @@ func resourceGitlabProjectCreate(ctx context.Context, d *schema.ResourceData, me
options.TemplateProjectID = gitlab.Int(v.(int))
}

if v, ok := d.GetOk("use_custom_template"); ok {
// nolint:staticcheck // SA1019 ignore deprecated GetOkExists
// lintignore: XR001 // TODO: replace with alternative for GetOkExists
if v, ok := d.GetOkExists("use_custom_template"); ok {
options.UseCustomTemplate = gitlab.Bool(v.(bool))
}

Expand All @@ -844,7 +848,9 @@ func resourceGitlabProjectCreate(ctx context.Context, d *schema.ResourceData, me
options.CIConfigPath = gitlab.String(v.(string))
}

if v, ok := d.GetOk("resolve_outdated_diff_discussions"); ok {
// nolint:staticcheck // SA1019 ignore deprecated GetOkExists
// lintignore: XR001 // TODO: replace with alternative for GetOkExists
if v, ok := d.GetOkExists("resolve_outdated_diff_discussions"); ok {
options.ResolveOutdatedDiffDiscussions = gitlab.Bool(v.(bool))
}

Expand All @@ -860,11 +866,15 @@ func resourceGitlabProjectCreate(ctx context.Context, d *schema.ResourceData, me
options.AutoDevopsDeployStrategy = gitlab.String(v.(string))
}

if v, ok := d.GetOk("auto_devops_enabled"); ok {
// nolint:staticcheck // SA1019 ignore deprecated GetOkExists
// lintignore: XR001 // TODO: replace with alternative for GetOkExists
if v, ok := d.GetOkExists("auto_devops_enabled"); ok {
options.AutoDevopsEnabled = gitlab.Bool(v.(bool))
}

if v, ok := d.GetOk("autoclose_referenced_issues"); ok {
// nolint:staticcheck // SA1019 ignore deprecated GetOkExists
// lintignore: XR001 // TODO: replace with alternative for GetOkExists
if v, ok := d.GetOkExists("autoclose_referenced_issues"); ok {
options.AutocloseReferencedIssues = gitlab.Bool(v.(bool))
}

Expand All @@ -888,7 +898,9 @@ func resourceGitlabProjectCreate(ctx context.Context, d *schema.ResourceData, me
options.ContainerRegistryAccessLevel = stringToAccessControlValue(v.(string))
}

if v, ok := d.GetOk("emails_disabled"); ok {
// nolint:staticcheck // SA1019 ignore deprecated GetOkExists
// lintignore: XR001 // TODO: replace with alternative for GetOkExists
if v, ok := d.GetOkExists("emails_disabled"); ok {
options.EmailsDisabled = gitlab.Bool(v.(bool))
}

Expand All @@ -912,7 +924,9 @@ func resourceGitlabProjectCreate(ctx context.Context, d *schema.ResourceData, me
options.OperationsAccessLevel = stringToAccessControlValue(v.(string))
}

if v, ok := d.GetOk("public_builds"); ok {
// nolint:staticcheck // SA1019 ignore deprecated GetOkExists
// lintignore: XR001 // TODO: replace with alternative for GetOkExists
if v, ok := d.GetOkExists("public_builds"); ok {
options.PublicBuilds = gitlab.Bool(v.(bool))
}

Expand Down Expand Up @@ -1120,12 +1134,16 @@ func resourceGitlabProjectCreate(ctx context.Context, d *schema.ResourceData, me

var editProjectOptions gitlab.EditProjectOptions

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

if v, ok := d.GetOk("only_mirror_protected_branches"); ok {
// nolint:staticcheck // SA1019 ignore deprecated GetOkExists
// lintignore: XR001 // TODO: replace with alternative for GetOkExists
if v, ok := d.GetOkExists("only_mirror_protected_branches"); ok {
editProjectOptions.OnlyMirrorProtectedBranches = gitlab.Bool(v.(bool))
editProjectOptions.ImportURL = gitlab.String(d.Get("import_url").(string))
}
Expand All @@ -1138,11 +1156,15 @@ func resourceGitlabProjectCreate(ctx context.Context, d *schema.ResourceData, me
editProjectOptions.MergeRequestsTemplate = gitlab.String(v.(string))
}

if v, ok := d.GetOk("merge_pipelines_enabled"); ok {
// nolint:staticcheck // SA1019 ignore deprecated GetOkExists
// lintignore: XR001 // TODO: replace with alternative for GetOkExists
if v, ok := d.GetOkExists("merge_pipelines_enabled"); ok {
editProjectOptions.MergePipelinesEnabled = gitlab.Bool(v.(bool))
}

if v, ok := d.GetOk("merge_trains_enabled"); ok {
// nolint:staticcheck // SA1019 ignore deprecated GetOkExists
// lintignore: XR001 // TODO: replace with alternative for GetOkExists
if v, ok := d.GetOkExists("merge_trains_enabled"); ok {
editProjectOptions.MergeTrainsEnabled = gitlab.Bool(v.(bool))
}

Expand Down
37 changes: 35 additions & 2 deletions internal/provider/resource_gitlab_project_test.go
Expand Up @@ -1126,7 +1126,7 @@ func TestAccGitlabProject_ImportURLMirrored(t *testing.T) {
func TestAccGitlabProject_templateMutualExclusiveNameAndID(t *testing.T) {
rInt := acctest.RandInt()

resource.Test(t, resource.TestCase{
resource.ParallelTest(t, resource.TestCase{
ProviderFactories: providerFactories,
CheckDestroy: testAccCheckGitlabProjectDestroy,
Steps: []resource.TestStep{
Expand Down Expand Up @@ -1225,7 +1225,7 @@ func TestAccGitlabProject_DeprecatedBuildCoverageRegex(t *testing.T) {
var received gitlab.Project
rInt := acctest.RandInt()

resource.Test(t, resource.TestCase{
resource.ParallelTest(t, resource.TestCase{
ProviderFactories: providerFactories,
CheckDestroy: testAccCheckGitlabProjectDestroy,
Steps: []resource.TestStep{
Expand All @@ -1252,6 +1252,39 @@ func TestAccGitlabProject_DeprecatedBuildCoverageRegex(t *testing.T) {
})
}

func TestAccGitlabProject_SetDefaultFalseBooleansOnCreate(t *testing.T) {
rInt := acctest.RandInt()

resource.ParallelTest(t, resource.TestCase{
ProviderFactories: providerFactories,
CheckDestroy: testAccCheckGitlabProjectDestroy,
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(`
resource "gitlab_project" "this" {
name = "foo-%d"
visibility_level = "public"
initialize_with_readme = false
resolve_outdated_diff_discussions = false
auto_devops_enabled = false
autoclose_referenced_issues = false
emails_disabled = false
public_builds = false
merge_pipelines_enabled = false
merge_trains_enabled = false
}`, rInt),
},
{
ResourceName: "gitlab_project.this",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"initialize_with_readme"},
},
},
})
}

func testAccCheckGitlabProjectExists(n string, project *gitlab.Project) resource.TestCheckFunc {
return func(s *terraform.State) error {
var err error
Expand Down

0 comments on commit 0bca00d

Please sign in to comment.