Skip to content

Commit

Permalink
PowerOperations - clean up
Browse files Browse the repository at this point in the history
remove all the unused helper functions,
changing the functions to simply match the list of states against what getPowerState returns
  • Loading branch information
himdel committed Jul 29, 2020
1 parent 54a4fa2 commit 6738694
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 116 deletions.
94 changes: 19 additions & 75 deletions client/app/services/poweroperations.service.js
Original file line number Diff line number Diff line change
@@ -1,54 +1,41 @@
/* eslint-disable camelcase */

/** @ngInject */
export function PowerOperationsFactory (CollectionsApi, EventNotifications, sprintf) {
export function PowerOperationsFactory (CollectionsApi, EventNotifications, sprintf, lodash) {
var service = {
startService: startService,
stopService: stopService,
suspendService: suspendService,

allowStartService: allowStart,
allowStopService: allowStop,
allowSuspendService: allowSuspend,

allowStartVm: allowStart,
allowStopVm: allowStop,
allowSuspendVm: allowSuspend,

getPowerState: getPowerState,
powerOperationUnknownState: powerOperationUnknownState,
powerOperationInProgressState: powerOperationInProgressState,
powerOperationOnState: powerOperationOnState,
powerOperationOffState: powerOperationOffState,
powerOperationSuspendState: powerOperationSuspendState,
powerOperationTimeoutState: powerOperationTimeoutState,
powerOperationStartTimeoutState: powerOperationStartTimeoutState,
powerOperationStopTimeoutState: powerOperationStopTimeoutState,
powerOperationSuspendTimeoutState: powerOperationSuspendTimeoutState
}

function powerStatesMatch (powerStates, match) {
var matches = angular.isArray(powerStates) && powerStates.length > 0

angular.forEach(powerStates, function (powerState) {
matches = matches && (powerState === match)
})
if (!powerStates || !powerStates.length) {
return false
}

return matches
return lodash.every(powerStates, (powerState) => powerState === match)
}

function allowStart (item) {
return powerOperationUnknownState(item) ||
powerOperationOffState(item) ||
powerOperationSuspendState(item) ||
powerOperationTimeoutState(item)
return ['', 'off', 'suspended', 'timeout'].includes(getPowerState(item))
}

function allowStop (item) {
return !powerOperationUnknownState(item) &&
!powerOperationOffState(item)
return !['', 'off'].includes(getPowerState(item))
}

function allowSuspend (item) {
return !powerOperationUnknownState(item) &&
!powerOperationSuspendState(item)
return !['', 'suspend'].includes(getPowerState(item))
}

function getPowerState (item) {
Expand All @@ -68,45 +55,6 @@ export function PowerOperationsFactory (CollectionsApi, EventNotifications, spri
return powerState
}

function powerOperationUnknownState (item) {
return getPowerState(item) === ''
}

function powerOperationInProgressState (item) {
return !powerOperationTimeoutState(item) &&
((item.power_status === 'starting') ||
(item.power_status === 'stopping') ||
(item.power_status === 'suspending'))
}

function powerOperationOnState (item) {
return getPowerState(item) === 'on'
}

function powerOperationOffState (item) {
return getPowerState(item) === 'off'
}

function powerOperationSuspendState (item) {
return getPowerState(item) === 'suspended'
}

function powerOperationTimeoutState (item) {
return getPowerState(item) === 'timeout'
}

function powerOperationStartTimeoutState (item) {
return powerOperationTimeoutState(item) && item.power_status === 'starting'
}

function powerOperationStopTimeoutState (item) {
return powerOperationTimeoutState(item) && item.power_status === 'stopping'
}

function powerOperationSuspendTimeoutState (item) {
return powerOperationTimeoutState(item) && item.power_status === 'suspending'
}

function startService (item) {
item.power_state = ''
item.power_status = 'starting'
Expand All @@ -128,12 +76,12 @@ export function PowerOperationsFactory (CollectionsApi, EventNotifications, spri
return servicePowerOperation('suspend', item);
}

function powerOperation (apiType, powerAction, item, itemType) {
return CollectionsApi.post(apiType, item.id, {}, {action: powerAction})
function servicePowerOperation (action, item) {
return CollectionsApi.post('services', item.id, {}, { action })
.then(actionSuccess, actionFailure);

function actionSuccess (response) {
switch (powerAction) {
switch (action) {
case 'start':
EventNotifications.success(sprintf(__('%s was started. %s'), item.name, response.message))
break
Expand All @@ -150,26 +98,22 @@ export function PowerOperationsFactory (CollectionsApi, EventNotifications, spri
}

function actionFailure () {
switch (powerAction) {
switch (action) {
case 'start':
EventNotifications.error(sprintf(__('There was an error starting this %s.'), itemType))
EventNotifications.error(__('There was an error starting this service.'))
break
case 'stop':
EventNotifications.error(sprintf(__('There was an error stopping this %s.'), itemType))
EventNotifications.error(__('There was an error stopping this service.'))
break
case 'suspend':
EventNotifications.error(sprintf(__('There was an error suspending this %s.'), itemType))
EventNotifications.error(__('There was an error suspending this service.'))
break
case 'retire':
EventNotifications.error(sprintf(__('There was an error retiring this %s.'), itemType))
EventNotifications.error(__('There was an error retiring this service.'))
break
}
}
}

function servicePowerOperation (powerAction, item) {
return powerOperation('services', powerAction, item, 'service');
}

return service
}
47 changes: 8 additions & 39 deletions client/app/services/poweroperations.service.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,59 +131,28 @@ describe('Service: PowerOperationsFactory', () => {
'power_state': 'on',
'power_status': 'starting'
}
const progressState = PowerOperations.powerOperationInProgressState(testVm)
expect(progressState).to.eql(true)
const progressState = PowerOperations.getPowerState(testVm)
expect(progressState).to.eql('starting')
})
it('Should retrieve the power on state', () => {
const testVm = {
'power_state': 'on'
}
const testPowerState = PowerOperations.powerOperationOnState(testVm)
expect(testPowerState).to.eq(true)
const testPowerState = PowerOperations.getPowerState(testVm)
expect(testPowerState).to.eq('on')
})
it('Should retrieve the power off state', () => {
const testVm = {
'power_state': 'off'
}
const testPowerState = PowerOperations.powerOperationOffState(testVm)
expect(testPowerState).to.eq(true)
const testPowerState = PowerOperations.getPowerState(testVm)
expect(testPowerState).to.eq('off')
})
it('Should retrieve the power suspend state', () => {
const testVm = {
'power_state': 'suspended'
}
const testPowerState = PowerOperations.powerOperationSuspendState(testVm)
expect(testPowerState).to.eq(true)
})
it('Should retrieve the power timeout state', () => {
const testVm = {
'power_state': 'timeout'
}
const testPowerState = PowerOperations.powerOperationTimeoutState(testVm)
expect(testPowerState).to.eq(true)
})
it('Should retrieve the power on in progress timeout state', () => {
const testVm = {
'power_state': 'timeout',
'power_status': 'starting'
}
const testPowerState = PowerOperations.powerOperationStartTimeoutState(testVm)
expect(testPowerState).to.eq(true)
})
it('Should retrieve the power off in progress timeout state', () => {
const testVm = {
'power_state': 'timeout',
'power_status': 'stopping'
}
const testPowerState = PowerOperations.powerOperationStopTimeoutState(testVm)
expect(testPowerState).to.eq(true)
})
it('Should retrieve the power suspend in progress timeout state', () => {
const testVm = {
'power_state': 'timeout',
'power_status': 'suspending'
}
const testPowerState = PowerOperations.powerOperationSuspendTimeoutState(testVm)
expect(testPowerState).to.eq(true)
const testPowerState = PowerOperations.getPowerState(testVm)
expect(testPowerState).to.eq('suspended')
})
})
2 changes: 1 addition & 1 deletion client/app/services/vm-power.service.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* eslint-disable semi, space-before-function-paren, comma-dangle */
/* eslint-disable semi, space-before-function-paren */

/** @ngInject */
export function VmPowerFactory(CollectionsApi, EventNotifications, sprintf) {
Expand Down
2 changes: 1 addition & 1 deletion client/app/services/vm-power.service.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* global readJSON, EventNotifications, CollectionsApi, VmPower */
/* eslint-disable no-unused-expressions, semi, space-before-function-paren, comma-dangle */
/* eslint-disable no-unused-expressions, semi, space-before-function-paren */

function fixture(name) {
return readJSON('tests/mock/vm-power/' + name + '.json');
Expand Down

0 comments on commit 6738694

Please sign in to comment.