digitalocean
Advanced tools
Comparing version 0.1.1 to 0.2.0
@@ -8,2 +8,3 @@ var https = require('https'); | ||
var EventEmitter = require('events').EventEmitter; | ||
var entityRootFactory = require('./templates').entityRootFactory; | ||
@@ -17,4 +18,4 @@ var entities = require('./entities.js'); | ||
this.droplets = _entityRootFactory(this, 'droplet', entities.Droplet); | ||
this.images = _entityRootFactory(this, 'image', entities.Image); | ||
this.droplets = entityRootFactory(this, 'droplet', entities.Droplet); | ||
this.images = entityRootFactory(this, 'image', entities.Image); | ||
@@ -29,6 +30,6 @@ this.images.my = function(onComplete) { | ||
this.ssh_keys = _entityRootFactory(this, 'ssh_key', entities.SshKey); | ||
this.sizes = _entityRootFactory(this, 'size', entities.Size); | ||
this.domains = _entityRootFactory(this, 'domain', entities.Domain); | ||
this.regions = _entityRootFactory(this, 'region', entities.Region); | ||
this.ssh_keys = entityRootFactory(this, 'ssh_key', entities.SshKey); | ||
this.sizes = entityRootFactory(this, 'size', entities.Size); | ||
this.domains = entityRootFactory(this, 'domain', entities.Domain); | ||
this.regions = entityRootFactory(this, 'region', entities.Region); | ||
} | ||
@@ -69,69 +70,1 @@ | ||
exports.Api = Api; | ||
/* | ||
* Эти функции вызываются в контексте инстанса апи, но не объявлены в прототипе, | ||
* чтобы не захламлять область видимости вспомогательным хламом | ||
*/ | ||
function _cast(type, data) { | ||
var o = new type(this); | ||
o.unserializeFromApiResponse(data); | ||
return o; | ||
}; | ||
function _castArray(type, list) { | ||
var o = new type(this); | ||
var res = []; | ||
for(var i = 0; i < list.length; i++) | ||
res.push(_cast.call(this, type, list[i])); | ||
return res; | ||
}; | ||
/** | ||
* @param string names название сущности в REST: "droplet" / "image" / ... | ||
* @param Function type конструктор типа | ||
* @returns Function | ||
*/ | ||
function _entityRootFactory(parent, name, type) { | ||
var mname = name + 's'; | ||
var method = {}; | ||
method.get = function(id, onComplete) { | ||
return parent._request('/' + mname + '/' + id, {}, name).on('response', function(obj) { | ||
var res = _cast.call(parent, type, obj); | ||
this.emit('success', res); | ||
if(onComplete) | ||
onComplete(res); | ||
}); | ||
}; | ||
method.all = function(params, onComplete) { | ||
onComplete = params instanceof Function ? params : onComplete; | ||
params = params instanceof Function ? {} : params; | ||
return parent._request('/' + mname, params, mname).on('response', function(list) { | ||
var res = _castArray.call(parent, type, list); | ||
this.emit('success', res); | ||
if(onComplete) | ||
onComplete.call(this, res); | ||
}); | ||
}; | ||
method.new = function(params, onComplete) { | ||
return parent._request('/' + mname + '/new', params, name).on('response', function(list) { | ||
var res = _cast.call(parent, type, list); | ||
this.emit('success', res); | ||
if(onComplete) | ||
onComplete.call(this, res); | ||
}); | ||
}; | ||
return method; | ||
}; |
var util = require('util'); | ||
var ArgumentError = require('./exceptions/ArgumentError.js').ArgumentError; | ||
var Api = require('./Api.js').Api; | ||
var entityRootFactory = require('./templates').entityRootFactory; | ||
function ApiEntity(api) { | ||
this.api = api; | ||
function ApiEntity(api, name) { | ||
Object.defineProperty(this, '_api', { | ||
value: api, | ||
enumerable: false | ||
}); | ||
Object.defineProperty(this, '_name', { | ||
value: name, | ||
enumerable: false | ||
}); | ||
}; | ||
@@ -25,50 +35,49 @@ | ||
function _entityTemplate(name, actions) { | ||
var ctor = function(api) { | ||
this.constructor.super_.call(this, api); | ||
this._name = name; | ||
}; | ||
function _createAction(action, mandatoryParams) { | ||
return function(arg1, arg2) { | ||
var params = {}; | ||
var onComplete = undefined; | ||
util.inherits(ctor, ApiEntity); | ||
if(arg1 instanceof Function) | ||
onComplete = arg1; | ||
else if(arg1 instanceof Object) | ||
params = arg1; | ||
if(actions) | ||
_registerActions(ctor, actions); | ||
if(arg2) | ||
onComplete = arg2; | ||
return ctor; | ||
} | ||
for(var i = 0; i < mandatoryParams.length; i++) { | ||
var p = mandatoryParams[i]; | ||
if(!params.hasOwnProperty(p)) | ||
throw new ArgumentError('Mandatory argument `' + p + '` is not specified'); | ||
} | ||
function _createAction(action, mandatoryParams) { | ||
return function(arg1, arg2) { | ||
var params = {}; | ||
var onComplete = undefined; | ||
return this._api._action( | ||
this._getRelativeUrl() + '/' + action, | ||
params, | ||
onComplete | ||
).on('response', function(res) { | ||
this.emit('success', res); | ||
if(arg1 instanceof Function) | ||
onComplete = arg1; | ||
else if(arg1 instanceof Object) | ||
params = arg1; | ||
if(onComplete) | ||
onComplete(res); | ||
}); | ||
}; | ||
} | ||
if(arg2) | ||
onComplete = arg2; | ||
function _registerActions(ctor, actions) { | ||
for(var action in actions) | ||
ctor.prototype[action] = _createAction(action, actions[action]); | ||
} | ||
for(var i = 0; i < mandatoryParams.length; i++) { | ||
var p = mandatoryParams[i]; | ||
if(!params.hasOwnProperty(p)) | ||
throw new ArgumentError('Mandatory argument `' + p + '` is not specified'); | ||
} | ||
var ctor = function(api) { | ||
this.constructor.super_.call(this, api, name); | ||
}; | ||
return this.api._action( | ||
this._getRelativeUrl() + '/' + action, | ||
params, | ||
onComplete | ||
).on('response', function(res) { | ||
this.emit('success', res); | ||
util.inherits(ctor, ApiEntity); | ||
if(onComplete) | ||
onComplete(res); | ||
}); | ||
}; | ||
} | ||
if(actions) | ||
_registerActions(ctor, actions); | ||
function _registerActions(ctor, actions) { | ||
for(var action in actions) | ||
ctor.prototype[action] = _createAction(action, actions[action]); | ||
return ctor; | ||
} | ||
@@ -112,2 +121,3 @@ | ||
{ | ||
edit: ['ssh_pub_key'], | ||
destroy: [] | ||
@@ -118,4 +128,20 @@ } | ||
var Domain = _entityTemplate('domain'); | ||
var DomainRecord = _entityTemplate(); | ||
Domain.prototype._wakeup = function() { | ||
this.records = entityRootFactory(this._api, 'record', DomainRecord); | ||
this.records.rootUrl = '/domains/' + this.id; | ||
}; | ||
var DomainRecord = _entityTemplate( | ||
'record', | ||
{ | ||
edit: ['record_type', 'data'], | ||
destroy: [] | ||
} | ||
); | ||
DomainRecord.prototype._getRelativeUrl = function() { | ||
return '/domains/' + this.domain_id + '/records/' + this.id; | ||
}; | ||
exports.Droplet = Droplet; | ||
@@ -122,0 +148,0 @@ exports.Image = Image; |
@@ -44,3 +44,6 @@ var url = require('url'); | ||
up.query[p] = parameters[p]; | ||
if(parameters[p] instanceof Array) | ||
up.query[p] = parameters[p].join(','); | ||
else | ||
up.query[p] = parameters[p]; | ||
} | ||
@@ -47,0 +50,0 @@ } |
{ | ||
"name": "digitalocean", | ||
"version": "0.1.1", | ||
"version": "0.2.0", | ||
"description": "DigitalOcean API", | ||
@@ -5,0 +5,0 @@ "keywords": ["digitalocean"], |
170
Readme.md
# DigitalOcean API [![Build Status](https://secure.travis-ci.org/avz/node-digitalocean.png?branch=master)](http://travis-ci.org/avz/node-digitalocean) | ||
## Installation | ||
``` | ||
npm install digitalocean | ||
``` | ||
## Examples | ||
```javascript | ||
@@ -8,3 +14,2 @@ var Api = require('digitalocean').Api; | ||
## Examples | ||
```javascript | ||
@@ -50,41 +55,134 @@ /* | ||
## API | ||
### Droplets | ||
- ``api.droplets.all([onSuccess])`` | ||
- ``api.droplets.get(id, [onSuccess])`` | ||
- ``api.droplets.new({new droplet fields}, [onSuccess])`` | ||
Complete API documentation can be found at [https://api.digitalocean.com/](https://api.digitalocean.com/) | ||
### Droplet object | ||
Properties | ||
```javascript | ||
{ | ||
id: 100823, | ||
backups_active: null, | ||
image_id: 420, | ||
name: "test222", | ||
region_id: 1, | ||
size_id: 33, | ||
status: "active", | ||
} | ||
``` | ||
### Droplets ``/droplets`` | ||
- ``Api.droplets.all(onSuccess)`` - get all active droplets | ||
- ``Api.droplets.get(id, onSuccess)`` - get droplet by id | ||
- ``Api.droplets.new({name: "Name", size_id: 1, image_id: 1, region_id: 1, ssh_key_ids: [1]}, onSuccess)`` - create new droplet | ||
Methods | ||
```javascript | ||
reboot([onSuccess]); | ||
power_cycle([onSuccess]); | ||
shutdown([onSuccess]); | ||
power_off([onSuccess]); | ||
power_on([onSuccess]); | ||
password_reset([onSuccess]); | ||
resize({size_id: 1}, [onSuccess]); | ||
snapshot({name: "Name"}, [onSuccess]); | ||
restore({image_id: 1}, [onSuccess]); | ||
rebuild({image_id: 1}, [onSuccess]); | ||
enable_backups([onSuccess]); | ||
disable_backups([onSuccess]); | ||
rename({name: "New name"}, [onSuccess]); | ||
destroy([onSuccess]); | ||
``` | ||
#### Droplet object | ||
##### Properties | ||
- ``Droplet.id`` | ||
- ``Droplet.backups_active`` | ||
- ``Droplet.image_id`` | ||
- ``Droplet.name`` | ||
- ``Droplet.region_id`` | ||
- ``Droplet.size_id`` | ||
- ``Droplet.status`` | ||
Complete API documentation can be found at [https://www.digitalocean.com/api_access](https://www.digitalocean.com/api_access) | ||
##### Methods | ||
- ``Droplet.reboot(onSuccess)`` | ||
- ``Droplet.power_cycle(onSuccess)`` | ||
- ``Droplet.shutdown(onSuccess)`` | ||
- ``Droplet.power_off(onSuccess)`` | ||
- ``Droplet.power_on(onSuccess)`` | ||
- ``Droplet.password_reset(onSuccess)`` | ||
- ``Droplet.resize({size_id: 1}, onSuccess)`` | ||
- ``Droplet.snapshot({name: "Name"}, onSuccess)`` | ||
- ``Droplet.restore({image_id: 1}, onSuccess)`` | ||
- ``Droplet.rebuild({image_id: 1}, onSuccess)`` | ||
- ``Droplet.enable_backups(onSuccess)`` | ||
- ``Droplet.disable_backups(onSuccess)`` | ||
- ``Droplet.rename({name: "New name"}, onSuccess)`` | ||
- ``Droplet.destroy(onSuccess)`` | ||
### Images ``/images`` | ||
- ``Api.images.all(onSuccess)`` - get all available images (global and my own) | ||
- ``Api.images.global(onSuccess)`` - get all available global images | ||
- ``Api.images.my(onSuccess)`` - get all my own images | ||
- ``Api.images.get(id, onSuccess)`` - get image by id | ||
#### Image object | ||
##### Properties | ||
- ``Image.id`` | ||
- ``Image.name`` | ||
- ``Image.distribution`` | ||
##### Methods | ||
- ``Image.transfer({region_id: 1}, onSuccess)`` | ||
- ``Image.destroy(onSuccess)`` | ||
### SSH Keys ``/ssh_keys`` | ||
- ``Api.ssh_keys.all(onSuccess)`` - get *short* info about my SSH keys | ||
- ``Api.ssh_keys.get(id, onSuccess)`` - get *full* info about specified SSH key | ||
- ``Api.ssh_keys.new({name: "My new key", ssh_pub_key: "ssh-rsa ... user@host"}, onSuccess)`` - register new SSH key | ||
#### SshKey object | ||
##### Properties | ||
- ``SshKey.id`` | ||
- ``SshKey.name`` | ||
- ``SshKey.ssh_pub_key`` - *available only in full info* | ||
##### Methods | ||
- ``SshKey.edit({ssh_pub_key: "ssh-rsa ... user@host"}, onSuccess)`` | ||
- ``SshKey.destroy(onSuccess)`` | ||
### Sizes ``/sizes`` | ||
- ``Api.sizes.all(onSuccess)`` - get *short* info about all available instance types | ||
- ``Api.sizes.get(id, onSuccess)`` - get *full* info about specified instance type | ||
#### Size object | ||
##### Properties | ||
- ``Size.id`` | ||
- ``Size.name`` | ||
- ``Size.slug`` | ||
- ``Size.cpu`` - number of CPUs, *available only in full info* | ||
- ``Size.memory`` - memory amount (in Mb), *available only in full info* | ||
- ``Size.cost_per_hour`` - price per hour (in dollars), *available only in full info* | ||
- ``Size.disk`` - disk space (in Gb), *available only in full info* | ||
### Regions ``/regions`` | ||
- ``Api.regions.all(onSuccess)`` - get all available regions | ||
- ``Api.regions.get(id, onSuccess)`` - get region by id | ||
#### Region object | ||
##### Properties | ||
- ``Region.id`` | ||
- ``Region.name`` | ||
- ``Region.slug`` | ||
### Domains ``/domains`` | ||
- ``Api.domains.all(onSuccess)`` - get all domains | ||
- ``Api.domains.get(id, onSuccess)`` - get domain by id | ||
- ``Api.domains.new({name: 'example.com', ip_address: '8.8.8.8'}, onSuccess)`` - create new domain | ||
#### Domain object | ||
##### Properties | ||
- ``Domain.id`` | ||
- ``Domain.name`` | ||
- ``Domain.ttl`` | ||
- ``Domain.live_zone_file`` | ||
- ``Domain.error`` | ||
- ``Domain.zone_file_with_error`` | ||
- ``Domain.records`` - see "Domain Records" section | ||
##### Methods | ||
- ``Domain.destroy(onSuccess)`` | ||
### Domain Records ``/domains/[id]/records`` | ||
- ``Domain.records.all(onSuccess)`` | ||
- ``Domain.records.get(id, onSuccess)`` | ||
- ``Domain.records.new({domain_id: 1, data: '@', record_type: 'A', name: 'ya.ru'}, onSuccess)`` | ||
#### DomainRecord object | ||
##### Properties | ||
- ``DomainRecord.id`` | ||
- ``DomainRecord.domain_id`` | ||
- ``DomainRecord.record_type`` | ||
- ``DomainRecord.name`` | ||
- ``DomainRecord.data`` | ||
- ``DomainRecord.priority`` | ||
- ``DomainRecord.port`` | ||
- ``DomainRecord.weight`` | ||
##### Methods | ||
- ``DomainRecord.edit({}, onSuccess)`` | ||
- ``DomainRecord.destroy(onSuccess)`` | ||
## Error handling | ||
@@ -91,0 +189,0 @@ By default all error will asynchronously throw an error. To catch them |
@@ -17,2 +17,7 @@ var LowLevelApi = require('../lib/LowLevelApi.js').LowLevelApi; | ||
resultUrl: 'http://localhost/hello/?client_id=CLIENTID&api_key=APIKEY&a=b&b=c' | ||
}, | ||
{ | ||
rel: '/hello/', | ||
params: {array: [1, 2, 3, 4]}, | ||
resultUrl: 'http://localhost/hello/?client_id=CLIENTID&api_key=APIKEY&array=1%2C2%2C3%2C4' | ||
} | ||
@@ -19,0 +24,0 @@ ]; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
23140
18
577
196