github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/ibm/helpers.go (about)

     1  package ibm
     2  
     3  import (
     4  	"fmt"
     5  	"math/rand"
     6  	"regexp"
     7  	"strings"
     8  )
     9  
    10  func normalizeResourceName(s string, rand bool) string {
    11  	specialChars := `-<>()*#{}[]|@_ .%'",&`
    12  	for _, c := range specialChars {
    13  		s = strings.ReplaceAll(s, string(c), "_")
    14  	}
    15  	s = regexp.MustCompile(`^[^a-zA-Z_]+`).ReplaceAllLiteralString(s, "")
    16  	s = strings.TrimSuffix(s, "`_")
    17  	if rand {
    18  		randString := RandStringBytes(4)
    19  		return fmt.Sprintf("%s_%s", strings.ToLower(s), randString)
    20  	}
    21  	return strings.ToLower(s)
    22  }
    23  
    24  const letterBytes = "abcdefghijklmnopqrstuvwxyz0123456789"
    25  
    26  func RandStringBytes(n int) string {
    27  	b := make([]byte, n)
    28  	for i := range b {
    29  		b[i] = letterBytes[rand.Intn(len(letterBytes))]
    30  	}
    31  	return string(b)
    32  }
    33  func getRandom(names map[string]struct{}, name string, random bool) (map[string]struct{}, bool) {
    34  	if _, ok := names[name]; ok {
    35  		random = true
    36  	}
    37  	names[name] = struct{}{}
    38  	return names, random
    39  }