-
Notifications
You must be signed in to change notification settings - Fork 161
/
pool_or_instance_slug.go
56 lines (44 loc) · 1.48 KB
/
pool_or_instance_slug.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package director
import (
"fmt"
"strings"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
)
type InstanceGroupOrInstanceSlug struct {
name string
indexOrID string // optional
}
func NewInstanceGroupOrInstanceSlug(name, indexOrID string) InstanceGroupOrInstanceSlug {
if len(name) == 0 {
panic("Expected pool or instance to specify non-empty name")
}
return InstanceGroupOrInstanceSlug{name: name, indexOrID: indexOrID}
}
func NewInstanceGroupOrInstanceSlugFromString(str string) (InstanceGroupOrInstanceSlug, error) {
pieces := strings.Split(str, "/")
if len(pieces) != 1 && len(pieces) != 2 {
return InstanceGroupOrInstanceSlug{}, bosherr.Errorf(
"Expected pool or instance '%s' to be in format 'name' or 'name/id-or-index'", str)
}
if len(pieces[0]) == 0 {
return InstanceGroupOrInstanceSlug{}, bosherr.Errorf(
"Expected pool or instance '%s' to specify non-empty name", str)
}
slug := InstanceGroupOrInstanceSlug{name: pieces[0]}
if len(pieces) == 2 {
if len(pieces[1]) == 0 {
return InstanceGroupOrInstanceSlug{}, bosherr.Errorf(
"Expected instance '%s' to specify non-empty ID or index", str)
}
slug.indexOrID = pieces[1]
}
return slug, nil
}
func (s InstanceGroupOrInstanceSlug) Name() string { return s.name }
func (s InstanceGroupOrInstanceSlug) IndexOrID() string { return s.indexOrID }
func (s InstanceGroupOrInstanceSlug) String() string {
if len(s.indexOrID) > 0 {
return fmt.Sprintf("%s/%s", s.name, s.indexOrID)
}
return s.name
}