Skip to content

Commit

Permalink
Fix: change type of attribute public to bool (#393)
Browse files Browse the repository at this point in the history
#392

---------

Signed-off-by: Niklas Beinghaus <[email protected]>
Signed-off-by: flbla <[email protected]>
Co-authored-by: flbla <[email protected]>
  • Loading branch information
Niklas Beinghaus and flbla authored Feb 7, 2024
1 parent dd9723d commit dc03210
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 32 deletions.
8 changes: 8 additions & 0 deletions client/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package client

import (
"regexp"
"strconv"
"strings"
)

Expand Down Expand Up @@ -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)
}
29 changes: 29 additions & 0 deletions client/misc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
}
}
2 changes: 1 addition & 1 deletion client/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 != "" {
Expand Down
49 changes: 18 additions & 31 deletions provider/resource_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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,
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit dc03210

Please sign in to comment.