David K. Bainbridge | 528b318 | 2017-01-23 08:51:59 -0800 | [diff] [blame] | 1 | // Copyright 2016 Canonical Ltd. |
| 2 | // Licensed under the LGPLv3, see LICENCE file for details. |
| 3 | |
| 4 | package names |
| 5 | |
| 6 | import ( |
| 7 | "regexp" |
| 8 | ) |
| 9 | |
| 10 | // ControllerTagKind indicates that a tag belongs to a controller. |
| 11 | const ControllerTagKind = "controller" |
| 12 | |
| 13 | // ControllerTag represents a tag used to describe a controller. |
| 14 | type ControllerTag struct { |
| 15 | uuid string |
| 16 | } |
| 17 | |
| 18 | // Lowercase letters, digits and (non-leading) hyphens. |
| 19 | var validControllerName = regexp.MustCompile(`^[a-z0-9]+[a-z0-9-]*$`) |
| 20 | |
| 21 | // NewControllerTag returns the tag of an controller with the given controller UUID. |
| 22 | func NewControllerTag(uuid string) ControllerTag { |
| 23 | return ControllerTag{uuid: uuid} |
| 24 | } |
| 25 | |
| 26 | // ParseControllerTag parses an environ tag string. |
| 27 | func ParseControllerTag(controllerTag string) (ControllerTag, error) { |
| 28 | tag, err := ParseTag(controllerTag) |
| 29 | if err != nil { |
| 30 | return ControllerTag{}, err |
| 31 | } |
| 32 | et, ok := tag.(ControllerTag) |
| 33 | if !ok { |
| 34 | return ControllerTag{}, invalidTagError(controllerTag, ControllerTagKind) |
| 35 | } |
| 36 | return et, nil |
| 37 | } |
| 38 | |
| 39 | // String implements Tag. |
| 40 | func (t ControllerTag) String() string { return t.Kind() + "-" + t.Id() } |
| 41 | |
| 42 | // Kind implements Tag. |
| 43 | func (t ControllerTag) Kind() string { return ControllerTagKind } |
| 44 | |
| 45 | // Id implements Tag. |
| 46 | func (t ControllerTag) Id() string { return t.uuid } |
| 47 | |
| 48 | // IsValidController returns whether id is a valid controller UUID. |
| 49 | func IsValidController(id string) bool { |
| 50 | return validUUID.MatchString(id) |
| 51 | } |
| 52 | |
| 53 | // IsValidControllerName returns whether name is a valid string safe for a controller name. |
| 54 | func IsValidControllerName(name string) bool { |
| 55 | return validControllerName.MatchString(name) |
| 56 | } |