Files
backroad/app/portainer/components/datatables/endpoints-datatable/endpointsDatatableController.js
Marcelo Rydel 738ec4316d feat(fdo): add import device UI [INT-20] (#6240)
feat(fdo): add import device UI INT-20
2021-12-14 19:51:16 -03:00

164 lines
5.2 KiB
JavaScript

import EndpointHelper from '@/portainer/helpers/endpointHelper';
angular.module('portainer.app').controller('EndpointsDatatableController', [
'$scope',
'$state',
'$controller',
'DatatableService',
'PaginationService',
'SettingsService',
'ModalService',
'Notifications',
'OpenAMTService',
function ($scope, $state, $controller, DatatableService, PaginationService, SettingsService, ModalService, Notifications, OpenAMTService) {
angular.extend(this, $controller('GenericDatatableController', { $scope: $scope }));
this.state = Object.assign(this.state, {
orderBy: this.orderBy,
loading: true,
filteredDataSet: [],
totalFilteredDataset: 0,
pageNumber: 1,
showAMTInfo: false,
amtDevices: {},
amtDevicesErrors: {},
showFDOInfo: false,
});
this.paginationChanged = function () {
this.state.loading = true;
this.state.filteredDataSet = [];
const start = (this.state.pageNumber - 1) * this.state.paginatedItemLimit + 1;
this.retrievePage(start, this.state.paginatedItemLimit, this.state.textFilter)
.then((data) => {
this.state.filteredDataSet = data.endpoints;
this.state.totalFilteredDataSet = data.totalCount;
})
.finally(() => {
this.state.loading = false;
});
};
this.onPageChange = function (newPageNumber) {
this.state.pageNumber = newPageNumber;
this.paginationChanged();
};
/**
* Overridden
*/
this.onTextFilterChange = function () {
var filterValue = this.state.textFilter;
DatatableService.setDataTableTextFilters(this.tableKey, filterValue);
this.paginationChanged();
};
/**
* Overridden
*/
this.changePaginationLimit = function () {
PaginationService.setPaginationLimit(this.tableKey, this.state.paginatedItemLimit);
this.paginationChanged();
};
this.setShowIntelInfo = async function () {
this.settings = await SettingsService.settings();
const openAMTFeatureFlagValue = this.settings && this.settings.FeatureFlagSettings && this.settings.FeatureFlagSettings['open-amt'];
const openAMTFeatureEnabled = this.settings && this.settings.OpenAMTConfiguration && this.settings.OpenAMTConfiguration.Enabled;
this.state.showAMTInfo = openAMTFeatureFlagValue && openAMTFeatureEnabled;
const fdoFeatureFlagValue = this.settings && this.settings.FeatureFlagSettings && this.settings.FeatureFlagSettings['fdo'];
const fdoFeatureEnabled = this.settings && this.settings.FDOConfiguration && this.settings.FDOConfiguration.Enabled;
this.state.showFDOInfo = fdoFeatureFlagValue && fdoFeatureEnabled;
};
this.showAMTNodes = function (item) {
return this.state.showAMTInfo && EndpointHelper.isAgentEndpoint(item) && item.AMTDeviceGUID;
};
this.expandItem = function (item, expanded) {
if (!this.showAMTNodes(item)) {
return;
}
item.Expanded = expanded;
this.fetchAMTDeviceInfo(item);
};
this.fetchAMTDeviceInfo = function (endpoint) {
if (!this.showAMTNodes(endpoint) || this.state.amtDevices[endpoint.Id]) {
return;
}
OpenAMTService.getDevices(endpoint.Id)
.then((data) => {
this.state.amtDevices[endpoint.Id] = data.Devices;
})
.catch((e) => {
console.log(e);
this.state.amtDevicesErrors[endpoint.Id] = 'Error fetching devices information: ' + e.data.details;
});
};
this.associateOpenAMT = function (endpoints) {
ModalService.confirm({
title: 'Are you sure?',
message: 'This operation will associate the selected environments with OpenAMT.',
buttons: {
confirm: {
label: 'Associate',
className: 'btn-primary',
},
},
callback: async function onConfirm(confirmed) {
if (!confirmed) {
return;
}
for (let endpoint of endpoints) {
try {
await OpenAMTService.activateDevice(endpoint.Id);
Notifications.success('Successfully associated with OpenAMT', endpoint.Name);
} catch (err) {
Notifications.error('Failure', err, 'Unable to associate with OpenAMT');
}
}
$state.reload();
},
});
};
/**
* Overridden
*/
this.$onInit = async function () {
this.setDefaults();
this.prepareTableFromDataset();
var storedOrder = DatatableService.getDataTableOrder(this.tableKey);
if (storedOrder !== null) {
this.state.reverseOrder = storedOrder.reverse;
this.state.orderBy = storedOrder.orderBy;
}
var textFilter = DatatableService.getDataTableTextFilters(this.tableKey);
if (textFilter !== null) {
this.state.textFilter = textFilter;
this.onTextFilterChange();
}
var storedFilters = DatatableService.getDataTableFilters(this.tableKey);
if (storedFilters !== null) {
this.filters = storedFilters;
}
if (this.filters && this.filters.state) {
this.filters.state.open = false;
}
await this.setShowIntelInfo();
this.paginationChanged();
};
},
]);