Browse Source

Merge "Create common service version abstraction"

Jenkins 2 years ago
parent
commit
10d3fb032c
3 changed files with 61 additions and 67 deletions
  1. 4
    34
      src/glance.js
  2. 6
    28
      src/keystone.js
  3. 51
    5
      src/util/abstract_service.js

+ 4
- 34
src/glance.js View File

@@ -43,15 +43,16 @@ export default class Glance extends AbstractService {
43 43
    * @param {{}} endpointConfig The configuration element for a specific glance endpoint.
44 44
    */
45 45
   constructor (endpointConfig) {
46
-    super();
47
-
48 46
     // Sanity checks.
49 47
     if (!endpointConfig || !endpointConfig.url) {
50 48
       throw new Error('An endpoint configuration is required.');
51 49
     }
52 50
     // Clone the config, so that this instance is immutable
53 51
     // at runtime (no modifying the config after the fact).
54
-    this._config = Object.assign({}, endpointConfig);
52
+    endpointConfig = Object.assign({}, endpointConfig);
53
+
54
+    super(endpointConfig.url, supportedGlanceVersions);
55
+    this._config = endpointConfig;
55 56
   }
56 57
 
57 58
   /**
@@ -76,37 +77,6 @@ export default class Glance extends AbstractService {
76 77
     return Promise.all([this.serviceEndpoint(), headerPromise]);
77 78
   }
78 79
 
79
-  /**
80
-   * Retrieve all the API versions available.
81
-   *
82
-   * @returns {Promise.<T>} A promise that will resolve with the list of API versions.
83
-   */
84
-  versions () {
85
-    return this.http
86
-      .httpGet(this._config.url)
87
-      .then((response) => response.json())
88
-      .then((body) => body.versions);
89
-  }
90
-
91
-  /**
92
-   * Retrieve the API version declaration that is currently in use by this glance API.
93
-   *
94
-   * @returns {Promise.<T>} A promise that will resolve with the specific API version.
95
-   */
96
-  version () {
97
-    return this
98
-      .versions()
99
-      .then((versions) => {
100
-        const version = versions.find((element) => {
101
-          return supportedGlanceVersions.indexOf(element.id) > -1;
102
-        });
103
-        if (version) {
104
-          return version;
105
-        }
106
-        throw new Error("No supported Glance API version available.");
107
-      });
108
-  }
109
-
110 80
   /**
111 81
    * Return the root API endpoint for the current supported glance version.
112 82
    *

+ 6
- 28
src/keystone.js View File

@@ -23,15 +23,16 @@ export default class Keystone extends AbstractService {
23 23
    * @see http://docs.openstack.org/developer/os-client-config/#site-specific-file-locations
24 24
    */
25 25
   constructor (cloudConfig) {
26
-    super();
27
-
28 26
     // Sanity checks.
29 27
     if (!cloudConfig) {
30 28
       throw new Error('A configuration is required.');
31 29
     }
32 30
     // Clone the config, so that this instance is immutable
33 31
     // at runtime (no modifying the config after the fact).
34
-    this.cloudConfig = Object.assign({}, cloudConfig);
32
+    cloudConfig = Object.assign({}, cloudConfig);
33
+
34
+    super(cloudConfig.auth.auth_url, supportedKeystoneVersions);
35
+    this.cloudConfig = cloudConfig;
35 36
   }
36 37
 
37 38
   /**
@@ -87,31 +88,8 @@ export default class Keystone extends AbstractService {
87 88
    * @returns {Promise.<T>} A promise that will resolve with the list of API versions.
88 89
    */
89 90
   versions () {
90
-    return this.http
91
-      .httpGet(this.cloudConfig.auth.auth_url)
92
-      .then((response) => response.json())
93
-      .then((body) => {
94
-        return body.versions.values;
95
-      });
96
-  }
97
-
98
-  /**
99
-   * Retrieve the API version declaration that is currently in use by this keystone
100
-   * instance.
101
-   *
102
-   * @returns {Promise.<T>} A promise that will resolve with the specific API version.
103
-   */
104
-  version () {
105
-    return this
106
-      .versions()
107
-      .then((versions) => {
108
-        for (let version of versions) {
109
-          if (supportedKeystoneVersions.indexOf(version.id) > -1) {
110
-            return version;
111
-          }
112
-        }
113
-        throw new Error("No supported Keystone API version available.");
114
-      });
91
+    return super.versions()
92
+      .then((versions) => versions.values);
115 93
   }
116 94
 
117 95
   /**

+ 51
- 5
src/util/abstract_service.js View File

@@ -16,13 +16,20 @@
16 16
 
17 17
 import Http from './http';
18 18
 
19
-/**
20
- * An abstract implementation of our services, which includes logic common to all of our services.
21
- *
22
- * @author Michael Krotscheck
23
- */
24 19
 export default class AbstractService {
25 20
 
21
+  /**
22
+   * This class provides an abstract implementation of our services, which includes logic common to
23
+   * all of our services.
24
+   *
25
+   * @param {string} endpointUrl The endpoint URL.
26
+   * @param {Array} supportedVersions The list of all supported versions.
27
+   */
28
+  constructor (endpointUrl, supportedVersions) {
29
+    this._endpointUrl = endpointUrl;
30
+    this._supportedVersions = supportedVersions;
31
+  }
32
+
26 33
   /**
27 34
    * Our HTTP service instance.
28 35
    *
@@ -34,4 +41,43 @@ export default class AbstractService {
34 41
     }
35 42
     return this._http;
36 43
   }
44
+
45
+  /**
46
+   * List of all supported versions.
47
+   *
48
+   * @returns {Array} The list of all supported versions, or empty array.
49
+   */
50
+  get supportedVersions () {
51
+    return this._supportedVersions || [];
52
+  }
53
+
54
+  /**
55
+   * Retrieve all the API versions available.
56
+   *
57
+   * @returns {Promise.<T>} A promise that will resolve with the list of API versions.
58
+   */
59
+  versions () {
60
+    return this.http
61
+      .httpGet(this._endpointUrl)
62
+      .then((response) => response.json())
63
+      .then((body) => body.versions);
64
+  }
65
+
66
+  /**
67
+   * Retrieve the API version declaration that is currently in use by this instance.
68
+   *
69
+   * @returns {Promise.<T>} A promise that will resolve with the specific API version.
70
+   */
71
+  version () {
72
+    return this
73
+      .versions()
74
+      .then((versions) => {
75
+        for (let version of versions) {
76
+          if (this.supportedVersions.indexOf(version.id) > -1) {
77
+            return version;
78
+          }
79
+        }
80
+        throw new Error("No supported API version available.");
81
+      });
82
+  }
37 83
 }

Loading…
Cancel
Save