github.com/m3db/m3@v1.5.0/src/ctl/service/r2/routes.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 "fmt" 25 "net/http" 26 27 "github.com/m3db/m3/src/metrics/rules/view" 28 29 "github.com/gorilla/mux" 30 ) 31 32 func fetchNamespaces(s *service, _ *http.Request) (data interface{}, err error) { 33 return s.store.FetchNamespaces() 34 } 35 36 func fetchNamespace(s *service, r *http.Request) (data interface{}, err error) { 37 return s.store.FetchRuleSetSnapshot(mux.Vars(r)[namespaceIDVar]) 38 } 39 40 func createNamespace(s *service, r *http.Request) (data interface{}, err error) { 41 var n view.Namespace 42 if err := parseRequest(&n, r.Body); err != nil { 43 return nil, err 44 } 45 46 uOpts, err := s.newUpdateOptions(r) 47 if err != nil { 48 return nil, err 49 } 50 51 return s.store.CreateNamespace(n.ID, uOpts) 52 } 53 54 func validateRuleSet(s *service, r *http.Request) (data interface{}, err error) { 55 vars := mux.Vars(r) 56 var ruleset view.RuleSet 57 if err := parseRequest(&ruleset, r.Body); err != nil { 58 return nil, err 59 } 60 61 if vars[namespaceIDVar] != ruleset.Namespace { 62 return nil, fmt.Errorf( 63 "namespaceID param %s and ruleset namespaceID %s do not match", 64 vars[namespaceIDVar], 65 ruleset.Namespace, 66 ) 67 } 68 69 if err := s.store.ValidateRuleSet(ruleset); err != nil { 70 return nil, err 71 } 72 73 return "Ruleset is valid", nil 74 } 75 76 func updateRuleSet(s *service, r *http.Request) (data interface{}, err error) { 77 var req updateRuleSetRequest 78 if err := parseRequest(&req, r.Body); err != nil { 79 return nil, NewBadInputError(err.Error()) 80 } 81 if len(req.RuleSetChanges.MappingRuleChanges) == 0 && 82 len(req.RuleSetChanges.RollupRuleChanges) == 0 { 83 return nil, NewBadInputError( 84 "invalid request: no ruleset changes detected", 85 ) 86 } 87 88 uOpts, err := s.newUpdateOptions(r) 89 if err != nil { 90 return nil, err 91 } 92 93 return s.store.UpdateRuleSet(req.RuleSetChanges, req.RuleSetVersion, uOpts) 94 } 95 96 func deleteNamespace(s *service, r *http.Request) (data interface{}, err error) { 97 vars := mux.Vars(r) 98 namespaceID := vars[namespaceIDVar] 99 100 uOpts, err := s.newUpdateOptions(r) 101 if err != nil { 102 return nil, err 103 } 104 105 if err := s.store.DeleteNamespace(namespaceID, uOpts); err != nil { 106 return nil, err 107 } 108 return fmt.Sprintf("Deleted namespace %s", namespaceID), nil 109 } 110 111 func fetchMappingRule(s *service, r *http.Request) (data interface{}, err error) { 112 vars := mux.Vars(r) 113 return s.store.FetchMappingRule(vars[namespaceIDVar], vars[ruleIDVar]) 114 } 115 116 func createMappingRule(s *service, r *http.Request) (data interface{}, err error) { 117 vars := mux.Vars(r) 118 var mr view.MappingRule 119 if err := parseRequest(&mr, r.Body); err != nil { 120 return nil, err 121 } 122 123 uOpts, err := s.newUpdateOptions(r) 124 if err != nil { 125 return nil, err 126 } 127 128 return s.store.CreateMappingRule(vars[namespaceIDVar], mr, uOpts) 129 } 130 131 func updateMappingRule(s *service, r *http.Request) (data interface{}, err error) { 132 vars := mux.Vars(r) 133 134 var mrj view.MappingRule 135 if err := parseRequest(&mrj, r.Body); err != nil { 136 return nil, err 137 } 138 139 uOpts, err := s.newUpdateOptions(r) 140 if err != nil { 141 return nil, err 142 } 143 144 return s.store.UpdateMappingRule(vars[namespaceIDVar], vars[ruleIDVar], mrj, uOpts) 145 } 146 147 func deleteMappingRule(s *service, r *http.Request) (data interface{}, err error) { 148 vars := mux.Vars(r) 149 namespaceID := vars[namespaceIDVar] 150 mappingRuleID := vars[ruleIDVar] 151 152 uOpts, err := s.newUpdateOptions(r) 153 if err != nil { 154 return nil, err 155 } 156 157 if err := s.store.DeleteMappingRule(namespaceID, mappingRuleID, uOpts); err != nil { 158 return nil, err 159 } 160 161 return fmt.Sprintf("Deleted mapping rule: %s in namespace %s", mappingRuleID, namespaceID), nil 162 } 163 164 func fetchMappingRuleHistory(s *service, r *http.Request) (data interface{}, err error) { 165 vars := mux.Vars(r) 166 snapshots, err := s.store.FetchMappingRuleHistory(vars[namespaceIDVar], vars[ruleIDVar]) 167 if err != nil { 168 return nil, err 169 } 170 return view.MappingRuleSnapshots{MappingRules: snapshots}, nil 171 } 172 173 func fetchRollupRule(s *service, r *http.Request) (data interface{}, err error) { 174 vars := mux.Vars(r) 175 return s.store.FetchRollupRule(vars[namespaceIDVar], vars[ruleIDVar]) 176 } 177 178 func createRollupRule(s *service, r *http.Request) (data interface{}, err error) { 179 vars := mux.Vars(r) 180 namespaceID := vars[namespaceIDVar] 181 182 var rrj view.RollupRule 183 if err := parseRequest(&rrj, r.Body); err != nil { 184 return nil, err 185 } 186 187 uOpts, err := s.newUpdateOptions(r) 188 if err != nil { 189 return nil, err 190 } 191 192 return s.store.CreateRollupRule(namespaceID, rrj, uOpts) 193 } 194 195 func updateRollupRule(s *service, r *http.Request) (data interface{}, err error) { 196 vars := mux.Vars(r) 197 var rrj view.RollupRule 198 if err := parseRequest(&rrj, r.Body); err != nil { 199 return nil, err 200 } 201 202 uOpts, err := s.newUpdateOptions(r) 203 if err != nil { 204 return nil, err 205 } 206 207 return s.store.UpdateRollupRule(vars[namespaceIDVar], vars[ruleIDVar], rrj, uOpts) 208 } 209 210 func deleteRollupRule(s *service, r *http.Request) (data interface{}, err error) { 211 vars := mux.Vars(r) 212 namespaceID := vars[namespaceIDVar] 213 rollupRuleID := vars[ruleIDVar] 214 215 uOpts, err := s.newUpdateOptions(r) 216 if err != nil { 217 return nil, err 218 } 219 220 if err := s.store.DeleteRollupRule(namespaceID, rollupRuleID, uOpts); err != nil { 221 return nil, err 222 } 223 224 return fmt.Sprintf("Deleted rollup rule: %s in namespace %s", rollupRuleID, namespaceID), nil 225 } 226 227 func fetchRollupRuleHistory(s *service, r *http.Request) (data interface{}, err error) { 228 vars := mux.Vars(r) 229 snapshots, err := s.store.FetchRollupRuleHistory(vars[namespaceIDVar], vars[ruleIDVar]) 230 if err != nil { 231 return nil, err 232 } 233 return view.RollupRuleSnapshots{RollupRules: snapshots}, nil 234 }