func NewEventFromBBS(bbsEvent models.Event) (receptor.Event, error) {
	switch bbsEvent := bbsEvent.(type) {
	case *models.ActualLRPCreatedEvent:
		actualLRP, evacuating := bbsEvent.ActualLrpGroup.Resolve()
		return receptor.NewActualLRPCreatedEvent(serialization.ActualLRPProtoToResponse(actualLRP, evacuating)), nil
	case *models.ActualLRPChangedEvent:
		before, evacuating := bbsEvent.Before.Resolve()
		after, evacuating := bbsEvent.After.Resolve()
		return receptor.NewActualLRPChangedEvent(
			serialization.ActualLRPProtoToResponse(before, evacuating),
			serialization.ActualLRPProtoToResponse(after, evacuating),
		), nil
	case *models.ActualLRPRemovedEvent:
		actualLRP, evacuating := bbsEvent.ActualLrpGroup.Resolve()
		return receptor.NewActualLRPRemovedEvent(serialization.ActualLRPProtoToResponse(actualLRP, evacuating)), nil
	case *models.DesiredLRPCreatedEvent:
		return receptor.NewDesiredLRPCreatedEvent(serialization.DesiredLRPProtoToResponse(bbsEvent.DesiredLrp)), nil
	case *models.DesiredLRPChangedEvent:
		return receptor.NewDesiredLRPChangedEvent(
			serialization.DesiredLRPProtoToResponse(bbsEvent.Before),
			serialization.DesiredLRPProtoToResponse(bbsEvent.After),
		), nil
	case *models.DesiredLRPRemovedEvent:
		return receptor.NewDesiredLRPRemovedEvent(serialization.DesiredLRPProtoToResponse(bbsEvent.DesiredLrp)), nil
	}
	return nil, fmt.Errorf("unknown event type: %#v", bbsEvent)
}
func (h *DesiredLRPHandler) Get(w http.ResponseWriter, r *http.Request) {
	processGuid := r.FormValue(":process_guid")
	logger := h.logger.Session("get", lager.Data{
		"ProcessGuid": processGuid,
	})

	if processGuid == "" {
		err := errors.New("process_guid missing from request")
		logger.Error("missing-process-guid", err)
		writeBadRequestResponse(w, receptor.InvalidRequest, err)
		return
	}

	desiredLRP, err := h.bbs.DesiredLRPByProcessGuid(processGuid)
	if e, ok := err.(*models.Error); ok && e.Equal(models.ErrResourceNotFound) {
		writeDesiredLRPNotFoundResponse(w, processGuid)
		return
	}

	if err != nil {
		logger.Error("unknown-error", err)
		writeUnknownErrorResponse(w, err)
		return
	}

	writeJSONResponse(w, http.StatusOK, serialization.DesiredLRPProtoToResponse(desiredLRP))
}
func writeDesiredLRPProtoResponse(w http.ResponseWriter, logger lager.Logger, desiredLRPs []*models.DesiredLRP, err error) {
	if err != nil {
		logger.Error("failed-to-fetch-desired-lrps", err)
		writeUnknownErrorResponse(w, err)
		return
	}

	responses := make([]receptor.DesiredLRPResponse, 0, len(desiredLRPs))
	for _, desiredLRP := range desiredLRPs {
		responses = append(responses, serialization.DesiredLRPProtoToResponse(desiredLRP))
	}

	writeJSONResponse(w, http.StatusOK, responses)
}
Beispiel #4
0
		It("receives events", func() {
			By("creating a DesiredLRP")
			err := legacyBBS.DesireLRP(logger, oldDesiredLRP)
			Expect(err).NotTo(HaveOccurred())

			desiredLRP, err := bbsClient.DesiredLRPByProcessGuid(oldDesiredLRP.ProcessGuid)
			Expect(err).NotTo(HaveOccurred())

			var event receptor.Event
			Eventually(events).Should(Receive(&event))

			desiredLRPCreatedEvent, ok := event.(receptor.DesiredLRPCreatedEvent)
			Expect(ok).To(BeTrue())

			actualJSON, _ := json.Marshal(desiredLRPCreatedEvent.DesiredLRPResponse)
			expectedJSON, _ := json.Marshal(serialization.DesiredLRPProtoToResponse(desiredLRP))
			Expect(actualJSON).To(MatchJSON(expectedJSON))

			By("updating an existing DesiredLRP")
			routeMessage := json.RawMessage([]byte(`[{"port":8080,"hostnames":["new-route"]}]`))
			newRoutes := map[string]*json.RawMessage{
				"cf-router": &routeMessage,
			}
			err = legacyBBS.UpdateDesiredLRP(logger, oldDesiredLRP.ProcessGuid, oldmodels.DesiredLRPUpdate{Routes: newRoutes})
			Expect(err).NotTo(HaveOccurred())

			Eventually(events).Should(Receive(&event))

			desiredLRPChangedEvent, ok := event.(receptor.DesiredLRPChangedEvent)
			Expect(ok).To(BeTrue())
			Expect(desiredLRPChangedEvent.After.Routes).To(Equal(receptor.RoutingInfo(newRoutes)))
	BeforeEach(func() {
		fakeRawEventSource = new(fake_receptor.FakeRawEventSource)
		eventSource = receptor.NewEventSource(fakeRawEventSource)
	})

	Describe("Next", func() {
		Describe("Desired LRP events", func() {
			var desiredLRPResponse receptor.DesiredLRPResponse

			BeforeEach(func() {
				desiredLRPResponse = serialization.DesiredLRPProtoToResponse(
					&models.DesiredLRP{
						ProcessGuid: "some-guid",
						Domain:      "some-domain",
						RootFs:      "some-rootfs",
						Action: models.WrapAction(&models.RunAction{
							Path: "true",
							User: "******",
						}),
					},
				)
			})

			Context("when receiving a DesiredLRPCreatedEvent", func() {
				var expectedEvent receptor.DesiredLRPCreatedEvent

				BeforeEach(func() {
					expectedEvent = receptor.NewDesiredLRPCreatedEvent(desiredLRPResponse)
					payload, err := json.Marshal(expectedEvent)
					Expect(err).NotTo(HaveOccurred())
				response := &http.Response{}
				Eventually(responseChan).Should(Receive(&response))
				reader := sse.NewReadCloser(response.Body)

				desiredLRP := &models.DesiredLRP{
					ProcessGuid: "some-guid",
					Domain:      "some-domain",
					RootFs:      "some-rootfs",
					Action: models.WrapAction(&models.RunAction{
						Path: "true",
						User: "******",
					}),
				}
				eventChannel <- models.NewDesiredLRPCreatedEvent(desiredLRP)

				data, err := json.Marshal(receptor.NewDesiredLRPCreatedEvent(serialization.DesiredLRPProtoToResponse(desiredLRP)))
				Expect(err).NotTo(HaveOccurred())

				event, err := reader.Next()
				Expect(err).NotTo(HaveOccurred())
				Expect(event.ID).To(Equal("0"))
				Expect(event.Name).To(Equal(string(receptor.EventTypeDesiredLRPCreated)))
				Expect(event.Data).To(MatchJSON(data))

				actualLRP := models.NewUnclaimedActualLRP(models.NewActualLRPKey("some-guid", 3, "some-domain"), 0)
				actualLRPGroup := &models.ActualLRPGroup{Instance: actualLRP}
				eventChannel <- models.NewActualLRPCreatedEvent(actualLRPGroup)

				data, err = json.Marshal(receptor.NewActualLRPCreatedEvent(serialization.ActualLRPProtoToResponse(actualLRP, false)))
				Expect(err).NotTo(HaveOccurred())
			lrpRequest = newValidDesiredLRPCreateRequest()
			err := client.CreateDesiredLRP(lrpRequest)
			Expect(err).NotTo(HaveOccurred())

			desiredLRP, err = bbsClient.DesiredLRPByProcessGuid(lrpRequest.ProcessGuid)
			Expect(err).NotTo(HaveOccurred())

			lrpResponse, getErr = client.GetDesiredLRP(lrpRequest.ProcessGuid)
		})

		It("responds without an error", func() {
			Expect(getErr).NotTo(HaveOccurred())
		})

		It("fetches the desired lrp with the matching process guid", func() {
			expectedLRPResponse := serialization.DesiredLRPProtoToResponse(desiredLRP)
			actualJSON, err := json.Marshal(lrpResponse)
			Expect(err).NotTo(HaveOccurred())
			expectedJSON, err := json.Marshal(expectedLRPResponse)
			Expect(err).NotTo(HaveOccurred())
			Expect(actualJSON).To(MatchJSON(expectedJSON))
		})
	})

	Describe("PUT /v1/desired_lrps/:process_guid", func() {
		var updateErr error

		instances := 6
		annotation := "update-annotation"
		rawMessage := json.RawMessage([]byte(`[{"port":8080,"hostnames":["updated-route"]}]`))