blob: b92a389d161c2b427f2e3a7fb9cec2bb62f33284 [file] [log] [blame]
David K. Bainbridge528b3182017-01-23 08:51:59 -08001// Copyright 2015 Canonical Ltd.
2// Licensed under the LGPLv3, see LICENCE file for details.
3
4package names
5
6import (
7 "github.com/juju/utils"
8)
9
10const 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.
14func IsValidIPAddress(id string) bool {
15 return utils.IsValidUUIDString(id)
16}
17
18type IPAddressTag struct {
19 id utils.UUID
20}
21
22func (t IPAddressTag) String() string { return t.Kind() + "-" + t.id.String() }
23func (t IPAddressTag) Kind() string { return IPAddressTagKind }
24func (t IPAddressTag) Id() string { return t.id.String() }
25
26// NewIPAddressTag returns the tag for the IP address with the given ID (UUID).
27func 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.
36func 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}