Create common service version abstraction
This patch updates common class named AbstractService to propose common logic for multiple version negociation. Change-Id: I999d95fca8ff01afea02ed9953c6b4a3828fa3eb
This commit is contained in:
parent
acbc25ce99
commit
07a5dde06c
|
@ -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.
|
||||
*
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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.");
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue