Add more service abstraction

This patch add _requestComponents and serviceEndpoint methods to
abstract service, as these methods ared used for both Keystone and
Glance services.

Change-Id: Iccc334c0bbd79c1207855260932dab984f2d9d6c
This commit is contained in:
Corentin Ardeois 2016-09-06 11:44:43 -04:00
parent a9bac97fab
commit 773b2b8ee3
3 changed files with 47 additions and 92 deletions

View File

@ -55,51 +55,6 @@ export default class Glance extends AbstractService {
this._config = endpointConfig;
}
/**
* This method resolves any passed token into an appropriate header, as well as the base URL
* for the glance API. these variables may then be used to feed other requests.
*
* @param {Promise|String} token A promise, or string, representing a token.
* @returns {Promise} A promise which resolves with [url, token].
* @private
*/
_requestComponents (token = null) {
// Make sure the token is a promise.
let headerPromise = new Promise((resolve) => resolve(token))
.then((token) => {
if (token) {
return {
'X-Auth-Token': token
};
}
return {};
});
return Promise.all([this.serviceEndpoint(), headerPromise]);
}
/**
* Return the root API endpoint for the current supported glance version.
*
* @returns {Promise.<T>|*} A promise which will resolve with the endpoint URL string.
*/
serviceEndpoint () {
if (!this._endpointPromise) {
this._endpointPromise = this.version()
.then((version) => {
if (version.links) {
for (let i = 0; i < version.links.length; i++) {
let link = version.links[i];
if (link.rel === 'self' && link.href) {
return link.href;
}
}
}
throw new Error("No service endpoint discovered.");
});
}
return this._endpointPromise;
}
/**
* List the images available on glance.
*

View File

@ -58,30 +58,6 @@ export default class Keystone extends AbstractService {
return pointer;
}
/**
* This method builds common components of a keystone request. It converts any passed token
* into a promise, resolves the base URL, and then passes the results as an .all() promise,
* which may be destructured in a followup request.
*
* @param {Promise|String} token A promise, or string, representing a token.
* @returns {Promise} A promise which resolves with [url, token].
* @private
*/
_requestComponents (token = null) {
// Make sure the token is a promise.
let headerPromise = Promise
.resolve(token)
.then((token) => {
if (token) {
return {
'X-Auth-Token': token
};
}
return {};
});
return Promise.all([this.serviceEndpoint(), headerPromise]);
}
/**
* Retrieve all the API versions available.
*
@ -92,29 +68,6 @@ export default class Keystone extends AbstractService {
.then((versions) => versions.values);
}
/**
* Return the root API endpoint for the current supported keystone version.
*
* @returns {Promise.<T>|*} A promise which will resolve with the endpoint URL string.
*/
serviceEndpoint () {
if (!this._endpointPromise) {
this._endpointPromise = this.version()
.then((version) => {
if (version.links) {
for (let i = 0; i < version.links.length; i++) {
let link = version.links[i];
if (link.rel === 'self' && link.href) {
return link.href;
}
}
}
throw new Error("No service endpoint discovered.");
});
}
return this._endpointPromise;
}
/**
* Issue a token from the provided credentials. Credentials will be read from the
* configuration, unless they have been explicitly provided. Note that both the userDomainName

View File

@ -80,4 +80,51 @@ export default class AbstractService {
throw new Error("No supported API version available.");
});
}
/**
* Return the root API endpoint for the current supported glance version.
*
* @returns {Promise.<T>|*} A promise which will resolve with the endpoint URL string.
*/
serviceEndpoint () {
if (!this._endpointPromise) {
this._endpointPromise = this.version()
.then((version) => {
if (version.links) {
for (let i = 0; i < version.links.length; i++) {
let link = version.links[i];
if (link.rel === 'self' && link.href) {
return link.href;
}
}
}
throw new Error("No service endpoint discovered.");
});
}
return this._endpointPromise;
}
/**
* This method builds common components for a request to the implemented service.
* It converts any passed token into a promise, resolves the base URL, and then passes
* the results as an .all() promise, which may be destructured in a followup request.
*
* @param {Promise|String} token A promise, or string, representing a token.
* @returns {Promise} A promise which resolves with [url, token].
* @private
*/
_requestComponents (token = null) {
// Make sure the token is a promise.
let headerPromise = Promise
.resolve(token)
.then((token) => {
if (token) {
return {
'X-Auth-Token': token
};
}
return {};
});
return Promise.all([this.serviceEndpoint(), headerPromise]);
}
}