2020-07-23 07:45:47 +00:00
|
|
|
import angular from 'angular';
|
2018-05-06 07:15:57 +00:00
|
|
|
|
2020-07-23 07:45:47 +00:00
|
|
|
import { AgentViewModel } from '../models/agent';
|
2018-10-07 22:44:08 +00:00
|
|
|
|
2020-07-23 07:45:47 +00:00
|
|
|
angular.module('portainer.agent').factory('AgentService', AgentServiceFactory);
|
|
|
|
|
|
|
|
function AgentServiceFactory(Agent, AgentVersion1, HttpRequestHelper, Host, StateManager) {
|
|
|
|
return {
|
|
|
|
agents,
|
|
|
|
hostInfo,
|
|
|
|
};
|
|
|
|
|
|
|
|
function getAgentApiVersion() {
|
|
|
|
const state = StateManager.getState();
|
|
|
|
return state.endpoint.agentApiVersion;
|
|
|
|
}
|
|
|
|
|
2022-11-02 11:29:26 +00:00
|
|
|
function hostInfo(endpointId, nodeName) {
|
2020-07-23 07:45:47 +00:00
|
|
|
HttpRequestHelper.setPortainerAgentTargetHeader(nodeName);
|
2022-11-02 11:29:26 +00:00
|
|
|
return Host.info({ endpointId }).$promise;
|
2020-07-23 07:45:47 +00:00
|
|
|
}
|
|
|
|
|
2022-11-02 11:29:26 +00:00
|
|
|
async function agents(endpointId) {
|
2020-07-23 07:45:47 +00:00
|
|
|
const agentVersion = getAgentApiVersion();
|
|
|
|
const service = agentVersion > 1 ? Agent : AgentVersion1;
|
|
|
|
try {
|
2022-11-02 11:29:26 +00:00
|
|
|
const agents = await service.query({ version: agentVersion, endpointId }).$promise;
|
2020-07-23 07:45:47 +00:00
|
|
|
return agents.map(function (item) {
|
|
|
|
return new AgentViewModel(item);
|
|
|
|
});
|
|
|
|
} catch (err) {
|
|
|
|
throw { msg: 'Unable to retrieve agents', err };
|
2018-10-07 22:44:08 +00:00
|
|
|
}
|
2020-07-23 07:45:47 +00:00
|
|
|
}
|
|
|
|
}
|