github.com/franc20/ayesa_sap@v7.0.0-beta.28.0.20200124003224-302d4d52fa6c+incompatible/integration/helpers/name_generator.go (about) 1 package helpers 2 3 import ( 4 "fmt" 5 "sort" 6 "strings" 7 8 uuid "github.com/nu7hatch/gouuid" 9 ) 10 11 // TODO: Is this working??? 12 13 // GenerateHigherName will use the passed randomNameGenerator to generate a name with a higher 14 // sort value than all the passed names 15 func GenerateHigherName(randomNameGenerator func() string, names ...string) string { 16 if len(names) == 0 { 17 return randomNameGenerator() 18 } 19 sort.Strings(names) 20 21 maxName := names[len(names)-1] 22 23 for { 24 name := randomNameGenerator() 25 // regenerate name if name is NOT higher 26 if strings.Compare(name, maxName) > 0 { 27 return name 28 } 29 } 30 } 31 32 // TODO: Is this working??? 33 34 // GenerateLowerName will use the passed randomNameGenerator to generate a name with a lower 35 // sort value than all the passed names 36 func GenerateLowerName(randomNameGenerator func() string, names ...string) string { 37 if len(names) == 0 { 38 return randomNameGenerator() 39 } 40 41 sort.Strings(names) 42 43 minName := names[0] 44 45 for { 46 name := randomNameGenerator() 47 // regenerate name if name is NOT higher 48 if strings.Compare(name, minName) < 0 { 49 return name 50 } 51 } 52 } 53 54 // NewAppName provides a random name prefixed with INTEGRATION-APP 55 func NewAppName() string { 56 return PrefixedRandomName("INTEGRATION-APP") 57 } 58 59 // NewIsolationSegmentName provides a random name prefixed with INTEGRATION-ISOLATION-SEGMENT 60 func NewIsolationSegmentName(infix ...string) string { 61 return PrefixedRandomName("INTEGRATION-ISOLATION-SEGMENT") 62 } 63 64 // NewOrgName provides a random name prefixed with INTEGRATION-ORG 65 func NewOrgName() string { 66 return PrefixedRandomName("INTEGRATION-ORG") 67 } 68 69 // NewServiceInstanceName provides a random name prefixed with INT-SI 70 func NewServiceInstanceName() string { 71 return PrefixedRandomName("INT-SI") 72 } 73 74 // NewServiceBrokerName provides a random name prefixed with INTEGRATION-SERVICE-BROKER 75 func NewServiceBrokerName() string { 76 return PrefixedRandomName("INTEGRATION-SERVICE-BROKER") 77 } 78 79 // NewPlanName provides a random name prefixed with INTEGRATION-PLAN 80 func NewPlanName() string { 81 return PrefixedRandomName("INTEGRATION-PLAN") 82 } 83 84 // NewPassword provides a random string prefixed with INTEGRATION-PASSWORD 85 func NewPassword() string { 86 return PrefixedRandomName("INTEGRATION-PASSWORD") 87 } 88 89 // NewSecurityGroupName provides a random name prefixed with INTEGRATION-SEC-GROUP. If an infix is provided, it 90 // is placed between INTEGRATION-SEC-GROUP and the random string. 91 func NewSecurityGroupName(infix ...string) string { 92 if len(infix) > 0 { 93 return PrefixedRandomName("INTEGRATION-SEC-GROUP-" + infix[0]) 94 } 95 96 return PrefixedRandomName("INTEGRATION-SEC-GROUP") 97 } 98 99 // NewSpaceName provides a random name prefixed with INTEGRATION-SPACE 100 func NewSpaceName() string { 101 return PrefixedRandomName("INTEGRATION-SPACE") 102 } 103 104 // NewUsername provides a random name prefixed with INTEGRATION-USER 105 func NewUsername() string { 106 return PrefixedRandomName("INTEGRATION-USER") 107 } 108 109 // NewBuildpackName provides a random name prefixed with INTEGRATION-BUILDPACK 110 func NewBuildpackName() string { 111 return PrefixedRandomName("INTEGRATION-BUILDPACK") 112 } 113 114 // NewStackName provides a random name prefixed with INTEGRATION-STACK 115 func NewStackName() string { 116 return PrefixedRandomName("INTEGRATION-STACK") 117 } 118 119 // NewDomainName provides a random domain name prefixed with integration. If prefix is provided the domain name 120 // will have structure "integration-prefix-randomstring.com" else it will have structure "integration-randomstring.com" 121 func NewDomainName(prefix ...string) string { 122 if len(prefix) > 0 { 123 return fmt.Sprintf("integration-%s.com", PrefixedRandomName(prefix[0])) 124 } 125 return fmt.Sprintf("integration%s.com", PrefixedRandomName("")) 126 } 127 128 // NewHostName provides a random name prefixed with HOST 129 func NewHostName() string { 130 return PrefixedRandomName("HOST") 131 } 132 133 // NewPath provides a random path prefixed with /PATH 134 func NewPath() string { 135 return PrefixedRandomName("/PATH") 136 } 137 138 // PrefixedRandomName provides a random name with structure "namePrefix-randomstring" 139 func PrefixedRandomName(namePrefix string) string { 140 return namePrefix + "-" + RandomName() 141 } 142 143 // RandomName provides a random string 144 func RandomName() string { 145 guid, err := uuid.NewV4() 146 if err != nil { 147 panic(err) 148 } 149 150 return guid.String() 151 }