// title: logs config set // path: /docker/logs // method: POST // consume: application/x-www-form-urlencoded // produce: application/x-json-stream // responses: // 200: Ok // 400: Invalid data // 401: Unauthorized func logsConfigSetHandler(w http.ResponseWriter, r *http.Request, t auth.Token) (err error) { err = r.ParseForm() if err != nil { return &tsuruErrors.HTTP{ Code: http.StatusBadRequest, Message: fmt.Sprintf("unable to parse form values: %s", err), } } pool := r.FormValue("pool") restart, _ := strconv.ParseBool(r.FormValue("restart")) delete(r.Form, "pool") delete(r.Form, "restart") var conf container.DockerLogConfig dec := form.NewDecoder(nil) dec.IgnoreUnknownKeys(true) err = dec.DecodeValues(&conf, r.Form) if err != nil { return &tsuruErrors.HTTP{ Code: http.StatusBadRequest, Message: fmt.Sprintf("unable to parse fields in docker log config: %s", err), } } var ctxs []permission.PermissionContext if pool != "" { ctxs = append(ctxs, permission.Context(permission.CtxPool, pool)) } hasPermission := permission.Check(t, permission.PermPoolUpdateLogs, ctxs...) if !hasPermission { return permission.ErrUnauthorized } evt, err := event.New(&event.Opts{ Target: event.Target{Type: event.TargetTypePool, Value: pool}, Kind: permission.PermPoolUpdateLogs, Owner: t, CustomData: event.FormToCustomData(r.Form), DisableLock: true, Allowed: event.Allowed(permission.PermPoolReadEvents, ctxs...), }) if err != nil { return err } defer func() { evt.Done(err) }() err = conf.Save(pool) if err != nil { return err } w.Header().Set("Content-Type", "application/x-json-stream") keepAliveWriter := tsuruIo.NewKeepAliveWriter(w, 15*time.Second, "") defer keepAliveWriter.Stop() writer := &tsuruIo.SimpleJsonMessageEncoderWriter{Encoder: json.NewEncoder(keepAliveWriter)} fmt.Fprintln(writer, "Log config successfully updated.") if restart { filter := &app.Filter{} if pool != "" { filter.Pools = []string{pool} } return tryRestartAppsByFilter(filter, writer) } return nil }
func (s *HandlersSuite) TestDockerLogsInfoHandler(c *check.C) { recorder := httptest.NewRecorder() request, err := http.NewRequest("GET", "/docker/logs", nil) c.Assert(err, check.IsNil) request.Header.Set("Authorization", "bearer "+s.token.GetValue()) server := api.RunServer(true) server.ServeHTTP(recorder, request) c.Assert(recorder.Code, check.Equals, http.StatusOK) c.Assert(recorder.Header().Get("Content-Type"), check.Equals, "application/json") var conf map[string]container.DockerLogConfig err = json.Unmarshal(recorder.Body.Bytes(), &conf) c.Assert(err, check.IsNil) c.Assert(conf, check.DeepEquals, map[string]container.DockerLogConfig{ "": {}, }) newConf := container.DockerLogConfig{Driver: "syslog"} err = newConf.Save("p1") c.Assert(err, check.IsNil) request, err = http.NewRequest("GET", "/docker/logs", nil) c.Assert(err, check.IsNil) request.Header.Set("Authorization", "bearer "+s.token.GetValue()) recorder = httptest.NewRecorder() server.ServeHTTP(recorder, request) c.Assert(recorder.Code, check.Equals, http.StatusOK) var conf2 map[string]container.DockerLogConfig err = json.Unmarshal(recorder.Body.Bytes(), &conf2) c.Assert(err, check.IsNil) c.Assert(conf2, check.DeepEquals, map[string]container.DockerLogConfig{ "": {}, "p1": {Driver: "syslog", LogOpts: map[string]string{}}, }) }
func logsConfigSetHandler(w http.ResponseWriter, r *http.Request, t auth.Token) error { err := r.ParseForm() if err != nil { return &errors.HTTP{ Code: http.StatusBadRequest, Message: fmt.Sprintf("unable to parse form values: %s", err), } } pool := r.Form.Get("pool") restart, _ := strconv.ParseBool(r.Form.Get("restart")) delete(r.Form, "pool") delete(r.Form, "restart") var conf container.DockerLogConfig dec := form.NewDecoder(nil) dec.IgnoreUnknownKeys(true) err = dec.DecodeValues(&conf, r.Form) if err != nil { return &errors.HTTP{ Code: http.StatusBadRequest, Message: fmt.Sprintf("unable to parse fields in docker log config: %s", err), } } if pool == "" && !permission.Check(t, permission.PermPoolUpdateLogs) { return permission.ErrUnauthorized } hasPermission := permission.Check(t, permission.PermPoolUpdateLogs, permission.Context(permission.CtxPool, pool)) if !hasPermission { return permission.ErrUnauthorized } err = conf.Save(pool) if err != nil { return err } keepAliveWriter := tsuruIo.NewKeepAliveWriter(w, 15*time.Second, "") defer keepAliveWriter.Stop() writer := &tsuruIo.SimpleJsonMessageEncoderWriter{Encoder: json.NewEncoder(keepAliveWriter)} fmt.Fprintln(writer, "Log config successfully updated.") if restart { filter := &app.Filter{} if pool != "" { filter.Pools = []string{pool} } tryRestartAppsByFilter(filter, writer) } return nil }