handlers

package
v0.3.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 9, 2017 License: Apache-2.0 Imports: 25 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var CreateAppHandler apps.CreateAppHandlerFunc = func(params apps.CreateAppParams, principal interface{}) middleware.Responder {
	tk := k8s.IToToken(principal)

	app := &models.App{AppIn: *params.Body}

	l := log.WithField("app", *app.Name).WithField("team", *app.Team).WithField("token", *tk.Email).WithField("requestId", helpers.NewShortUUID())

	if err := k8s.Client.Apps().Create(app, helpers.FileStorage, tk); err != nil {
		if k8s.IsInputError(err) {
			l.WithError(err).Warn("error creating app")
			return NewBadRequestError(err)
		} else if k8s.IsAlreadyExistsError(err) {
			l.WithError(err).Debug("error creating app")
			return NewConflictError("app already exists")
		} else if k8s.IsUnauthorizedError(err) {
			l.WithError(err).Debug("error creating app")
			return NewUnauthorizedError("either the team doesn't exist or the user doesn't have access to it")
		}
		l.WithError(err).Error("error creating app")
		return NewInternalServerError(err)
	}
	return apps.NewCreateAppCreated().WithPayload(app)
}

CreateAppHandler handler for "-X POST /apps"

View Source
var CreateDeploymentHandler deployments.CreateDeploymentHandlerFunc = func(params deployments.CreateDeploymentParams, principal interface{}) middleware.Responder {
	var r middleware.ResponderFunc = func(rw http.ResponseWriter, pr runtime.Producer) {
		tk := k8s.IToToken(principal)

		var description string
		if params.Description != nil {
			description = *params.Description
		}

		l := log.WithField("app", params.AppName).WithField("token", *tk.Email).WithField("requestId", helpers.NewShortUUID())
		r, err := k8s.Client.Deployments().Create(params.AppName, description, &params.AppTarball, helpers.FileStorage, tk)
		if err != nil {
			// FIXME: improve this... is it possible?
			var httpErr *GenericError
			if k8s.IsInputError(err) {
				l.WithError(err).Warn("error during deploy")
				httpErr = NewBadRequestError(err)
			} else if k8s.IsUnauthorizedError(err) {
				l.WithError(err).Warn("error during deploy")
				httpErr = NewUnauthorizedError(err)
			} else {
				l.WithError(err).Error("error during deploy")
				httpErr = NewInternalServerError(err)
			}
			rw.WriteHeader(int(*httpErr.Payload.Code))
			rw.Write([]byte(*httpErr.Payload.Message))
			return
		}
		l.Debug("starting the streaming of the build")
		defer r.Close()

		w := newFlushResponseWriter(rw)

		io.Copy(w, r)
	}
	return r
}

CreateDeploymentHandler handler triggered when a deploy url is requested

View Source
var (
	ErrMissingKey = std_errors.New("Missing key")
)
View Source
var GetAppDetailsHandler apps.GetAppDetailsHandlerFunc = func(params apps.GetAppDetailsParams, principal interface{}) middleware.Responder {
	tk := k8s.IToToken(principal)

	l := log.WithField("app", params.AppName).WithField("token", *tk.Email).WithField("requestId", helpers.NewShortUUID())

	app, err := k8s.Client.Apps().Get(params.AppName, tk)
	if err != nil {
		if k8s.IsNotFoundError(err) {
			l.WithError(err).Debug("error getting app details")
			return NewNotFoundError(err)
		} else if k8s.IsUnauthorizedError(err) {
			l.WithError(err).Info("error getting app details")
			return NewUnauthorizedError(err)
		}
		l.WithError(err).Error("error getting app details")
		return NewInternalServerError(err)
	}
	return apps.NewGetAppDetailsOK().WithPayload(app)
}

GetAppDetailsHandler return app details

View Source
var GetAppLogsHandler apps.GetAppLogsHandlerFunc = func(params apps.GetAppLogsParams, principal interface{}) middleware.Responder {
	var r middleware.ResponderFunc = func(rw http.ResponseWriter, pr runtime.Producer) {
		tk := k8s.IToToken(principal)
		opts := &api.PodLogOptions{
			Follow:    *params.Follow,
			TailLines: params.Lines,
		}
		l := log.WithField("app", params.AppName).WithField("token", *tk.Email).WithField("requestId", helpers.NewShortUUID())
		r, err := k8s.Client.Apps().GetLogs(params.AppName, tk, opts)
		if err != nil {
			var httpErr *GenericError
			if k8s.IsInputError(err) {
				l.WithError(err).Warn("error requesting logs")
				httpErr = NewBadRequestError(err)
			} else if k8s.IsUnauthorizedError(err) {
				l.WithError(err).Warn("error requesting logs")
				httpErr = NewUnauthorizedError(err)
			} else if k8s.IsNotFoundError(err) {
				l.WithError(err).Warn("error requesting logs")
				httpErr = NewNotFoundError(err)
			} else {
				l.WithError(err).Warn("error requesting logs")
				httpErr = NewInternalServerError(err)
			}
			rw.WriteHeader(int(*httpErr.Payload.Code))
			rw.Write([]byte(*httpErr.Payload.Message))
			return
		}
		defer r.Close()
		w := newFlushResponseWriter(rw)
		io.Copy(w, r)
	}
	return r
}
View Source
var GetAppsHandler apps.GetAppsHandlerFunc = func(params apps.GetAppsParams, principal interface{}) middleware.Responder {
	tk := k8s.IToToken(principal)
	l := log.WithField("token", *tk.Email).WithField("requestId", helpers.NewShortUUID())
	appList, err := k8s.Client.Apps().List(tk)
	if err != nil {
		if k8s.IsNotFoundError(err) {
			l.WithError(err).Debug("error getting app list")
			return NewNotFoundError(err)
		}
		l.WithError(err).Error("error getting app list")
		return NewInternalServerError(err)
	}
	return apps.NewGetAppsOK().WithPayload(appList)
}

GetAppsHandler returns all apps for the user

View Source
var PartialUpdateAppHandler apps.PartialUpdateAppHandlerFunc = func(params apps.PartialUpdateAppParams, principal interface{}) middleware.Responder {
	tk := k8s.IToToken(principal)
	l := log.WithField("app", params.AppName).WithField("token", *tk.Email).WithField("requestId", helpers.NewShortUUID())

	app, err := k8s.Client.Apps().UpdateEnvVars(params.AppName, params.Body, helpers.FileStorage, tk)
	if err != nil {
		if k8s.IsInputError(err) {
			l.WithError(err).Warn("error on app partial update")
			return NewBadRequestError(err)
		} else if k8s.IsNotFoundError(err) {
			l.WithError(err).Debug("error on app partial update")
			return NewNotFoundError(err)
		} else if k8s.IsUnauthorizedError(err) {
			l.WithError(err).Warn("error on app partial update")
			return NewUnauthorizedError(err)
		}
		l.WithError(err).Error("error on app partial update")
		return NewInternalServerError(err)
	}
	return apps.NewPartialUpdateAppOK().WithPayload(app)
}

PartialUpdateAppHandler partial updating app... only envvars for now

View Source
var UpdateAppAutoScaleHandler apps.UpdateAppAutoScaleHandlerFunc = func(params apps.UpdateAppAutoScaleParams, principal interface{}) middleware.Responder {
	tk := k8s.IToToken(principal)
	l := log.WithField("app", params.AppName).WithField("token", *tk.Email).WithField("requestId", helpers.NewShortUUID())
	app, err := k8s.Client.Apps().UpdateAutoScale(params.AppName, params.Body, helpers.FileStorage, tk)
	if err != nil {
		if k8s.IsInputError(err) {
			l.WithError(err).Warn("error updating the app auto scale")
			return NewBadRequestError(err)
		} else if k8s.IsNotFoundError(err) {
			l.WithError(err).Debug("error updating the app auto scale")
			return NewNotFoundError(err)
		} else if k8s.IsUnauthorizedError(err) {
			l.WithError(err).Warn("error updating the app auto scale")
			return NewUnauthorizedError(err)
		}
		l.WithError(err).Error("error updating the app auto scale")
		return NewInternalServerError(err)
	}
	l.Debug(`app auto scale updated with success`)
	return apps.NewUpdateAppAutoScaleOK().WithPayload(app)
}

UpdateAppAutoScaleHandler update app autoscale info

View Source
var UpdateAppHandler apps.UpdateAppHandlerFunc = func(params apps.UpdateAppParams, principal interface{}) middleware.Responder {
	return middleware.NotImplemented("operation apps.UpdateAppHandlerFunc has not yet been implemented")
}

UpdateAppHandler handler for update app

View Source
var UpdateAppScaleHandler apps.UpdateAppScaleHandlerFunc = func(params apps.UpdateAppScaleParams, principal interface{}) middleware.Responder {
	tk := k8s.IToToken(principal)
	l := log.WithField("app", params.AppName).WithField("token", *tk.Email).WithField("requestId", helpers.NewShortUUID())

	app, err := k8s.Client.Apps().UpdateScale(params.AppName, *params.Body.Scale, helpers.FileStorage, tk)
	if err != nil {
		if k8s.IsInputError(err) {
			l.WithError(err).Warn("error updating app scale")
			return NewBadRequestError(err)
		} else if k8s.IsNotFoundError(err) {
			l.WithError(err).Debug("error updating app scale")
			return NewNotFoundError(err)
		} else if k8s.IsUnauthorizedError(err) {
			l.WithError(err).Warn("error updating app scale")
			return NewUnauthorizedError(err)
		}
		l.WithError(err).Error("error updating app scale")
		return NewInternalServerError(err)
	}
	l.Debugf(`app scale updated with success to: %d`, app.Scale)
	return apps.NewUpdateAppScaleOK().WithPayload(app)
}

UpdateAppScaleHandler handler for app scale -XPUT

Functions

func AddUserToTeam

func AddUserToTeam(params teams.AddUserToTeamParams, principal interface{}) middleware.Responder

AddUserToTeam add user to a specific team

func CreateTeamHandler

func CreateTeamHandler(params teams.CreateTeamParams, principal interface{}) middleware.Responder

CreateTeamHandler ...

func CreateUserHandler

func CreateUserHandler(params users.CreateUserParams, principal interface{}) middleware.Responder

CreateUserHandler ...

func DeleteTeamHandler

func DeleteTeamHandler(params teams.DeleteTeamParams, principal interface{}) middleware.Responder

DeleteTeamHandler ...

func DeleteUserHandler

func DeleteUserHandler(params users.DeleteUserParams, principal interface{}) middleware.Responder

DeleteUserHandler ...

func GetCurrentUserHandler

func GetCurrentUserHandler(principal interface{}) middleware.Responder

GetCurrentUserHandler ...

func GetTeamDetailsHandler

func GetTeamDetailsHandler(params teams.GetTeamDetailParams, principal interface{}) middleware.Responder

GetTeamDetailsHandler ...

func GetTeamsHandler

func GetTeamsHandler(params teams.GetTeamsParams, principal interface{}) middleware.Responder

GetTeamsHandler ...

func GetUserDetailsHandler

func GetUserDetailsHandler(params users.GetUserDetailsParams, principal interface{}) middleware.Responder

GetUserDetailsHandler ...

func LoginHandler

func LoginHandler(params auth.UserLoginParams) middleware.Responder

LoginHandler validates a user

func TokenAuthHandler

func TokenAuthHandler(t string) (interface{}, error)

TokenAuthHandler try and validate the jwt token

func UpdateTeamHandler

func UpdateTeamHandler(params teams.UpdateTeamParams, principal interface{}) middleware.Responder

UpdateTeamHandler ...

Types

type GenericError added in v0.2.0

type GenericError struct {
	Payload *models.Error `json:"body,omitempty"`
}

GenericError is used to help when returning simple and descritive errors to the api

func NewBadRequestError added in v0.2.0

func NewBadRequestError(message ...interface{}) *GenericError

NewBadRequestError returns a Bad Request (http status code 400) to the Api

func NewConflictError added in v0.2.0

func NewConflictError(message ...interface{}) *GenericError

NewConflictError returns a Conflict Error (http status code 409) to the Api

func NewForbiddenError added in v0.2.0

func NewForbiddenError() *GenericError

NewForbiddenError returns a Forbidden Error (http status code 403) to the Api

func NewGenericError added in v0.2.0

func NewGenericError(code int32, message ...interface{}) *GenericError

NewGenericError is a helper to create a GenericError

func NewInternalServerError added in v0.2.0

func NewInternalServerError(message ...interface{}) *GenericError

NewInternalServerError returns a Internal Server Error (http status code 500) to the Api

func NewNotFoundError added in v0.2.0

func NewNotFoundError(message ...interface{}) *GenericError

NewNotFoundError returns a Not Found Error (http status code 404) to the Api

func NewUnauthorizedError added in v0.2.0

func NewUnauthorizedError(message ...interface{}) *GenericError

NewUnauthorizedError returns a Unauthorized Error (http status code 401) to the Api

func (*GenericError) WithMessage added in v0.2.0

func (e *GenericError) WithMessage(message string) *GenericError

WithMessage add a message to the error

func (*GenericError) WithReason added in v0.2.0

func (e *GenericError) WithReason(reason Reason) *GenericError

WithReason add a reason to the error

func (*GenericError) WriteResponse added in v0.2.0

func (e *GenericError) WriteResponse(rw http.ResponseWriter, producer runtime.Producer)

WriteResponse to fits the interface of middleware.Responder

type Reason added in v0.2.0

type Reason string

Reason is used to keep the reasons to attach with the error

const (
	BadRequest          Reason = "BadRequest"
	Unauthorized        Reason = "Unauthorized"
	Forbidden           Reason = "Forbidden"
	NotFound            Reason = "NotFound"
	Conflict            Reason = "Conflict"
	InternalServerError Reason = "InternalServerError"
)

List of reasons to attach with the error response

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL