angular.module('portainer.services') .factory('VolumeService', ['$q', 'Volume', function VolumeServiceFactory($q, Volume) { 'use strict'; var service = {}; service.getVolumes = function() { return Volume.query({}).$promise; }; function prepareVolumeQueries(template, containerConfig) { var volumeQueries = []; if (template.volumes) { template.volumes.forEach(function (vol) { volumeQueries.push( Volume.create({}, function (d) { if (d.message) { Messages.error("Unable to create volume", {}, d.message); } else { Messages.send("Volume created", d.Name); containerConfig.Volumes[vol] = {}; containerConfig.HostConfig.Binds.push(d.Name + ':' + vol); } }, function (e) { Messages.error("Failure", e, "Unable to create volume"); }).$promise ); }); } return volumeQueries; } service.createVolume = function(volumeConfiguration) { var deferred = $q.defer(); Volume.create(volumeConfiguration).$promise .then(function success(data) { if (data.message) { deferred.reject({ msg: data.message }); } else { deferred.resolve(data); } }) .catch(function error(err) { deferred.reject({ msg: 'Unable to create volume', err: err }); }); return deferred.promise; }; service.createVolumes = function(volumes) { var createVolumeQueries = volumes.map(function(volume) { return service.createVolume(volume); }); return $q.all(createVolumeQueries); }; service.createXAutoGeneratedLocalVolumes = function (x) { var createVolumeQueries = []; for (var i = 0; i < x; i++) { createVolumeQueries.push(service.createVolume({})); } return $q.all(createVolumeQueries); }; return service; }]);