2019-03-21 05:46:49 +00:00
|
|
|
import { VolumeViewModel } from '../models/volume';
|
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
angular.module('portainer.docker').factory('VolumeService', [
|
|
|
|
'$q',
|
|
|
|
'Volume',
|
|
|
|
'VolumeHelper',
|
|
|
|
function VolumeServiceFactory($q, Volume, VolumeHelper) {
|
|
|
|
'use strict';
|
|
|
|
var service = {};
|
2017-02-10 01:11:36 +00:00
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
service.volumes = function (params) {
|
|
|
|
var deferred = $q.defer();
|
|
|
|
Volume.query(params)
|
|
|
|
.$promise.then(function success(data) {
|
|
|
|
var volumes = data.Volumes || [];
|
|
|
|
volumes = volumes.map(function (item) {
|
|
|
|
return new VolumeViewModel(item);
|
|
|
|
});
|
|
|
|
deferred.resolve(volumes);
|
|
|
|
})
|
|
|
|
.catch(function error(err) {
|
|
|
|
deferred.reject({ msg: 'Unable to retrieve volumes', err: err });
|
|
|
|
});
|
|
|
|
return deferred.promise;
|
|
|
|
};
|
2017-05-23 18:56:10 +00:00
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
service.volume = function (id) {
|
|
|
|
var deferred = $q.defer();
|
|
|
|
Volume.get({ id: id })
|
|
|
|
.$promise.then(function success(data) {
|
|
|
|
var volume = new VolumeViewModel(data);
|
|
|
|
deferred.resolve(volume);
|
|
|
|
})
|
|
|
|
.catch(function error(err) {
|
|
|
|
deferred.reject({ msg: 'Unable to retrieve volume details', err: err });
|
|
|
|
});
|
|
|
|
return deferred.promise;
|
|
|
|
};
|
2017-05-23 18:56:10 +00:00
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
service.getVolumes = function () {
|
|
|
|
return Volume.query({}).$promise;
|
|
|
|
};
|
2017-02-13 05:16:14 +00:00
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
service.remove = function (volume) {
|
|
|
|
var deferred = $q.defer();
|
2017-05-23 18:56:10 +00:00
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
Volume.remove({ id: volume.Id })
|
|
|
|
.$promise.then(function success(data) {
|
|
|
|
if (data.message) {
|
|
|
|
deferred.reject({ msg: data.message, err: data.message });
|
|
|
|
} else {
|
|
|
|
deferred.resolve();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(function error(err) {
|
|
|
|
deferred.reject({ msg: 'Unable to remove volume', err: err });
|
|
|
|
});
|
2017-05-23 18:56:10 +00:00
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
return deferred.promise;
|
|
|
|
};
|
2017-05-23 18:56:10 +00:00
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
service.createVolumeConfiguration = function (name, driver, driverOptions) {
|
|
|
|
var volumeConfiguration = {
|
|
|
|
Name: name,
|
|
|
|
Driver: driver,
|
|
|
|
DriverOpts: VolumeHelper.createDriverOptions(driverOptions),
|
|
|
|
};
|
|
|
|
return volumeConfiguration;
|
2017-04-01 10:18:46 +00:00
|
|
|
};
|
2017-02-10 01:11:36 +00:00
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
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 {
|
|
|
|
var volume = new VolumeViewModel(data);
|
|
|
|
deferred.resolve(volume);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(function error(err) {
|
|
|
|
deferred.reject({ msg: 'Unable to create volume', err: err });
|
|
|
|
});
|
|
|
|
return deferred.promise;
|
|
|
|
};
|
2017-02-10 01:11:36 +00:00
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
service.createVolumes = function (volumeConfigurations) {
|
|
|
|
var createVolumeQueries = volumeConfigurations.map(function (volumeConfiguration) {
|
|
|
|
return service.createVolume(volumeConfiguration);
|
|
|
|
});
|
|
|
|
return $q.all(createVolumeQueries);
|
|
|
|
};
|
2017-02-10 01:11:36 +00:00
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
service.createXAutoGeneratedLocalVolumes = function (x) {
|
|
|
|
var createVolumeQueries = [];
|
|
|
|
for (var i = 0; i < x; i++) {
|
|
|
|
createVolumeQueries.push(service.createVolume({ Driver: 'local' }));
|
|
|
|
}
|
|
|
|
return $q.all(createVolumeQueries);
|
|
|
|
};
|
2017-02-10 01:11:36 +00:00
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
return service;
|
|
|
|
},
|
|
|
|
]);
|