Skip to content

Commit

Permalink
Merge pull request #411 from ycombinator/gh-410
Browse files Browse the repository at this point in the history
Adding support for nestedDepth option to getResources method.
  • Loading branch information
ycombinator committed Mar 7, 2015
2 parents bd377a1 + 413e745 commit e7144d0
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 6 deletions.
9 changes: 8 additions & 1 deletion docs/providers/openstack/orchestration.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,16 @@ Get the template for a provided stack. Will callback with `f(err, template)`.
Get the resource for a provided stack and resource or resourceName in the callback `f(err,
resource)`

#### client.getResources(stack, callback)
#### client.getResources(stack, [options], callback)
Get the resources for a provided stack. Callback is `f(err, resources)`.

Options are as follows:
```js
{
nestedDepth: 3 // include resources from nested stacks up to nestedDepth levels of recursion
}
```

#### client.getResourceTypes(callback)
Get a list of valid resource types. Callback is `f(err, resourceTypes)`.

Expand Down
9 changes: 8 additions & 1 deletion docs/providers/rackspace/orchestration.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,16 @@ Get the template for a provided stack. Will callback with `f(err, template)`.
Get the resource for a provided stack and resource or resourceName in the callback `f(err,
resource)`

#### client.getResources(stack, callback)
#### client.getResources(stack, [options], callback)
Get the resources for a provided stack. Callback is `f(err, resources)`.

Options are as follows:
```js
{
nestedDepth: 3 // include resources from nested stacks up to nestedDepth levels of recursion
}
```

#### client.getResourceTypes(callback)
Get a list of valid resource types. Callback is `f(err, resourceTypes)`.

Expand Down
20 changes: 16 additions & 4 deletions lib/pkgcloud/openstack/orchestration/client/resources.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,25 @@ exports.getResource = function (stack, resource, callback) {
* @param {function} callback f(err, resources) where stacks is an array of Resource
* @returns {*}
*/
exports.getResources = function (stack, callback) {
exports.getResources = function (stack, options, callback) {
var self = this;

if (typeof options === 'function') {
callback = options;
options = {};
}

function getResources(stack) {
return self._request({
path: urlJoin('/stacks', stack.name, stack.id, 'resources')
}, function (err, body) {
var requestOptions = {
path: urlJoin('/stacks', stack.name, stack.id, 'resources'),
qs: []
};

if (options.nestedDepth) {
requestOptions.qs['nested_depth'] = options.nestedDepth;
}

return self._request(requestOptions, function (err, body) {
if (err) {
return callback(err);
}
Expand Down
130 changes: 130 additions & 0 deletions test/openstack/orchestration/get-stack-resources-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/*
* get-stack-resources-test.js: Test Methods for OpenStack Heat stack resources
*
* (C) 2015 Rackspace
* Shaunak Kashyap
* MIT LICENSE
*/

var helpers = require('../../helpers');

var should = require('should'),
async = require('async'),
hock = require('hock'),
http = require('http'),
mock = !!process.env.MOCK;

var client = helpers.createClient('openstack', 'orchestration');

describe('pkgcloud/openstack/orchestration/resources[getResources]', function() {

var authHockInstance, hockInstance, authServer, server;

before(function (done) {

if (!mock) {
return done();
}

hockInstance = hock.createHock({ throwOnUnmatched: false });
authHockInstance = hock.createHock();

server = http.createServer(hockInstance.handler);
authServer = http.createServer(authHockInstance.handler);

async.parallel([
function (next) {
server.listen(12345, next);
},
function (next) {
authServer.listen(12346, next);
}
], done);
});

it('the getResources method should return an empty array', function (done) {
if (mock) {
authHockInstance
.post('/v2.0/tokens', {
auth: {
passwordCredentials: {
username: 'MOCK-USERNAME',
password: 'MOCK-PASSWORD'
}
}
})
.replyWithFile(200, __dirname + '/../../fixtures/openstack/initialToken.json')
.get('/v2.0/tenants')
.replyWithFile(200, __dirname + '/../../fixtures/openstack/tenantId.json')
.post('/v2.0/tokens', {
auth: {
passwordCredentials: {
username: 'MOCK-USERNAME',
password: 'MOCK-PASSWORD'
},
tenantId: '72e90ecb69c44d0296072ea39e537041'
}
})
.reply(200, helpers.getOpenstackAuthResponse());

hockInstance
.get('/v1/72e90ecb69c44d0296072ea39e537041/stacks/emptystack')
.reply(302, {}, { Location: 'http://localhost:12345/v1/72e90ecb69c44d0296072ea39e537041/stacks/emptystack/87xxxx1-9xx9-4xxe-bxxf-a7xxxxx068' })
.get('/v1/72e90ecb69c44d0296072ea39e537041/stacks/emptystack/87xxxx1-9xx9-4xxe-bxxf-a7xxxxx068')
.reply(200, { stack: { id: '87xxxx1-9xx9-4xxe-bxxf-a7xxxxx068', stack_name: 'emptystack' }})
.get('/v1/72e90ecb69c44d0296072ea39e537041/stacks/emptystack/87xxxx1-9xx9-4xxe-bxxf-a7xxxxx068/resources')
.reply(200, { resources: [] });
}

client.getResources('emptystack', function (err, resources) {
should.not.exist(err);
resources.should.be.an.Array;
resources.length.should.equal(0);
authHockInstance && authHockInstance.done();
hockInstance && hockInstance.done();

done();
});

});

it('the getResources method with nested_depth should pass option correctly', function (done) {
if (mock) {
hockInstance
.get('/v1/72e90ecb69c44d0296072ea39e537041/stacks/emptystack')
.reply(302, {}, { Location: 'http://localhost:12345/v1/72e90ecb69c44d0296072ea39e537041/stacks/emptystack/87xxxx1-9xx9-4xxe-bxxf-a7xxxxx068' })
.get('/v1/72e90ecb69c44d0296072ea39e537041/stacks/emptystack/87xxxx1-9xx9-4xxe-bxxf-a7xxxxx068')
.reply(200, { stack: { id: '87xxxx1-9xx9-4xxe-bxxf-a7xxxxx068', stack_name: 'emptystack' }})
.get('/v1/72e90ecb69c44d0296072ea39e537041/stacks/emptystack/87xxxx1-9xx9-4xxe-bxxf-a7xxxxx068/resources?nested_depth=3')
.reply(200, { resources: [] });
}

client.getResources('emptystack', { nestedDepth: 3 }, function (err, resources) {
console.log(err);
should.not.exist(err);
resources.should.be.an.Array;
resources.length.should.equal(0);
authHockInstance && authHockInstance.done();
hockInstance && hockInstance.done();

done();
});

});

after(function (done) {
if (!mock) {
return done();
}

async.parallel([
function (next) {
server.close(next);
},
function (next) {
authServer.close(next);
}
], done);
});

});

0 comments on commit e7144d0

Please sign in to comment.