make controllerinformer interface private;

use named regexp groups
This commit is contained in:
Murat Kabilov 2017-08-15 14:07:16 +02:00
parent 228639b839
commit 38e0ffecf7
3 changed files with 66 additions and 14 deletions

View File

@ -14,6 +14,7 @@ import (
"github.com/Sirupsen/logrus" "github.com/Sirupsen/logrus"
"github.com/zalando-incubator/postgres-operator/pkg/spec" "github.com/zalando-incubator/postgres-operator/pkg/spec"
"github.com/zalando-incubator/postgres-operator/pkg/util"
"github.com/zalando-incubator/postgres-operator/pkg/util/config" "github.com/zalando-incubator/postgres-operator/pkg/util/config"
) )
@ -24,7 +25,7 @@ const (
) )
// ControllerInformer describes stats methods of a controller // ControllerInformer describes stats methods of a controller
type ControllerInformer interface { type controllerInformer interface {
GetConfig() *spec.ControllerConfig GetConfig() *spec.ControllerConfig
GetOperatorConfig() *config.Config GetOperatorConfig() *config.Config
GetStatus() *spec.ControllerStatus GetStatus() *spec.ControllerStatus
@ -40,7 +41,7 @@ type ControllerInformer interface {
type Server struct { type Server struct {
logger *logrus.Entry logger *logrus.Entry
http http.Server http http.Server
controller ControllerInformer controller controllerInformer
} }
var ( var (
@ -54,7 +55,7 @@ var (
) )
// New creates new HTTP API server // New creates new HTTP API server
func New(controller ControllerInformer, port int, logger *logrus.Logger) *Server { func New(controller controllerInformer, port int, logger *logrus.Logger) *Server {
s := &Server{ s := &Server{
logger: logger.WithField("pkg", "apiserver"), logger: logger.WithField("pkg", "apiserver"),
controller: controller, controller: controller,
@ -140,24 +141,24 @@ func (s *Server) clusters(w http.ResponseWriter, req *http.Request) {
err error err error
) )
if matches := clusterStatusURL.FindAllStringSubmatch(req.URL.Path, -1); matches != nil { if matches := util.FindNamedStringSubmatch(clusterStatusURL, req.URL.Path); matches != nil {
resp, err = s.controller.ClusterStatus(matches[0][1], matches[0][2]) resp, err = s.controller.ClusterStatus(matches["team"], matches["cluster"])
} else if matches := teamURL.FindAllStringSubmatch(req.URL.Path, -1); matches != nil { } else if matches := util.FindNamedStringSubmatch(teamURL, req.URL.Path); matches != nil {
teamClusters := s.controller.TeamClusterList() teamClusters := s.controller.TeamClusterList()
clusters, found := teamClusters[matches[0][1]] clusters, found := teamClusters[matches["team"]]
if !found { if !found {
s.respond(nil, fmt.Errorf("could not find clusters for the team"), w) s.respond(nil, fmt.Errorf("could not find clusters for the team"), w)
} }
clusterNames := make([]string, 0) clusterNames := make([]string, 0)
for _, cluster := range clusters { for _, cluster := range clusters {
clusterNames = append(clusterNames, cluster.Name[len(matches[0][1])+1:]) clusterNames = append(clusterNames, cluster.Name[len(matches["team"])+1:])
} }
s.respond(clusterNames, nil, w) s.respond(clusterNames, nil, w)
return return
} else if matches := clusterLogsURL.FindAllStringSubmatch(req.URL.Path, -1); matches != nil { } else if matches := util.FindNamedStringSubmatch(clusterLogsURL, req.URL.Path); matches != nil {
resp, err = s.controller.ClusterLogs(matches[0][1], matches[0][2]) resp, err = s.controller.ClusterLogs(matches["team"], matches["cluster"])
} else if req.URL.Path == clustersURL { } else if req.URL.Path == clustersURL {
res := make(map[string][]string) res := make(map[string][]string)
for team, clusters := range s.controller.TeamClusterList() { for team, clusters := range s.controller.TeamClusterList() {
@ -181,15 +182,16 @@ func (s *Server) workers(w http.ResponseWriter, req *http.Request) {
resp interface{} resp interface{}
err error err error
) )
if workerAllQueue.MatchString(req.URL.Path) { if workerAllQueue.MatchString(req.URL.Path) {
s.allQueues(w, req) s.allQueues(w, req)
return return
} else if matches := workerLogsURL.FindAllStringSubmatch(req.URL.Path, -1); matches != nil { } else if matches := util.FindNamedStringSubmatch(workerLogsURL, req.URL.Path); matches != nil {
workerID, _ := strconv.Atoi(matches[0][1]) workerID, _ := strconv.Atoi(matches["id"])
resp, err = s.controller.WorkerLogs(uint32(workerID)) resp, err = s.controller.WorkerLogs(uint32(workerID))
} else if matches := workerEventsQueueURL.FindAllStringSubmatch(req.URL.Path, -1); matches != nil { } else if matches := util.FindNamedStringSubmatch(workerEventsQueueURL, req.URL.Path); matches != nil {
workerID, _ := strconv.Atoi(matches[0][1]) workerID, _ := strconv.Atoi(matches["id"])
resp, err = s.controller.ListQueue(uint32(workerID)) resp, err = s.controller.ListQueue(uint32(workerID))
} else { } else {

View File

@ -10,6 +10,8 @@ import (
"github.com/motomux/pretty" "github.com/motomux/pretty"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"regexp"
"github.com/zalando-incubator/postgres-operator/pkg/spec" "github.com/zalando-incubator/postgres-operator/pkg/spec"
) )
@ -71,3 +73,29 @@ OUTER:
} }
return result, len(result) == 0 return result, len(result) == 0
} }
func FindNamedStringSubmatch(r *regexp.Regexp, s string) map[string]string {
matches := r.FindStringSubmatch(s)
grNames := r.SubexpNames()
if matches == nil {
return nil
}
groupMatches := 0
res := make(map[string]string, len(grNames))
for i, n := range grNames {
if n == "" {
continue
}
res[n] = matches[i]
groupMatches++
}
if groupMatches == 0 {
return nil
}
return res
}

View File

@ -6,6 +6,8 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"regexp"
"github.com/zalando-incubator/postgres-operator/pkg/spec" "github.com/zalando-incubator/postgres-operator/pkg/spec"
) )
@ -44,6 +46,17 @@ var substractTest = []struct {
{[]string{"a", "b", "c", "d"}, []string{"a", "bb", "c", "d"}, []string{"b"}, false}, {[]string{"a", "b", "c", "d"}, []string{"a", "bb", "c", "d"}, []string{"b"}, false},
} }
var substringMatch = []struct {
inRegex *regexp.Regexp
inStr string
out map[string]string
}{
{regexp.MustCompile(`aaaa (?P<num>\d+) bbbb`), "aaaa 123 bbbb", map[string]string{"num": "123"}},
{regexp.MustCompile(`aaaa (?P<num>\d+) bbbb`), "a aa 123 bbbb", nil},
{regexp.MustCompile(`aaaa \d+ bbbb`), "aaaa 123 bbbb", nil},
{regexp.MustCompile(`aaaa (\d+) bbbb`), "aaaa 123 bbbb", nil},
}
func TestRandomPassword(t *testing.T) { func TestRandomPassword(t *testing.T) {
const pwdLength = 10 const pwdLength = 10
pwd := RandomPassword(pwdLength) pwd := RandomPassword(pwdLength)
@ -100,3 +113,12 @@ func TestSubstractSlices(t *testing.T) {
} }
} }
} }
func TestFindNamedStringSubmatch(t *testing.T) {
for _, tt := range substringMatch {
actualRes := FindNamedStringSubmatch(tt.inRegex, tt.inStr)
if !reflect.DeepEqual(actualRes, tt.out) {
t.Errorf("FindNamedStringSubmatch expected: %#v, got: %#v", tt.out, actualRes)
}
}
}