This repository has been archived by the owner on Nov 20, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
141 additions
and
188 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,64 @@ | ||
var Request = require('./request'); | ||
var request = require('./request'); | ||
|
||
module.exports = function Client(options) { | ||
this.host = options.host || '127.0.0.1'; | ||
this.port = options.port || 8080; | ||
this.account = options.account || ''; | ||
this.apiKey = options.apiKey || ''; | ||
this.apiSecret = options.apiSecret || ''; | ||
|
||
this.listVirtualMachines = function(callback) { | ||
new Request({ | ||
this.host = options.host || 'localhost', | ||
this.port = options.port || 8080, | ||
this.account = options.account || '', | ||
this.apiKey = options.apiKey || '', | ||
this.apiSecret = options.apiSecret || '' | ||
this.buildArgs = function(command, params) { | ||
var args = { | ||
host: this.host, | ||
port: this.port, | ||
account: this.account, | ||
apiKey: this.apiKey, | ||
apiSecret: this.apiSecret, | ||
command: 'listVirtualMachines' | ||
}).exec(callback); | ||
apiSecret: this.apiSecret | ||
}; | ||
|
||
args.command = command; | ||
args.params = params; | ||
|
||
return args; | ||
}; | ||
|
||
this.listVirtualMachines = function(callback) { | ||
var args = this.buildArgs('listVirtualMachines'); | ||
|
||
new request(args).exec(callback); | ||
}; | ||
|
||
this.deployVirtualMachine = function(templateId, serviceOfferingId, zoneId, callback) { | ||
var args = this.buildArgs('deployVirtualMachine', { | ||
templateId: templateId, | ||
serviceOfferingId: serviceOfferingId, | ||
zoneId: zoneId | ||
}); | ||
|
||
new request(args).exec(callback); | ||
}; | ||
|
||
this.destroyVirtualMachine = function(id, callback) { | ||
var args = this.buildArgs('destroyVirtualMachine', { | ||
id: id | ||
}); | ||
|
||
request(args).exec(callback); | ||
}; | ||
|
||
this.stopVirtualMachine = function(id, callback) { | ||
var args = this.buildArgs('stopVirtualMachine', { | ||
id: id | ||
}); | ||
|
||
request(args).exec(callback); | ||
}; | ||
|
||
this.startVirtualMachine = function(id, callback) { | ||
var args = this.buildArgs('startVirtualMachine', { | ||
id: id | ||
}); | ||
|
||
request(args).exec(callback); | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,91 +1,25 @@ | ||
var vows = require('vows') | ||
, assert = require('assert') | ||
, client = require('../lib/client') | ||
var client = require('../lib/client') | ||
, nock = require('nock'); | ||
|
||
vows.describe('Client Tests').addBatch({ | ||
'when client is constructed': { | ||
'with a null options object': { | ||
topic: function() { | ||
return new client({}); | ||
}, | ||
'the host property defaults to localhost': function(topic) { | ||
assert.equal(topic.host, '127.0.0.1'); | ||
}, | ||
'the port property defaults to 8080': function(topic) { | ||
assert.equal(topic.port, 8080); | ||
}, | ||
'the account property defaults to empty string': function(topic) { | ||
assert.equal(topic.account, ''); | ||
}, | ||
'the apiKey property defaults to empty string': function(topic) { | ||
assert.equal(topic.apiKey, ''); | ||
}, | ||
'the apiSecret property defaults to empty string': function(topic) { | ||
assert.equal(topic.apiSecret, ''); | ||
} | ||
}, | ||
'with a complete options object': { | ||
topic: function() { | ||
return new client({ | ||
host: 'cloudhost', | ||
port: 54321, | ||
account: 'TestUser', | ||
apiKey: 'TestUserApiKey', | ||
apiSecret: 'TestUserApiSecret' | ||
}); | ||
}, | ||
'the host property is equal to the specified value': function(topic) { | ||
assert.equal(topic.host, 'cloudhost'); | ||
}, | ||
'the port property is equal to the specified value': function(topic) { | ||
assert.equal(topic.port, 54321); | ||
}, | ||
'the account property is equal to the specified value': function(topic) { | ||
assert.equal(topic.account, 'TestUser'); | ||
}, | ||
'the apiKey property is equal to the specified value': function(topic) { | ||
assert.equal(topic.apiKey, 'TestUserApiKey'); | ||
}, | ||
'the apiSecret property is equal to the specified value': function(topic) { | ||
assert.equal(topic.apiSecret, 'TestUserApiSecret'); | ||
} | ||
} | ||
}, | ||
|
||
'when listVirtualMachines is called': { | ||
'and no machines are running': { | ||
'the result': { | ||
topic: function() { | ||
if(process.env.NOCK) { | ||
nock("http://cloudhost:54321") | ||
.get("/client/api?account=TestUser&apiKey=TestUserApiKey&command=listVirtualMachines&domainId=1&response=json&signature=acSwdq7p634LFmIvR3em%2FZzlKEE%3D") | ||
.reply(200, { | ||
listvirtualmachinesresponse: {} | ||
}); | ||
} | ||
var args = { | ||
host: 'controller', | ||
account: 'TestUser', | ||
apiKey: 'TestUserApiKey', | ||
apiSecret: 'TestUserApiSecret' | ||
}; | ||
|
||
new client({ | ||
host: 'cloudhost', | ||
port: 54321, | ||
account: 'TestUser', | ||
apiKey: 'TestUserApiKey', | ||
apiSecret: 'TestUserApiSecret' | ||
}).listVirtualMachines(this.callback); | ||
}, | ||
'has a "status" property': function(topic) { | ||
assert.isDefined(topic.status); | ||
}, | ||
'has a "data" property': function(topic) { | ||
assert.isDefined(topic.data); | ||
}, | ||
'the value of the status property is "ok"': function(topic) { | ||
assert.equal(topic.status, "ok"); | ||
}, | ||
'the value of the data property is an empty object': function(topic) { | ||
assert.deepEqual(topic.data, {}); | ||
} | ||
} | ||
} | ||
exports['listVirtualMachines'] = function(test) { | ||
if(process.env.NOCK) { | ||
nock("http://controller:8080") | ||
.get("/client/api?account=TestUser&apiKey=TestUserApiKey&command=listVirtualMachines&domainId=1&response=json&signature=acSwdq7p634LFmIvR3em%2FZzlKEE%3D") | ||
.reply(200, { | ||
listvirtualmachinesresponse: {} | ||
}); | ||
} | ||
}).export(module, { error: false }); | ||
|
||
new client(args).listVirtualMachines(function(res) { | ||
test.equal(res.status, 'ok'); | ||
test.deepEqual(res.data, {}); | ||
test.done(); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,15 @@ | ||
var vows = require('vows') | ||
, assert = require('assert') | ||
, Client = require('../lib/client') | ||
, CloudStack = require('../lib/cloudstack'); | ||
var CloudstackClient = require('../lib/client'); | ||
|
||
vows.describe('CloudStack Object Tests').addBatch({ | ||
'When you call .createClient': { | ||
topic: function() { | ||
return CloudStack.createClient({ | ||
host: 'cloudhost' | ||
}); | ||
}, | ||
'an object of type Client is returned': function(topic) { | ||
assert.instanceOf(topic, Client); | ||
}, | ||
'the options are passed to the constructor': function(topic) { | ||
assert.equal(topic.host, 'cloudhost'); | ||
} | ||
} | ||
}).export(module); | ||
exports['CreateClient'] = function(test) { | ||
var client = (require('../lib/cloudstack')).createClient({ | ||
host: 'cloudhost' | ||
}); | ||
|
||
test.equal(client.host, 'cloudhost'); | ||
test.equal(client.port, 8080); | ||
test.equal(client.apiKey, ''); | ||
test.equal(client.apiSecret, ''); | ||
test.equal(client.account, ''); | ||
|
||
test.done(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,32 @@ | ||
var vows = require('vows') | ||
, assert = require('assert') | ||
, QueryString = require('../lib/querystring'); | ||
|
||
vows.describe('QueryString Tests').addBatch({ | ||
'when constructed with an "apiKey" value in the options': { | ||
topic: function() { | ||
return new QueryString({ | ||
apiKey: 'ApiKeyString' | ||
}); | ||
}, | ||
'the params object has a property called "apiKey"': function(topic) { | ||
assert.isDefined(topic.params['apiKey']); | ||
}, | ||
'the "apiKey" property has the correct value': function(topic) { | ||
assert.equal(topic.params['apiKey'], 'ApiKeyString'); | ||
} | ||
}, | ||
'when a parameter is added': { | ||
topic: function() { | ||
var qs = new QueryString(); | ||
qs.addParam('Property', 'Property Value'); | ||
|
||
return qs; | ||
}, | ||
'the property now exists in the .params collection': function(topic) { | ||
assert.isDefined(topic.params['Property']); | ||
}, | ||
'the property value has been set and uri encoded': function(topic) { | ||
assert.equal(topic.params['Property'], 'Property%20Value'); | ||
} | ||
}, | ||
'when converted to a string': { | ||
topic: function() { | ||
return new QueryString({ | ||
apiKey: 'ApiKeyString', | ||
apiSecret: 'ApiSecretString' | ||
}).toString(); | ||
}, | ||
'the apiKey is included as a parameter': function(topic) { | ||
assert.include(topic, 'apiKey=ApiKeyString'); | ||
}, | ||
'the signature is included as a parameter': function(topic) { | ||
assert.include(topic, 'signature='); | ||
}, | ||
'the signature is generated properly': function(topic) { | ||
assert.include(topic, 'signature=9MQbwfKzCXVlfuNV80wmrXnzL%2B4%3D'); | ||
} | ||
} | ||
}).export(module); | ||
var querystring = require('../lib/querystring'); | ||
|
||
exports['when constructed with an "apiKey" value in the options'] = function(test) { | ||
var qs = new querystring({ | ||
apiKey: 'TestApiKey' | ||
}); | ||
|
||
test.equal(qs.params.apiKey, 'TestApiKey'); | ||
|
||
test.done(); | ||
}; | ||
|
||
exports['when a parameter is added'] = function(test) { | ||
var qs = new querystring(); | ||
qs.addParam('Property', 'Property Value'); | ||
|
||
test.equal(qs.params.Property, 'Property%20Value'); | ||
|
||
test.done(); | ||
}; | ||
|
||
exports['when converted to a string'] = function(test) { | ||
var qs = new querystring({ | ||
apiKey: 'ApiKeyString', | ||
apiSecret: 'ApiSecretString' | ||
}).toString(); | ||
|
||
test.ok(qs.match(/apiKey=ApiKeyString/)); | ||
test.ok(qs.match(/signature=9MQbwfKzCXVlfuNV80wmrXnzL%2B4%3D/)); | ||
|
||
test.done(); | ||
}; |