Skip to content

Commit af565bd

Browse files
committed
feat: support UUID v7 tags for spaces and subnets
A bug has been discovered recently in Juju 4.0 where we cannot move spaces because the subnet (which is automatically retrieved and inserted into the db with a new UUID v7 as primary key) cannot be passed as a new Subnet tag and therefore it panics. The fix is to add a new validation regex for spaces and subnets which validate UUIDs v7 and keep the legacy (and deprecated) sequence ID as fallback validation for backward compatibility.
1 parent 383bcab commit af565bd

File tree

5 files changed

+23
-4
lines changed

5 files changed

+23
-4
lines changed

space.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,16 @@ const (
1313
SpaceSnippet = "(?:[a-z0-9]+(?:-[a-z0-9]+)*)"
1414
)
1515

16-
var validSpace = regexp.MustCompile("^" + SpaceSnippet + "$")
16+
var (
17+
validSpace = regexp.MustCompile("^" + UUIDv7Snippet + "$")
18+
// Deprecated: Juju 4.0 should have space IDs in the form of UUIDv7.
19+
fallbackValidSpace = regexp.MustCompile("^" + SpaceSnippet + "$")
20+
)
1721

1822
// IsValidSpace reports whether name is a valid space name.
1923
func IsValidSpace(name string) bool {
20-
return validSpace.MatchString(name)
24+
return validSpace.MatchString(name) ||
25+
fallbackValidSpace.MatchString(name)
2126
}
2227

2328
type SpaceTag struct {

space_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ var parseSpaceTagTests = []struct {
6060
}, {
6161
tag: "space-1",
6262
expected: names.NewSpaceTag("1"),
63+
}, {
64+
tag: "space-0195847b-95bb-7ca1-a7ee-2211d802d5b3",
65+
expected: names.NewSpaceTag("0195847b-95bb-7ca1-a7ee-2211d802d5b3"),
6366
}, {
6467
tag: "-space1",
6568
err: names.InvalidTagError("-space1", ""),

subnet.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,16 @@ import (
1010

1111
const SubnetTagKind = "subnet"
1212

13-
var validSubnet = regexp.MustCompile("^" + NumberSnippet + "$")
13+
var (
14+
validSubnet = regexp.MustCompile("^" + UUIDv7Snippet + "$")
15+
// Deprecated: Juju 4.0 should have subnet IDs in the form of UUIDv7.
16+
fallbackValidSubnet = regexp.MustCompile("^" + NumberSnippet + "$")
17+
)
1418

1519
// IsValidSubnet returns whether id is a valid subnet id.
1620
func IsValidSubnet(id string) bool {
17-
return validSubnet.MatchString(id)
21+
return validSubnet.MatchString(id) ||
22+
fallbackValidSubnet.MatchString(id)
1823
}
1924

2025
type SubnetTag struct {

subnet_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ var parseSubnetTagTests = []struct {
3838
}, {
3939
tag: "subnet-16",
4040
expected: names.NewSubnetTag("16"),
41+
}, {
42+
tag: "subnet-0195847b-95bb-7ca1-a7ee-2211d802d5b3",
43+
expected: names.NewSubnetTag("0195847b-95bb-7ca1-a7ee-2211d802d5b3"),
4144
}, {
4245
tag: "subnet-foo",
4346
err: names.InvalidTagError("subnet-foo", names.SubnetTagKind),

tag.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ const (
1919
// NumberSnippet is a non-compiled regexp that can be composed with other
2020
// snippets for validating small number sequences.
2121
NumberSnippet = "(?:0|[1-9][0-9]*)"
22+
// UUIDv7Snippet is a non-compiled regexp that can be composed with other
23+
// snippets for validating UUID v7 strings.
24+
UUIDv7Snippet = "[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}"
2225
)
2326

2427
var (

0 commit comments

Comments
 (0)