blob: 60e0db2f9dba2f27b22f3476429dabbd01d78627 [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
10const ModelTagKind = "model"
11
12// ModelTag represents a tag used to describe a model.
13type ModelTag struct {
14 uuid string
15}
16
17var validUUID = regexp.MustCompile(`[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}`)
18
19// Lowercase letters, digits and (non-leading) hyphens, as per LP:1568944 #5.
20var validModelName = regexp.MustCompile(`^[a-z0-9]+[a-z0-9-]*$`)
21
22// NewModelTag returns the tag of an model with the given model UUID.
23func NewModelTag(uuid string) ModelTag {
24 return ModelTag{uuid: uuid}
25}
26
27// ParseModelTag parses an environ tag string.
28func ParseModelTag(modelTag string) (ModelTag, error) {
29 tag, err := ParseTag(modelTag)
30 if err != nil {
31 return ModelTag{}, err
32 }
33 et, ok := tag.(ModelTag)
34 if !ok {
35 return ModelTag{}, invalidTagError(modelTag, ModelTagKind)
36 }
37 return et, nil
38}
39
40func (t ModelTag) String() string { return t.Kind() + "-" + t.Id() }
41func (t ModelTag) Kind() string { return ModelTagKind }
42func (t ModelTag) Id() string { return t.uuid }
43
44// IsValidModel returns whether id is a valid model UUID.
45func IsValidModel(id string) bool {
46 return validUUID.MatchString(id)
47}
48
49// IsValidModelName returns whether name is a valid string safe for a model name.
50func IsValidModelName(name string) bool {
51 return validModelName.MatchString(name)
52}