~juju-qa/ubuntu/xenial/juju/xenial-2.0-beta3

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/worker/discoverspaces/discoverspaces.go

  • Committer: Martin Packman
  • Date: 2016-03-30 19:31:08 UTC
  • mfrom: (1.1.41)
  • Revision ID: martin.packman@canonical.com-20160330193108-h9iz3ak334uk0z5r
Merge new upstream source 2.0~beta3

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
package discoverspaces
5
5
 
6
6
import (
7
 
        "fmt"
8
 
        "regexp"
9
 
        "strings"
10
 
 
11
7
        "github.com/juju/errors"
12
8
        "github.com/juju/loggo"
13
9
        "github.com/juju/names"
29
25
        discoveringSpaces chan struct{}
30
26
}
31
27
 
32
 
var dashPrefix = regexp.MustCompile("^-*")
33
 
var dashSuffix = regexp.MustCompile("-*$")
34
 
var multipleDashes = regexp.MustCompile("--+")
35
 
 
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
49
 
        // characters!
50
 
        if name == "" {
51
 
                name = "empty"
52
 
        }
53
 
        // If this name is in use add a numerical suffix.
54
 
        if existing.Contains(name) {
55
 
                counter := 2
56
 
                for existing.Contains(name + fmt.Sprintf("-%d", counter)) {
57
 
                        counter += 1
58
 
                }
59
 
                name = name + fmt.Sprintf("-%d", counter)
60
 
        }
61
 
        return name
62
 
}
63
 
 
64
28
// NewWorker returns a worker
65
29
func NewWorker(api *discoverspaces.API) (worker.Worker, chan struct{}) {
66
30
        dw := &discoverspacesWorker{
177
141
                } else {
178
142
                        // The space is new, we need to create a valid name for it
179
143
                        // in state.
180
 
                        spaceName := string(space.ProviderId)
 
144
                        spaceName := string(space.Name)
181
145
                        // Convert the name into a valid name that isn't already in
182
146
                        // use.
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.