summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCorentin Ardeois <cardeois@internap.com>2016-09-06 11:44:43 -0400
committerCorentin Ardeois <cardeois@internap.com>2016-09-06 13:21:29 -0400
commit773b2b8ee34998bf20264d43ac59b378b2329326 (patch)
tree02e69da7513613ec7095d00ea72287db6b8080f5
parenta9bac97fabc06c9312d80dd6a0dd8f1308d1c283 (diff)
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
Notes
Notes (review): Code-Review+2: Vitaly Kramskikh <vkramskikh@mirantis.com> Code-Review+1: Dong Ma <winterma.dong@gmail.com> Code-Review+2: Michael Krotscheck <krotscheck@gmail.com> Workflow+1: Michael Krotscheck <krotscheck@gmail.com> Verified+2: Jenkins Submitted-by: Jenkins Submitted-at: Wed, 14 Sep 2016 02:56:03 +0000 Reviewed-on: https://review.openstack.org/366222 Project: openstack/js-openstack-lib Branch: refs/heads/master
-rw-r--r--src/glance.js45
-rw-r--r--src/keystone.js47
-rw-r--r--src/util/abstract_service.js47
3 files changed, 47 insertions, 92 deletions
diff --git a/src/glance.js b/src/glance.js
index bbfa0a1..c869aee 100644
--- a/src/glance.js
+++ b/src/glance.js
@@ -56,51 +56,6 @@ export default class Glance extends AbstractService {
56 } 56 }
57 57
58 /** 58 /**
59 * This method resolves any passed token into an appropriate header, as well as the base URL
60 * for the glance API. these variables may then be used to feed other requests.
61 *
62 * @param {Promise|String} token A promise, or string, representing a token.
63 * @returns {Promise} A promise which resolves with [url, token].
64 * @private
65 */
66 _requestComponents (token = null) {
67 // Make sure the token is a promise.
68 let headerPromise = new Promise((resolve) => resolve(token))
69 .then((token) => {
70 if (token) {
71 return {
72 'X-Auth-Token': token
73 };
74 }
75 return {};
76 });
77 return Promise.all([this.serviceEndpoint(), headerPromise]);
78 }
79
80 /**
81 * Return the root API endpoint for the current supported glance version.
82 *
83 * @returns {Promise.<T>|*} A promise which will resolve with the endpoint URL string.
84 */
85 serviceEndpoint () {
86 if (!this._endpointPromise) {
87 this._endpointPromise = this.version()
88 .then((version) => {
89 if (version.links) {
90 for (let i = 0; i < version.links.length; i++) {
91 let link = version.links[i];
92 if (link.rel === 'self' && link.href) {
93 return link.href;
94 }
95 }
96 }
97 throw new Error("No service endpoint discovered.");
98 });
99 }
100 return this._endpointPromise;
101 }
102
103 /**
104 * List the images available on glance. 59 * List the images available on glance.
105 * 60 *
106 * @param {String} token An authorization token, or a promise which will resolve into one. 61 * @param {String} token An authorization token, or a promise which will resolve into one.
diff --git a/src/keystone.js b/src/keystone.js
index eee0961..029e871 100644
--- a/src/keystone.js
+++ b/src/keystone.js
@@ -59,30 +59,6 @@ export default class Keystone extends AbstractService {
59 } 59 }
60 60
61 /** 61 /**
62 * This method builds common components of a keystone request. It converts any passed token
63 * into a promise, resolves the base URL, and then passes the results as an .all() promise,
64 * which may be destructured in a followup request.
65 *
66 * @param {Promise|String} token A promise, or string, representing a token.
67 * @returns {Promise} A promise which resolves with [url, token].
68 * @private
69 */
70 _requestComponents (token = null) {
71 // Make sure the token is a promise.
72 let headerPromise = Promise
73 .resolve(token)
74 .then((token) => {
75 if (token) {
76 return {
77 'X-Auth-Token': token
78 };
79 }
80 return {};
81 });
82 return Promise.all([this.serviceEndpoint(), headerPromise]);
83 }
84
85 /**
86 * Retrieve all the API versions available. 62 * Retrieve all the API versions available.
87 * 63 *
88 * @returns {Promise.<T>} A promise that will resolve with the list of API versions. 64 * @returns {Promise.<T>} A promise that will resolve with the list of API versions.
@@ -93,29 +69,6 @@ export default class Keystone extends AbstractService {
93 } 69 }
94 70
95 /** 71 /**
96 * Return the root API endpoint for the current supported keystone version.
97 *
98 * @returns {Promise.<T>|*} A promise which will resolve with the endpoint URL string.
99 */
100 serviceEndpoint () {
101 if (!this._endpointPromise) {
102 this._endpointPromise = this.version()
103 .then((version) => {
104 if (version.links) {
105 for (let i = 0; i < version.links.length; i++) {
106 let link = version.links[i];
107 if (link.rel === 'self' && link.href) {
108 return link.href;
109 }
110 }
111 }
112 throw new Error("No service endpoint discovered.");
113 });
114 }
115 return this._endpointPromise;
116 }
117
118 /**
119 * Issue a token from the provided credentials. Credentials will be read from the 72 * Issue a token from the provided credentials. Credentials will be read from the
120 * configuration, unless they have been explicitly provided. Note that both the userDomainName 73 * configuration, unless they have been explicitly provided. Note that both the userDomainName
121 * and the projectDomainName are only required if the user/project names are given, rather 74 * and the projectDomainName are only required if the user/project names are given, rather
diff --git a/src/util/abstract_service.js b/src/util/abstract_service.js
index adfebfe..8a8e425 100644
--- a/src/util/abstract_service.js
+++ b/src/util/abstract_service.js
@@ -80,4 +80,51 @@ export default class AbstractService {
80 throw new Error("No supported API version available."); 80 throw new Error("No supported API version available.");
81 }); 81 });
82 } 82 }
83
84 /**
85 * Return the root API endpoint for the current supported glance version.
86 *
87 * @returns {Promise.<T>|*} A promise which will resolve with the endpoint URL string.
88 */
89 serviceEndpoint () {
90 if (!this._endpointPromise) {
91 this._endpointPromise = this.version()
92 .then((version) => {
93 if (version.links) {
94 for (let i = 0; i < version.links.length; i++) {
95 let link = version.links[i];
96 if (link.rel === 'self' && link.href) {
97 return link.href;
98 }
99 }
100 }
101 throw new Error("No service endpoint discovered.");
102 });
103 }
104 return this._endpointPromise;
105 }
106
107 /**
108 * This method builds common components for a request to the implemented service.
109 * It converts any passed token into a promise, resolves the base URL, and then passes
110 * the results as an .all() promise, which may be destructured in a followup request.
111 *
112 * @param {Promise|String} token A promise, or string, representing a token.
113 * @returns {Promise} A promise which resolves with [url, token].
114 * @private
115 */
116 _requestComponents (token = null) {
117 // Make sure the token is a promise.
118 let headerPromise = Promise
119 .resolve(token)
120 .then((token) => {
121 if (token) {
122 return {
123 'X-Auth-Token': token
124 };
125 }
126 return {};
127 });
128 return Promise.all([this.serviceEndpoint(), headerPromise]);
129 }
83} 130}