blob: c48c326c0876946cd6504ab523f43dec0ac5c0c5 [file] [log] [blame]
David K. Bainbridge528b3182017-01-23 08:51:59 -08001// Copyright 2016 Canonical Ltd.
2// Licensed under the LGPLv3, see LICENCE file for details.
3
4package names
5
6import (
7 "regexp"
8)
9
10// ControllerTagKind indicates that a tag belongs to a controller.
11const ControllerTagKind = "controller"
12
13// ControllerTag represents a tag used to describe a controller.
14type ControllerTag struct {
15 uuid string
16}
17
18// Lowercase letters, digits and (non-leading) hyphens.
19var validControllerName = regexp.MustCompile(`^[a-z0-9]+[a-z0-9-]*$`)
20
21// NewControllerTag returns the tag of an controller with the given controller UUID.
22func NewControllerTag(uuid string) ControllerTag {
23 return ControllerTag{uuid: uuid}
24}
25
26// ParseControllerTag parses an environ tag string.
27func 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.
40func (t ControllerTag) String() string { return t.Kind() + "-" + t.Id() }
41
42// Kind implements Tag.
43func (t ControllerTag) Kind() string { return ControllerTagKind }
44
45// Id implements Tag.
46func (t ControllerTag) Id() string { return t.uuid }
47
48// IsValidController returns whether id is a valid controller UUID.
49func 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.
54func IsValidControllerName(name string) bool {
55 return validControllerName.MatchString(name)
56}