2019-03-21 05:46:49 +00:00
|
|
|
import { NetworkViewModel } from '../models/network';
|
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
angular.module('portainer.docker').factory('NetworkService', [
|
|
|
|
'$q',
|
|
|
|
'Network',
|
|
|
|
function NetworkServiceFactory($q, Network) {
|
|
|
|
'use strict';
|
|
|
|
var service = {};
|
2017-02-10 01:11:36 +00:00
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
service.create = function (networkConfiguration) {
|
|
|
|
var deferred = $q.defer();
|
2017-09-19 14:58:30 +00:00
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
Network.create(networkConfiguration)
|
|
|
|
.$promise.then(function success(data) {
|
|
|
|
deferred.resolve(data);
|
|
|
|
})
|
|
|
|
.catch(function error(err) {
|
|
|
|
deferred.reject({ msg: 'Unable to create network', err: err });
|
|
|
|
});
|
|
|
|
return deferred.promise;
|
|
|
|
};
|
2017-09-19 14:58:30 +00:00
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
service.network = function (id) {
|
|
|
|
var deferred = $q.defer();
|
2017-09-19 14:58:30 +00:00
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
Network.get({ id: id })
|
|
|
|
.$promise.then(function success(data) {
|
|
|
|
var network = new NetworkViewModel(data);
|
|
|
|
deferred.resolve(network);
|
|
|
|
})
|
|
|
|
.catch(function error(err) {
|
|
|
|
deferred.reject({ msg: 'Unable to retrieve network details', err: err });
|
|
|
|
});
|
2017-09-19 14:58:30 +00:00
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
return deferred.promise;
|
|
|
|
};
|
2017-05-27 07:23:49 +00:00
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
service.networks = function (localNetworks, swarmNetworks, swarmAttachableNetworks, filters) {
|
|
|
|
var deferred = $q.defer();
|
2017-08-11 07:46:55 +00:00
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
Network.query({ filters: filters })
|
|
|
|
.$promise.then(function success(data) {
|
|
|
|
var networks = data;
|
2017-08-11 07:46:55 +00:00
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
var filteredNetworks = networks
|
|
|
|
.filter(function (network) {
|
|
|
|
if (localNetworks && network.Scope === 'local') {
|
|
|
|
return network;
|
|
|
|
}
|
|
|
|
if (swarmNetworks && network.Scope === 'swarm') {
|
|
|
|
return network;
|
|
|
|
}
|
|
|
|
if (swarmAttachableNetworks && network.Scope === 'swarm' && network.Attachable === true) {
|
|
|
|
return network;
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.map(function (item) {
|
|
|
|
return new NetworkViewModel(item);
|
|
|
|
});
|
2017-05-27 07:23:49 +00:00
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
deferred.resolve(filteredNetworks);
|
|
|
|
})
|
|
|
|
.catch(function error(err) {
|
|
|
|
deferred.reject({ msg: 'Unable to retrieve networks', err: err });
|
|
|
|
});
|
2017-05-27 07:23:49 +00:00
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
return deferred.promise;
|
|
|
|
};
|
2017-12-06 11:04:02 +00:00
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
service.remove = function (id) {
|
|
|
|
return Network.remove({ id: id }).$promise;
|
|
|
|
};
|
2018-05-06 07:15:57 +00:00
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
service.disconnectContainer = function (networkId, containerId, force) {
|
|
|
|
return Network.disconnect({ id: networkId }, { Container: containerId, Force: force }).$promise;
|
2019-11-27 20:35:14 +00:00
|
|
|
};
|
2020-04-10 21:54:53 +00:00
|
|
|
|
|
|
|
service.connectContainer = function (networkId, containerId, aliases) {
|
|
|
|
var payload = {
|
|
|
|
Container: containerId,
|
|
|
|
};
|
|
|
|
if (aliases) {
|
|
|
|
payload.EndpointConfig = {
|
|
|
|
Aliases: aliases,
|
|
|
|
};
|
2019-11-27 20:35:14 +00:00
|
|
|
}
|
2020-04-10 21:54:53 +00:00
|
|
|
return Network.connect({ id: networkId }, payload).$promise;
|
|
|
|
};
|
2018-05-06 07:15:57 +00:00
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
return service;
|
|
|
|
},
|
|
|
|
]);
|