github.com/m3db/m3@v1.5.0/src/query/api/v1/handler/topic/init.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  	// InitURL is the url for the topic init handler (with the POST method).
    41  	InitURL = route.Prefix + "/topic/init"
    42  
    43  	// InitHTTPMethod is the HTTP method used with this resource.
    44  	InitHTTPMethod = http.MethodPost
    45  )
    46  
    47  // InitHandler is the handler for topic inits.
    48  type InitHandler Handler
    49  
    50  // newInitHandler returns a new instance of InitHandler.
    51  func newInitHandler(
    52  	client clusterclient.Client,
    53  	cfg config.Configuration,
    54  	instrumentOpts instrument.Options,
    55  ) http.Handler {
    56  	return &InitHandler{
    57  		client:         client,
    58  		cfg:            cfg,
    59  		serviceFn:      Service,
    60  		instrumentOpts: instrumentOpts,
    61  	}
    62  }
    63  
    64  func (h *InitHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    65  	var (
    66  		ctx    = r.Context()
    67  		logger = logging.WithContext(ctx, h.instrumentOpts)
    68  		req    admin.TopicInitRequest
    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 := topic.NewTopic().
    87  		SetName(topicName(r.Header)).
    88  		SetNumberOfShards(req.NumberOfShards)
    89  	t, err = service.CheckAndSet(t, 0)
    90  	if err != nil {
    91  		logger.Error("unable to init topic", zap.Error(err))
    92  		xhttp.WriteError(w, err)
    93  		return
    94  	}
    95  
    96  	topicProto, err := topic.ToProto(t)
    97  	if err != nil {
    98  		logger.Error("unable to get topic protobuf", zap.Error(err))
    99  		xhttp.WriteError(w, err)
   100  		return
   101  	}
   102  
   103  	resp := &admin.TopicGetResponse{
   104  		Topic:   topicProto,
   105  		Version: uint32(t.Version()),
   106  	}
   107  
   108  	xhttp.WriteProtoMsgJSONResponse(w, resp, logger)
   109  }