Use text-download service on horizon

The text-download service is ported on horizon, so we use it.

Change-Id: I0ff3f1dd3f1ff702e799b709f8cf798e6aca127e
This commit is contained in:
Shu Muto 2018-01-16 11:45:32 +09:00
parent 64b8af65b9
commit 1972ddfe8e
4 changed files with 19 additions and 40 deletions

View File

@ -27,12 +27,13 @@
showCertificateService); showCertificateService);
showCertificateService.$inject = [ showCertificateService.$inject = [
'horizon.framework.util.q.extensions', 'horizon.app.core.openstack-service-api.magnum',
'horizon.app.core.openstack-service-api.magnum' 'horizon.framework.util.file.text-download',
'horizon.framework.util.q.extensions'
]; ];
function showCertificateService( function showCertificateService(
$qExtensions, magnum magnum, textDownload, $qExtensions
) { ) {
var service = { var service = {
@ -51,7 +52,7 @@
function perform(selected) { function perform(selected) {
// get certificate // get certificate
return magnum.showCertificate(selected.id).success(function(response) { return magnum.showCertificate(selected.id).success(function(response) {
magnum.downloadTextAsFile(response.pem, selected.name + "_ca.pem"); textDownload.downloadTextFile(response.pem, selected.name + "_ca.pem");
}); });
} }

View File

@ -29,17 +29,19 @@
signCertificateService.$inject = [ signCertificateService.$inject = [
'$uibModal', '$uibModal',
'horizon.app.core.openstack-service-api.magnum', 'horizon.app.core.openstack-service-api.magnum',
'horizon.framework.util.actions.action-result.service',
'horizon.framework.util.i18n.gettext',
'horizon.framework.util.q.extensions',
'horizon.framework.widgets.toast.service',
'horizon.dashboard.container-infra.clusters.basePath', 'horizon.dashboard.container-infra.clusters.basePath',
'horizon.dashboard.container-infra.clusters.resourceType', 'horizon.dashboard.container-infra.clusters.resourceType',
'horizon.dashboard.container-infra.clusters.sign-certificate-model' 'horizon.dashboard.container-infra.clusters.sign-certificate-model',
'horizon.framework.util.actions.action-result.service',
'horizon.framework.util.file.text-download',
'horizon.framework.util.i18n.gettext',
'horizon.framework.util.q.extensions',
'horizon.framework.widgets.toast.service'
]; ];
function signCertificateService( function signCertificateService(
$uibModal, magnum, actionResult, gettext, $qExtensions, toast, basePath, resourceType, model $uibModal, magnum, basePath, resourceType, model, actionResult, textDownload,
gettext, $qExtensions, toast
) { ) {
var message = { var message = {
@ -82,7 +84,7 @@
} }
function success(response) { function success(response) {
magnum.downloadTextAsFile(response.data.pem, model.cluster_name + "_cert.pem"); textDownload.downloadTextFile(response.data.pem, model.cluster_name + "_cert.pem");
response.data.id = response.data.uuid; response.data.id = response.data.uuid;
toast.add('success', interpolate(message.success, [response.data.id])); toast.add('success', interpolate(message.success, [response.data.id]));

View File

@ -20,7 +20,7 @@
describe('horizon.dashboard.container-infra.clusters.sign-certificate.service', describe('horizon.dashboard.container-infra.clusters.sign-certificate.service',
function() { function() {
var $q, service, magnum, deferred, fakeDeferred, model; var $q, service, textDownload, deferred, fakeDeferred, model;
var fakeModal = { var fakeModal = {
result: { result: {
@ -44,10 +44,10 @@
$q = _$q_; $q = _$q_;
service = $injector.get( service = $injector.get(
'horizon.dashboard.container-infra.clusters.sign-certificate.service'); 'horizon.dashboard.container-infra.clusters.sign-certificate.service');
magnum = $injector.get('horizon.app.core.openstack-service-api.magnum'); textDownload = $injector.get('horizon.framework.util.file.text-download');
deferred = $q.defer(); deferred = $q.defer();
deferred.resolve({data: {uuid: 1}}); deferred.resolve({data: {uuid: 1}});
spyOn(magnum, 'downloadTextAsFile').and.returnValue(deferred.promise); spyOn(textDownload, 'downloadTextFile').and.returnValue(deferred.promise);
model = $injector.get('horizon.dashboard.container-infra.clusters.sign-certificate-model'); model = $injector.get('horizon.dashboard.container-infra.clusters.sign-certificate-model');
fakeDeferred = $q.defer(); fakeDeferred = $q.defer();
@ -70,7 +70,7 @@
$timeout.flush(); $timeout.flush();
expect(model.signCertificate).toHaveBeenCalled(); expect(model.signCertificate).toHaveBeenCalled();
expect(magnum.downloadTextAsFile).toHaveBeenCalled(); expect(textDownload.downloadTextFile).toHaveBeenCalled();
})); }));

View File

@ -50,8 +50,7 @@
createQuota: createQuota, createQuota: createQuota,
updateQuota: updateQuota, updateQuota: updateQuota,
deleteQuota: deleteQuota, deleteQuota: deleteQuota,
getNetworks: getNetworks, getNetworks: getNetworks
downloadTextAsFile: downloadTextAsFile
}; };
return service; return service;
@ -247,28 +246,5 @@
toastService.add('error', gettext('Unable to retrieve the networks.')); toastService.add('error', gettext('Unable to retrieve the networks.'));
}); });
} }
///////////
// Utils //
///////////
function downloadTextAsFile(text, filename) {
// create text file as object url
var blob = new Blob([ text ], { "type" : "text/plain" });
window.URL = window.URL || window.webkitURL;
var fileurl = window.URL.createObjectURL(blob);
// provide text as downloaded file
$timeout(function() {
//Update view
var a = angular.element('<a></a>');
a.attr("href", fileurl);
a.attr("download", filename);
a.attr("target", "_blank");
angular.element(document.body).append(a);
a[0].click();
a.remove();
}, 0);
}
} }
}()); }());