Skip to content

Commit

Permalink
OCM-5397 | feat: add truncate common function
Browse files Browse the repository at this point in the history
  • Loading branch information
gdbranco committed Jan 8, 2024
1 parent 255ae8b commit 3ab619e
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pkg/aws/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"strings"

"github.com/aws/aws-sdk-go-v2/aws/arn"
commonUtils "github.com/openshift-online/ocm-common/pkg/utils"
)

func GetPathFromArn(arnStr string) (string, error) {
Expand All @@ -20,3 +21,7 @@ func GetPathFromArn(arnStr string) (string, error) {
path := resource[firstIndex : lastIndex+1]
return path, nil
}

func TruncateRoleName(name string) string {
return commonUtils.Truncate(name, commonUtils.MaxByteSize)
}
14 changes: 14 additions & 0 deletions pkg/aws/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,18 @@ var _ = Describe("AWS Utils", func() {
Expect(err).To(HaveOccurred())
})
})

var _ = Describe("Validates TruncateRoleName function", func() {
It("Doesn't do anything", func() {
smallerThanByteInLength := "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
truncated := TruncateRoleName(smallerThanByteInLength)
Expect(smallerThanByteInLength).To(Equal(truncated))
})
It("Truncate when bigger than 64", func() {
smallerThanByteInLength := "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
truncated := TruncateRoleName(smallerThanByteInLength)
Expect(len(truncated)).To(Equal(64))
Expect(smallerThanByteInLength).To(Equal(truncated + "a"))
})
})
})
7 changes: 7 additions & 0 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,10 @@ func RandomLabel(size int) string {
}
return string(chars)
}

func Truncate(s string, truncateLength int) string {
if len(s) > truncateLength {
s = s[0:truncateLength]
}
return s
}
13 changes: 13 additions & 0 deletions pkg/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,17 @@ var _ = Describe("Utils", func() {
})
})
})
var _ = Describe("Validates Truncate function", func() {
It("Doesn't do anything", func() {
smallerThanByteInLength := "aaaaa"
truncated := Truncate(smallerThanByteInLength, 5)
Expect(smallerThanByteInLength).To(Equal(truncated))
})
It("Truncate when bigger than supplied value", func() {
smallerThanByteInLength := "aaaaaa"
truncated := Truncate(smallerThanByteInLength, 5)
Expect(len(truncated)).To(Equal(5))
Expect(smallerThanByteInLength).To(Equal(truncated + "a"))
})
})
})

0 comments on commit 3ab619e

Please sign in to comment.