Skip to content

Commit 78e8330

Browse files
erthalionsdudoladov
authored andcommitted
API url regexps (zalando#400)
* Make url regexp more flexible, to accept identifier with dashes * Add few simple tests * Check also numerics
1 parent 1b4181a commit 78e8330

File tree

2 files changed

+45
-4
lines changed

2 files changed

+45
-4
lines changed

pkg/apiserver/apiserver.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,22 @@ type Server struct {
4848
controller controllerInformer
4949
}
5050

51+
const (
52+
teamRe = `(?P<team>[a-zA-Z][a-zA-Z0-9\-_]*)`
53+
namespaceRe = `(?P<namespace>[a-z0-9]([-a-z0-9\-_]*[a-z0-9])?)`
54+
clusterRe = `(?P<cluster>[a-zA-Z][a-zA-Z0-9\-_]*)`
55+
)
56+
5157
var (
52-
clusterStatusURL = regexp.MustCompile(`^/clusters/(?P<team>[a-zA-Z][a-zA-Z0-9]*)/(?P<namespace>[a-z0-9]([-a-z0-9]*[a-z0-9])?)/(?P<cluster>[a-zA-Z][a-zA-Z0-9-]*)/?$`)
53-
clusterLogsURL = regexp.MustCompile(`^/clusters/(?P<team>[a-zA-Z][a-zA-Z0-9]*)/(?P<namespace>[a-z0-9]([-a-z0-9]*[a-z0-9])?)/(?P<cluster>[a-zA-Z][a-zA-Z0-9-]*)/logs/?$`)
54-
clusterHistoryURL = regexp.MustCompile(`^/clusters/(?P<team>[a-zA-Z][a-zA-Z0-9]*)/(?P<namespace>[a-z0-9]([-a-z0-9]*[a-z0-9])?)/(?P<cluster>[a-zA-Z][a-zA-Z0-9-]*)/history/?$`)
55-
teamURL = regexp.MustCompile(`^/clusters/(?P<team>[a-zA-Z][a-zA-Z0-9]*)/?$`)
58+
clusterStatusRe = fmt.Sprintf(`^/clusters/%s/%s/%s/?$`, teamRe, namespaceRe, clusterRe)
59+
clusterLogsRe = fmt.Sprintf(`^/clusters/%s/%s/%s/logs/?$`, teamRe, namespaceRe, clusterRe)
60+
clusterHistoryRe = fmt.Sprintf(`^/clusters/%s/%s/%s/history/?$`, teamRe, namespaceRe, clusterRe)
61+
teamURLRe = fmt.Sprintf(`^/clusters/%s/?$`, teamRe)
62+
63+
clusterStatusURL = regexp.MustCompile(clusterStatusRe)
64+
clusterLogsURL = regexp.MustCompile(clusterLogsRe)
65+
clusterHistoryURL = regexp.MustCompile(clusterHistoryRe)
66+
teamURL = regexp.MustCompile(teamURLRe)
5667
workerLogsURL = regexp.MustCompile(`^/workers/(?P<id>\d+)/logs/?$`)
5768
workerEventsQueueURL = regexp.MustCompile(`^/workers/(?P<id>\d+)/queue/?$`)
5869
workerStatusURL = regexp.MustCompile(`^/workers/(?P<id>\d+)/status/?$`)

pkg/apiserver/apiserver_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package apiserver
2+
3+
import (
4+
"testing"
5+
)
6+
7+
const (
8+
clusterStatusTest = "/clusters/test-id/test_namespace/testcluster/"
9+
clusterStatusNumericTest = "/clusters/test-id-1/test_namespace/testcluster/"
10+
clusterLogsTest = "/clusters/test-id/test_namespace/testcluster/logs/"
11+
teamTest = "/clusters/test-id/"
12+
)
13+
14+
func TestUrlRegexps(t *testing.T) {
15+
if clusterStatusURL.FindStringSubmatch(clusterStatusTest) == nil {
16+
t.Errorf("clusterStatusURL can't match %s", clusterStatusTest)
17+
}
18+
19+
if clusterStatusURL.FindStringSubmatch(clusterStatusNumericTest) == nil {
20+
t.Errorf("clusterStatusURL can't match %s", clusterStatusNumericTest)
21+
}
22+
23+
if clusterLogsURL.FindStringSubmatch(clusterLogsTest) == nil {
24+
t.Errorf("clusterLogsURL can't match %s", clusterLogsTest)
25+
}
26+
27+
if teamURL.FindStringSubmatch(teamTest) == nil {
28+
t.Errorf("teamURL can't match %s", teamTest)
29+
}
30+
}

0 commit comments

Comments
 (0)