diff --git a/app/components/endpointInit/endpointInit.html b/app/components/endpointInit/endpointInit.html index faf06866c..8c7abf963 100644 --- a/app/components/endpointInit/endpointInit.html +++ b/app/components/endpointInit/endpointInit.html @@ -21,10 +21,10 @@
- +
- +
diff --git a/app/components/endpointInit/endpointInitController.js b/app/components/endpointInit/endpointInitController.js index 911f6c994..afaf48321 100644 --- a/app/components/endpointInit/endpointInitController.js +++ b/app/components/endpointInit/endpointInitController.js @@ -5,6 +5,7 @@ function ($scope, $state, EndpointService, StateManager, EndpointProvider, Messa error: '', uploadInProgress: false }; + $scope.formValues = { endpointType: "remote", Name: '', @@ -19,10 +20,29 @@ function ($scope, $state, EndpointService, StateManager, EndpointProvider, Messa $state.go('dashboard'); } - $scope.cleanError = function() { + $scope.resetErrorMessage = function() { $scope.state.error = ''; }; + function showErrorMessage(message) { + $scope.state.uploadInProgress = false; + $scope.state.error = message; + } + + function updateEndpointState(endpointID) { + EndpointProvider.setEndpointID(endpointID); + StateManager.updateEndpointState(false) + .then(function success(data) { + $state.go('dashboard'); + }) + .catch(function error(err) { + EndpointService.deleteEndpoint(endpointID) + .then(function success() { + showErrorMessage('Unable to connect to the Docker endpoint'); + }); + }); + } + $scope.createLocalEndpoint = function() { $('#initEndpointSpinner').show(); $scope.state.error = ''; @@ -31,22 +51,10 @@ function ($scope, $state, EndpointService, StateManager, EndpointProvider, Messa var TLS = false; EndpointService.createLocalEndpoint(name, URL, TLS, true) - .then( - function success(data) { + .then(function success(data) { var endpointID = data.Id; - EndpointProvider.setEndpointID(endpointID); - StateManager.updateEndpointState(false).then( - function success() { - $state.go('dashboard'); - }, - function error(err) { - EndpointService.deleteEndpoint(endpointID) - .then(function success() { - $scope.state.error = 'Unable to connect to the Docker endpoint'; - }); - }); - }, - function error() { + updateEndpointState(data.Id); + }, function error() { $scope.state.error = 'Unable to create endpoint'; }) .finally(function final() { @@ -63,28 +71,20 @@ function ($scope, $state, EndpointService, StateManager, EndpointProvider, Messa var TLSCAFile = $scope.formValues.TLSCACert; var TLSCertFile = $scope.formValues.TLSCert; var TLSKeyFile = $scope.formValues.TLSKey; - EndpointService.createRemoteEndpoint(name, URL, TLS, TLSCAFile, TLSCertFile, TLSKeyFile, TLS ? false : true) + + EndpointService.createRemoteEndpoint(name, URL, TLS, TLSCAFile, TLSCertFile, TLSKeyFile) .then(function success(data) { var endpointID = data.Id; - EndpointProvider.setEndpointID(endpointID); - StateManager.updateEndpointState(false) - .then(function success() { - $state.go('dashboard'); - }, function error(err) { - EndpointService.deleteEndpoint(endpointID) - .then(function success() { - $('#initEndpointSpinner').hide(); - $scope.state.error = 'Unable to connect to the Docker endpoint'; - }); - }); + updateEndpointState(endpointID); }, function error(err) { - $('#initEndpointSpinner').hide(); - $scope.state.uploadInProgress = false; - $scope.state.error = err.msg; + showErrorMessage(err.msg); }, function update(evt) { if (evt.upload) { $scope.state.uploadInProgress = evt.upload; } + }) + .finally(function final() { + $('#initEndpointSpinner').hide(); }); }; }]); diff --git a/app/services/endpointService.js b/app/services/endpointService.js index 9531cceff..9202c695e 100644 --- a/app/services/endpointService.js +++ b/app/services/endpointService.js @@ -53,37 +53,30 @@ angular.module('portainer.services') return Endpoints.create({}, endpoint).$promise; }; - service.createRemoteEndpoint = function(name, URL, TLS, TLSCAFile, TLSCertFile, TLSKeyFile, active) { + service.createRemoteEndpoint = function(name, URL, TLS, TLSCAFile, TLSCertFile, TLSKeyFile) { var endpoint = { Name: name, URL: 'tcp://' + URL, TLS: TLS }; var deferred = $q.defer(); - Endpoints.create({active: active}, endpoint, function success(data) { + Endpoints.create({}, endpoint).$promise + .then(function success(data) { var endpointID = data.Id; if (TLS) { deferred.notify({upload: true}); - FileUploadService.uploadTLSFilesForEndpoint(endpointID, TLSCAFile, TLSCertFile, TLSKeyFile).then(function success(data) { + FileUploadService.uploadTLSFilesForEndpoint(endpointID, TLSCAFile, TLSCertFile, TLSKeyFile) + .then(function success() { deferred.notify({upload: false}); - if (active) { - Endpoints.setActiveEndpoint({}, {id: endpointID}, function success(data) { - deferred.resolve(data); - }, function error(err) { - deferred.reject({msg: 'Unable to create endpoint', err: err}); - }); - } else { - deferred.resolve(data); - } - }, function error(err) { - deferred.notify({upload: false}); - deferred.reject({msg: 'Unable to upload TLS certs', err: err}); + deferred.resolve(data); }); } else { deferred.resolve(data); } - }, function error(err) { - deferred.reject({msg: 'Unable to create endpoint', err: err}); + }) + .catch(function error(err) { + deferred.notify({upload: false}); + deferred.reject({msg: 'Unable to upload TLS certs', err: err}); }); return deferred.promise; };