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 | "fmt" |
| 8 | "regexp" |
| 9 | ) |
| 10 | |
| 11 | const CloudTagKind = "cloud" |
| 12 | |
| 13 | var ( |
| 14 | cloudSnippet = "[a-zA-Z0-9][a-zA-Z0-9.-]*" |
| 15 | validCloud = regexp.MustCompile("^" + cloudSnippet + "$") |
| 16 | ) |
| 17 | |
| 18 | type CloudTag struct { |
| 19 | id string |
| 20 | } |
| 21 | |
| 22 | func (t CloudTag) String() string { return t.Kind() + "-" + t.id } |
| 23 | func (t CloudTag) Kind() string { return CloudTagKind } |
| 24 | func (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. |
| 28 | func 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. |
| 36 | func 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. |
| 49 | func IsValidCloud(id string) bool { |
| 50 | return validCloud.MatchString(id) |
| 51 | } |