fix(endpoint-init): fix an issue when connecting to a remote TLS endpoint (#783)

pull/695/merge
Anthony Lapenna 2017-04-08 19:38:19 +01:00 committed by GitHub
parent 3883cc8b67
commit 44e48423ed
3 changed files with 43 additions and 50 deletions

View File

@ -21,10 +21,10 @@
<!-- endpoin-type radio --> <!-- endpoin-type radio -->
<div class="form-group"> <div class="form-group">
<div class="radio"> <div class="radio">
<label><input type="radio" name="endpointType" value="local" ng-model="formValues.endpointType" ng-click="cleanError()">Manage the Docker instance where Portainer is running</label> <label><input type="radio" name="endpointType" value="local" ng-model="formValues.endpointType" ng-click="resetErrorMessage()">Manage the Docker instance where Portainer is running</label>
</div> </div>
<div class="radio"> <div class="radio">
<label><input type="radio" name="endpointType" value="remote" ng-model="formValues.endpointType" ng-click="cleanError()">Manage a remote Docker instance</label> <label><input type="radio" name="endpointType" value="remote" ng-model="formValues.endpointType" ng-click="resetErrorMessage()">Manage a remote Docker instance</label>
</div> </div>
</div> </div>
<!-- endpoint-type radio --> <!-- endpoint-type radio -->

View File

@ -5,6 +5,7 @@ function ($scope, $state, EndpointService, StateManager, EndpointProvider, Messa
error: '', error: '',
uploadInProgress: false uploadInProgress: false
}; };
$scope.formValues = { $scope.formValues = {
endpointType: "remote", endpointType: "remote",
Name: '', Name: '',
@ -19,10 +20,29 @@ function ($scope, $state, EndpointService, StateManager, EndpointProvider, Messa
$state.go('dashboard'); $state.go('dashboard');
} }
$scope.cleanError = function() { $scope.resetErrorMessage = function() {
$scope.state.error = ''; $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() { $scope.createLocalEndpoint = function() {
$('#initEndpointSpinner').show(); $('#initEndpointSpinner').show();
$scope.state.error = ''; $scope.state.error = '';
@ -31,22 +51,10 @@ function ($scope, $state, EndpointService, StateManager, EndpointProvider, Messa
var TLS = false; var TLS = false;
EndpointService.createLocalEndpoint(name, URL, TLS, true) EndpointService.createLocalEndpoint(name, URL, TLS, true)
.then( .then(function success(data) {
function success(data) {
var endpointID = data.Id; var endpointID = data.Id;
EndpointProvider.setEndpointID(endpointID); updateEndpointState(data.Id);
StateManager.updateEndpointState(false).then( }, function error() {
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() {
$scope.state.error = 'Unable to create endpoint'; $scope.state.error = 'Unable to create endpoint';
}) })
.finally(function final() { .finally(function final() {
@ -63,28 +71,20 @@ function ($scope, $state, EndpointService, StateManager, EndpointProvider, Messa
var TLSCAFile = $scope.formValues.TLSCACert; var TLSCAFile = $scope.formValues.TLSCACert;
var TLSCertFile = $scope.formValues.TLSCert; var TLSCertFile = $scope.formValues.TLSCert;
var TLSKeyFile = $scope.formValues.TLSKey; 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) { .then(function success(data) {
var endpointID = data.Id; var endpointID = data.Id;
EndpointProvider.setEndpointID(endpointID); updateEndpointState(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';
});
});
}, function error(err) { }, function error(err) {
$('#initEndpointSpinner').hide(); showErrorMessage(err.msg);
$scope.state.uploadInProgress = false;
$scope.state.error = err.msg;
}, function update(evt) { }, function update(evt) {
if (evt.upload) { if (evt.upload) {
$scope.state.uploadInProgress = evt.upload; $scope.state.uploadInProgress = evt.upload;
} }
})
.finally(function final() {
$('#initEndpointSpinner').hide();
}); });
}; };
}]); }]);

View File

@ -53,37 +53,30 @@ angular.module('portainer.services')
return Endpoints.create({}, endpoint).$promise; 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 = { var endpoint = {
Name: name, Name: name,
URL: 'tcp://' + URL, URL: 'tcp://' + URL,
TLS: TLS TLS: TLS
}; };
var deferred = $q.defer(); var deferred = $q.defer();
Endpoints.create({active: active}, endpoint, function success(data) { Endpoints.create({}, endpoint).$promise
.then(function success(data) {
var endpointID = data.Id; var endpointID = data.Id;
if (TLS) { if (TLS) {
deferred.notify({upload: true}); 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}); deferred.notify({upload: false});
if (active) { deferred.resolve(data);
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});
}); });
} else { } else {
deferred.resolve(data); 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; return deferred.promise;
}; };