refactor(agent): refactor agentService to es6 (#4091)

pull/4098/head
Chaim Lev-Ari 2020-07-23 10:45:47 +03:00 committed by GitHub
parent 1ef7347f19
commit f761e65167
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 30 additions and 45 deletions

View File

@ -1,50 +1,35 @@
import angular from 'angular';
import { AgentViewModel } from '../models/agent';
angular.module('portainer.agent').factory('AgentService', [
'$q',
'Agent',
'AgentVersion1',
'HttpRequestHelper',
'Host',
'StateManager',
function AgentServiceFactory($q, Agent, AgentVersion1, HttpRequestHelper, Host, StateManager) {
'use strict';
var service = {};
angular.module('portainer.agent').factory('AgentService', AgentServiceFactory);
service.agents = agents;
service.hostInfo = hostInfo;
function AgentServiceFactory(Agent, AgentVersion1, HttpRequestHelper, Host, StateManager) {
return {
agents,
hostInfo,
};
function getAgentApiVersion() {
var state = StateManager.getState();
return state.endpoint.agentApiVersion;
function getAgentApiVersion() {
const state = StateManager.getState();
return state.endpoint.agentApiVersion;
}
function hostInfo(nodeName) {
HttpRequestHelper.setPortainerAgentTargetHeader(nodeName);
return Host.info().$promise;
}
async function agents() {
const agentVersion = getAgentApiVersion();
const service = agentVersion > 1 ? Agent : AgentVersion1;
try {
const agents = await service.query({ version: agentVersion }).$promise;
return agents.map(function (item) {
return new AgentViewModel(item);
});
} catch (err) {
throw { msg: 'Unable to retrieve agents', err };
}
function hostInfo(nodeName) {
HttpRequestHelper.setPortainerAgentTargetHeader(nodeName);
return Host.info().$promise;
}
function agents() {
var deferred = $q.defer();
var agentVersion = getAgentApiVersion();
var service = agentVersion > 1 ? Agent : AgentVersion1;
service
.query({ version: agentVersion })
.$promise.then(function success(data) {
var agents = data.map(function (item) {
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;
},
]);
}
}