Merge "Create common service version abstraction"

This commit is contained in:
Jenkins 2016-09-05 17:03:19 +00:00 committed by Gerrit Code Review
commit 10d3fb032c
3 changed files with 61 additions and 67 deletions

View File

@ -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.<T>} 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.<T>} 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.
*

View File

@ -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.<T>} 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.<T>} 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);
}
/**

View File

@ -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.<T>} 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.<T>} 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.");
});
}
}