From acbc25ce998dbae3d4ddb71d1120bc270f76147c Mon Sep 17 00:00:00 2001 From: Michael Krotscheck Date: Tue, 30 Aug 2016 13:11:40 -0700 Subject: [PATCH] Created common service abstraction This patch creates a common class named AbstractService from which all API classes can inherit common logic. Change-Id: I18339ac19ed2b97a7b9b3d6a0f1e01292080c422 --- src/glance.js | 7 ++++--- src/keystone.js | 7 ++++--- src/util/abstract_service.js | 37 ++++++++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 6 deletions(-) create mode 100644 src/util/abstract_service.js diff --git a/src/glance.js b/src/glance.js index 1d42002..53c4300 100644 --- a/src/glance.js +++ b/src/glance.js @@ -13,7 +13,7 @@ * the License for the specific language governing permissions and limitations * under the License. */ -import Http from './util/http'; +import AbstractService from './util/abstract_service'; /** * A list of all supported versions. Please keep this array sorted by most recent. @@ -25,7 +25,7 @@ const supportedGlanceVersions = [ 'v2.3' ]; -export default class Glance { +export default class Glance extends AbstractService { /** * This class provides direct, idempotent, low-level access to the Glance API of a specific @@ -43,6 +43,8 @@ export default class Glance { * @param {{}} endpointConfig The configuration element for a specific glance endpoint. */ constructor (endpointConfig) { + super(); + // Sanity checks. if (!endpointConfig || !endpointConfig.url) { throw new Error('An endpoint configuration is required.'); @@ -50,7 +52,6 @@ export default class Glance { // Clone the config, so that this instance is immutable // at runtime (no modifying the config after the fact). this._config = Object.assign({}, endpointConfig); - this.http = new Http(); } /** diff --git a/src/keystone.js b/src/keystone.js index f5ec530..fd774c3 100644 --- a/src/keystone.js +++ b/src/keystone.js @@ -1,4 +1,4 @@ -import Http from './util/http'; +import AbstractService from './util/abstract_service'; /** * A list of all supported versions. Please keep this array sorted by most recent. @@ -10,7 +10,7 @@ const supportedKeystoneVersions = [ 'v3.7' ]; -export default class Keystone { +export default class Keystone extends AbstractService { /** * This class provides direct, idempotent, low-level access to the Keystone API of a specific @@ -23,6 +23,8 @@ export default class Keystone { * @see http://docs.openstack.org/developer/os-client-config/#site-specific-file-locations */ constructor (cloudConfig) { + super(); + // Sanity checks. if (!cloudConfig) { throw new Error('A configuration is required.'); @@ -30,7 +32,6 @@ export default class Keystone { // Clone the config, so that this instance is immutable // at runtime (no modifying the config after the fact). this.cloudConfig = Object.assign({}, cloudConfig); - this.http = new Http(); } /** diff --git a/src/util/abstract_service.js b/src/util/abstract_service.js new file mode 100644 index 0000000..11ba0d3 --- /dev/null +++ b/src/util/abstract_service.js @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2016 Hewlett Packard Enterprise Development L.P. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy + * of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See + * the License for the specific language governing permissions and limitations + * under the License. + */ + +import Http from './http'; + +/** + * An abstract implementation of our services, which includes logic common to all of our services. + * + * @author Michael Krotscheck + */ +export default class AbstractService { + + /** + * Our HTTP service instance. + * + * @returns {Http} Our HTTP service instance. + */ + get http () { + if (!this._http) { + this._http = new Http(); + } + return this._http; + } +}