func (h *TaskHandler) Create(w http.ResponseWriter, r *http.Request) {
	log := h.logger.Session("create")
	taskRequest := receptor.TaskCreateRequest{}

	err := json.NewDecoder(r.Body).Decode(&taskRequest)
	if err != nil {
		log.Error("invalid-json", err)
		writeJSONResponse(w, http.StatusBadRequest, receptor.Error{
			Type:    receptor.InvalidJSON,
			Message: err.Error(),
		})
		return
	}

	task, err := serialization.TaskFromRequest(taskRequest)
	if err == nil {
		if task.GetCompletionCallbackUrl() != "" {
			_, err = url.ParseRequestURI(task.GetCompletionCallbackUrl())
		}
	}
	if err != nil {
		log.Error("task-request-invalid", err)
		writeJSONResponse(w, http.StatusBadRequest, receptor.Error{
			Type:    receptor.InvalidTask,
			Message: err.Error(),
		})
		return
	}

	log.Debug("creating-task", lager.Data{"task-guid": task.TaskGuid})

	err = h.bbs.DesireTask(task.TaskGuid, task.Domain, task.TaskDefinition)
	if err != nil {
		log.Error("failed-to-desire-task", err)
		if mErr, ok := err.(*models.Error); ok {
			if mErr.Equal(models.ErrBadRequest) {
				writeJSONResponse(w, http.StatusBadRequest, receptor.Error{
					Type:    receptor.InvalidTask,
					Message: err.Error(),
				})
				return
			} else if mErr.Equal(models.ErrResourceExists) {
				writeJSONResponse(w, http.StatusConflict, receptor.Error{
					Type:    receptor.TaskGuidAlreadyExists,
					Message: "task already exists",
				})
				return
			}
		}
		writeUnknownErrorResponse(w, err)
		return
	}

	log.Info("created", lager.Data{"task-guid": task.TaskGuid})
	w.WriteHeader(http.StatusCreated)
}
Esempio n. 2
0
func (h *TaskHandler) Create(w http.ResponseWriter, r *http.Request) {
	log := h.logger.Session("create")
	taskRequest := receptor.TaskCreateRequest{}

	err := json.NewDecoder(r.Body).Decode(&taskRequest)
	if err != nil {
		log.Error("invalid-json", err)
		writeJSONResponse(w, http.StatusBadRequest, receptor.Error{
			Type:    receptor.InvalidJSON,
			Message: err.Error(),
		})
		return
	}

	task, err := serialization.TaskFromRequest(taskRequest)
	if err != nil {
		log.Error("task-request-invalid", err)
		writeJSONResponse(w, http.StatusBadRequest, receptor.Error{
			Type:    receptor.InvalidTask,
			Message: err.Error(),
		})
		return
	}

	log.Debug("creating-task", lager.Data{"task-guid": task.TaskGuid})

	err = h.bbs.DesireTask(log, task)
	if err != nil {
		log.Error("failed-to-desire-task", err)

		if _, ok := err.(models.ValidationError); ok {
			writeJSONResponse(w, http.StatusBadRequest, receptor.Error{
				Type:    receptor.InvalidTask,
				Message: err.Error(),
			})
			return
		}

		if err == bbserrors.ErrStoreResourceExists {
			writeJSONResponse(w, http.StatusConflict, receptor.Error{
				Type:    receptor.TaskGuidAlreadyExists,
				Message: "task already exists",
			})
		} else {
			writeUnknownErrorResponse(w, err)
		}
		return
	}

	log.Info("created", lager.Data{"task-guid": task.TaskGuid})
	w.WriteHeader(http.StatusCreated)
}
Esempio n. 3
0
				},
				EgressRules: []models.SecurityGroupRule{
					{
						Protocol:     "tcp",
						Destinations: []string{"0.0.0.0/0"},
						PortRange: &models.PortRange{
							Start: 1,
							End:   1024,
						},
					},
				},
			}
		})

		It("translates the request into a task model, preserving attributes", func() {
			actualTask, err := serialization.TaskFromRequest(request)
			Expect(err).NotTo(HaveOccurred())

			Expect(actualTask).To(Equal(expectedTask))
		})

		Context("when the request contains a parseable completion_callback_url", func() {
			BeforeEach(func() {
				request.CompletionCallbackURL = "http://stager.service.discovery.thing/endpoint"
			})

			It("parses the URL", func() {
				actualTask, err := serialization.TaskFromRequest(request)
				Expect(err).NotTo(HaveOccurred())

				Expect(actualTask.CompletionCallbackURL).To(Equal(&url.URL{