github.com/m3db/m3@v1.5.0/src/query/api/v1/handler/topic/add.go (about) 1 // Copyright (c) 2018 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 "net/http" 25 26 clusterclient "github.com/m3db/m3/src/cluster/client" 27 "github.com/m3db/m3/src/cluster/placementhandler/handleroptions" 28 "github.com/m3db/m3/src/cmd/services/m3query/config" 29 "github.com/m3db/m3/src/msg/topic" 30 "github.com/m3db/m3/src/query/api/v1/route" 31 "github.com/m3db/m3/src/query/generated/proto/admin" 32 "github.com/m3db/m3/src/query/util/logging" 33 "github.com/m3db/m3/src/x/instrument" 34 xhttp "github.com/m3db/m3/src/x/net/http" 35 36 "go.uber.org/zap" 37 ) 38 39 const ( 40 // AddURL is the url for the topic add handler (with the POST method). 41 AddURL = route.Prefix + "/topic" 42 43 // AddHTTPMethod is the HTTP method used with this resource. 44 AddHTTPMethod = http.MethodPost 45 ) 46 47 // AddHandler is the handler for topic adds. 48 type AddHandler Handler 49 50 // newAddHandler returns a new instance of AddHandler. 51 func newAddHandler( 52 client clusterclient.Client, 53 cfg config.Configuration, 54 instrumentOpts instrument.Options, 55 ) http.Handler { 56 return &AddHandler{ 57 client: client, 58 cfg: cfg, 59 serviceFn: Service, 60 instrumentOpts: instrumentOpts, 61 } 62 } 63 64 func (h *AddHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { 65 var ( 66 ctx = r.Context() 67 logger = logging.WithContext(ctx, h.instrumentOpts) 68 req admin.TopicAddRequest 69 ) 70 rErr := parseRequest(r, &req) 71 if rErr != nil { 72 logger.Error("unable to parse request", zap.Error(rErr)) 73 xhttp.WriteError(w, rErr) 74 return 75 } 76 77 serviceCfg := handleroptions.ServiceNameAndDefaults{} 78 svcOpts := handleroptions.NewServiceOptions(serviceCfg, r.Header, nil) 79 service, err := h.serviceFn(h.client, svcOpts) 80 if err != nil { 81 logger.Error("unable to get service", zap.Error(err)) 82 xhttp.WriteError(w, err) 83 return 84 } 85 86 t, err := service.Get(topicName(r.Header)) 87 if err != nil { 88 logger.Error("unable to get topic", zap.Error(err)) 89 xhttp.WriteError(w, err) 90 return 91 } 92 93 cs, err := topic.NewConsumerServiceFromProto(req.ConsumerService) 94 if err != nil { 95 logger.Error("unable to parse consumer service", zap.Error(err)) 96 xhttp.WriteError(w, xhttp.NewError(err, http.StatusBadRequest)) 97 return 98 } 99 100 t, err = t.AddConsumerService(cs) 101 if err != nil { 102 logger.Error("unable to add consumer service", zap.Error(err)) 103 xhttp.WriteError(w, err) 104 return 105 } 106 107 t, err = service.CheckAndSet(t, t.Version()) 108 if err != nil { 109 logger.Error("unable to persist consumer service", zap.Error(err)) 110 xhttp.WriteError(w, err) 111 return 112 } 113 114 topicProto, err := topic.ToProto(t) 115 if err != nil { 116 logger.Error("unable to get topic protobuf", zap.Error(err)) 117 xhttp.WriteError(w, err) 118 return 119 } 120 121 resp := &admin.TopicGetResponse{ 122 Topic: topicProto, 123 Version: uint32(t.Version()), 124 } 125 126 xhttp.WriteProtoMsgJSONResponse(w, resp, logger) 127 }