Browse Source

OpenStack wrapper instance

Create wrapper class that takes clouds.yaml instance

Change-Id: I37429b7bda35ca09efc3bf153841d4dc92d96b55
tags/0.0.1
Dong Ma 2 years ago
parent
commit
9677a06a2d
3 changed files with 92 additions and 0 deletions
  1. 23
    0
      src/index.js
  2. 37
    0
      test/unit/helpers/data/openstack.js
  3. 32
    0
      test/unit/indexTest.js

+ 23
- 0
src/index.js View File

@@ -1,3 +1,26 @@
1 1
 export {default as Keystone} from './keystone';
2 2
 export {default as Glance} from './glance';
3 3
 export {default as Neutron} from './neutron';
4
+
5
+export default class OpenStack {
6
+  /**
7
+   * Create wrapper class that takes clouds.yaml instance
8
+   *
9
+   * @param {{}} cloudConfig The configuration object for a specific cloud.
10
+   */
11
+  constructor(cloudConfig) {
12
+    // Sanity checks.
13
+    if (!cloudConfig) {
14
+      throw new Error('A configuration is required.');
15
+    }
16
+    // Clone the config, so that this instance is immutable
17
+    // at runtime (no modifying the config after the fact).
18
+    cloudConfig = Object.assign({}, cloudConfig);
19
+
20
+    this.cloudConfig = cloudConfig;
21
+  }
22
+  getConfig() {
23
+    // Returns the config instance
24
+    return this.cloudConfig;
25
+  }
26
+}

+ 37
- 0
test/unit/helpers/data/openstack.js View File

@@ -0,0 +1,37 @@
1
+/*
2
+ * Copyright (c) 2016 Hewlett Packard Enterprise Development L.P.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5
+ * use this file except in compliance with the License. You may obtain a copy
6
+ * of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
13
+ * the License for the specific language governing permissions and limitations
14
+ * under the License.
15
+ */
16
+
17
+/**
18
+ * This file contains test data for fetchMock, to simplify bootstrapping of unit tests for
19
+ * openstack class.
20
+ */
21
+
22
+/**
23
+ * Mock cloud configuration that matches our test data below. This is not a full clouds.yaml
24
+ * format, rather just the subsection pointing to a particular cloud.
25
+ */
26
+const cloudConfig = {
27
+  region_name: 'Region1',
28
+  auth: {
29
+    username: 'user',
30
+    password: 'pass',
31
+    project_name: 'js-openstack-lib',
32
+    auth_url: 'http://192.168.99.99/'
33
+  }
34
+};
35
+export {
36
+  cloudConfig as config,
37
+};

+ 32
- 0
test/unit/indexTest.js View File

@@ -0,0 +1,32 @@
1
+import OpenStack from "../../src/index";
2
+import * as mockData from './helpers/data/openstack';
3
+
4
+const FetchMock = require('fetch-mock');
5
+
6
+describe("Simple test", () => {
7
+
8
+  afterEach(() => {
9
+    FetchMock.reset();
10
+  });
11
+
12
+  it("should export a class", () => {
13
+    let t = new OpenStack(mockData.config);
14
+    expect(t).toBeDefined();
15
+  });
16
+
17
+  it("should throw an error for an empty config", () => {
18
+    try {
19
+      let t = new OpenStack();
20
+      t.getConfig();
21
+    } catch (e) {
22
+      expect(e.message).toEqual('A configuration is required.');
23
+    }
24
+  });
25
+
26
+  it("getConfig should returns the config", () => {
27
+    let openstack = new OpenStack(mockData.config);
28
+    let config = openstack.getConfig();
29
+    expect(config.region_name).toEqual('Region1');
30
+  });
31
+
32
+});

Loading…
Cancel
Save