github.com/loafoe/cli@v7.1.0+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 // NewServiceOfferingName provides a random name prefixed with INTEGRATION-SERVICE 70 func NewServiceOfferingName() string { 71 return PrefixedRandomName("INTEGRATION-SERVICE") 72 } 73 74 // NewServiceInstanceName provides a random name prefixed with INT-SI 75 func NewServiceInstanceName() string { 76 return PrefixedRandomName("INT-SI") 77 } 78 79 // NewServiceBrokerName provides a random name prefixed with INTEGRATION-SERVICE-BROKER 80 func NewServiceBrokerName() string { 81 return PrefixedRandomName("INTEGRATION-SERVICE-BROKER") 82 } 83 84 // NewPlanName provides a random name prefixed with INTEGRATION-PLAN 85 func NewPlanName() string { 86 return PrefixedRandomName("INTEGRATION-PLAN") 87 } 88 89 // NewPassword provides a random string prefixed with INTEGRATION-PASSWORD 90 func NewPassword() string { 91 return PrefixedRandomName("INTEGRATION-PASSWORD") 92 } 93 94 // NewSecurityGroupName provides a random name prefixed with INTEGRATION-SEC-GROUP. If an infix is provided, it 95 // is placed between INTEGRATION-SEC-GROUP and the random string. 96 func NewSecurityGroupName(infix ...string) string { 97 if len(infix) > 0 { 98 return PrefixedRandomName("INTEGRATION-SEC-GROUP-" + infix[0]) 99 } 100 101 return PrefixedRandomName("INTEGRATION-SEC-GROUP") 102 } 103 104 // NewSpaceName provides a random name prefixed with INTEGRATION-SPACE 105 func NewSpaceName() string { 106 return PrefixedRandomName("INTEGRATION-SPACE") 107 } 108 109 // NewUsername provides a random name prefixed with INTEGRATION-USER 110 func NewUsername() string { 111 return PrefixedRandomName("INTEGRATION-USER") 112 } 113 114 // NewBuildpackName provides a random name prefixed with INTEGRATION-BUILDPACK 115 func NewBuildpackName() string { 116 return PrefixedRandomName("INTEGRATION-BUILDPACK") 117 } 118 119 // NewStackName provides a random name prefixed with INTEGRATION-STACK 120 func NewStackName() string { 121 return PrefixedRandomName("INTEGRATION-STACK") 122 } 123 124 // NewDomainName provides a random domain name prefixed with integration. If prefix is provided the domain name 125 // will have structure "integration-prefix-randomstring.com" else it will have structure "integration-randomstring.com" 126 func NewDomainName(prefix ...string) string { 127 if len(prefix) > 0 { 128 return fmt.Sprintf("integration-%s.com", PrefixedRandomName(prefix[0])) 129 } 130 return fmt.Sprintf("integration%s.com", PrefixedRandomName("")) 131 } 132 133 // NewRouterGroupName provides a random name prefixed with INTEGRATION-ROUTER-GROUP 134 func NewRouterGroupName() string { 135 return PrefixedRandomName("INTEGRATION-ROUTER-GROUP") 136 } 137 138 // NewHostName provides a random name prefixed with `host` 139 func NewHostName() string { 140 return PrefixedRandomName("host") 141 } 142 143 // NewOrgQuotaName provides a random name prefixed with ORG-QUOTA 144 func NewOrgQuotaName() string { 145 return PrefixedRandomName("ORG-QUOTA") 146 } 147 148 // NewPath provides a random path prefixed with /PATH 149 func NewPath() string { 150 return PrefixedRandomName("/PATH") 151 } 152 153 // PrefixedRandomName provides a random name with structure "namePrefix-randomstring" 154 func PrefixedRandomName(namePrefix string) string { 155 return namePrefix + "-" + RandomName() 156 } 157 158 // RandomName provides a random string 159 func RandomName() string { 160 guid, err := uuid.NewV4() 161 if err != nil { 162 panic(err) 163 } 164 165 return guid.String() 166 }