From dc0321072349fbe36a90596fd7ed959121e347b3 Mon Sep 17 00:00:00 2001 From: Niklas Beinghaus Date: Wed, 7 Feb 2024 15:12:31 +0100 Subject: [PATCH] Fix: change type of attribute `public` to bool (#393) https://github.com/goharbor/terraform-provider-harbor/issues/392 --------- Signed-off-by: Niklas Beinghaus Signed-off-by: flbla Co-authored-by: flbla --- client/misc.go | 8 ++++++ client/misc_test.go | 29 +++++++++++++++++++++ client/project.go | 2 +- provider/resource_project.go | 49 +++++++++++++----------------------- 4 files changed, 56 insertions(+), 32 deletions(-) diff --git a/client/misc.go b/client/misc.go index bbe49b3a..4e537896 100644 --- a/client/misc.go +++ b/client/misc.go @@ -2,6 +2,7 @@ package client import ( "regexp" + "strconv" "strings" ) @@ -31,3 +32,10 @@ func GetSchedule(schedule string) (typefmt string, cronfmt string) { } var regexCron = regexp.MustCompile(`(?m)((((\d+,)+\d+|(\d+(\/|-)\d+)|\d+|\*) ?){5,7})`) + +func ParseBoolOrDefault(value string, defaultValue bool) (bool, error) { + if value == "" { + return defaultValue, nil + } + return strconv.ParseBool(value) +} diff --git a/client/misc_test.go b/client/misc_test.go index 6c3a64e9..15644527 100644 --- a/client/misc_test.go +++ b/client/misc_test.go @@ -20,3 +20,32 @@ func TestGetSchedule(t *testing.T) { t.Error("Invalid cron string") } } + +func TestParseBoolOrDefault(t *testing.T) { + tests := []struct { + name string + value string + defaultVal bool + expectedVal bool + expectError bool + }{ + {"Empty string, default false", "", false, false, false}, + {"Empty string, default true", "", true, true, false}, + {"Value 'true', default false", "true", false, true, false}, + {"Value 'false', default true", "false", true, false, false}, + {"Invalid value", "invalid", false, false, true}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + result, err := ParseBoolOrDefault(tt.value, tt.defaultVal) + if (err != nil) != tt.expectError { + t.Errorf("ParseBoolOrDefault() error = %v, expectError %v", err, tt.expectError) + return + } + if result != tt.expectedVal { + t.Errorf("ParseBoolOrDefault() = %v, want %v", result, tt.expectedVal) + } + }) + } +} diff --git a/client/project.go b/client/project.go index 136e445c..f64ba201 100644 --- a/client/project.go +++ b/client/project.go @@ -26,7 +26,7 @@ func ProjectBody(d *schema.ResourceData) models.ProjectsBodyPost { } body.Metadata.AutoScan = strconv.FormatBool(d.Get("vulnerability_scanning").(bool)) - body.Metadata.Public = d.Get("public").(string) + body.Metadata.Public = strconv.FormatBool(d.Get("public").(bool)) security := d.Get("deployment_security").(string) if security != "" { diff --git a/provider/resource_project.go b/provider/resource_project.go index 4acf0f35..b457aa71 100644 --- a/provider/resource_project.go +++ b/provider/resource_project.go @@ -3,7 +3,6 @@ package provider import ( "encoding/json" "fmt" - "strconv" "github.com/goharbor/terraform-provider-harbor/client" "github.com/goharbor/terraform-provider-harbor/models" @@ -29,9 +28,9 @@ func resourceProject() *schema.Resource { Optional: true, }, "public": { - Type: schema.TypeString, + Type: schema.TypeBool, Optional: true, - Default: "false", + Default: false, }, "vulnerability_scanning": { Type: schema.TypeBool, @@ -120,43 +119,31 @@ func resourceProjectRead(d *schema.ResourceData, m interface{}) error { if err != nil { return fmt.Errorf("resource not found %s", d.Id()) } - autoScan := jsonData.Metadata.AutoScan - var vuln bool - if autoScan == "" { - vuln = false - } else { - vuln, err = strconv.ParseBool(autoScan) - if err != nil { - return err - } + + vuln, err := client.ParseBoolOrDefault(jsonData.Metadata.AutoScan, false) + if err != nil { + return err } - var trust bool - trustContent := jsonData.Metadata.EnableContentTrust - if trustContent == "" { - trust = false - } else { - trust, err = strconv.ParseBool(trustContent) - if err != nil { - return err - } + trust, err := client.ParseBoolOrDefault(jsonData.Metadata.EnableContentTrust, false) + if err != nil { + return err } - var trustCosign bool - trustContentCosign := jsonData.Metadata.EnableContentTrustCosign - if trustContentCosign == "" { - trustCosign = false - } else { - trustCosign, err = strconv.ParseBool(trustContentCosign) - if err != nil { - return err - } + trustCosign, err := client.ParseBoolOrDefault(jsonData.Metadata.EnableContentTrustCosign, false) + if err != nil { + return err + } + + public, err := client.ParseBoolOrDefault(jsonData.Metadata.Public, false) + if err != nil { + return err } d.Set("name", jsonData.Name) d.Set("project_id", jsonData.ProjectID) d.Set("registry_id", jsonData.RegistryID) - d.Set("public", jsonData.Metadata.Public) + d.Set("public", public) d.Set("vulnerability_scanning", vuln) d.Set("enable_content_trust", trust) d.Set("enable_content_trust_cosign", trustCosign)