David K. Bainbridge | 528b318 | 2017-01-23 08:51:59 -0800 | [diff] [blame] | 1 | // Copyright 2015 Canonical Ltd. |
| 2 | // Licensed under the LGPLv3, see LICENCE file for details. |
| 3 | |
| 4 | package names |
| 5 | |
| 6 | import ( |
| 7 | "github.com/juju/utils" |
| 8 | ) |
| 9 | |
| 10 | const IPAddressTagKind = "ipaddress" |
| 11 | |
| 12 | // IsValidIPAddress returns whether id is a valid IP address ID. |
| 13 | // Here it simply is checked if it is a valid UUID. |
| 14 | func IsValidIPAddress(id string) bool { |
| 15 | return utils.IsValidUUIDString(id) |
| 16 | } |
| 17 | |
| 18 | type IPAddressTag struct { |
| 19 | id utils.UUID |
| 20 | } |
| 21 | |
| 22 | func (t IPAddressTag) String() string { return t.Kind() + "-" + t.id.String() } |
| 23 | func (t IPAddressTag) Kind() string { return IPAddressTagKind } |
| 24 | func (t IPAddressTag) Id() string { return t.id.String() } |
| 25 | |
| 26 | // NewIPAddressTag returns the tag for the IP address with the given ID (UUID). |
| 27 | func NewIPAddressTag(id string) IPAddressTag { |
| 28 | uuid, err := utils.UUIDFromString(id) |
| 29 | if err != nil { |
| 30 | panic(err) |
| 31 | } |
| 32 | return IPAddressTag{id: uuid} |
| 33 | } |
| 34 | |
| 35 | // ParseIPAddressTag parses an IP address tag string. |
| 36 | func ParseIPAddressTag(ipAddressTag string) (IPAddressTag, error) { |
| 37 | tag, err := ParseTag(ipAddressTag) |
| 38 | if err != nil { |
| 39 | return IPAddressTag{}, err |
| 40 | } |
| 41 | ipat, ok := tag.(IPAddressTag) |
| 42 | if !ok { |
| 43 | return IPAddressTag{}, invalidTagError(ipAddressTag, IPAddressTagKind) |
| 44 | } |
| 45 | return ipat, nil |
| 46 | } |