add agent serializer

This commit adds a serializer for the Agent. When the factory was originally
designed to have the Member properities directly on the Agent class which was
not set-up properly technically, but since we didn't really make use of the
Agent endpoint.
This commit is contained in:
Jai Bhagat 2021-06-16 11:40:24 -04:00
parent b92ab047ff
commit 2efa6cc7e4
4 changed files with 47 additions and 36 deletions

View file

@ -39,10 +39,12 @@ export default class SystemService extends Service {
.authorizedRawRequest(`/${namespace}/agent/self`)
.then(jsonWithDefault({}))
.then(agent => {
const { Version, VersionPrerelease, VersionMetadata } = agent.config.Version;
agent.version = Version;
if (VersionPrerelease) agent.version += `-${VersionPrerelease}`;
if (VersionMetadata) agent.version += `+${VersionMetadata}`;
if (agent.config.Version) {
const { Version, VersionPrerelease, VersionMetadata } = agent.config.Version;
agent.version = Version;
if (VersionPrerelease) agent.version = `${agent.version}-${VersionPrerelease}`;
if (VersionMetadata) agent.version = `${agent.version}+${VersionMetadata}`;
}
return agent;
}),
});

View file

@ -335,21 +335,12 @@ export default function() {
const firstRegion = regions.first();
return {
ServerRegion: firstRegion ? firstRegion.id : null,
Members: this.serialize(agents.all()),
Members: this.serialize(agents.all()).map(({ member }) => ({ ...member })),
};
});
this.get('/agent/self', function({ agents }) {
return {
member: this.serialize(agents.first()),
config: {
Version: {
Version: '1.1.0',
VersionMetadata: 'ent',
VersionPrerelease: 'dev',
},
},
};
return agents.first();
});
this.get('/agent/monitor', function({ agents, nodes }, { queryParams }) {

View file

@ -8,32 +8,40 @@ const AGENT_STATUSES = ['alive', 'leaving', 'left', 'failed'];
export default Factory.extend({
id: i => (i / 100 >= 1 ? `${UUIDS[i]}-${i}` : UUIDS[i]),
name: () => `nomad@${faker.random.boolean() ? faker.internet.ip() : faker.internet.ipv6()}`,
status: () => faker.helpers.randomize(AGENT_STATUSES),
serfPort: () => faker.random.number({ min: 4000, max: 4999 }),
address() {
return this.name.split('@')[1];
},
config: makeConfig,
tags() {
const rpcPortCandidate = faker.random.number({ min: 4000, max: 4999 });
return {
port: rpcPortCandidate === this.serfPort ? rpcPortCandidate + 1 : rpcPortCandidate,
dc: faker.helpers.randomize(DATACENTERS),
};
},
});
function makeConfig() {
return {
config: {
Version: {
Version: '1.1.0',
VersionMetadata: 'ent',
VersionPrerelease: 'dev',
},
},
member: () => {
const name = generateName();
const serfPort = faker.random.number({ min: 4000, max: 4999 });
return {
Name: name,
Port: serfPort,
Status: faker.helpers.randomize(AGENT_STATUSES),
Address: generateAddress(name),
Tags: generateTags(serfPort),
};
},
});
function generateName() {
return `nomad@${faker.random.boolean() ? faker.internet.ip() : faker.internet.ipv6()}`;
}
function generateAddress(name) {
return name.split('@')[1];
}
function generateTags(serfPort) {
const rpcPortCandidate = faker.random.number({ min: 4000, max: 4999 });
return {
port: rpcPortCandidate === serfPort ? rpcPortCandidate + 1 : rpcPortCandidate,
dc: faker.helpers.randomize(DATACENTERS),
};
}

View file

@ -0,0 +1,10 @@
import ApplicationSerializer from './application';
export default ApplicationSerializer.extend({
keyForAttribute(str) {
if (str === 'config' || str === 'member') {
return str;
}
return ApplicationSerializer.prototype.keyForAttribute.apply(this, arguments);
},
});