Skip to content

Commit

Permalink
fix terraform names
Browse files Browse the repository at this point in the history
  • Loading branch information
gonzalogarciajaubert committed Jan 25, 2024
1 parent bb846a7 commit cc1d165
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,21 @@ export function toTerraformName(name: string, suffix = ''): string {
const cleanName = terraformCleanText(name)
const totalLength = cleanName.length + cleanSuffix.length
if (totalLength <= MAX_TERRAFORM_SIZE_NAME) {
return cleanName + cleanSuffix
return startsWithLetter(cleanName + cleanSuffix)
}

if (cleanName.length <= cleanSuffix.length) {
return (cleanName + cleanSuffix).substr(0, MAX_TERRAFORM_SIZE_NAME - 1)
const newName = (cleanName + cleanSuffix).substr(0, MAX_TERRAFORM_SIZE_NAME - 1)
return startsWithLetter(newName)
}

const extraLength = totalLength - MAX_TERRAFORM_SIZE_NAME
const fixedCleanName = cleanName.substr(extraLength)
return fixedCleanName + cleanSuffix
return startsWithLetter(fixedCleanName + cleanSuffix)
}

function startsWithLetter(text: string): string {
return text.match(/^\d/) ? 'B' + text.substring(1) : text
}

export function toAzureName(name: string, maxSize = 24): string {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('Users want to use utility methods', () => {

it('with a long name and suffix return last name characters and the whole suffix', () => {
const result = toTerraformName('0123456789012345678901234', 'suffix')
expect(result).to.be.equal('789012345678901234suffix')
expect(result).to.be.equal('B89012345678901234suffix')
})

it('with a name and a 24 characters suffix return name and first suffix characters', () => {
Expand Down

0 comments on commit cc1d165

Please sign in to comment.