Skip to content

Commit

Permalink
Better handling of uint casts and missing values
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierre Cauchois committed Feb 2, 2023
1 parent 2b21989 commit aec698c
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 14 deletions.
33 changes: 28 additions & 5 deletions internal/sdkv2provider/resource_cloudflare_dlp_profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,30 @@ func resourceCloudflareDLPProfileRead(ctx context.Context, d *schema.ResourceDat
return nil
}

func uintFromResourceData(untypedVal interface{}) (uint, error) {
if intVal, ok := untypedVal.(int); ok && intVal >= 0 {
return uint(intVal), nil
}
return uint(0), fmt.Errorf("invalid value")
}

func resourceCloudflareDLPProfileCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*cloudflare.API)
identifier := cloudflare.AccountIdentifier(d.Get(consts.AccountIDSchemaKey).(string))

newDLPProfile := cloudflare.DLPProfile{
Name: d.Get("name").(string),
Type: d.Get("type").(string),
Description: d.Get("description").(string),
AllowedMatchCount: d.Get("allowed_match_count").(uint),
Name: d.Get("name").(string),
Type: d.Get("type").(string),
Description: d.Get("description").(string),
}

// Only set allowedMatchCount if the value exists and is valid
if allowedMatchCountRes := d.Get("allowed_match_count"); allowedMatchCountRes != nil {
allowedMatchCount, err := uintFromResourceData(allowedMatchCountRes)
if err != nil {
return diag.FromErr(fmt.Errorf("DLP Profiles: allowed_match_count: %v", err))
}
newDLPProfile.AllowedMatchCount = allowedMatchCount
}

if newDLPProfile.Type == DLPProfileTypePredefined {
Expand Down Expand Up @@ -163,7 +178,15 @@ func resourceCloudflareDLPProfileUpdate(ctx context.Context, d *schema.ResourceD
Type: d.Get("type").(string),
}
updatedDLPProfile.Description, _ = d.Get("description").(string)
updatedDLPProfile.AllowedMatchCount, _ = d.Get("allowed_match_count").(uint)

// Only set allowedMatchCount if the value exists. if it's omitted, don't overwrite.
if allowedMatchCountRes := d.Get("allowed_match_count"); allowedMatchCountRes != nil {
allowedMatchCount, err := uintFromResourceData(allowedMatchCountRes)
if err != nil {
return diag.FromErr(fmt.Errorf("DLP Profiles: allowed_match_count: %v", err))
}
updatedDLPProfile.AllowedMatchCount = allowedMatchCount
}

if entries, ok := d.GetOk("entry"); ok {
for _, entry := range entries.(*schema.Set).List() {
Expand Down
87 changes: 78 additions & 9 deletions internal/sdkv2provider/resource_cloudflare_dlp_profile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/stretchr/testify/assert"
)

func TestAccCloudflareDLPProfile_Custom(t *testing.T) {
Expand All @@ -18,13 +19,13 @@ func TestAccCloudflareDLPProfile_Custom(t *testing.T) {
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
Config: testAccCloudflareDLPProfileConfigCustom(accountID, rnd, "custom profile", 42),
Config: testAccCloudflareDLPProfileConfigCustom(accountID, rnd, "custom profile"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(name, "account_id", accountID),
resource.TestCheckResourceAttr(name, "name", rnd),
resource.TestCheckResourceAttr(name, "description", "custom profile"),
resource.TestCheckResourceAttr(name, "type", "custom"),
resource.TestCheckResourceAttr(name, "allowed_match_count", "42"),
resource.TestCheckResourceAttr(name, "allowed_match_count", "0"),
resource.TestCheckResourceAttr(name, "entry.0.name", fmt.Sprintf("%s_entry1", rnd)),
resource.TestCheckResourceAttr(name, "entry.0.enabled", "true"),
resource.TestCheckResourceAttr(name, "entry.0.pattern.0.regex", "^4[0-9]"),
Expand All @@ -46,12 +47,12 @@ func TestAccCloudflareDLPProfile_Custom_MultipleEntries(t *testing.T) {
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
Config: testAccCloudflareDLPProfileConfigCustomMultipleEntries(accountID, rnd, "custom profile 2", 0),
Config: testAccCloudflareDLPProfileConfigCustomMultipleEntries(accountID, rnd, "custom profile 2"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(name, "account_id", accountID),
resource.TestCheckResourceAttr(name, "name", rnd),
resource.TestCheckResourceAttr(name, "description", "custom profile 2"),
resource.TestCheckResourceAttr(name, "allowed_match_count", "0"),
resource.TestCheckResourceAttr(name, "description", "custom profile 2"),
resource.TestCheckResourceAttr(name, "type", "custom"),

resource.TestCheckTypeSetElemNestedAttrs(name, "entry.*", map[string]string{
Expand All @@ -73,13 +74,62 @@ func TestAccCloudflareDLPProfile_Custom_MultipleEntries(t *testing.T) {
})
}

func testAccCloudflareDLPProfileConfigCustom(accountID, rnd, description string, allowedMatchCount uint) string {
func TestAccCloudflareDLPProfile_CustomWithAllowedMatchCount(t *testing.T) {
rnd := generateRandomResourceName()
name := fmt.Sprintf("cloudflare_dlp_profile.%s", rnd)

resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheckAccount(t)
},
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
Config: testAccCloudflareDLPProfileConfigCustomWithAllowedMatchCount(accountID, rnd, "custom profile", 42),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(name, "account_id", accountID),
resource.TestCheckResourceAttr(name, "name", rnd),
resource.TestCheckResourceAttr(name, "description", "custom profile"),
resource.TestCheckResourceAttr(name, "allowed_match_count", "42"),
resource.TestCheckResourceAttr(name, "type", "custom"),
resource.TestCheckResourceAttr(name, "entry.0.name", fmt.Sprintf("%s_entry1", rnd)),
resource.TestCheckResourceAttr(name, "entry.0.enabled", "true"),
resource.TestCheckResourceAttr(name, "entry.0.pattern.0.regex", "^4[0-9]"),
resource.TestCheckResourceAttr(name, "entry.0.pattern.0.validation", "luhn"),
),
},
},
})
}

func TestUintFromResourceData(t *testing.T) {
for _, testVal := range []struct {
expectedVal uint
expectError bool
testObj interface{}
}{
{expectedVal: uint(0), expectError: false, testObj: 0},
{expectedVal: uint(42), expectError: false, testObj: 42},
{expectedVal: uint(0), expectError: true, testObj: "foo"},
{expectedVal: uint(0), expectError: true, testObj: -10},
{expectedVal: uint(0), expectError: true, testObj: nil},
} {
actual, err := uintFromResourceData(testVal.testObj)
if testVal.expectError {
assert.Error(t, err)
} else {
assert.Equal(t, testVal.expectedVal, actual)
assert.Nil(t, err)
}
}
}

func testAccCloudflareDLPProfileConfigCustom(accountID, rnd, description string) string {
return fmt.Sprintf(`
resource "cloudflare_dlp_profile" "%[1]s" {
account_id = "%[3]s"
name = "%[1]s"
description = "%[2]s"
allowed_match_count = %[4]d
type = "custom"
entry {
name = "%[1]s_entry1"
Expand All @@ -90,16 +140,15 @@ resource "cloudflare_dlp_profile" "%[1]s" {
}
}
}
`, rnd, description, accountID, allowedMatchCount)
`, rnd, description, accountID)
}

func testAccCloudflareDLPProfileConfigCustomMultipleEntries(accountID, rnd, description string, allowedMatchCount uint) string {
func testAccCloudflareDLPProfileConfigCustomMultipleEntries(accountID, rnd, description string) string {
return fmt.Sprintf(`
resource "cloudflare_dlp_profile" "%[1]s" {
account_id = "%[3]s"
name = "%[1]s"
description = "%[2]s"
allowed_match_count = %[4]d
type = "custom"
entry {
name = "%[1]s_entry1"
Expand All @@ -119,5 +168,25 @@ resource "cloudflare_dlp_profile" "%[1]s" {
}
}
}
`, rnd, description, accountID)
}

func testAccCloudflareDLPProfileConfigCustomWithAllowedMatchCount(accountID, rnd, description string, allowedMatchCount uint) string {
return fmt.Sprintf(`
resource "cloudflare_dlp_profile" "%[1]s" {
account_id = "%[3]s"
name = "%[1]s"
description = "%[2]s"
allowed_match_count = %[4]d
type = "custom"
entry {
name = "%[1]s_entry1"
enabled = true
pattern {
regex = "^4[0-9]"
validation = "luhn"
}
}
}
`, rnd, description, accountID, allowedMatchCount)
}

0 comments on commit aec698c

Please sign in to comment.