Browse Source

Added imageList() method to glance API

This patch adds the imageList() method (and associated token handling)
to the glance API.

Change-Id: I840f347d446b9b774c532afe38d62a94e5336a73
Michael Krotscheck 2 years ago
parent
commit
45809be72b
4 changed files with 176 additions and 1 deletions
  1. 36
    0
      src/glance.js
  2. 18
    0
      test/functional/glanceTest.js
  3. 41
    0
      test/unit/glanceTest.js
  4. 81
    1
      test/unit/helpers/data/glance.js

+ 36
- 0
src/glance.js View File

@@ -53,6 +53,28 @@ export default class Glance {
53 53
     this.http = new Http();
54 54
   }
55 55
 
56
+  /**
57
+   * This method resolves any passed token into an appropriate header, as well as the base URL
58
+   * for the glance API. these variables may then be used to feed other requests.
59
+   *
60
+   * @param {Promise|String} token A promise, or string, representing a token.
61
+   * @returns {Promise} A promise which resolves with [url, token].
62
+   * @private
63
+   */
64
+  _requestComponents (token = null) {
65
+    // Make sure the token is a promise.
66
+    let headerPromise = new Promise((resolve) => resolve(token))
67
+      .then((token) => {
68
+        if (token) {
69
+          return {
70
+            'X-Auth-Token': token
71
+          };
72
+        }
73
+        return {};
74
+      });
75
+    return Promise.all([this.serviceEndpoint(), headerPromise]);
76
+  }
77
+
56 78
   /**
57 79
    * Retrieve all the API versions available.
58 80
    *
@@ -106,4 +128,18 @@ export default class Glance {
106 128
     }
107 129
     return this._endpointPromise;
108 130
   }
131
+
132
+  /**
133
+   * List the images available on glance.
134
+   *
135
+   * @param {String} token An authorization token, or a promise which will resolve into one.
136
+   * @returns {Promise.<T>} A promise which will resolve with the list of images.
137
+   */
138
+  imageList (token = null) {
139
+    return this
140
+      ._requestComponents(token)
141
+      .then(([url, headers]) => this.http.httpRequest('GET', `${url}images`, headers))
142
+      .then((response) => response.json())
143
+      .then((body) => body.images);
144
+  }
109 145
 }

+ 18
- 0
test/functional/glanceTest.js View File

@@ -79,4 +79,22 @@ describe("Glance", () => {
79 79
         .catch((error) => done.fail(error));
80 80
     });
81 81
   });
82
+
83
+  describe("imageList()", () => {
84
+
85
+    /**
86
+     * Assert that we can get a list of images.
87
+     */
88
+    it("should return a supported version.", (done) => {
89
+      configPromise
90
+        .then((config) => new Glance(config))
91
+        .then((glance) => glance.imageList(tokenPromise))
92
+        .then((images) => {
93
+          expect(images.length > 0).toBeTruthy();
94
+          done();
95
+        })
96
+        .catch((error) => done.fail(error));
97
+    });
98
+  });
99
+
82 100
 });

+ 41
- 0
test/unit/glanceTest.js View File

@@ -187,4 +187,45 @@ describe('Glance', () => {
187 187
         .catch((error) => done.fail(error));
188 188
     });
189 189
   });
190
+
191
+  describe("imageList()", () => {
192
+    let glance = null;
193
+
194
+    beforeEach(() => {
195
+      fetchMock.mock(mockData.root());
196
+      glance = new Glance(mockData.config);
197
+    });
198
+
199
+    it("should return the images as an array.", (done) => {
200
+      const token = 'test_token';
201
+
202
+      fetchMock.mock(mockData.imageList(token));
203
+      glance
204
+        .imageList(token)
205
+        .then((images) => {
206
+          expect(images.length).not.toBe(0);
207
+          done();
208
+        })
209
+        .catch((error) => done.fail(error));
210
+    });
211
+
212
+    it("Should not cache its results", (done) => {
213
+      const token = 'test_token';
214
+
215
+      let mockOptions = mockData.imageList(token);
216
+      fetchMock.mock(mockOptions);
217
+
218
+      glance
219
+        .imageList(token)
220
+        .then(() => {
221
+          expect(fetchMock.calls(mockOptions.name).length).toEqual(1);
222
+          return glance.imageList(token);
223
+        })
224
+        .then(() => {
225
+          expect(fetchMock.calls(mockOptions.name).length).toEqual(2);
226
+          done();
227
+        })
228
+        .catch((error) => done.fail(error));
229
+    });
230
+  });
190 231
 });

+ 81
- 1
test/unit/helpers/data/glance.js View File

@@ -108,7 +108,87 @@ function rootResponse () {
108 108
   };
109 109
 }
110 110
 
111
+function imageList (token) {
112
+  return {
113
+    method: 'GET',
114
+    matcher: 'http://192.168.99.99:9292/v2/images',
115
+    headers: {
116
+      'X-Auth-Token': token
117
+    },
118
+    response: {
119
+      images: [
120
+        {
121
+          status: 'active',
122
+          name: 'cirros-0.3.4-x86_64-uec',
123
+          tags: [],
124
+          kernel_id: '7c26de84-1ad7-4851-aea5-5c173d0605c8',
125
+          container_format: 'ami',
126
+          created_at: '2016-08-26T17:16:10Z',
127
+          ramdisk_id: '3ac21034-3764-407a-baab-966db753e3e5',
128
+          disk_format: 'ami',
129
+          updated_at: '2016-08-26T17:16:10Z',
130
+          visibility: 'public',
131
+          self: '/v2/images/8f3c7c9a-d812-46b1-9223-aa2d8f12c10a',
132
+          min_disk: 0,
133
+          protected: false,
134
+          id: '8f3c7c9a-d812-46b1-9223-aa2d8f12c10a',
135
+          size: 25165824,
136
+          file: '/v2/images/8f3c7c9a-d812-46b1-9223-aa2d8f12c10a/file',
137
+          checksum: 'eb9139e4942121f22bbc2afc0400b2a4',
138
+          owner: 'fcfe212681764e0595df8df83fd019f6',
139
+          virtual_size: null,
140
+          min_ram: 0,
141
+          schema: '/v2/schemas/image'
142
+        },
143
+        {
144
+          status: 'active',
145
+          name: 'cirros-0.3.4-x86_64-uec-ramdisk',
146
+          tags: [],
147
+          container_format: 'ari',
148
+          created_at: '2016-08-26T17:16:09Z',
149
+          size: 3740163,
150
+          disk_format: 'ari',
151
+          updated_at: '2016-08-26T17:16:09Z',
152
+          visibility: 'public',
153
+          self: '/v2/images/3ac21034-3764-407a-baab-966db753e3e5',
154
+          min_disk: 0,
155
+          protected: false,
156
+          id: '3ac21034-3764-407a-baab-966db753e3e5',
157
+          file: '/v2/images/3ac21034-3764-407a-baab-966db753e3e5/file',
158
+          checksum: 'be575a2b939972276ef675752936977f',
159
+          owner: 'fcfe212681764e0595df8df83fd019f6',
160
+          virtual_size: null,
161
+          min_ram: 0,
162
+          schema: '/v2/schemas/image'
163
+        },
164
+        {
165
+          status: 'active',
166
+          name: 'cirros-0.3.4-x86_64-uec-kernel',
167
+          tags: [],
168
+          container_format: 'aki',
169
+          created_at: '2016-08-26T17:16:08Z',
170
+          size: 4979632,
171
+          disk_format: 'aki',
172
+          updated_at: '2016-08-26T17:16:08Z',
173
+          visibility: 'public',
174
+          self: '/v2/images/7c26de84-1ad7-4851-aea5-5c173d0605c8',
175
+          min_disk: 0,
176
+          protected: false,
177
+          id: '7c26de84-1ad7-4851-aea5-5c173d0605c8',
178
+          file: '/v2/images/7c26de84-1ad7-4851-aea5-5c173d0605c8/file',
179
+          checksum: '8a40c862b5735975d82605c1dd395796',
180
+          owner: 'fcfe212681764e0595df8df83fd019f6',
181
+          virtual_size: null,
182
+          min_ram: 0,
183
+          schema: '/v2/schemas/image'
184
+        }],
185
+      schema: '/v2/schemas/images',
186
+      first: '/v2/images'
187
+    }
188
+  };
189
+}
111 190
 export {
112 191
   glanceConfig as config,
113
-  rootResponse as root
192
+  rootResponse as root,
193
+  imageList
114 194
 };

Loading…
Cancel
Save