From be86176883c501ca6ba3b4c4843f630d0e4ca5de Mon Sep 17 00:00:00 2001 From: Michael Krotscheck Date: Fri, 19 Aug 2016 12:23:29 -0700 Subject: [PATCH] Added catalogList() method to keystone API Given a token, this method will return a promise which resolves to the service catalog. Change-Id: If1b7832f5e5e48902e0ef31d28384bb9aeab2fd5 --- src/keystone.js | 19 ++++ test/functional/keystoneTest.js | 31 +++++ test/unit/helpers/data/keystone.js | 177 ++++++++++++++++++++++++++++- test/unit/keystoneTest.js | 41 +++++++ 4 files changed, 267 insertions(+), 1 deletion(-) diff --git a/src/keystone.js b/src/keystone.js index 485c0a5..b85da98 100644 --- a/src/keystone.js +++ b/src/keystone.js @@ -183,6 +183,25 @@ export default class Keystone { .then((url) => this.http.httpRequest('DELETE', `${url}auth/tokens`, headers)); } + /** + * List the service catalog for the configured cloud. + * + * @param {String} token The authorization token. + * @returns {Promise.} A promise which will resolve with the service catalog. + */ + catalogList (token = null) { + const headers = {}; + if (token) { + headers['X-Auth-Token'] = token; + } + + return this + .serviceEndpoint() + .then((url) => this.http.httpRequest('GET', `${url}auth/catalog`, headers)) + .then((response) => response.json()) + .then((body) => body.catalog); + } + authenticate () { const body = { auth: { diff --git a/test/functional/keystoneTest.js b/test/functional/keystoneTest.js index 6903efd..11a87f9 100644 --- a/test/functional/keystoneTest.js +++ b/test/functional/keystoneTest.js @@ -161,5 +161,36 @@ describe("Keystone", () => { done(); }); }); + + describe("catalogList()", () => { + let keystone = null; + + beforeEach(() => { + keystone = new Keystone(config.clouds.devstack); + }); + + it("should list a catalog.", (done) => { + keystone + .tokenIssue() + .then((token) => { + return keystone.catalogList(token); + }) + .then((catalog) => { + expect(catalog.length).not.toBe(0); + done(); + }) + .catch((error) => done.fail(error)); + }); + + it("should error if not authenticated.", (done) => { + keystone + .catalogList() + .then((response) => done.fail(response)) + .catch((error) => { + expect(error).not.toBeNull(); + done(); + }); + }); + }); }); }); diff --git a/test/unit/helpers/data/keystone.js b/test/unit/helpers/data/keystone.js index 7138289..a77b203 100644 --- a/test/unit/helpers/data/keystone.js +++ b/test/unit/helpers/data/keystone.js @@ -388,9 +388,184 @@ function tokenRevoke (token, adminToken = null) { } }; } + +function catalogList (token) { + return { + method: 'GET', + matcher: 'http://192.168.99.99/identity_v2_admin/v3/auth/catalog', + headers: { + 'X-Auth-Token': token + }, + response: { + catalog: [{ + endpoints: [{ + region_id: "RegionOne", + url: "http://192.168.99.99/identity_v2_admin", + region: "RegionOne", + interface: "admin", + id: "940664e070864b638dfafc53cfcbe887" + }, { + region_id: "RegionOne", + url: "http://192.168.99.99/identity", + region: "RegionOne", + interface: "internal", + id: "c3707565bccb407c888040fa9b7e77b0" + }, { + region_id: "RegionOne", + url: "http://192.168.99.99/identity", + region: "RegionOne", + interface: "public", + id: "fb28f261810449ea98b2df646b847a74" + }], type: "identity", id: "0599684d07a145659fa858c1deb4e885", name: "keystone" + }, { + endpoints: [{ + region_id: "RegionOne", + url: "http://192.168.99.99:8776/v3/8b2aa635109f4d0ab355e18a269d341f", + region: "RegionOne", + interface: "internal", + id: "611a5108ef0b4f999ad439b0e1abd9da" + }, { + region_id: "RegionOne", + url: "http://192.168.99.99:8776/v3/8b2aa635109f4d0ab355e18a269d341f", + region: "RegionOne", + interface: "public", + id: "ae08047e33d848c8b1c77f99bc572e22" + }, { + region_id: "RegionOne", + url: "http://192.168.99.99:8776/v3/8b2aa635109f4d0ab355e18a269d341f", + region: "RegionOne", + interface: "admin", + id: "e26c6757baa549469772e16e03c051b8" + }], type: "volumev3", id: "1092f88a41c64fc7b0331fce96e7df6c", name: "cinderv3" + }, { + endpoints: [{ + region_id: "RegionOne", + url: "http://192.168.99.99:8776/v1/8b2aa635109f4d0ab355e18a269d341f", + region: "RegionOne", + interface: "public", + id: "14ad1642b0874816a7ff08eb0e24be87" + }, { + region_id: "RegionOne", + url: "http://192.168.99.99:8776/v1/8b2aa635109f4d0ab355e18a269d341f", + region: "RegionOne", + interface: "internal", + id: "8bb7b28802d44e9d80fbb358a3e133af" + }, { + region_id: "RegionOne", + url: "http://192.168.99.99:8776/v1/8b2aa635109f4d0ab355e18a269d341f", + region: "RegionOne", + interface: "admin", + id: "c271745ff29c4c9d829ab3187d41cab7" + }], type: "volume", id: "5067360b6f264558945b7d2c312dd126", name: "cinder" + }, { + endpoints: [{ + region_id: "RegionOne", + url: "http://192.168.99.99:9292", + region: "RegionOne", + interface: "admin", + id: "0b8b5f0f14904136ab5a4f83f27ec49a" + }, { + region_id: "RegionOne", + url: "http://192.168.99.99:9292", + region: "RegionOne", + interface: "internal", + id: "97c90e43e1fe473b85ef47627006dcdd" + }, { + region_id: "RegionOne", + url: "http://192.168.99.99:9292", + region: "RegionOne", + interface: "public", + id: "ee114418c77a45d2a3cc28240dc4281d" + }], type: "image", id: "6512ca68fbd543928768201198cd7e42", name: "glance" + }, { + endpoints: [{ + region_id: "RegionOne", + url: "http://192.168.99.99:8774/v2.1", + region: "RegionOne", + interface: "internal", + id: "14129d81da0e44abae0c082c535b58cc" + }, { + region_id: "RegionOne", + url: "http://192.168.99.99:8774/v2.1", + region: "RegionOne", + interface: "public", + id: "be681632633d4a62a781148c2fedd6aa" + }, { + region_id: "RegionOne", + url: "http://192.168.99.99:8774/v2.1", + region: "RegionOne", + interface: "admin", + id: "f8979efb0903442a9068d57fce4eafb2" + }], type: "compute", id: "6d3dd68ae2224fd39503342220b5d2c2", name: "nova" + }, { + endpoints: [{ + region_id: "RegionOne", + url: "http://192.168.99.99:8774/v2/8b2aa635109f4d0ab355e18a269d341f", + region: "RegionOne", + interface: "admin", + id: "308f5ed663a7417db3f078f7e3b66db8" + }, { + region_id: "RegionOne", + url: "http://192.168.99.99:8774/v2/8b2aa635109f4d0ab355e18a269d341f", + region: "RegionOne", + interface: "public", + id: "9f08e41e8156498ba01b5cc83cc9e1da" + }, { + region_id: "RegionOne", + url: "http://192.168.99.99:8774/v2/8b2aa635109f4d0ab355e18a269d341f", + region: "RegionOne", + interface: "internal", + id: "b855d4c048f1468f9df5a9950ae811c6" + }], type: "compute_legacy", id: "8ca07a04d03145a094c404b5edf70c18", name: "nova_legacy" + }, { + endpoints: [{ + region_id: "RegionOne", + url: "http://192.168.99.99:8776/v2/8b2aa635109f4d0ab355e18a269d341f", + region: "RegionOne", + interface: "internal", + id: "2b6e28e0aade41b5b80baa9012e54ca4" + }, { + region_id: "RegionOne", + url: "http://192.168.99.99:8776/v2/8b2aa635109f4d0ab355e18a269d341f", + region: "RegionOne", + interface: "admin", + id: "79c96252a8ab4c7181ef4fe97237c314" + }, { + region_id: "RegionOne", + url: "http://192.168.99.99:8776/v2/8b2aa635109f4d0ab355e18a269d341f", + region: "RegionOne", + interface: "public", + id: "8d4cbc86845a4ecb90f19903636205a7" + }], type: "volumev2", id: "a7967e90d1044b1fa6d80b033f1da510", name: "cinderv2" + }, { + endpoints: [{ + region_id: "RegionOne", + url: "http://192.168.99.99:9696/", + region: "RegionOne", + interface: "public", + id: "7033fa4ebed74e3fa51753162150a1f2" + }, { + region_id: "RegionOne", + url: "http://192.168.99.99:9696/", + region: "RegionOne", + interface: "internal", + id: "7aa942d402a34d4c90454b9d84285855" + }, { + region_id: "RegionOne", + url: "http://192.168.99.99:9696/", + region: "RegionOne", + interface: "admin", + id: "bd8db1bafe41489bbbc45641e525ee7d" + }], type: "network", id: "f36b9e68ef114769b85024513ee61047", name: "neutron" + }] + } + }; +} + export { cloudConfig as config, rootResponse as root, tokenIssue as tokenIssue, - tokenRevoke as tokenRevoke + tokenRevoke as tokenRevoke, + catalogList, }; diff --git a/test/unit/keystoneTest.js b/test/unit/keystoneTest.js index b68a589..022b1aa 100644 --- a/test/unit/keystoneTest.js +++ b/test/unit/keystoneTest.js @@ -284,4 +284,45 @@ describe('Keystone', () => { .catch((error) => done.fail(error)); }); }); + + describe("catalogList()", () => { + let keystone = null; + + beforeEach(() => { + fetchMock.mock(mockData.root()); + keystone = new Keystone(mockData.config); + }); + + it("should return the catalog as an array.", (done) => { + const token = 'test_token'; + + fetchMock.mock(mockData.catalogList(token)); + keystone + .catalogList(token) + .then((catalog) => { + expect(catalog.length).not.toBe(0); + done(); + }) + .catch((error) => done.fail(error)); + }); + + it("Should not cache its results", (done) => { + const token = 'test_token'; + + let mockOptions = mockData.catalogList(token); + fetchMock.mock(mockOptions); + + keystone + .catalogList(token) + .then(() => { + expect(fetchMock.calls(mockOptions.name).length).toEqual(1); + return keystone.catalogList(token); + }) + .then(() => { + expect(fetchMock.calls(mockOptions.name).length).toEqual(2); + done(); + }) + .catch((error) => done.fail(error)); + }); + }); });