Use the JP Style Guide for Nova Service

Splits the nova.service.js into multiple files
as based off the functions they provide.

Change-Id: Ic5713c573162598bff75d245ec5c1fe3396da9c2
Implements: blueprint john-papa-style-guide
This commit is contained in:
Rajat Vig 2015-08-11 16:57:36 -07:00
parent 5796aa97a7
commit a51855707e
4 changed files with 189 additions and 139 deletions

View File

@ -0,0 +1,100 @@
/*
* (c) Copyright 2015 Hewlett-Packard Development Company, 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.
*/
(function () {
'use strict';
angular
.module('horizon.app.core.openstack-service-api')
.factory('horizon.app.core.openstack-service-api.novaExtensions', NovaExtensionsAPI);
NovaExtensionsAPI.$inject = [
'$cacheFactory',
'$q',
'horizon.app.core.openstack-service-api.nova'
];
/**
* @ngdoc service
* @name horizon.app.core.openstack-service-api.novaExtensions
* @description
* Provides cached access to Nova Extensions with utilities to help
* with asynchronous data loading. The cache may be reset at any time
* by accessing the cache and calling removeAll. The next call to any
* function will retrieve fresh results.
*
* The enabled extensions do not change often, so using cached data will
* speed up results. Even on a local devstack in informal testing,
* this saved between 30 - 100 ms per request.
*/
function NovaExtensionsAPI($cacheFactory, $q, novaAPI) {
var service = {
cache: $cacheFactory(
'horizon.app.core.openstack-service-api.novaExtensions',
{capacity: 1}
),
get: get,
ifNameEnabled: ifNameEnabled,
// This is an alias to support the extension directive default interface
ifEnabled: ifNameEnabled
};
return service;
///////////
function get() {
return novaAPI.getExtensions({cache: service.cache}).then(onGetExtensions);
}
function onGetExtensions(data) {
return data.data.items;
}
function ifNameEnabled(desired) {
var deferred = $q.defer();
service.get().then(onDataLoaded, onDataFailure);
function onDataLoaded(extensions) {
if (enabled(extensions, 'name', desired)) {
deferred.resolve();
} else {
deferred.reject(interpolate(
gettext('Extension is not enabled: %(extension)s.'),
{extension: desired},
true));
}
}
function onDataFailure() {
deferred.reject(gettext('Cannot get the Nova extension list.'));
}
return deferred.promise;
}
function enabled(resources, key, desired) {
if (resources) {
return resources.some(function matchResource(resource) {
return resource[key] === desired;
});
} else {
return false;
}
}
}
}());

View File

@ -0,0 +1,88 @@
/*
* (c) Copyright 2015 Hewlett-Packard Development Company, 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.
*/
(function() {
'use strict';
describe("novaExtensions", function() {
var factory, q, novaAPI;
beforeEach(module('horizon.app.core.openstack-service-api'));
beforeEach(module(function($provide) {
novaAPI = {getExtensions: function() {return {then: angular.noop};}};
q = {defer: function() { return {resolve: angular.noop}; }};
$provide.value('$cacheFactory', function() {return "cache";});
$provide.value('$q', q);
$provide.value('horizon.app.core.openstack-service-api.nova', novaAPI);
}));
beforeEach(inject(function($injector) {
factory = $injector.get('horizon.app.core.openstack-service-api.novaExtensions');
}));
it("is defined", function() {
expect(factory).toBeDefined();
});
it("defines .cache", function() {
expect(factory.cache).toBeDefined();
});
it("defines .get", function() {
expect(factory.get).toBeDefined();
var postAction = {then: angular.noop};
spyOn(novaAPI, 'getExtensions').and.returnValue(postAction);
spyOn(postAction, 'then');
factory.get();
expect(novaAPI.getExtensions).toHaveBeenCalledWith({cache: factory.cache});
expect(postAction.then).toHaveBeenCalled();
var func = postAction.then.calls.argsFor(0)[0];
var testData = {data: {items: [1, 2, 3]}};
expect(func(testData)).toEqual([1, 2, 3]);
});
it("defines .ifNameEnabled", function() {
expect(factory.ifNameEnabled).toBeDefined();
var postAction = {then: angular.noop};
var deferred = {reject: angular.noop, resolve: angular.noop};
spyOn(q, 'defer').and.returnValue(deferred);
spyOn(factory, 'get').and.returnValue(postAction);
spyOn(postAction, 'then');
factory.ifNameEnabled("desired");
expect(factory.get).toHaveBeenCalled();
var func1 = postAction.then.calls.argsFor(0)[0];
var func2 = postAction.then.calls.argsFor(0)[1];
spyOn(deferred, 'reject');
func1();
expect(deferred.reject).toHaveBeenCalled();
spyOn(deferred, 'resolve');
var extensions = [{name: "desired"}];
func1(extensions);
expect(deferred.resolve).toHaveBeenCalled();
deferred.reject.calls.reset();
func2();
expect(deferred.reject).toHaveBeenCalledWith('Cannot get the Nova extension list.');
});
it("defines .ifEnabled", function() {
expect(factory.ifEnabled).toBeDefined();
});
});
})();

View File

@ -277,74 +277,4 @@ limitations under the License.
};
}
/**
* @ngdoc service
* @name horizon.app.core.openstack-service-api.novaExtensions
* @description
* Provides cached access to Nova Extensions with utilities to help
* with asynchronous data loading. The cache may be reset at any time
* by accessing the cache and calling removeAll. The next call to any
* function will retrieve fresh results.
*
* The enabled extensions do not change often, so using cached data will
* speed up results. Even on a local devstack in informal testing,
* this saved between 30 - 100 ms per request.
*/
angular
.module('horizon.app.core.openstack-service-api')
.factory('horizon.app.core.openstack-service-api.novaExtensions', NovaExtensionsAPI);
NovaExtensionsAPI.$inject = ['$cacheFactory',
'$q',
'horizon.app.core.openstack-service-api.nova'];
function NovaExtensionsAPI($cacheFactory, $q, novaAPI) {
var service = {};
service.cache = $cacheFactory('horizon.app.core.openstack-service-api.novaExtensions', {capacity: 1});
service.get = function () {
return novaAPI.getExtensions({cache: service.cache})
.then(function (data) {
return data.data.items;
});
};
service.ifNameEnabled = function(desired) {
var deferred = $q.defer();
service.get().then(onDataLoaded, onDataFailure);
function onDataLoaded(extensions) {
if (enabled(extensions, 'name', desired)) {
deferred.resolve();
} else {
deferred.reject(interpolate(
gettext('Extension is not enabled: %(extension)s.'),
{extension: desired},
true));
}
}
function onDataFailure() {
deferred.reject(gettext('Cannot get the Nova extension list.'));
}
return deferred.promise;
};
// This is an alias to support the extension directive default interface
service.ifEnabled = service.ifNameEnabled;
function enabled(resources, key, desired) {
if (resources) {
return resources.some(function (resource) {
return resource[key] === desired;
});
} else {
return false;
}
}
return service;
}
}());

View File

@ -1,5 +1,5 @@
/*
* (c) Copyright 2015 Hewlett-Packard Development Company, L.P.
* (c) Copyright 2015 Hewlett-Packard Development Company, L.P.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -247,72 +247,4 @@
});
});
describe("novaExtensions", function() {
var factory, q, novaAPI;
beforeEach(module('horizon.app.core.openstack-service-api'));
beforeEach(module(function($provide) {
novaAPI = {getExtensions: function() {return {then: angular.noop};}};
q = {defer: function() { return {resolve: angular.noop}; }};
$provide.value('$cacheFactory', function() {return "cache";});
$provide.value('$q', q);
$provide.value('horizon.app.core.openstack-service-api.nova', novaAPI);
}));
beforeEach(inject(function($injector) {
factory = $injector.get('horizon.app.core.openstack-service-api.novaExtensions');
}));
it("is defined", function() {
expect(factory).toBeDefined();
});
it("defines .cache", function() {
expect(factory.cache).toBeDefined();
});
it("defines .get", function() {
expect(factory.get).toBeDefined();
var postAction = {then: angular.noop};
spyOn(novaAPI, 'getExtensions').and.returnValue(postAction);
spyOn(postAction, 'then');
factory.get();
expect(novaAPI.getExtensions).toHaveBeenCalledWith({cache: factory.cache});
expect(postAction.then).toHaveBeenCalled();
var func = postAction.then.calls.argsFor(0)[0];
var testData = {data: {items: [1, 2, 3]}};
expect(func(testData)).toEqual([1, 2, 3]);
});
it("defines .ifNameEnabled", function() {
expect(factory.ifNameEnabled).toBeDefined();
var postAction = {then: angular.noop};
var deferred = {reject: angular.noop, resolve: angular.noop};
spyOn(q, 'defer').and.returnValue(deferred);
spyOn(factory, 'get').and.returnValue(postAction);
spyOn(postAction, 'then');
factory.ifNameEnabled("desired");
expect(factory.get).toHaveBeenCalled();
var func1 = postAction.then.calls.argsFor(0)[0];
var func2 = postAction.then.calls.argsFor(0)[1];
spyOn(deferred, 'reject');
func1();
expect(deferred.reject).toHaveBeenCalled();
spyOn(deferred, 'resolve');
var extensions = [{name: "desired"}];
func1(extensions);
expect(deferred.resolve).toHaveBeenCalled();
deferred.reject.calls.reset();
func2();
expect(deferred.reject).toHaveBeenCalledWith('Cannot get the Nova extension list.');
});
it("defines .ifEnabled", function() {
expect(factory.ifEnabled).toBeDefined();
});
});
})();