2017-01-31 23:26:29 +00:00
|
|
|
angular.module('portainer.services')
|
|
|
|
.factory('EndpointService', ['$q', 'Endpoints', 'FileUploadService', function EndpointServiceFactory($q, Endpoints, FileUploadService) {
|
|
|
|
'use strict';
|
2017-03-12 16:24:15 +00:00
|
|
|
var service = {};
|
|
|
|
|
|
|
|
service.endpoint = function(endpointID) {
|
|
|
|
return Endpoints.get({id: endpointID}).$promise;
|
|
|
|
};
|
|
|
|
|
|
|
|
service.endpoints = function() {
|
|
|
|
return Endpoints.query({}).$promise;
|
|
|
|
};
|
|
|
|
|
2017-05-23 18:56:10 +00:00
|
|
|
service.updateAccess = function(id, authorizedUserIDs, authorizedTeamIDs) {
|
|
|
|
return Endpoints.updateAccess({id: id}, {authorizedUsers: authorizedUserIDs, authorizedTeams: authorizedTeamIDs}).$promise;
|
2017-03-12 16:24:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
service.updateEndpoint = function(id, endpointParams) {
|
|
|
|
var query = {
|
|
|
|
name: endpointParams.name,
|
2017-05-01 10:19:43 +00:00
|
|
|
PublicURL: endpointParams.PublicURL,
|
2017-03-12 16:24:15 +00:00
|
|
|
TLS: endpointParams.TLS,
|
2017-09-14 06:08:37 +00:00
|
|
|
TLSSkipVerify: endpointParams.TLSSkipVerify,
|
|
|
|
TLSSkipClientVerify: endpointParams.TLSSkipClientVerify,
|
2017-03-12 16:24:15 +00:00
|
|
|
authorizedUsers: endpointParams.authorizedUsers
|
|
|
|
};
|
|
|
|
if (endpointParams.type && endpointParams.URL) {
|
2017-05-23 18:56:10 +00:00
|
|
|
query.URL = endpointParams.type === 'local' ? ('unix://' + endpointParams.URL) : ('tcp://' + endpointParams.URL);
|
2017-03-12 16:24:15 +00:00
|
|
|
}
|
2017-04-09 18:38:41 +00:00
|
|
|
|
2017-03-12 16:24:15 +00:00
|
|
|
var deferred = $q.defer();
|
2017-04-09 18:38:41 +00:00
|
|
|
FileUploadService.uploadTLSFilesForEndpoint(id, endpointParams.TLSCACert, endpointParams.TLSCert, endpointParams.TLSKey)
|
2017-03-12 16:24:15 +00:00
|
|
|
.then(function success() {
|
2017-04-09 18:38:41 +00:00
|
|
|
deferred.notify({upload: false});
|
|
|
|
return Endpoints.update({id: id}, query).$promise;
|
2017-03-12 16:24:15 +00:00
|
|
|
})
|
|
|
|
.then(function success(data) {
|
|
|
|
deferred.resolve(data);
|
|
|
|
})
|
|
|
|
.catch(function error(err) {
|
|
|
|
deferred.notify({upload: false});
|
|
|
|
deferred.reject({msg: 'Unable to update endpoint', err: err});
|
|
|
|
});
|
|
|
|
return deferred.promise;
|
|
|
|
};
|
|
|
|
|
|
|
|
service.deleteEndpoint = function(endpointID) {
|
|
|
|
return Endpoints.remove({id: endpointID}).$promise;
|
|
|
|
};
|
|
|
|
|
|
|
|
service.createLocalEndpoint = function(name, URL, TLS, active) {
|
|
|
|
var endpoint = {
|
2017-05-23 18:56:10 +00:00
|
|
|
Name: 'local',
|
|
|
|
URL: 'unix:///var/run/docker.sock',
|
2017-03-12 16:24:15 +00:00
|
|
|
TLS: false
|
|
|
|
};
|
|
|
|
return Endpoints.create({}, endpoint).$promise;
|
|
|
|
};
|
|
|
|
|
2017-09-14 06:08:37 +00:00
|
|
|
service.createRemoteEndpoint = function(name, URL, PublicURL, TLS, TLSSkipVerify, TLSSkipClientVerify, TLSCAFile, TLSCertFile, TLSKeyFile) {
|
2017-03-12 16:24:15 +00:00
|
|
|
var endpoint = {
|
|
|
|
Name: name,
|
|
|
|
URL: 'tcp://' + URL,
|
2017-05-01 10:19:43 +00:00
|
|
|
PublicURL: PublicURL,
|
2017-09-14 06:08:37 +00:00
|
|
|
TLS: TLS,
|
|
|
|
TLSSkipVerify: TLSSkipVerify,
|
|
|
|
TLSSkipClientVerify: TLSSkipClientVerify
|
2017-03-12 16:24:15 +00:00
|
|
|
};
|
|
|
|
var deferred = $q.defer();
|
2017-04-08 18:38:19 +00:00
|
|
|
Endpoints.create({}, endpoint).$promise
|
|
|
|
.then(function success(data) {
|
2017-03-12 16:24:15 +00:00
|
|
|
var endpointID = data.Id;
|
2017-09-14 06:08:37 +00:00
|
|
|
if (!TLSSkipVerify || !TLSSkipClientVerify) {
|
2017-03-12 16:24:15 +00:00
|
|
|
deferred.notify({upload: true});
|
2017-04-08 18:38:19 +00:00
|
|
|
FileUploadService.uploadTLSFilesForEndpoint(endpointID, TLSCAFile, TLSCertFile, TLSKeyFile)
|
|
|
|
.then(function success() {
|
2017-01-31 23:26:29 +00:00
|
|
|
deferred.notify({upload: false});
|
2017-04-08 18:38:19 +00:00
|
|
|
deferred.resolve(data);
|
2017-01-31 23:26:29 +00:00
|
|
|
});
|
2017-03-12 16:24:15 +00:00
|
|
|
} else {
|
|
|
|
deferred.resolve(data);
|
|
|
|
}
|
2017-04-08 18:38:19 +00:00
|
|
|
})
|
|
|
|
.catch(function error(err) {
|
|
|
|
deferred.notify({upload: false});
|
|
|
|
deferred.reject({msg: 'Unable to upload TLS certs', err: err});
|
2017-03-12 16:24:15 +00:00
|
|
|
});
|
|
|
|
return deferred.promise;
|
2017-01-31 23:26:29 +00:00
|
|
|
};
|
2017-03-12 16:24:15 +00:00
|
|
|
|
|
|
|
return service;
|
2017-01-31 23:26:29 +00:00
|
|
|
}]);
|