diff --git a/src/glance.js b/src/glance.js index 53c4300..bbfa0a1 100644 --- a/src/glance.js +++ b/src/glance.js @@ -43,15 +43,16 @@ export default class Glance extends AbstractService { * @param {{}} endpointConfig The configuration element for a specific glance endpoint. */ constructor (endpointConfig) { - super(); - // Sanity checks. if (!endpointConfig || !endpointConfig.url) { throw new Error('An endpoint configuration is required.'); } // Clone the config, so that this instance is immutable // at runtime (no modifying the config after the fact). - this._config = Object.assign({}, endpointConfig); + endpointConfig = Object.assign({}, endpointConfig); + + super(endpointConfig.url, supportedGlanceVersions); + this._config = endpointConfig; } /** @@ -76,37 +77,6 @@ export default class Glance extends AbstractService { return Promise.all([this.serviceEndpoint(), headerPromise]); } - /** - * Retrieve all the API versions available. - * - * @returns {Promise.} A promise that will resolve with the list of API versions. - */ - versions () { - return this.http - .httpGet(this._config.url) - .then((response) => response.json()) - .then((body) => body.versions); - } - - /** - * Retrieve the API version declaration that is currently in use by this glance API. - * - * @returns {Promise.} A promise that will resolve with the specific API version. - */ - version () { - return this - .versions() - .then((versions) => { - const version = versions.find((element) => { - return supportedGlanceVersions.indexOf(element.id) > -1; - }); - if (version) { - return version; - } - throw new Error("No supported Glance API version available."); - }); - } - /** * Return the root API endpoint for the current supported glance version. * diff --git a/src/keystone.js b/src/keystone.js index fd774c3..eee0961 100644 --- a/src/keystone.js +++ b/src/keystone.js @@ -23,15 +23,16 @@ export default class Keystone extends AbstractService { * @see http://docs.openstack.org/developer/os-client-config/#site-specific-file-locations */ constructor (cloudConfig) { - super(); - // Sanity checks. if (!cloudConfig) { throw new Error('A configuration is required.'); } // Clone the config, so that this instance is immutable // at runtime (no modifying the config after the fact). - this.cloudConfig = Object.assign({}, cloudConfig); + cloudConfig = Object.assign({}, cloudConfig); + + super(cloudConfig.auth.auth_url, supportedKeystoneVersions); + this.cloudConfig = cloudConfig; } /** @@ -87,31 +88,8 @@ export default class Keystone extends AbstractService { * @returns {Promise.} A promise that will resolve with the list of API versions. */ versions () { - return this.http - .httpGet(this.cloudConfig.auth.auth_url) - .then((response) => response.json()) - .then((body) => { - return body.versions.values; - }); - } - - /** - * Retrieve the API version declaration that is currently in use by this keystone - * instance. - * - * @returns {Promise.} A promise that will resolve with the specific API version. - */ - version () { - return this - .versions() - .then((versions) => { - for (let version of versions) { - if (supportedKeystoneVersions.indexOf(version.id) > -1) { - return version; - } - } - throw new Error("No supported Keystone API version available."); - }); + return super.versions() + .then((versions) => versions.values); } /** diff --git a/src/util/abstract_service.js b/src/util/abstract_service.js index 11ba0d3..adfebfe 100644 --- a/src/util/abstract_service.js +++ b/src/util/abstract_service.js @@ -16,13 +16,20 @@ import Http from './http'; -/** - * An abstract implementation of our services, which includes logic common to all of our services. - * - * @author Michael Krotscheck - */ export default class AbstractService { + /** + * This class provides an abstract implementation of our services, which includes logic common to + * all of our services. + * + * @param {string} endpointUrl The endpoint URL. + * @param {Array} supportedVersions The list of all supported versions. + */ + constructor (endpointUrl, supportedVersions) { + this._endpointUrl = endpointUrl; + this._supportedVersions = supportedVersions; + } + /** * Our HTTP service instance. * @@ -34,4 +41,43 @@ export default class AbstractService { } return this._http; } + + /** + * List of all supported versions. + * + * @returns {Array} The list of all supported versions, or empty array. + */ + get supportedVersions () { + return this._supportedVersions || []; + } + + /** + * Retrieve all the API versions available. + * + * @returns {Promise.} A promise that will resolve with the list of API versions. + */ + versions () { + return this.http + .httpGet(this._endpointUrl) + .then((response) => response.json()) + .then((body) => body.versions); + } + + /** + * Retrieve the API version declaration that is currently in use by this instance. + * + * @returns {Promise.} A promise that will resolve with the specific API version. + */ + version () { + return this + .versions() + .then((versions) => { + for (let version of versions) { + if (this.supportedVersions.indexOf(version.id) > -1) { + return version; + } + } + throw new Error("No supported API version available."); + }); + } }