Beispiel #1
0
func (s *Server) UnpauseResource(pipelineDB db.PipelineDB) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		resourceName := rata.Param(r, "resource_name")

		_, found, err := pipelineDB.GetResource(resourceName)
		if err != nil {
			s.logger.Error("failed-to-get-resource", err)
			w.WriteHeader(http.StatusInternalServerError)
			return
		}

		if !found {
			s.logger.Debug("resource-not-found", lager.Data{"resource": resourceName})
			w.WriteHeader(http.StatusNotFound)
			return
		}

		err = pipelineDB.UnpauseResource(resourceName)
		if err != nil {
			w.WriteHeader(http.StatusInternalServerError)
			return
		}

		w.WriteHeader(http.StatusOK)
	})
}
Beispiel #2
0
func (s *Server) GetConfig(w http.ResponseWriter, r *http.Request) {
	logger := s.logger.Session("get-config")
	pipelineName := rata.Param(r, "pipeline_name")
	config, rawConfig, id, err := s.db.GetConfig(atc.DefaultTeamName, pipelineName)
	if err != nil {
		if malformedErr, ok := err.(atc.MalformedConfigError); ok {
			getConfigResponse := atc.ConfigResponse{
				Errors:    []string{malformedErr.Error()},
				RawConfig: rawConfig,
			}

			responseJSON, err := json.Marshal(getConfigResponse)
			if err != nil {
				w.WriteHeader(http.StatusInternalServerError)
			}

			w.Header().Set(atc.ConfigVersionHeader, fmt.Sprintf("%d", id))
			w.Write(responseJSON)

			return
		}

		logger.Error("failed-to-get-config", err)
		w.WriteHeader(http.StatusInternalServerError)

		return
	}

	w.Header().Set(atc.ConfigVersionHeader, fmt.Sprintf("%d", id))

	json.NewEncoder(w).Encode(atc.ConfigResponse{
		Config:    &config,
		RawConfig: rawConfig,
	})
}
func (h *NetworksListContainers) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
	logger := h.Logger.Session("networks-list-containers")
	id := rata.Param(req, "network_id")

	allContainers, err := h.Datastore.All()
	if err != nil {
		logger.Error("datastore-all-failed", err)
		resp.WriteHeader(http.StatusInternalServerError)
		return
	}

	containers := []models.Container{}
	for _, c := range allContainers {
		if c.NetworkID == id {
			containers = append(containers, c)
		}
	}

	payload, err := h.Marshaler.Marshal(containers)
	if err != nil {
		logger.Error("marshal-failed", err)
		resp.WriteHeader(http.StatusInternalServerError)
		return
	}

	resp.WriteHeader(http.StatusOK)
	resp.Write(payload)
}
func (h *GetContainer) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
	logger := h.Logger.Session("get-container")
	id := rata.Param(req, "container_id")

	container, err := h.Datastore.Get(id)
	if err != nil {
		if err == store.RecordNotFoundError {
			logger.Error("record-not-found", err)
			resp.WriteHeader(http.StatusNotFound)
			return
		}
		logger.Error("database-error", err)
		resp.WriteHeader(http.StatusInternalServerError)
		return
	}

	payload, err := h.Marshaler.Marshal(container)
	if err != nil {
		logger.Error("marshal-failed", err)
		resp.WriteHeader(http.StatusInternalServerError)
		return
	}

	resp.WriteHeader(http.StatusOK)
	resp.Write(payload)
}
Beispiel #5
0
Datei: get.go Projekt: utako/atc
func (s *Server) GetConfig(w http.ResponseWriter, r *http.Request) {
	pipelineName := rata.Param(r, "pipeline_name")
	config, id, err := s.db.GetConfig(pipelineName)
	if err != nil {
		w.WriteHeader(http.StatusInternalServerError)
		return
	}

	w.Header().Set(atc.ConfigVersionHeader, fmt.Sprintf("%d", id))

	json.NewEncoder(w).Encode(config)
}
Beispiel #6
0
func (s *Server) PauseJob(pipelineDB db.PipelineDB) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		jobName := rata.Param(r, "job_name")

		err := pipelineDB.PauseJob(jobName)
		if err != nil {
			w.WriteHeader(http.StatusInternalServerError)
			return
		}

		w.WriteHeader(http.StatusOK)
	})
}
Beispiel #7
0
func (s *Server) UnpauseResource(pipelineDB db.PipelineDB) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		resourceName := rata.Param(r, "resource_name")

		err := pipelineDB.UnpauseResource(resourceName)
		if err != nil {
			w.WriteHeader(http.StatusInternalServerError)
			return
		}

		w.WriteHeader(http.StatusOK)
	})
}
Beispiel #8
0
func (s *Server) GetConfig(w http.ResponseWriter, r *http.Request) {
	logger := s.logger.Session("get-config")
	pipelineName := rata.Param(r, "pipeline_name")
	config, id, err := s.db.GetConfig(atc.DefaultTeamName, pipelineName)
	if err != nil {
		logger.Error("failed-to-get-config", err)
		w.WriteHeader(http.StatusInternalServerError)
		return
	}

	w.Header().Set(atc.ConfigVersionHeader, fmt.Sprintf("%d", id))

	json.NewEncoder(w).Encode(config)
}
Beispiel #9
0
func (s *Server) UnpauseJob(pipelineDB db.PipelineDB) http.Handler {
	logger := s.logger.Session("unpause-job")
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		jobName := rata.Param(r, "job_name")

		err := pipelineDB.UnpauseJob(jobName)
		if err != nil {
			logger.Error("failed-to-unpause-job", err)
			w.WriteHeader(http.StatusInternalServerError)
			return
		}

		w.WriteHeader(http.StatusOK)
	})
}
Beispiel #10
0
func (s *Server) PauseResource(pipelineDB db.PipelineDB) http.Handler {
	logger := s.logger.Session("pause-resource")
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		resourceName := rata.Param(r, "resource_name")

		err := pipelineDB.PauseResource(resourceName)
		if err != nil {
			logger.Error("failed-to-pause-resource", err)
			w.WriteHeader(http.StatusInternalServerError)
			return
		}

		w.WriteHeader(http.StatusOK)
	})
}
Beispiel #11
0
func (s *Server) EnableResourceVersion(pipelineDB db.PipelineDB) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		resourceID, err := strconv.Atoi(rata.Param(r, "resource_version_id"))
		if err != nil {
			w.WriteHeader(http.StatusBadRequest)
			return
		}

		err = pipelineDB.EnableVersionedResource(resourceID)
		if err != nil {
			w.WriteHeader(http.StatusInternalServerError)
			return
		}

		w.WriteHeader(http.StatusOK)
	})
}
Beispiel #12
0
func (s *Server) DisableResourceVersion(pipelineDB db.PipelineDB) http.Handler {
	logger := s.logger.Session("disable-resource-version")
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		resourceID, err := strconv.Atoi(rata.Param(r, "resource_version_id"))
		if err != nil {
			w.WriteHeader(http.StatusBadRequest)
			return
		}

		err = pipelineDB.DisableVersionedResource(resourceID)
		if err != nil {
			logger.Error("failed-to-disable-versioned-resource", err)
			w.WriteHeader(http.StatusInternalServerError)
			return
		}

		w.WriteHeader(http.StatusOK)
	})
}
Beispiel #13
0
func (s *Server) CheckResource(pipelineDB db.PipelineDB) http.Handler {
	logger := s.logger.Session("check-resource")

	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		resourceName := rata.Param(r, "resource_name")

		var reqBody atc.CheckRequestBody
		err := json.NewDecoder(r.Body).Decode(&reqBody)
		if err != nil {
			logger.Info("malformed-request", lager.Data{"error": err.Error()})
			w.WriteHeader(http.StatusBadRequest)
			return
		}

		scanner := s.scannerFactory.NewResourceScanner(pipelineDB)

		err = scanner.ScanFromVersion(logger, resourceName, reqBody.From)
		switch scanErr := err.(type) {
		case resource.ErrResourceScriptFailed:
			checkResponseBody := atc.CheckResponseBody{
				ExitStatus: scanErr.ExitStatus,
				Stderr:     scanErr.Stderr,
			}

			w.Header().Set("Content-Type", "application/json")
			w.WriteHeader(http.StatusBadRequest)
			json.NewEncoder(w).Encode(checkResponseBody)
		case db.ResourceNotFoundError:
			w.WriteHeader(http.StatusNotFound)
		case error:
			w.WriteHeader(http.StatusInternalServerError)
		default:
			w.WriteHeader(http.StatusOK)
		}
	})
}
Beispiel #14
0
func (s *Server) SaveConfig(w http.ResponseWriter, r *http.Request) {
	session := s.logger.Session("set-config")

	configVersionStr := r.Header.Get(atc.ConfigVersionHeader)
	if len(configVersionStr) == 0 {
		w.WriteHeader(http.StatusBadRequest)
		fmt.Fprintf(w, "no config version specified")
		return
	}

	var version db.ConfigVersion
	_, err := fmt.Sscanf(configVersionStr, "%d", &version)
	if err != nil {
		session.Error("malformed-config-version", err)
		w.WriteHeader(http.StatusBadRequest)
		fmt.Fprintf(w, "config version is malformed: %s", err)
		return
	}

	config, pausedState, err := saveConfigRequestUnmarshler(r)

	switch err {
	case ErrStatusUnsupportedMediaType:
		w.WriteHeader(http.StatusUnsupportedMediaType)
		return
	case ErrMalformedRequestPayload:
		session.Error("malformed-request-payload", err, lager.Data{
			"content-type": r.Header.Get("Content-Type"),
		})

		w.WriteHeader(http.StatusBadRequest)
		return
	case ErrFailedToConstructDecoder:
		session.Error("failed-to-construct-decoder", err)
		w.WriteHeader(http.StatusInternalServerError)
		return
	case ErrCouldNotDecode:
		session.Error("could-not-decode", err)
		w.WriteHeader(http.StatusBadRequest)
		return
	case ErrInvalidPausedValue:
		session.Error("invalid-paused-value", err)
		w.WriteHeader(http.StatusBadRequest)
		fmt.Fprintf(w, "invalid paused value")
		return
	default:
		if err != nil {
			if eke, ok := err.(ExtraKeysError); ok {
				w.WriteHeader(http.StatusBadRequest)
				fmt.Fprintln(w, eke)
			} else {
				session.Error("unexpected-error", err)
				w.WriteHeader(http.StatusInternalServerError)
			}

			return
		}
	}

	err = s.validate(config)
	if err != nil {
		session.Error("ignoring-invalid-config", err)
		w.WriteHeader(http.StatusBadRequest)
		fmt.Fprintf(w, "%s", err)
		return
	}

	session.Info("saving")

	pipelineName := rata.Param(r, "pipeline_name")
	created, err := s.db.SaveConfig(pipelineName, config, version, pausedState)
	if err != nil {
		session.Error("failed-to-save-config", err)
		w.WriteHeader(http.StatusInternalServerError)
		fmt.Fprintf(w, "failed to save config: %s", err)
		return
	}

	_, _ = s.saveNewPlugins(config)

	session.Info("saved")

	if created {
		w.WriteHeader(http.StatusCreated)
	} else {
		w.WriteHeader(http.StatusOK)
	}
}
Beispiel #15
0
					"smeller": ghttp.RespondWith(http.StatusOK, "smell response"),
				}
				r, err = rata.NewRouter(invalidRoutes, handlers)

				Ω(err).Should(HaveOccurred())
			})
		})
	})

	Describe("parsing params", func() {
		// this is basically done for us by PAT we simply want to verify some assumptions in these tests
		Context("when all the handlers are present", func() {
			var resp *httptest.ResponseRecorder
			var handlers = rata.Handlers{
				"getter": http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
					w.Write([]byte(rata.Param(req, "neato")))
				}),
			}

			Context("when a named path param is provided", func() {
				var r http.Handler
				var err error
				var routes = rata.Routes{
					{Path: "/something/:neato", Method: "GET", Name: "getter"},
				}

				BeforeEach(func() {
					resp = httptest.NewRecorder()
					r, err = rata.NewRouter(routes, handlers)
					Ω(err).ShouldNot(HaveOccurred())
				})