Example #1
0
File: task.go Project: Gerg/bbs
func (task *Task) MarshalJSON() ([]byte, error) {
	b, err := json.Marshal(task.Action)
	if err != nil {
		return nil, err
	}
	var oldAction oldmodels.Action
	if UnwrapAction(task.Action) != nil {
		oldAction, err = oldmodels.UnmarshalAction(b)
		if err != nil {
			return nil, err
		}
	}

	b, err = json.Marshal(task.EgressRules)
	if err != nil {
		return nil, err
	}
	var oldEgress []oldmodels.SecurityGroupRule
	err = json.Unmarshal(b, &oldEgress)

	oldUrl, err := url.Parse(task.CompletionCallbackUrl)
	if err != nil {
		return nil, err
	}

	oldtask := oldmodels.Task{
		TaskGuid:              task.TaskGuid,
		Domain:                task.Domain,
		RootFS:                task.RootFs,
		EnvironmentVariables:  EnvironmentVariablesFromProto(task.EnvironmentVariables),
		CellID:                task.CellId,
		Action:                oldAction,
		ResultFile:            task.ResultFile,
		Result:                task.Result,
		Failed:                task.Failed,
		FailureReason:         task.FailureReason,
		MemoryMB:              int(task.MemoryMb),
		DiskMB:                int(task.DiskMb),
		CPUWeight:             uint(task.CpuWeight),
		Privileged:            task.Privileged,
		LogGuid:               task.LogGuid,
		LogSource:             task.LogSource,
		MetricsGuid:           task.MetricsGuid,
		CreatedAt:             task.CreatedAt,
		UpdatedAt:             task.UpdatedAt,
		FirstCompletedAt:      task.FirstCompletedAt,
		State:                 oldmodels.TaskState(Task_State_value[task.State.String()]),
		Annotation:            task.Annotation,
		EgressRules:           oldEgress,
		CompletionCallbackURL: oldUrl,
	}

	return json.Marshal(&oldtask)
}
Example #2
0
func (request *DesiredLRPCreateRequest) UnmarshalJSON(payload []byte) error {
	mRequest := &mDesiredLRPCreateRequest{InnerDesiredLRPCreateRequest: (*InnerDesiredLRPCreateRequest)(request)}
	err := json.Unmarshal(payload, mRequest)
	if err != nil {
		return err
	}

	var a oldmodels.Action

	if mRequest.ActionRaw == nil {
		a = nil
	} else {
		a, err = oldmodels.UnmarshalAction(*mRequest.ActionRaw)
		if err != nil {
			return err
		}
	}
	request.Action = a

	if mRequest.SetupRaw == nil {
		a = nil
	} else {
		a, err = oldmodels.UnmarshalAction(*mRequest.SetupRaw)
		if err != nil {
			return err
		}
	}
	request.Setup = a

	if mRequest.MonitorRaw == nil {
		a = nil
	} else {
		a, err = oldmodels.UnmarshalAction(*mRequest.MonitorRaw)
		if err != nil {
			return err
		}
	}
	request.Monitor = a

	return nil
}
Example #3
0
func (response *DesiredLRPResponse) UnmarshalJSON(payload []byte) error {
	mResponse := &mDesiredLRPResponse{InnerDesiredLRPResponse: (*InnerDesiredLRPResponse)(response)}
	err := json.Unmarshal(payload, mResponse)
	if err != nil {
		return err
	}

	var a models.Action

	if mResponse.ActionRaw == nil {
		a = nil
	} else {
		a, err = models.UnmarshalAction(*mResponse.ActionRaw)
		if err != nil {
			return err
		}
	}
	response.Action = a

	if mResponse.SetupRaw == nil {
		a = nil
	} else {
		a, err = models.UnmarshalAction(*mResponse.SetupRaw)
		if err != nil {
			return err
		}
	}
	response.Setup = a

	if mResponse.MonitorRaw == nil {
		a = nil
	} else {
		a, err = models.UnmarshalAction(*mResponse.MonitorRaw)
		if err != nil {
			return err
		}
	}
	response.Monitor = a

	return nil
}
Example #4
0
func (request *TaskCreateRequest) UnmarshalJSON(payload []byte) error {
	mRequest := &mTaskCreateRequest{InnerTaskCreateRequest: (*InnerTaskCreateRequest)(request)}
	err := json.Unmarshal(payload, mRequest)
	if err != nil {
		return err
	}

	var a oldmodels.Action
	if mRequest.ActionRaw == nil {
		a = nil
	} else {
		a, err = oldmodels.UnmarshalAction(mRequest.ActionRaw)
		if err != nil {
			return err
		}
	}
	request.Action = a

	return nil
}
Example #5
0
func (response *TaskResponse) UnmarshalJSON(payload []byte) error {
	mResponse := &mTaskResponse{InnerTaskResponse: (*InnerTaskResponse)(response)}
	err := json.Unmarshal(payload, mResponse)
	if err != nil {
		return err
	}

	var a oldmodels.Action
	if mResponse.ActionRaw == nil {
		a = nil
	} else {
		a, err = oldmodels.UnmarshalAction(mResponse.ActionRaw)
		if err != nil {
			return err
		}
	}
	response.Action = a

	return nil
}
				marshalledAction := action

				json, err := models.MarshalAction(marshalledAction)
				Expect(err).NotTo(HaveOccurred())
				Expect(json).To(MatchJSON(wrappedJSON))
			})
		})
	}

	itDeserializes := func(actionPayload string, action models.Action) {
		It("JSON -> Action for "+string(action.ActionType()), func() {
			wrappedJSON := fmt.Sprintf(`{"%s":%s}`, action.ActionType(), actionPayload)

			By("unwrapping", func() {
				var unmarshalledAction models.Action
				unmarshalledAction, err := models.UnmarshalAction([]byte(wrappedJSON))
				Expect(err).NotTo(HaveOccurred())
				Expect(unmarshalledAction).To(Equal(action))
			})
		})
	}

	itSerializesAndDeserializes := func(actionPayload string, action models.Action) {
		itSerializes(actionPayload, action)
		itDeserializes(actionPayload, action)
	}

	Describe("UnmarshalAction", func() {
		It("returns an error when the action is not registered", func() {
			_, err := models.UnmarshalAction([]byte(`{"bogusAction": {}}`))
			Expect(err).To(MatchError("Unknown action: bogusAction"))