-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathsubnet.go
47 lines (38 loc) · 1.09 KB
/
subnet.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Copyright 2015 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package names
import (
"fmt"
"regexp"
)
const SubnetTagKind = "subnet"
var validSubnet = regexp.MustCompile("^" + NumberSnippet + "$")
// IsValidSubnet returns whether id is a valid subnet id.
func IsValidSubnet(id string) bool {
return validSubnet.MatchString(id)
}
type SubnetTag struct {
id string
}
func (t SubnetTag) String() string { return t.Kind() + "-" + t.id }
func (t SubnetTag) Kind() string { return SubnetTagKind }
func (t SubnetTag) Id() string { return t.id }
// NewSubnetTag returns the tag for subnet with the given ID.
func NewSubnetTag(id string) SubnetTag {
if !IsValidSubnet(id) {
panic(fmt.Sprintf("%s is not a valid subnet ID", id))
}
return SubnetTag{id: id}
}
// ParseSubnetTag parses a subnet tag string.
func ParseSubnetTag(subnetTag string) (SubnetTag, error) {
tag, err := ParseTag(subnetTag)
if err != nil {
return SubnetTag{}, err
}
subt, ok := tag.(SubnetTag)
if !ok {
return SubnetTag{}, invalidTagError(subnetTag, SubnetTagKind)
}
return subt, nil
}