From c31e3dab69fc0fa55c4ddc0db8ccfb3f523109ce Mon Sep 17 00:00:00 2001 From: Matt Schallert Date: Tue, 5 Mar 2019 21:50:36 -0500 Subject: [PATCH] refactor allowedservices --- src/query/api/v1/handler/allowed_services.go | 56 +++++++++++++++++++ .../api/v1/handler/allowed_services_test.go | 45 +++++++++++++++ src/query/api/v1/handler/database/create.go | 2 +- src/query/api/v1/handler/placement/common.go | 24 ++------ .../api/v1/handler/placement/common_test.go | 2 +- 5 files changed, 107 insertions(+), 22 deletions(-) create mode 100644 src/query/api/v1/handler/allowed_services.go create mode 100644 src/query/api/v1/handler/allowed_services_test.go diff --git a/src/query/api/v1/handler/allowed_services.go b/src/query/api/v1/handler/allowed_services.go new file mode 100644 index 0000000000..075b05c9f1 --- /dev/null +++ b/src/query/api/v1/handler/allowed_services.go @@ -0,0 +1,56 @@ +// Copyright (c) 2019 Uber Technologies, Inc. +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +package handler + +type allowedServicesSet map[string]bool + +func (a allowedServicesSet) String() []string { + s := make([]string, 0, len(a)) + for key := range a { + s = append(s, key) + } + return s +} + +var ( + allowedServices = allowedServicesSet{ + M3DBServiceName: true, + M3AggregatorServiceName: true, + M3CoordinatorServiceName: true, + } +) + +// IsAllowedService returns whether a service name is a valid M3 service. +func IsAllowedService(svc string) bool { + _, ok := allowedServices[svc] + return ok +} + +// AllowedServices returns the list of valid M3 services. +func AllowedServices() []string { + svcs := make([]string, 0, len(allowedServices)) + for svc, allowed := range allowedServices { + if allowed { + svcs = append(svcs, svc) + } + } + return svcs +} diff --git a/src/query/api/v1/handler/allowed_services_test.go b/src/query/api/v1/handler/allowed_services_test.go new file mode 100644 index 0000000000..8d3fe1fa25 --- /dev/null +++ b/src/query/api/v1/handler/allowed_services_test.go @@ -0,0 +1,45 @@ +// Copyright (c) 2019 Uber Technologies, Inc. +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +package handler + +import ( + "sort" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestIsAllowedService(t *testing.T) { + assert.True(t, IsAllowedService("m3db")) + assert.False(t, IsAllowedService("foo")) +} + +func TestAllowedServices(t *testing.T) { + exp := []string{ + "m3aggregator", + "m3coordinator", + "m3db", + } + + svcs := AllowedServices() + sort.Strings(svcs) + assert.Equal(t, exp, svcs) +} diff --git a/src/query/api/v1/handler/database/create.go b/src/query/api/v1/handler/database/create.go index b1b9a11033..8c8fac3478 100644 --- a/src/query/api/v1/handler/database/create.go +++ b/src/query/api/v1/handler/database/create.go @@ -205,7 +205,7 @@ func (h *createHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { return } - opts := handler.NewServiceOptions("kv", r.Header, nil) + opts := handler.NewServiceOptions(handler.M3DBServiceName, r.Header, nil) nsRegistry, err = h.namespaceAddHandler.Add(namespaceRequest, opts) if err != nil { logger.Error("unable to add namespace", zap.Error(err)) diff --git a/src/query/api/v1/handler/placement/common.go b/src/query/api/v1/handler/placement/common.go index 67a3ebcddc..4765fb2c35 100644 --- a/src/query/api/v1/handler/placement/common.go +++ b/src/query/api/v1/handler/placement/common.go @@ -42,16 +42,6 @@ import ( "github.com/gorilla/mux" ) -type allowedServicesSet map[string]bool - -func (a allowedServicesSet) String() []string { - s := make([]string, 0, len(a)) - for key := range a { - s = append(s, key) - } - return s -} - const ( // ServicesPathName is the services part of the API path. ServicesPathName = "services" @@ -70,12 +60,6 @@ var ( M3CoordinatorServicePlacementPathName = path.Join(ServicesPathName, handler.M3CoordinatorServiceName, PlacementPathName) errUnableToParseService = errors.New("unable to parse service") - - allowedServices = allowedServicesSet{ - handler.M3DBServiceName: true, - handler.M3AggregatorServiceName: true, - handler.M3CoordinatorServiceName: true, - } ) // HandlerOptions is the options struct for the handler. @@ -148,10 +132,10 @@ func ServiceWithAlgo( return nil, nil, err } - if _, ok := allowedServices[opts.ServiceName]; !ok { + if !handler.IsAllowedService(opts.ServiceName) { return nil, nil, fmt.Errorf( - "invalid service name: %s, must be one of: %s", - opts.ServiceName, allowedServices.String()) + "invalid service name: %s, must be one of: %v", + opts.ServiceName, handler.AllowedServices()) } sid := opts.ServiceID() @@ -438,7 +422,7 @@ func parseServiceFromRequest(r *http.Request) (string, error) { for i, c := range components { if c == "services" && i+1 < len(components) { service := components[i+1] - if _, ok := allowedServices[service]; ok { + if handler.IsAllowedService(service) { return service, nil } return "", fmt.Errorf("unknown service: %s", service) diff --git a/src/query/api/v1/handler/placement/common_test.go b/src/query/api/v1/handler/placement/common_test.go index bceaf5eb1b..317574319d 100644 --- a/src/query/api/v1/handler/placement/common_test.go +++ b/src/query/api/v1/handler/placement/common_test.go @@ -281,7 +281,7 @@ func TestValidateAllAvailable(t *testing.T) { } func runForAllAllowedServices(f func(service string)) { - for service := range allowedServices { + for _, service := range handler.AllowedServices() { f(service) } }