balena-cli/lib/resin/models/application.coffee

120 lines
2.8 KiB
CoffeeScript
Raw Normal View History

2014-12-08 12:48:19 +00:00
_ = require('lodash-contrib')
pine = require('./_pine')
2014-11-26 16:38:02 +00:00
errors = require('../errors/errors')
2014-11-26 17:54:05 +00:00
server = require('../server/server')
settings = require('../settings')
2014-12-08 12:48:19 +00:00
# Get all applications
#
# @param {Function} callback callback(error, applications)
#
# @throw {NotAny} Will throw if no applications were found
#
# @example Get all applications
# resin.models.application.getAll (error, applications) ->
# throw error if error?
# console.log(applications)
#
2014-11-26 17:54:05 +00:00
exports.getAll = (callback) ->
return pine.get
resource: 'application'
options:
orderby: 'app_name asc'
expand: 'device'
2014-11-26 14:32:57 +00:00
.then (applications) ->
if _.isEmpty(applications)
2014-11-26 17:54:05 +00:00
return callback(new errors.NotAny('applications'))
2014-11-26 14:32:57 +00:00
2014-11-26 17:54:05 +00:00
return callback(null, applications)
2014-11-19 13:23:40 +00:00
2014-11-26 17:54:05 +00:00
.catch (error) ->
return callback(error)
2014-12-08 12:48:19 +00:00
# Get a single application
#
# @param {String, Number} id application id
# @param {Function} callback callback(error, application)
#
# @throw {NotFound} Will throw if application was not found
#
# @example Find application
# resin.models.application.get 51, (error, application) ->
# throw error if error?
# console.log(application)
#
2014-11-26 17:54:05 +00:00
exports.get = (id, callback) ->
return pine.get
2014-11-19 13:23:40 +00:00
resource: 'application'
id: id
.then (application) ->
if not application?
2014-11-26 17:54:05 +00:00
return callback(new errors.NotFound("application #{id}"))
return callback(null, application)
2014-11-19 13:23:40 +00:00
2014-11-26 17:54:05 +00:00
.catch (error) ->
return callback(error)
2014-11-21 13:43:03 +00:00
2014-12-08 12:48:19 +00:00
# Create an application
#
# @param {String} name application name
# @param {String} deviceType device type (slug form)
# @param {Function} callback callback(error, id)
#
# @throw {NotFound} Will throw if the request doesn't returns an id
#
# @example Create an application
# resin.models.application.create 'My App', 'raspberry-pi', (error, id) ->
# throw error if error?
# console.log(id)
#
2014-11-26 17:54:05 +00:00
exports.create = (name, deviceType, callback) ->
return pine.post
resource: 'application'
data:
app_name: name
device_type: deviceType
.then (res) ->
id = res?.id
if not id?
2014-11-26 17:54:05 +00:00
return callback(new errors.NotFound('created application id'))
return callback(null, id)
2014-11-26 17:54:05 +00:00
.catch (error) ->
return callback(error)
2014-12-08 12:48:19 +00:00
# Remove application
#
# @param {String, Number} id application id
# @param {Function} callback callback(error)
#
# @example Remove application
# resin.models.application.remove 51, (error) ->
# throw error if error?
#
2014-11-26 17:54:05 +00:00
exports.remove = (id, callback) ->
return pine.delete
2014-11-21 13:43:03 +00:00
resource: 'application'
id: id
2014-11-26 17:54:05 +00:00
.then ->
return callback()
.catch (error) ->
return callback(error)
2014-12-08 12:48:19 +00:00
# Restart application
#
# @param {String, Number} id application id
# @param {Function} callback callback(error)
#
# @example Restart application
# resin.models.application.restart 51, (error) ->
# throw error if error?
#
2014-11-26 17:54:05 +00:00
exports.restart = (id, callback) ->
2014-12-05 14:53:59 +00:00
url = _.template(settings.get('urls.applicationRestart'), { id })
2014-12-08 12:48:19 +00:00
server.post(url, _.unary(callback))