Skip to content

Commit

Permalink
fix(amazon): preserve existing security groups on ELB edit
Browse files Browse the repository at this point in the history
  • Loading branch information
anotherchrisberry committed May 2, 2017
1 parent bca9b22 commit b4e77ac
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -159,14 +159,20 @@ module.exports = angular.module('spinnaker.loadBalancer.aws.create.controller',
}
}

function availableGroupsSorter(a, b) {
return $scope.loadBalancer.securityGroups.includes(a.id) ? -1 :
$scope.loadBalancer.securityGroups.includes(b.id) ? 1 :
0;
}

function updateAvailableSecurityGroups(availableVpcIds) {
var account = $scope.loadBalancer.credentials,
region = $scope.loadBalancer.region;

if (account && region && allSecurityGroups[account] && allSecurityGroups[account].aws[region]) {
$scope.availableSecurityGroups = _.filter(allSecurityGroups[account].aws[region], function(securityGroup) {
return availableVpcIds.includes(securityGroup.vpcId);
});
}).sort(availableGroupsSorter); // push existing groups to top
$scope.existingSecurityGroupNames = _.map($scope.availableSecurityGroups, 'name');
var existingNames = defaultSecurityGroups.filter(function(defaultName) {
return $scope.existingSecurityGroupNames.includes(defaultName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,29 @@ describe('Controller: awsCreateLoadBalancerCtrl', function () {
);

// Initialize the controller and a mock scope
beforeEach(window.inject(function ($controller, $rootScope, applicationModelBuilder) {
beforeEach(window.inject(function ($controller, $rootScope, $q, accountService, subnetReader, applicationModelBuilder, securityGroupReader, awsLoadBalancerTransformer) {
this.$scope = $rootScope.$new();
this.securityGroupReader = securityGroupReader;
this.accountService = accountService;
this.subnetReader = subnetReader;
this.$q = $q;
const app = applicationModelBuilder.createApplication({key: 'loadBalancers', lazy: true});
this.initialize = () => {
this.initialize = (loadBalancer = null) => {
if (loadBalancer) {
spyOn(awsLoadBalancerTransformer, 'convertLoadBalancerForEditing').and.returnValue(loadBalancer);
}
this.ctrl = $controller('awsCreateLoadBalancerCtrl', {
$scope: this.$scope,
$uibModalInstance: {dismiss: angular.noop, result: {then: angular.noop}},
infrastructureCaches: { get: () => { return {getStats: () => {return {}; } }; } },
application: app,
loadBalancer: null,
isNew: true,
forPipelineConfig: false
loadBalancer: loadBalancer,
isNew: loadBalancer === null,
forPipelineConfig: false,
securityGroupReader: securityGroupReader,
accountService: accountService,
subnetReader: subnetReader,
awsLoadBalancerTransformer: awsLoadBalancerTransformer,
});
};
}));
Expand Down Expand Up @@ -182,6 +193,34 @@ describe('Controller: awsCreateLoadBalancerCtrl', function () {
expect(this.$scope.loadBalancer.isInternal).toBe(false);
expect(this.$scope.state.hideInternalFlag).toBeUndefined();
});

it('should put existing security groups in the front of the available list', function () {
const availableSecurityGroups = {
test: {
aws: {
'us-east-1': [
{name: 'a', id: '1', vpcId: 'vpc-1'},
{name: 'b', id: '2', vpcId: 'vpc-1'},
{name: 'c', id: '3', vpcId: 'vpc-1'},
{name: 'd', id: '4', vpcId: 'vpc-1'}]
}
}
};
const existingLoadBalancer = {
name: 'elb-1',
vpcId: 'vpc-1',
credentials: 'test',
region: 'us-east-1',
securityGroups: ['4'],
listeners: [],
};
spyOn(this.securityGroupReader, 'getAllSecurityGroups').and.returnValue(this.$q.when(availableSecurityGroups));
spyOn(this.accountService, 'getAccountDetails').and.returnValue(this.$q.when([{name: 'test'}]));
spyOn(this.subnetReader, 'listSubnets').and.returnValue(this.$q.when([{account: 'test', region: 'us-east-1', vpcIds: ['vpc-1']}]));
this.initialize(existingLoadBalancer);
this.$scope.$digest();
expect(this.$scope.availableSecurityGroups.map(g => g.name)).toEqual(['d', 'a', 'b', 'c']);
});
});

});

0 comments on commit b4e77ac

Please sign in to comment.