2019-03-21 05:46:49 +00:00
|
|
|
import { AgentViewModel } from '../models/agent';
|
|
|
|
|
2018-10-07 22:44:08 +00:00
|
|
|
angular.module('portainer.agent').factory('AgentService', [
|
2020-04-10 21:54:53 +00:00
|
|
|
'$q',
|
|
|
|
'Agent',
|
|
|
|
'AgentVersion1',
|
|
|
|
'HttpRequestHelper',
|
|
|
|
'Host',
|
|
|
|
'StateManager',
|
2018-10-26 03:16:29 +00:00
|
|
|
function AgentServiceFactory($q, Agent, AgentVersion1, HttpRequestHelper, Host, StateManager) {
|
2018-10-07 22:44:08 +00:00
|
|
|
'use strict';
|
|
|
|
var service = {};
|
2018-05-06 07:15:57 +00:00
|
|
|
|
2018-10-07 22:44:08 +00:00
|
|
|
service.agents = agents;
|
|
|
|
service.hostInfo = hostInfo;
|
2018-05-06 07:15:57 +00:00
|
|
|
|
2018-10-26 03:16:29 +00:00
|
|
|
function getAgentApiVersion() {
|
|
|
|
var state = StateManager.getState();
|
|
|
|
return state.endpoint.agentApiVersion;
|
|
|
|
}
|
|
|
|
|
2018-10-07 22:44:08 +00:00
|
|
|
function hostInfo(nodeName) {
|
|
|
|
HttpRequestHelper.setPortainerAgentTargetHeader(nodeName);
|
|
|
|
return Host.info().$promise;
|
|
|
|
}
|
2018-05-06 07:15:57 +00:00
|
|
|
|
2018-10-07 22:44:08 +00:00
|
|
|
function agents() {
|
|
|
|
var deferred = $q.defer();
|
2018-05-06 07:15:57 +00:00
|
|
|
|
2018-10-26 03:16:29 +00:00
|
|
|
var agentVersion = getAgentApiVersion();
|
|
|
|
var service = agentVersion > 1 ? Agent : AgentVersion1;
|
2020-04-10 21:54:53 +00:00
|
|
|
|
|
|
|
service
|
|
|
|
.query({ version: agentVersion })
|
2018-10-07 22:44:08 +00:00
|
|
|
.$promise.then(function success(data) {
|
2020-04-10 21:54:53 +00:00
|
|
|
var agents = data.map(function (item) {
|
2018-10-07 22:44:08 +00:00
|
|
|
return new AgentViewModel(item);
|
|
|
|
});
|
|
|
|
deferred.resolve(agents);
|
|
|
|
})
|
|
|
|
.catch(function error(err) {
|
|
|
|
deferred.reject({ msg: 'Unable to retrieve agents', err: err });
|
|
|
|
});
|
|
|
|
|
|
|
|
return deferred.promise;
|
|
|
|
}
|
|
|
|
|
|
|
|
return service;
|
2020-04-10 21:54:53 +00:00
|
|
|
},
|
2018-10-07 22:44:08 +00:00
|
|
|
]);
|