blob: 78a3c8bdc04118ca4b532a0a47530f9996e2071c [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 "fmt"
8 "regexp"
9)
10
11const CloudTagKind = "cloud"
12
13var (
14 cloudSnippet = "[a-zA-Z0-9][a-zA-Z0-9.-]*"
15 validCloud = regexp.MustCompile("^" + cloudSnippet + "$")
16)
17
18type CloudTag struct {
19 id string
20}
21
22func (t CloudTag) String() string { return t.Kind() + "-" + t.id }
23func (t CloudTag) Kind() string { return CloudTagKind }
24func (t CloudTag) Id() string { return t.id }
25
26// NewCloudTag returns the tag for the cloud with the given ID.
27// It will panic if the given cloud ID is not valid.
28func NewCloudTag(id string) CloudTag {
29 if !IsValidCloud(id) {
30 panic(fmt.Sprintf("%q is not a valid cloud ID", id))
31 }
32 return CloudTag{id}
33}
34
35// ParseCloudTag parses a cloud tag string.
36func ParseCloudTag(cloudTag string) (CloudTag, error) {
37 tag, err := ParseTag(cloudTag)
38 if err != nil {
39 return CloudTag{}, err
40 }
41 dt, ok := tag.(CloudTag)
42 if !ok {
43 return CloudTag{}, invalidTagError(cloudTag, CloudTagKind)
44 }
45 return dt, nil
46}
47
48// IsValidCloud returns whether id is a valid cloud ID.
49func IsValidCloud(id string) bool {
50 return validCloud.MatchString(id)
51}