2018-02-01 12:27:52 +00:00
|
|
|
angular.module('portainer.docker')
|
2018-08-22 15:33:06 +00:00
|
|
|
.controller('NetworkController', ['$scope', '$state', '$transition$', '$filter', 'NetworkService', 'Container', 'Notifications', 'HttpRequestHelper',
|
|
|
|
function ($scope, $state, $transition$, $filter, NetworkService, Container, Notifications, HttpRequestHelper) {
|
2016-06-02 05:34:03 +00:00
|
|
|
|
2018-08-22 15:33:06 +00:00
|
|
|
$scope.removeNetwork = function removeNetwork() {
|
2018-05-06 07:15:57 +00:00
|
|
|
NetworkService.remove($transition$.params().id, $transition$.params().id)
|
2018-08-22 15:33:06 +00:00
|
|
|
.then(function success() {
|
2018-05-06 07:15:57 +00:00
|
|
|
Notifications.success('Network removed', $transition$.params().id);
|
|
|
|
$state.go('docker.networks', {});
|
|
|
|
})
|
|
|
|
.catch(function error(err) {
|
|
|
|
Notifications.error('Failure', err, 'Unable to remove network');
|
2016-06-02 05:34:03 +00:00
|
|
|
});
|
|
|
|
};
|
2015-12-21 01:13:53 +00:00
|
|
|
|
2018-05-06 07:15:57 +00:00
|
|
|
$scope.containerLeaveNetwork = function containerLeaveNetwork(network, container) {
|
|
|
|
HttpRequestHelper.setPortainerAgentTargetHeader(container.NodeName);
|
|
|
|
NetworkService.disconnectContainer($transition$.params().id, container.Id, false)
|
2018-08-22 15:33:06 +00:00
|
|
|
.then(function success() {
|
2018-05-06 07:15:57 +00:00
|
|
|
Notifications.success('Container left network', $transition$.params().id);
|
|
|
|
$state.go('docker.networks.network', { id: network.Id }, { reload: true });
|
|
|
|
})
|
|
|
|
.catch(function error(err) {
|
|
|
|
Notifications.error('Failure', err, 'Unable to disconnect container from network');
|
2016-12-25 20:28:54 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-01-23 03:06:51 +00:00
|
|
|
function filterContainersInNetwork(network, containers) {
|
|
|
|
var containersInNetwork = [];
|
|
|
|
containers.forEach(function(container) {
|
|
|
|
var containerInNetwork = network.Containers[container.Id];
|
2017-10-25 11:37:52 +00:00
|
|
|
if (containerInNetwork) {
|
|
|
|
containerInNetwork.Id = container.Id;
|
|
|
|
// Name is not available in Docker 1.9
|
|
|
|
if (!containerInNetwork.Name) {
|
|
|
|
containerInNetwork.Name = $filter('trimcontainername')(container.Names[0]);
|
|
|
|
}
|
|
|
|
containersInNetwork.push(containerInNetwork);
|
2017-01-23 03:06:51 +00:00
|
|
|
}
|
2016-12-25 20:28:54 +00:00
|
|
|
});
|
2017-01-23 03:06:51 +00:00
|
|
|
$scope.containersInNetwork = containersInNetwork;
|
2016-12-25 20:28:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function getContainersInNetwork(network) {
|
2017-08-13 11:31:50 +00:00
|
|
|
var apiVersion = $scope.applicationState.endpoint.apiVersion;
|
2016-12-25 20:28:54 +00:00
|
|
|
if (network.Containers) {
|
2017-08-13 11:31:50 +00:00
|
|
|
if (apiVersion < 1.24) {
|
2017-01-23 03:06:51 +00:00
|
|
|
Container.query({}, function success(data) {
|
|
|
|
var containersInNetwork = data.filter(function filter(container) {
|
|
|
|
if (container.HostConfig.NetworkMode === network.Name) {
|
|
|
|
return container;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
filterContainersInNetwork(network, containersInNetwork);
|
|
|
|
}, function error(err) {
|
2017-05-23 18:56:10 +00:00
|
|
|
Notifications.error('Failure', err, 'Unable to retrieve containers in network');
|
2016-12-25 20:28:54 +00:00
|
|
|
});
|
2017-01-23 03:06:51 +00:00
|
|
|
} else {
|
|
|
|
Container.query({
|
2017-10-25 11:37:52 +00:00
|
|
|
filters: { network: [$transition$.params().id] }
|
2017-01-23 03:06:51 +00:00
|
|
|
}, function success(data) {
|
|
|
|
filterContainersInNetwork(network, data);
|
|
|
|
}, function error(err) {
|
2017-05-23 18:56:10 +00:00
|
|
|
Notifications.error('Failure', err, 'Unable to retrieve containers in network');
|
2017-01-23 03:06:51 +00:00
|
|
|
});
|
|
|
|
}
|
2016-12-25 20:28:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-01 08:14:55 +00:00
|
|
|
function initView() {
|
2018-05-06 07:15:57 +00:00
|
|
|
var nodeName = $transition$.params().nodeName;
|
|
|
|
HttpRequestHelper.setPortainerAgentTargetHeader(nodeName);
|
|
|
|
$scope.nodeName = nodeName;
|
2017-09-21 14:00:53 +00:00
|
|
|
NetworkService.network($transition$.params().id)
|
2017-08-13 11:31:50 +00:00
|
|
|
.then(function success(data) {
|
2017-06-01 08:14:55 +00:00
|
|
|
$scope.network = data;
|
2017-08-13 11:31:50 +00:00
|
|
|
var endpointProvider = $scope.applicationState.endpoint.mode.provider;
|
|
|
|
if (endpointProvider !== 'VMWARE_VIC') {
|
|
|
|
getContainersInNetwork(data);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(function error(err) {
|
2017-06-01 08:14:55 +00:00
|
|
|
Notifications.error('Failure', err, 'Unable to retrieve network info');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
initView();
|
2016-06-02 05:34:03 +00:00
|
|
|
}]);
|