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

     1  // Copyright 2019 The Terraformer Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package okta
    16  
    17  import (
    18  	"fmt"
    19  	"math/rand"
    20  	"regexp"
    21  	"strings"
    22  )
    23  
    24  // Making resource's name less ugly
    25  func normalizeResourceName(s string) string {
    26  	specialChars := `<>()*#{}[]|@_ .%'",&`
    27  	for _, c := range specialChars {
    28  		s = strings.ReplaceAll(s, string(c), "-")
    29  	}
    30  
    31  	s = regexp.MustCompile(`^[^a-zA-Z_]+`).ReplaceAllLiteralString(s, "")
    32  	s = strings.TrimSuffix(s, "-")
    33  
    34  	return strings.ToLower(s)
    35  }
    36  
    37  func normalizeResourceNameWithRandom(s string, rand bool) string {
    38  	specialChars := `-<>()*#{}[]|@_ .%'",&`
    39  	for _, c := range specialChars {
    40  		s = strings.ReplaceAll(s, string(c), "_")
    41  	}
    42  	s = regexp.MustCompile(`^[^a-zA-Z_]+`).ReplaceAllLiteralString(s, "")
    43  	s = strings.TrimSuffix(s, "`_")
    44  	if rand {
    45  		randString := RandStringBytes(4)
    46  		return fmt.Sprintf("%s_%s", strings.ToLower(s), randString)
    47  	}
    48  	return strings.ToLower(s)
    49  }
    50  
    51  const letterBytes = "abcdefghijklmnopqrstuvwxyz0123456789"
    52  
    53  func RandStringBytes(n int) string {
    54  	b := make([]byte, n)
    55  	for i := range b {
    56  		b[i] = letterBytes[rand.Intn(len(letterBytes))]
    57  	}
    58  	return string(b)
    59  }