Skip to content

Commit

Permalink
feat(provider/azure): Add support for instance type in server group (s…
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeljqzq authored and Scott committed Mar 5, 2019
1 parent eae6b45 commit 4febe1a
Show file tree
Hide file tree
Showing 8 changed files with 978 additions and 412 deletions.
804 changes: 664 additions & 140 deletions app/scripts/modules/azure/instance/azureInstanceType.service.js

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -8,84 +8,12 @@ describe('Service: InstanceType', function() {
});

beforeEach(
window.inject(function(_azureInstanceTypeService_, _$httpBackend_) {
window.inject(function(_azureInstanceTypeService_) {
this.azureInstanceTypeService = _azureInstanceTypeService_;
this.$httpBackend = _$httpBackend_;

this.allTypes = [
{ account: 'test', region: 'us-west-2', name: 'm1.small', availabilityZone: 'us-west-2a' },
{ account: 'test', region: 'us-west-2', name: 'm2.xlarge', availabilityZone: 'us-west-2b' },
{ account: 'test', region: 'eu-west-1', name: 'hs1.8xlarge', availabilityZone: 'eu-west-1c' },
{ account: 'test', region: 'eu-west-1', name: 'm2.xlarge', availabilityZone: 'eu-west-1c' },
];

InfrastructureCaches.createCache('instanceTypes', {});
if (InfrastructureCaches.get('instanceTypes')) {
InfrastructureCaches.get('instanceTypes').removeAll();
}
}),
);

afterEach(function() {
this.$httpBackend.verifyNoOutstandingRequest();
});

describe('getAllTypesByRegion', function() {
it('returns types, indexed by region', function() {
this.$httpBackend.expectGET(API.baseUrl + '/instanceTypes').respond(200, this.allTypes);

var results = null;
this.azureInstanceTypeService.getAllTypesByRegion().then(function(result) {
results = result;
});

this.$httpBackend.flush();
expect(results['us-west-2'].length).toBe(2);
expect(_.map(results['us-west-2'], 'name').sort()).toEqual(['m1.small', 'm2.xlarge']);
});
});

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

var results = null,
service = this.azureInstanceTypeService;

this.azureInstanceTypeService.getAllTypesByRegion().then(function(result) {
results = service.getAvailableTypesForRegions(result, ['us-west-2']);
});

this.$httpBackend.flush();
expect(results).toEqual(['m1.small', 'm2.xlarge']);
});

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

var results = null,
service = this.azureInstanceTypeService;

this.azureInstanceTypeService.getAllTypesByRegion().then(function(result) {
results = service.getAvailableTypesForRegions(result, ['us-west-3']);
});

this.$httpBackend.flush();
expect(results).toEqual([]);
});

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

var results = null,
service = this.azureInstanceTypeService;

this.azureInstanceTypeService.getAllTypesByRegion().then(function(result) {
results = service.getAvailableTypesForRegions(result, ['us-west-2', 'eu-west-1']);
});

this.$httpBackend.flush();
expect(results).toEqual(['m2.xlarge']);
});
it('should instantiate the controller', function() {
expect(this.azureInstanceTypeService).toBeDefined();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,18 @@ module.exports = angular
useSimpleCapacity: true,
mode: 'editPipeline',
submitButtonLabel: 'Done',
instanceProfile: originalCluster.viewState.instanceProfile,
instanceTypeDetails: originalCluster.viewState.instanceTypeDetails,
};

var viewOverrides = {
region: region,
credentials: pipelineCluster.account,
viewState: viewState,
};
if (originalCluster.viewState.instanceTypeDetails) {
viewOverrides.instanceType = originalCluster.viewState.instanceTypeDetails.name;
}

pipelineCluster.strategy = pipelineCluster.strategy || '';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,23 @@ module.exports = angular
LOAD_BALANCER_READ_SERVICE,
SECURITY_GROUP_READER,
CACHE_INITIALIZER_SERVICE,
require('../../instance/azureInstanceType.service').name,
])
.factory('azureServerGroupConfigurationService', [
'$q',
'azureImageReader',
'securityGroupReader',
'cacheInitializer',
'loadBalancerReader',
function($q, azureImageReader, securityGroupReader, cacheInitializer, loadBalancerReader) {
'azureInstanceTypeService',
function(
$q,
azureImageReader,
securityGroupReader,
cacheInitializer,
loadBalancerReader,
azureInstanceTypeService,
) {
var healthCheckTypes = ['EC2', 'ELB'],
terminationPolicies = [
'OldestInstance',
Expand Down Expand Up @@ -67,8 +76,54 @@ module.exports = angular
});
}

function configureInstanceTypes(command) {
const result = {
dirty: {},
};
if (command.region) {
const results = [result.dirty];

// results.push(configureCustomInstanceTypes(command).dirty);
results.push(configureStandardInstanceTypes(command).dirty);

angular.extend(...results);
} else {
command.backingData.filtered.instanceTypes = [];
}
return result;
}

function configureStandardInstanceTypes(command) {
const c = command;
const result = {
dirty: {},
};

const locations = [c.region],
{ credentialsKeyedByAccount } = c.backingData,
{ locationToInstanceTypesMap } = credentialsKeyedByAccount[c.credentials];

if (locations.every(l => !l)) {
return result;
}

let filtered = azureInstanceTypeService
.getAvailableTypesForRegions(locationToInstanceTypesMap, locations)
.map(type => type.name);

const instanceType = c.instanceType;
if (_.every([instanceType, !_.startsWith(instanceType, 'custom'), !_.includes(filtered, instanceType)])) {
result.dirty.instanceType = c.instanceType;
c.instanceType = null;
}
c.backingData.filtered.instanceTypes = filtered;
return result;
}

function configureImages(command) {
var result = { dirty: {} };
var result = {
dirty: {},
};
var regionalImages = null;
if (command.viewState.disableImageSelection) {
return result;
Expand All @@ -79,7 +134,10 @@ module.exports = angular
return image.amis && image.amis[command.region];
})
.map(function(image) {
return { imageName: image.imageName, ami: image.amis ? image.amis[command.region][0] : null };
return {
imageName: image.imageName,
ami: image.amis ? image.amis[command.region][0] : null,
};
});
if (
command.amiName &&
Expand All @@ -98,14 +156,18 @@ module.exports = angular
}

function getRegionalSecurityGroups(command) {
var newSecurityGroups = command.backingData.securityGroups[command.credentials] || { azure: {} };
var newSecurityGroups = command.backingData.securityGroups[command.credentials] || {
azure: {},
};
return _.chain(newSecurityGroups[command.region])
.sortBy('name')
.value();
}

function configureSecurityGroupOptions(command) {
var result = { dirty: {} };
var result = {
dirty: {},
};
var currentOptions;
if (command.backingData.filtered.securityGroups) {
currentOptions = command.backingData.filtered.securityGroups;
Expand Down Expand Up @@ -151,7 +213,9 @@ module.exports = angular
}

function configureLoadBalancerOptions(command) {
var result = { dirty: {} };
var result = {
dirty: {},
};
var current = command.loadBalancers;
var newLoadBalancers = getLoadBalancerNames(command.backingData.loadBalancers);

Expand Down Expand Up @@ -179,7 +243,9 @@ module.exports = angular
}

function configureLoadBalancers(command) {
var result = { dirty: {} };
var result = {
dirty: {},
};
var temp = command.backingData.loadBalancers;
var filterlist = _.filter(temp, function(lb) {
return lb.account === command.credentials && lb.region === command.region;
Expand All @@ -193,10 +259,13 @@ module.exports = angular

function attachEventHandlers(cmd) {
cmd.regionChanged = function regionChanged(command, isInit = false) {
var result = { dirty: {} };
var result = {
dirty: {},
};
if (command.region && command.credentials) {
angular.extend(result.dirty, configureLoadBalancers(command).dirty);
angular.extend(result.dirty, configureSecurityGroupOptions(command).dirty);
angular.extend(result.dirty, configureInstanceTypes(command).dirty);
}
// reset previous set values
if (!isInit) {
Expand All @@ -216,7 +285,9 @@ module.exports = angular
};

cmd.credentialsChanged = function credentialsChanged(command, isInit) {
var result = { dirty: {} };
var result = {
dirty: {},
};
var backingData = command.backingData;
if (command.credentials) {
var regionsForAccount = backingData.credentialsKeyedByAccount[command.credentials] || {
Expand All @@ -226,7 +297,9 @@ module.exports = angular
backingData.filtered.regions = regionsForAccount.regions;
if (
!_.chain(backingData.filtered.regions)
.some({ name: command.region })
.some({
name: command.region,
})
.value()
) {
command.region = null;
Expand All @@ -237,13 +310,23 @@ module.exports = angular
if (command.region) {
angular.extend(result.dirty, configureLoadBalancers(command).dirty);
}
angular.extend(result.dirty, configureInstanceTypes(command).dirty);
} else {
command.region = null;
}
return result;
};
}

function refreshInstanceTypes(command) {
return cacheInitializer.refreshCache('instanceTypes').then(function() {
return azureInstanceTypeService.getAllTypesByRegion().then(function(instanceTypes) {
command.backingData.instanceTypes = instanceTypes;
configureInstanceTypes(command);
});
});
}

return {
configureUpdateCommand: configureUpdateCommand,
configureCommand: configureCommand,
Expand All @@ -253,6 +336,7 @@ module.exports = angular
refreshLoadBalancers: refreshLoadBalancers,
refreshSecurityGroups: refreshSecurityGroups,
getRegionalSecurityGroups: getRegionalSecurityGroups,
refreshInstanceTypes: refreshInstanceTypes,
};
},
]);
Loading

0 comments on commit 4febe1a

Please sign in to comment.