portainer/app/docker/services/networkService.js

70 lines
1.8 KiB
JavaScript
Raw Normal View History

angular.module('portainer.docker')
.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();
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});
});
return deferred.promise;
};
service.networks = function(localNetworks, swarmNetworks, swarmAttachableNetworks) {
var deferred = $q.defer();
Network.query({}).$promise
.then(function success(data) {
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);
});
deferred.resolve(filteredNetworks);
})
.catch(function error(err) {
deferred.reject({msg: 'Unable to retrieve networks', err: err});
});
return deferred.promise;
};
service.remove = function(id) {
return Network.remove({ id: id }).$promise;
};
return service;
}]);