29
25
discoveringSpaces chan struct{}
32
var dashPrefix = regexp.MustCompile("^-*")
33
var dashSuffix = regexp.MustCompile("-*$")
34
var multipleDashes = regexp.MustCompile("--+")
36
func convertSpaceName(name string, existing set.Strings) string {
37
// First lower case and replace spaces with dashes.
38
name = strings.Replace(name, " ", "-", -1)
39
name = strings.ToLower(name)
40
// Replace any character that isn't in the set "-", "a-z", "0-9".
41
name = network.SpaceInvalidChars.ReplaceAllString(name, "")
42
// Get rid of any dashes at the start as that isn't valid.
43
name = dashPrefix.ReplaceAllString(name, "")
44
// And any at the end.
45
name = dashSuffix.ReplaceAllString(name, "")
46
// Repleace multiple dashes with a single dash.
47
name = multipleDashes.ReplaceAllString(name, "-")
48
// Special case of when the space name was only dashes or invalid
53
// If this name is in use add a numerical suffix.
54
if existing.Contains(name) {
56
for existing.Contains(name + fmt.Sprintf("-%d", counter)) {
59
name = name + fmt.Sprintf("-%d", counter)
64
28
// NewWorker returns a worker
65
29
func NewWorker(api *discoverspaces.API) (worker.Worker, chan struct{}) {
66
30
dw := &discoverspacesWorker{
178
142
// The space is new, we need to create a valid name for it
180
spaceName := string(space.ProviderId)
144
spaceName := string(space.Name)
181
145
// Convert the name into a valid name that isn't already in
183
spaceName = convertSpaceName(spaceName, spaceNames)
147
spaceName = network.ConvertSpaceName(spaceName, spaceNames)
184
148
spaceNames.Add(spaceName)
185
149
spaceTag = names.NewSpaceTag(spaceName)
186
150
// We need to create the space.