github.com/m3db/m3@v1.5.0/src/ctl/service/r2/io.go (about) 1 // Copyright (c) 2017 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 r2 22 23 import ( 24 "encoding/json" 25 "fmt" 26 "io" 27 "net/http" 28 "reflect" 29 "strings" 30 31 "github.com/m3db/m3/src/metrics/rules/view/changes" 32 33 validator "gopkg.in/go-playground/validator.v9" 34 ) 35 36 // TODO(dgromov): Make this return a list of validation errors 37 func parseRequest(s interface{}, body io.ReadCloser) error { 38 if err := json.NewDecoder(body).Decode(s); err != nil { 39 return NewBadInputError(fmt.Sprintf("Malformed Json: %s", err.Error())) 40 } 41 42 // Invoking the validation explictely to have control over the format of the error output. 43 validate := validator.New() 44 validate.RegisterTagNameFunc(func(fld reflect.StructField) string { 45 parts := strings.SplitN(fld.Tag.Get("json"), ",", 2) 46 if len(parts) > 0 { 47 return parts[0] 48 } 49 return fld.Name 50 }) 51 52 var required []string 53 if err := validate.Struct(s); err != nil { 54 for _, e := range err.(validator.ValidationErrors) { 55 if e.ActualTag() == "required" { 56 required = append(required, e.Namespace()) 57 } 58 } 59 } 60 61 if len(required) > 0 { 62 return NewBadInputError(fmt.Sprintf("Required: [%v]", strings.Join(required, ", "))) 63 } 64 return nil 65 } 66 67 func writeAPIResponse(w http.ResponseWriter, code int, msg string) error { 68 j, err := json.Marshal(apiResponse{Code: code, Message: msg}) 69 if err != nil { 70 return err 71 } 72 return sendResponse(w, j, code) 73 } 74 75 func sendResponse(w http.ResponseWriter, data []byte, status int) error { 76 w.Header().Set("Content-Type", "application/json") 77 w.WriteHeader(status) 78 _, err := w.Write(data) 79 return err 80 } 81 82 type apiResponse struct { 83 Code int `json:"code"` 84 Message string `json:"message"` 85 } 86 87 type updateRuleSetRequest struct { 88 RuleSetChanges changes.RuleSetChanges `json:"rulesetChanges"` 89 RuleSetVersion int `json:"rulesetVersion"` 90 }