Skip to content

Commit

Permalink
Make duplicated keys after sanitization an error.
Browse files Browse the repository at this point in the history
  • Loading branch information
boris-smidt-klarrio committed Dec 4, 2024
1 parent f7b981f commit f0927f5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
12 changes: 8 additions & 4 deletions azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ import (
)

var (
ErrAzureTooManyTags error = errors.New("Only up to 50 tags can be set on an azure resource")
ErrAzureValueToLong error = errors.New("A value can only contain 256 characters")
ErrAzureTooManyTags error = errors.New("Only up to 50 tags can be set on an azure resource")
ErrAzureValueToLong error = errors.New("A value can only contain 256 characters")
ErrAzureDuplicatedTags error = errors.New("There are duplicated keys after sanitization")
)

type DiskTags = map[string]*string
Expand Down Expand Up @@ -92,13 +93,16 @@ func sanitizeLabelsForAzure(tags map[string]string) (DiskTags, error) {
return nil, ErrAzureTooManyTags
}
for k, v := range tags {
k = sanitizeKeyForAzure(k)
sanitizedKey := sanitizeKeyForAzure(k)
value, err := sanitizeValueForAzure(v)
if err != nil {
return nil, err
}

diskTags[k] = &value
if _, ok := diskTags[sanitizedKey]; ok {
return nil, fmt.Errorf("tag is duplicated after sanitization colliding tags key=%s sanitized-key=%s: %w", k, sanitizedKey, ErrAzureDuplicatedTags)
}
diskTags[sanitizedKey] = &value
}

return diskTags, nil
Expand Down
9 changes: 9 additions & 0 deletions azure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,15 @@ func Test_sanitizeLabelsForAzure(t *testing.T) {
_, err = sanitizeLabelsForAzure(tags)
assert.ErrorIs(t, err, ErrAzureTooManyTags)
})

t.Run("the sanitize lables gives an error when there a duplicated tags after sanitization", func(t *testing.T) {
t.Parallel()
tags := map[string]string{}
tags["Kubernetes/Cluster"] = "foo"
tags["KubernetesCluster"] = "bar"
_, err := sanitizeLabelsForAzure(tags)
assert.ErrorIs(t, err, ErrAzureDuplicatedTags)
})
}

func Test_diskScope(t *testing.T) {
Expand Down

0 comments on commit f0927f5

Please sign in to comment.