angular.module('portainer.services') .factory('VolumeService', ['$q', 'Volume', 'VolumeHelper', function VolumeServiceFactory($q, Volume, VolumeHelper) { 'use strict'; var service = {}; service.getVolumes = function() { return Volume.query({}).$promise; }; service.createVolumeConfiguration = function(name, driver, driverOptions) { var volumeConfiguration = { Name: name, Driver: driver, DriverOpts: VolumeHelper.createDriverOptions(driverOptions) }; return volumeConfiguration; }; 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(volumeConfigurations) { var createVolumeQueries = volumeConfigurations.map(function(volumeConfiguration) { return service.createVolume(volumeConfiguration); }); 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; }]);