Skip to content

Commit

Permalink
JIRA-1888: Remove dependency on Restangular. (spinnaker#2458)
Browse files Browse the repository at this point in the history
Added an API service (core/api/api.service.js) which mimics the fluid API style of Restangular.

removing calls to the "plain()" function call and fixing tests
  • Loading branch information
zanthrash authored Jul 27, 2016
1 parent 9af19b8 commit ad420c5
Show file tree
Hide file tree
Showing 94 changed files with 825 additions and 413 deletions.
15 changes: 11 additions & 4 deletions app/scripts/modules/amazon/image/image.reader.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
let angular = require('angular');

module.exports = angular.module('spinnaker.aws.image.reader', [
require('exports?"restangular"!imports?_=lodash!restangular'),
require('../../core/api/api.service')
])
.factory('awsImageReader', function ($q, Restangular) {
.factory('awsImageReader', function ($q, API) {

function findImages(params) {
if (!params.q || params.q.length < 3) {
return $q.when([{message: 'Please enter at least 3 characters...'}]);
}
return Restangular.all('images/find').getList(params, {})
return API.one('images/find').withParams(params).get()
.then(function(results) {
return results;
},
Expand All @@ -21,7 +21,14 @@ module.exports = angular.module('spinnaker.aws.image.reader', [
}

function getImage(amiName, region, credentials) {
return Restangular.all('images').one(credentials).one(region).all(amiName).getList({provider: 'aws'}).then(function(results) {
return API
.one('images')
.one(credentials)
.one(region)
.one(amiName)
.withParams({provider: 'aws'})
.get()
.then(function(results) {
return results && results.length ? results[0] : null;
},
function() {
Expand Down
13 changes: 7 additions & 6 deletions app/scripts/modules/amazon/image/image.reader.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,18 @@

describe('Service: aws Image Reader', function() {

var service, $http, scope;
var service, $http, scope, API;

beforeEach(
window.module(
require('./image.reader.js')
require('./image.reader.js'),
require('../../core/api/api.service')
)
);


beforeEach(window.inject(function (awsImageReader, $httpBackend, $rootScope) {

beforeEach(window.inject(function (awsImageReader, $httpBackend, $rootScope, _API_) {
API = _API_;
service = awsImageReader;
$http = $httpBackend;
scope = $rootScope.$new();
Expand All @@ -45,7 +46,7 @@ describe('Service: aws Image Reader', function() {
var query = 'abc', region = 'us-west-1';

function buildQueryString() {
return '/images/find?provider=aws&q=' + query + '&region=' + region;
return API.baseUrl + '/images/find?provider=aws&q=' + query + '&region=' + region;
}

it('queries gate when 3 characters are supplied', function() {
Expand Down Expand Up @@ -122,7 +123,7 @@ describe('Service: aws Image Reader', function() {
var imageName = 'abc', region = 'us-west-1', credentials = 'test';

function buildQueryString() {
return ['/images', credentials, region, imageName].join('/') + '?provider=aws';
return [API.baseUrl, 'images', credentials, region, imageName].join('/') + '?provider=aws';
}

it('returns null if server returns 404 or an empty list', function() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
let angular = require('angular');

module.exports = angular.module('spinnaker.aws.instanceType.service', [
require('exports?"restangular"!imports?_=lodash!restangular'),
require('../../core/api/api.service'),
require('../../core/cache/deckCacheFactory.js'),
require('../../core/utils/lodash.js'),
require('../../core/config/settings.js'),
require('../../core/cache/infrastructureCaches.js'),
])
.factory('awsInstanceTypeService', function ($http, $q, settings, _, Restangular, infrastructureCaches) {
.factory('awsInstanceTypeService', function ($http, $q, settings, _, API, infrastructureCaches) {

var m3 = {
type: 'm3',
Expand Down Expand Up @@ -199,8 +199,8 @@ module.exports = angular.module('spinnaker.aws.instanceType.service', [
if (cached) {
return $q.when(cached);
}
return Restangular.all('instanceTypes')
.getList().then(function (types) {
return API.one('instanceTypes').get()
.then(function (types) {
var result = _(types)
.map(function (type) {
return { region: type.region, account: type.account, name: type.name, key: [type.region, type.account, type.name].join(':') };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,18 @@

describe('Service: InstanceType', function () {

let API;

beforeEach(function() {
window.module(
require('./awsInstanceType.service')
require('./awsInstanceType.service'),
require('../../core/api/api.service')
);
});


beforeEach(window.inject(function (_awsInstanceTypeService_, _$httpBackend_, _settings_, infrastructureCaches) {

beforeEach(window.inject(function (_awsInstanceTypeService_, _$httpBackend_, _settings_, _API_, infrastructureCaches) {
API = _API_;
this.awsInstanceTypeService = _awsInstanceTypeService_;
this.$httpBackend = _$httpBackend_;
this.settings = _settings_;
Expand All @@ -52,7 +55,7 @@ describe('Service: InstanceType', function () {

it('returns types, indexed by region', function () {

this.$httpBackend.expectGET('/instanceTypes').respond(200, this.allTypes);
this.$httpBackend.expectGET( API.baseUrl + '/instanceTypes').respond(200, this.allTypes);

var results = null;
this.awsInstanceTypeService.getAllTypesByRegion().then(function(result) {
Expand All @@ -69,7 +72,7 @@ describe('Service: InstanceType', function () {
describe('getAvailableTypesForRegions', function() {

it('returns results for a single region', function() {
this.$httpBackend.expectGET('/instanceTypes').respond(200, this.allTypes);
this.$httpBackend.expectGET(API.baseUrl + '/instanceTypes').respond(200, this.allTypes);

var results = null,
service = this.awsInstanceTypeService;
Expand All @@ -83,7 +86,7 @@ describe('Service: InstanceType', function () {
});

it('returns empty list for region with no instance types', function() {
this.$httpBackend.expectGET('/instanceTypes').respond(200, this.allTypes);
this.$httpBackend.expectGET(API.baseUrl + '/instanceTypes').respond(200, this.allTypes);

var results = null,
service = this.awsInstanceTypeService;
Expand All @@ -97,7 +100,7 @@ describe('Service: InstanceType', function () {
});

it('returns an intersection when multiple regions are provided', function() {
this.$httpBackend.expectGET('/instanceTypes').respond(200, this.allTypes);
this.$httpBackend.expectGET(API.baseUrl + '/instanceTypes').respond(200, this.allTypes);

var results = null,
service = this.awsInstanceTypeService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ module.exports = angular.module('spinnaker.instance.detail.aws.controller', [
if ($scope.$$destroyed) {
return;
}
details = details.plain();
$scope.state.loading = false;
extractHealthMetrics(instanceSummary, details);
$scope.instance = _.defaults(details, instanceSummary);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,7 @@ describe('Controller: awsInstanceDetailsCtrl', function () {
};

spyOn(instanceReader, 'getInstanceDetails').and.returnValue(
$q.when({
plain: function() {
return details;
}
})
$q.when(details)
);
var application = { attributes: {} };

Expand Down
10 changes: 5 additions & 5 deletions app/scripts/modules/amazon/keyPairs/keyPairs.read.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ let angular = require('angular');

module.exports = angular
.module('spinnaker.keyPairs.read.service', [
require('exports?"restangular"!imports?_=lodash!restangular'),
require('../../core/api/api.service')
])
.factory('keyPairsReader', function ($q, Restangular) {
.factory('keyPairsReader', function ($q, API) {

function listKeyPairs() {
return Restangular.all('keyPairs')
.withHttpConfig({cache: true})
.getList()
return API.one('keyPairs')
.useCache()
.get()
.then(keyPairs => keyPairs.sort((a, b) => a.keyName.localeCompare(b.keyName)));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ module.exports = angular.module('spinnaker.securityGroup.aws.details.controller'
return securityGroupReader.getSecurityGroupDetails(application, securityGroup.accountId, securityGroup.provider, securityGroup.region, securityGroup.vpcId, securityGroup.name).then(function (details) {
$scope.state.loading = false;

if (!details || _.isEmpty( details.plain())) {
if (!details || _.isEmpty( details )) {
fourOhFour();
} else {
$scope.securityGroup = details;
Expand Down Expand Up @@ -67,7 +67,7 @@ module.exports = angular.module('spinnaker.securityGroup.aws.details.controller'
size: 'lg',
resolve: {
securityGroup: function() {
return angular.copy($scope.securityGroup.plain());
return angular.copy($scope.securityGroup);
},
application: function() { return application; }
}
Expand All @@ -82,7 +82,7 @@ module.exports = angular.module('spinnaker.securityGroup.aws.details.controller'
size: 'lg',
resolve: {
securityGroup: function() {
var securityGroup = angular.copy($scope.securityGroup.plain());
var securityGroup = angular.copy($scope.securityGroup);
if(securityGroup.region) {
securityGroup.regions = [securityGroup.region];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
let angular = require('angular');

module.exports = angular.module('spinnaker.aws.serverGroupCommandBuilder.service', [
require('exports?"restangular"!imports?_=lodash!restangular'),
require('../../../core/account/account.service.js'),
require('../../../core/subnet/subnet.read.service.js'),
require('../../../core/instance/instanceTypeService.js'),
require('../../../core/naming/naming.service.js'),
require('./serverGroupConfiguration.service.js'),
require('../../../core/utils/lodash.js'),
])
.factory('awsServerGroupCommandBuilder', function (settings, Restangular, $q,
.factory('awsServerGroupCommandBuilder', function (settings, $q,
accountService, subnetReader, namingService, instanceTypeService,
awsServerGroupConfigurationService, _) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ module.exports = angular.module('spinnaker.serverGroup.details.aws.controller',
.then((details) => {
cancelLoader();

var plainDetails = details.plain();
var plainDetails = details;
angular.extend(plainDetails, summary);
// it's possible the summary was not found because the clusters are still loading
plainDetails.account = serverGroup.accountId;
Expand Down
2 changes: 1 addition & 1 deletion app/scripts/modules/amazon/vpc/vpc.read.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ module.exports = angular
if (vpc.deprecated) {
vpc.label += ' (deprecated)';
}
return vpc.plain();
return vpc;
});
cachedVpcs = results;
return results;
Expand Down
10 changes: 6 additions & 4 deletions app/scripts/modules/amazon/vpc/vpc.read.service.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@

describe('vpcReader', function() {

var service, $http, $scope;
var service, $http, $scope, API;

beforeEach(
window.module(
require('./vpc.read.service.js')
require('./vpc.read.service.js'),
require('../../core/api/api.service')
)
);

beforeEach(window.inject(function ($httpBackend, $rootScope, _vpcReader_) {
beforeEach(window.inject(function ($httpBackend, $rootScope, _vpcReader_, _API_) {
API = _API_;
service = _vpcReader_;
$http = $httpBackend;
$scope = $rootScope.$new();
Expand All @@ -21,7 +23,7 @@ describe('vpcReader', function() {
});

beforeEach(function() {
$http.whenGET('/networks/aws').respond(200, [
$http.whenGET(API.baseUrl + '/networks/aws').respond(200, [
{ name: 'vpc1', id: 'vpc-1', deprecated: true },
{ name: 'vpc2', id: 'vpc-2', deprecated: false },
{ name: 'vpc3', id: 'vpc-3' },
Expand Down
15 changes: 11 additions & 4 deletions app/scripts/modules/azure/image/image.reader.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
let angular = require('angular');

module.exports = angular.module('spinnaker.azure.image.reader', [
require('exports?"restangular"!imports?_=lodash!restangular'),
require('../../core/api/api.service')
])
.factory('azureImageReader', function ($q, Restangular) {
.factory('azureImageReader', function ($q, API) {

function findImages(params) {
return Restangular.all('images/find').getList(params, {})
return API.one('images/find').get(params)
.then(function(results) {
return results;
},
Expand All @@ -18,7 +18,14 @@ module.exports = angular.module('spinnaker.azure.image.reader', [
}

function getImage(amiName, region, credentials) {
return Restangular.all('images').one(credentials).one(region).all(amiName).getList({provider: 'azure'}).then(function(results) {
return API
.one('images')
.one(credentials)
.one(region)
.one(amiName)
.withParams({provider: 'azure'})
.get()
.then(function(results) {
return results && results.length ? results[0] : null;
},
function() {
Expand Down
14 changes: 7 additions & 7 deletions app/scripts/modules/azure/image/image.reader.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,20 @@

describe('Service: Azure Image Reader', function() {

var service, $http;
var service, $http, API;

beforeEach(
window.module(
require('./image.reader.js')
require('./image.reader.js'),
require('../../core/api/api.service')
)
);


beforeEach(window.inject(function (azureImageReader, $httpBackend) {

beforeEach(window.inject(function (azureImageReader, $httpBackend, _API_) {
API = _API_;
service = azureImageReader;
$http = $httpBackend;

}));

afterEach(function() {
Expand All @@ -44,7 +44,7 @@ describe('Service: Azure Image Reader', function() {
var query = 'abc', region = 'usw';

function buildQueryString() {
return '/images/find?provider=azure&q=' + query + '&region=' + region;
return API.baseUrl + '/images/find?provider=azure&q=' + query + '&region=' + region;
}

it('queries gate when 3 characters are supplied', function() {
Expand Down Expand Up @@ -106,7 +106,7 @@ describe('Service: Azure Image Reader', function() {
var imageName = 'abc', region = 'usw', credentials = 'test';

function buildQueryString() {
return ['/images', credentials, region, imageName].join('/') + '?provider=azure';
return [API.baseUrl, 'images', credentials, region, imageName].join('/') + '?provider=azure';
}

it('returns null if server returns 404 or an empty list', function() {
Expand Down
10 changes: 6 additions & 4 deletions app/scripts/modules/azure/instance/azureInstanceType.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
let angular = require('angular');

module.exports = angular.module('spinnaker.azure.instanceType.service', [
require('exports?"restangular"!imports?_=lodash!restangular'),
require('../../core/api/api.service'),
require('../../core/cache/deckCacheFactory.js'),
require('../../core/utils/lodash.js'),
require('../../core/config/settings.js'),
require('../../core/cache/infrastructureCaches.js'),
])
.factory('azureInstanceTypeService', function ($http, $q, settings, _, Restangular, infrastructureCaches) {
.factory('azureInstanceTypeService', function ($http, $q, settings, _, API, infrastructureCaches) {

var m3 = {
type: 'M3',
Expand Down Expand Up @@ -260,8 +260,10 @@ module.exports = angular.module('spinnaker.azure.instanceType.service', [
if (cached) {
return $q.when(cached);
}
return Restangular.all('instanceTypes')
.getList().then(function (types) {
return API
.one('instanceTypes')
.get()
.then(function (types) {
var result = _(types)
.map(function (type) {
return { region: type.region, account: type.account, name: type.name, key: [type.region, type.account, type.name].join(':') };
Expand Down
Loading

0 comments on commit ad420c5

Please sign in to comment.