// Implementation of the PanamaxAdapter CreateServices interface
func (m *marathonAdapter) CreateServices(services []*api.Service) ([]*api.Service, *api.Error) {
	var apiErr *api.Error

	myGroup := m.deployer.BuildDeploymentGroup(services, m.client)
	status := m.deployer.DeployGroup(myGroup, DEPLOY_TIMEOUT)

	switch status.code {
	case FAIL:
		apiErr = api.NewError(http.StatusConflict, "Group deployment failed.")
	case TIMEOUT:
		apiErr = api.NewError(http.StatusInternalServerError, "Group deployment timed out.")
	}

	return services, apiErr
}
// Implementation of the PanamaxAdapter GetService interface
func (m *marathonAdapter) GetService(id string) (*api.Service, *api.Error) {
	var apiErr *api.Error

	response, err := m.client.GetApp(sanitizeMarathonAppURL(id))
	if err != nil {
		apiErr = api.NewError(http.StatusNotFound, err.Error())
		return nil, apiErr
	}
	return m.conv.convertToService(response.App), apiErr
}
// Implementation of the PanamaxAdapter GetServices interface
func (m *marathonAdapter) GetServices() ([]*api.Service, *api.Error) {
	var apiErr *api.Error

	response, err := m.client.ListApps()
	if err != nil {
		apiErr = api.NewError(http.StatusNotFound, err.Error())
		return nil, apiErr
	}
	return m.conv.convertToServices(response.Apps), apiErr
}
// Implementation of the PanamaxAdapter DestroyService interface
func (m *marathonAdapter) DestroyService(id string) *api.Error {
	var apiErr *api.Error
	group, _ := splitServiceId(id, ".")

	_, err := m.client.DeleteApp(sanitizeMarathonAppURL(id))
	if err != nil {
		apiErr = api.NewError(http.StatusNotFound, err.Error())
		return apiErr
	}

	m.client.DeleteGroup(group) // Remove group if possible we dont care about error or return.

	return apiErr
}
func TestFailedDeleteService(t *testing.T) {

	// setup
	testClient, _, _, adapter := setup()

	resp := new(gomarathon.Response)

	// set expectations
	testClient.On("DeleteApp", "/foo").Return(resp, api.NewError(404, "service not found"))

	// call the code to be tested
	err := adapter.DestroyService("foo")

	// assert if expectations are met
	assert.NotNil(t, err)
	assert.Equal(t, 404, err.Code)
	assert.Equal(t, "Error(404): service not found", err.Message)

	testClient.AssertExpectations(t)
	testClient.AssertNotCalled(t, "DeleteGroup", "")

}
func TestFailedGetService(t *testing.T) {

	// setup
	testClient, testConverter, _, adapter := setup()

	resp := new(gomarathon.Response)

	// set expectations
	testClient.On("GetApp", "/invalid").Return(resp, api.NewError(404, "service not found"))

	// call the code to be tested
	_, err := adapter.GetService("invalid")

	// assert if expectations are met
	assert.NotNil(t, err)
	assert.Equal(t, 404, err.Code)
	assert.Equal(t, "Error(404): service not found", err.Message)

	testClient.AssertExpectations(t)
	testConverter.AssertNotCalled(t, "convertToService", nil)

}