Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement try catch strategy ManagementController #3374

Merged
merged 12 commits into from
Jun 30, 2021
194 changes: 128 additions & 66 deletions public/controllers/management/management.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,22 @@ import { WazuhConfig } from '../../react-services/wazuh-config';
import { WzRequest } from '../../react-services/wz-request';
import { ErrorHandler } from '../../react-services/error-handler';
import { ShareAgent } from '../../factories/share-agent';
import { RulesetHandler, RulesetResources } from './components/management/ruleset/utils/ruleset-handler';
import {
RulesetHandler,
RulesetResources,
} from './components/management/ruleset/utils/ruleset-handler';

import { UI_ERROR_SEVERITIES } from '../../react-services/error-orchestrator/types';
import { UI_LOGGER_LEVELS } from '../../../common/constants';
import { ErrorOrchestratorService } from '../../react-services';

export class ManagementController {
/**
* Class constructor
* @param {*} $scope
* @param {*} $location
*/
constructor(
$scope,
$rootScope,
$location,
configHandler,
errorHandler,
$interval
) {
constructor($scope, $rootScope, $location, configHandler, errorHandler, $interval) {
this.$scope = $scope;
this.$rootScope = $rootScope;
this.$location = $location;
Expand All @@ -48,6 +48,7 @@ export class ManagementController {
this.logtestOpened = false;
this.uploadOpened = false;
this.rulesetTab = RulesetResources.RULES;
this.logger = new ErrorOrchestratorService();
Machi3mfl marked this conversation as resolved.
Show resolved Hide resolved

this.$scope.$on('setCurrentGroup', (ev, params) => {
this.currentGroup = (params || {}).currentGroup || false;
Expand Down Expand Up @@ -105,7 +106,7 @@ export class ManagementController {
this.$scope.$on('viewFileOnly', (ev, params) => {
$scope.$broadcast('viewFileOnlyTable', {
file: params.item,
path: params.path
path: params.path,
});
});

Expand Down Expand Up @@ -141,26 +142,24 @@ export class ManagementController {
this.$scope.$applyAsync();
});

this.$rootScope.$on('performRestart', ev => {
this.$rootScope.$on('performRestart', (ev) => {
ev.stopPropagation();
this.clusterInfo.status === 'enabled'
? this.restartCluster()
: this.restartManager();
this.clusterInfo.status === 'enabled' ? this.restartCluster() : this.restartManager();
});

this.welcomeCardsProps = {
switchTab: (tab, setNav) => this.switchTab(tab, setNav)
switchTab: (tab, setNav) => this.switchTab(tab, setNav),
};

this.managementTabsProps = {
clickAction: tab => this.switchTab(tab, true),
clickAction: (tab) => this.switchTab(tab, true),
selectedTab: this.tab,
tabs: [
{ id: 'status', name: 'Status' },
{ id: 'logs', name: 'Logs' },
{ id: 'monitoring', name: 'Cluster' },
{ id: 'reporting', name: 'Reporting' }
]
{ id: 'reporting', name: 'Reporting' },
],
};

this.logtestProps = {
Expand All @@ -171,18 +170,18 @@ export class ManagementController {
};

this.managementProps = {
switchTab: section => this.switchTab(section, true),
switchTab: (section) => this.switchTab(section, true),
section: '',
groupsProps: {},
configurationProps: {
agent: {
id: '000'
id: '000',
}, // TODO: get dynamically the agent?
updateWazuhNotReadyYet: status => {
updateWazuhNotReadyYet: (status) => {
this.$rootScope.wazuhNotReadyYet = status;
this.$scope.$applyAsync();
},
wazuhNotReadyYet: () => this.$rootScope.wazuhNotReadyYet
wazuhNotReadyYet: () => this.$rootScope.wazuhNotReadyYet,
},
logtestProps: this.logtestProps,
};
Expand All @@ -192,26 +191,44 @@ export class ManagementController {
* When controller loads
*/
$onInit() {
this.clusterInfo = AppState.getClusterInfo();
try {
this.clusterInfo = AppState.getClusterInfo();

if (this.shareAgent.getAgent() && this.shareAgent.getSelectedGroup()) {
this.tab = 'groups';
this.switchTab(this.tab);
return;
}
if (this.shareAgent.getAgent() && this.shareAgent.getSelectedGroup()) {
this.tab = 'groups';
this.switchTab(this.tab);
return;
}

const location = this.$location.search();
const location = this.$location.search();

if (location && location.tab) {
this.tab = location.tab;
this.switchTab(this.tab);
}
if (location && location.tab) {
this.tab = location.tab;
this.switchTab(this.tab);
}

this.uploadFilesProps = {
msg: this.$scope.mctrl.rulesetTab,
path: `etc/${this.$scope.mctrl.rulesetTab}`,
upload: (files) => this.uploadFiles(files, this.$scope.mctrl.rulesetTab)
};
this.uploadFilesProps = {
msg: this.$scope.mctrl.rulesetTab,
path: `etc/${this.$scope.mctrl.rulesetTab}`,
upload: (files) => this.uploadFiles(files, this.$scope.mctrl.rulesetTab),
};
} catch (error) {
const errorOptions = {
context: '$onInit',
location: 'ManagementController',
level: UI_LOGGER_LEVELS.ERROR,
severity: UI_ERROR_SEVERITIES.BUSINESS,
display: true,
store: true,
error: {
error: error,
message: error?.message,
title: 'Error restarting cluster',
},
};

this.logger.handleError(errorOptions);
}
}

/**
Expand All @@ -234,10 +251,22 @@ export class ManagementController {
} catch (error) {
this.isRestarting = false;
this.$scope.$applyAsync();
ErrorHandler.handle(
error.message || error,
'Error restarting manager'
);

const errorOptions = {
context: 'restartManager',
location: 'ManagementController',
level: UI_LOGGER_LEVELS.ERROR,
severity: UI_ERROR_SEVERITIES.BUSINESS,
display: true,
store: true,
error: {
error: error,
message: error?.message || '',
title: 'Error restarting manager',
},
};

this.logger.handleError(errorOptions);
gabiwassan marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand All @@ -248,16 +277,25 @@ export class ManagementController {
await this.configHandler.restartCluster();
this.isRestarting = false;
this.$scope.$applyAsync();
ErrorHandler.info(
'Restarting cluster, it will take up to 30 seconds.'
);
ErrorHandler.info('Restarting cluster, it will take up to 30 seconds.');
} catch (error) {
this.isRestarting = false;
this.$scope.$applyAsync();
ErrorHandler.handle(
error.message || error,
'Error restarting cluster'
);
const errorOptions = {
context: 'restartCluster',
location: 'ManagementController',
level: UI_LOGGER_LEVELS.ERROR,
severity: UI_ERROR_SEVERITIES.BUSINESS,
display: true,
store: true,
error: {
error: error,
message: error?.message,
Machi3mfl marked this conversation as resolved.
Show resolved Hide resolved
title: 'Error restarting cluster',
},
};

this.logger.handleError(errorOptions);
}
}

Expand All @@ -272,7 +310,7 @@ export class ManagementController {
this.$location.search('editSubTab', tab);
this.$scope.$broadcast('configurationIsReloaded', {
globalConfigTab: this.globalConfigTab,
reloadConfigSubTab: true
reloadConfigSubTab: true,
});
}

Expand Down Expand Up @@ -326,8 +364,7 @@ export class ManagementController {
this.currentList = false;
this.managementTabsProps.selectedTab = this.tab;
}
this.managementProps.section =
this.tab === 'ruleset' ? this.rulesetTab : this.tab;
this.managementProps.section = this.tab === 'ruleset' ? this.rulesetTab : this.tab;
this.$location.search('tab', this.tab);
this.loadNodeList();
}
Expand Down Expand Up @@ -392,26 +429,36 @@ export class ManagementController {
const clusterEnabled = clusterInfo.status === 'enabled';
if (clusterEnabled) {
const response = await WzRequest.apiReq('GET', '/cluster/nodes', {});
const nodeList =
(((response || {}).data || {}).data || {}).items || false;
const nodeList = (((response || {}).data || {}).data || {}).items || false;
if (Array.isArray(nodeList) && nodeList.length) {
this.nodeList = nodeList.map(item => item.name).reverse();
this.selectedNode = nodeList.filter(
item => item.type === 'master'
)[0].name;
this.nodeList = nodeList.map((item) => item.name).reverse();
this.selectedNode = nodeList.filter((item) => item.type === 'master')[0].name;
}
}
} catch (error) {
console.log(error.message || error); // eslint-disable-line
const errorOptions = {
context: 'loadNodeList',
location: 'ManagementController',
level: UI_LOGGER_LEVELS.ERROR,
severity: UI_ERROR_SEVERITIES.BUSINESS,
display: true,
store: true,
error: {
error: error,
message: error?.message,
Machi3mfl marked this conversation as resolved.
Show resolved Hide resolved
title: error?.name,
Machi3mfl marked this conversation as resolved.
Show resolved Hide resolved
},
};

this.logger.handleError(errorOptions);
}
this.loadingNodes = false;
this.$scope.$applyAsync();
}

openCloseFlyout() {
this.logtestOpened = !this.logtestOpened;
this.logtestProps.isRuleset = this.tab,
this.$scope.$applyAsync();
(this.logtestProps.isRuleset = this.tab), this.$scope.$applyAsync();
}

newFile() {
Expand All @@ -430,7 +477,7 @@ export class ManagementController {
this.uploadFilesProps = {
msg: this.rulesetTab,
path: `etc/${this.rulesetTab}`,
upload: (files) => this.uploadFiles(files, this.rulesetTab)
upload: (files) => this.uploadFiles(files, this.rulesetTab),
};
}

Expand All @@ -453,24 +500,39 @@ export class ManagementController {
index: idx,
uploaded: true,
file: file,
error: 0
error: 0,
});
} catch (error) {
this.errors = true;
this.results.push({
index: idx,
uploaded: false,
file: file,
error: error
error: error,
});
}
}
if (this.errors) throw this.results;
ErrorHandler.info('Upload successful');
return;
} catch (error) {
if (Array.isArray(error) && error.length) return Promise.reject(error);
ErrorHandler.handle('Files cannot be uploaded');
if (Array.isArray(error) && error.length) throw error;

const errorOptions = {
context: 'uploadFiles',
location: 'ManagementController',
Machi3mfl marked this conversation as resolved.
Show resolved Hide resolved
level: UI_LOGGER_LEVELS.ERROR,
severity: UI_ERROR_SEVERITIES.BUSINESS,
display: true,
store: true,
error: {
error: error,
message: error?.message || '',
title: 'Files cannot be uploaded',
},
};

this.logger.handleError(errorOptions);
}
}
}