2019-08-12 14:26:44 +00:00
|
|
|
import _ from 'lodash-es';
|
|
|
|
|
2018-02-01 12:27:52 +00:00
|
|
|
angular.module('portainer.docker')
|
2019-08-12 14:26:44 +00:00
|
|
|
.controller('NetworksController', ['$q', '$scope', '$state', 'NetworkService', 'Notifications', 'HttpRequestHelper', 'EndpointProvider', 'AgentService',
|
|
|
|
function ($q, $scope, $state, NetworkService, Notifications, HttpRequestHelper, EndpointProvider, AgentService) {
|
2016-09-14 04:28:38 +00:00
|
|
|
|
2017-12-06 11:04:02 +00:00
|
|
|
$scope.removeAction = function (selectedItems) {
|
|
|
|
var actionCount = selectedItems.length;
|
|
|
|
angular.forEach(selectedItems, function (network) {
|
2018-05-06 07:15:57 +00:00
|
|
|
HttpRequestHelper.setPortainerAgentTargetHeader(network.NodeName);
|
2017-12-06 11:04:02 +00:00
|
|
|
NetworkService.remove(network.Id)
|
|
|
|
.then(function success() {
|
|
|
|
Notifications.success('Network successfully removed', network.Name);
|
|
|
|
var index = $scope.networks.indexOf(network);
|
|
|
|
$scope.networks.splice(index, 1);
|
|
|
|
})
|
|
|
|
.catch(function error(err) {
|
|
|
|
Notifications.error('Failure', err, 'Unable to remove network');
|
|
|
|
})
|
|
|
|
.finally(function final() {
|
|
|
|
--actionCount;
|
|
|
|
if (actionCount === 0) {
|
|
|
|
$state.reload();
|
|
|
|
}
|
|
|
|
});
|
2016-06-02 05:34:03 +00:00
|
|
|
});
|
|
|
|
};
|
2015-12-21 02:07:57 +00:00
|
|
|
|
2018-10-28 09:27:06 +00:00
|
|
|
$scope.offlineMode = false;
|
|
|
|
|
2019-07-22 10:54:59 +00:00
|
|
|
$scope.getNetworks = getNetworks;
|
|
|
|
|
2019-08-12 14:26:44 +00:00
|
|
|
function groupSwarmNetworksManagerNodesFirst(networks, agents) {
|
|
|
|
const getRole = (item) => _.find(agents, (agent) => agent.NodeName === item.NodeName).NodeRole;
|
|
|
|
|
|
|
|
const nonSwarmNetworks = _.remove(networks, (item) => item.Scope !== 'swarm')
|
|
|
|
const grouped = _.toArray(_.groupBy(networks, (item) => item.Id));
|
|
|
|
const sorted = _.map(grouped, (arr) => _.sortBy(arr, (item) => getRole(item)));
|
|
|
|
const arr = _.map(sorted, (a) => {
|
|
|
|
const item = a[0];
|
|
|
|
for (let i = 1; i < a.length; i++) {
|
|
|
|
item.Subs.push(a[i]);
|
|
|
|
}
|
|
|
|
return item;
|
|
|
|
});
|
|
|
|
const res = _.concat(arr, ...nonSwarmNetworks);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2019-07-22 10:54:59 +00:00
|
|
|
function getNetworks() {
|
2019-08-12 14:26:44 +00:00
|
|
|
const req = {
|
|
|
|
networks: NetworkService.networks(true, true, true)
|
|
|
|
};
|
|
|
|
|
|
|
|
if ($scope.applicationState.endpoint.mode.agentProxy && $scope.applicationState.endpoint.mode.provider === 'DOCKER_SWARM_MODE') {
|
|
|
|
req.agents = AgentService.agents();
|
|
|
|
}
|
|
|
|
|
|
|
|
$q.all(req)
|
|
|
|
.then((data) => {
|
2018-10-28 09:27:06 +00:00
|
|
|
$scope.offlineMode = EndpointProvider.offlineMode();
|
2019-08-12 14:26:44 +00:00
|
|
|
const networks = _.forEach(data.networks, (item) => item.Subs = []);
|
|
|
|
if ($scope.applicationState.endpoint.mode.agentProxy && $scope.applicationState.endpoint.mode.provider === 'DOCKER_SWARM_MODE') {
|
|
|
|
$scope.networks = groupSwarmNetworksManagerNodesFirst(data.networks, data.agents);
|
|
|
|
} else {
|
|
|
|
$scope.networks = networks;
|
|
|
|
}
|
2017-09-19 14:58:30 +00:00
|
|
|
})
|
2019-08-12 14:26:44 +00:00
|
|
|
.catch((err) => {
|
2016-10-08 01:59:58 +00:00
|
|
|
$scope.networks = [];
|
2017-09-19 14:58:30 +00:00
|
|
|
Notifications.error('Failure', err, 'Unable to retrieve networks');
|
2016-06-02 05:34:03 +00:00
|
|
|
});
|
|
|
|
}
|
2016-08-10 06:20:18 +00:00
|
|
|
|
2019-07-22 10:54:59 +00:00
|
|
|
function initView() {
|
|
|
|
getNetworks();
|
|
|
|
}
|
|
|
|
|
2017-06-01 08:14:55 +00:00
|
|
|
initView();
|
2016-06-02 05:34:03 +00:00
|
|
|
}]);
|