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