github.com/m3db/m3@v1.5.0/src/query/api/v1/handler/topic/delete.go (about) 1 // Copyright (c) 2019 Uber Technologies, Inc. 2 // 3 // Permission is hereby granted, free of charge, to any person obtaining a copy 4 // of this software and associated documentation files (the "Software"), to deal 5 // in the Software without restriction, including without limitation the rights 6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 // copies of the Software, and to permit persons to whom the Software is 8 // furnished to do so, subject to the following conditions: 9 // 10 // The above copyright notice and this permission notice shall be included in 11 // all copies or substantial portions of the Software. 12 // 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 // THE SOFTWARE. 20 21 package topic 22 23 import ( 24 "fmt" 25 "net/http" 26 27 clusterclient "github.com/m3db/m3/src/cluster/client" 28 "github.com/m3db/m3/src/cluster/kv" 29 "github.com/m3db/m3/src/cluster/placementhandler/handleroptions" 30 "github.com/m3db/m3/src/cmd/services/m3query/config" 31 "github.com/m3db/m3/src/query/api/v1/route" 32 "github.com/m3db/m3/src/query/util/logging" 33 xerrors "github.com/m3db/m3/src/x/errors" 34 "github.com/m3db/m3/src/x/instrument" 35 xhttp "github.com/m3db/m3/src/x/net/http" 36 37 "go.uber.org/zap" 38 ) 39 40 const ( 41 // DeleteURL is the url for the topic delete handler (with the DELETE method). 42 DeleteURL = route.Prefix + "/topic" 43 44 // DeleteHTTPMethod is the HTTP method used with this resource. 45 DeleteHTTPMethod = http.MethodDelete 46 ) 47 48 // DeleteHandler is the handler for topic adds. 49 type DeleteHandler Handler 50 51 // newDeleteHandler returns a new instance of DeleteHandler. 52 func newDeleteHandler( 53 client clusterclient.Client, 54 cfg config.Configuration, 55 instrumentOpts instrument.Options, 56 ) http.Handler { 57 return &DeleteHandler{ 58 client: client, 59 cfg: cfg, 60 serviceFn: Service, 61 instrumentOpts: instrumentOpts, 62 } 63 } 64 65 func (h *DeleteHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { 66 var ( 67 ctx = r.Context() 68 logger = logging.WithContext(ctx, h.instrumentOpts) 69 ) 70 71 serviceCfg := handleroptions.ServiceNameAndDefaults{} 72 svcOpts := handleroptions.NewServiceOptions(serviceCfg, r.Header, nil) 73 service, err := h.serviceFn(h.client, svcOpts) 74 if err != nil { 75 logger.Error("unable to get service", zap.Error(err)) 76 xhttp.WriteError(w, err) 77 return 78 } 79 80 name := topicName(r.Header) 81 svcLogger := logger.With(zap.String("service", name)) 82 if err := service.Delete(name); err != nil { 83 svcLogger.Error("unable to delete service", zap.Error(err)) 84 if err == kv.ErrNotFound { 85 err = xerrors.NewInvalidParamsError(err) 86 } 87 err := xerrors.NewRenamedError(err, 88 fmt.Errorf("error deleting service '%s': %v", name, err)) 89 xhttp.WriteError(w, err) 90 return 91 } 92 93 svcLogger.Info("deleted service") 94 // This is technically not necessary but we prefer to be verbose in handler 95 // logic. 96 w.WriteHeader(http.StatusOK) 97 }