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

66 lines
1.4 KiB
CoffeeScript
Raw Normal View History

2014-11-26 14:32:57 +00:00
_ = require('lodash')
canvas = require('./_canvas')
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-11-26 17:54:05 +00:00
exports.getAll = (callback) ->
return canvas.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)
exports.get = (id, callback) ->
2014-11-19 13:23:40 +00:00
return canvas.get
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-11-26 17:54:05 +00:00
exports.create = (name, deviceType, callback) ->
return canvas.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-11-26 17:54:05 +00:00
exports.remove = (id, callback) ->
2014-11-21 13:43:03 +00:00
return canvas.delete
resource: 'application'
id: id
2014-11-26 17:54:05 +00:00
.then ->
return callback()
.catch (error) ->
return callback(error)
exports.restart = (id, callback) ->
2014-12-05 14:53:59 +00:00
url = _.template(settings.get('urls.applicationRestart'), { id })
2014-12-01 14:06:03 +00:00
server.post(url, callback)