2017-09-19 14:47:10 +00:00
|
|
|
import Ember from 'ember';
|
2017-10-10 18:23:10 +00:00
|
|
|
import Response from 'ember-cli-mirage/response';
|
2017-09-19 14:47:10 +00:00
|
|
|
import { HOSTS } from './common';
|
2017-11-16 02:12:16 +00:00
|
|
|
import { logFrames, logEncode } from './data/logs';
|
2017-09-19 14:47:10 +00:00
|
|
|
|
|
|
|
const { copy } = Ember;
|
|
|
|
|
|
|
|
export function findLeader(schema) {
|
|
|
|
const agent = schema.agents.first();
|
|
|
|
return `${agent.address}:${agent.tags.port}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default function() {
|
2017-10-10 16:36:36 +00:00
|
|
|
this.timing = 0; // delay for each request, automatically set to 0 during testing
|
2017-09-19 14:47:10 +00:00
|
|
|
|
|
|
|
this.namespace = 'v1';
|
|
|
|
|
2017-10-10 17:54:23 +00:00
|
|
|
this.get('/jobs', function({ jobs }, { queryParams }) {
|
2017-09-19 14:47:10 +00:00
|
|
|
const json = this.serialize(jobs.all());
|
2017-10-23 17:18:14 +00:00
|
|
|
const namespace = queryParams.namespace || 'default';
|
2017-10-10 17:54:23 +00:00
|
|
|
return json
|
2017-10-23 17:18:14 +00:00
|
|
|
.filter(
|
|
|
|
job =>
|
|
|
|
namespace === 'default'
|
|
|
|
? !job.NamespaceID || job.NamespaceID === namespace
|
|
|
|
: job.NamespaceID === namespace
|
|
|
|
)
|
2017-10-10 17:54:23 +00:00
|
|
|
.map(job => filterKeys(job, 'TaskGroups', 'NamespaceID'));
|
2017-09-19 14:47:10 +00:00
|
|
|
});
|
|
|
|
|
2017-10-23 17:18:14 +00:00
|
|
|
this.get('/job/:id', function({ jobs }, { params, queryParams }) {
|
|
|
|
const job = jobs.all().models.find(job => {
|
|
|
|
const jobIsDefault = !job.namespaceId || job.namespaceId === 'default';
|
|
|
|
const qpIsDefault = !queryParams.namespace || queryParams.namespace === 'default';
|
|
|
|
return (
|
|
|
|
job.id === params.id &&
|
|
|
|
(job.namespaceId === queryParams.namespace || (jobIsDefault && qpIsDefault))
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
return job ? this.serialize(job) : new Response(404, {}, null);
|
|
|
|
});
|
2017-09-19 14:47:10 +00:00
|
|
|
|
|
|
|
this.get('/job/:id/summary', function({ jobSummaries }, { params }) {
|
|
|
|
return this.serialize(jobSummaries.findBy({ jobId: params.id }));
|
|
|
|
});
|
|
|
|
|
|
|
|
this.get('/job/:id/allocations', function({ allocations }, { params }) {
|
|
|
|
return this.serialize(allocations.where({ jobId: params.id }));
|
|
|
|
});
|
|
|
|
|
|
|
|
this.get('/job/:id/versions', function({ jobVersions }, { params }) {
|
|
|
|
return this.serialize(jobVersions.where({ jobId: params.id }));
|
|
|
|
});
|
|
|
|
|
|
|
|
this.get('/job/:id/deployments', function({ deployments }, { params }) {
|
|
|
|
return this.serialize(deployments.where({ jobId: params.id }));
|
|
|
|
});
|
|
|
|
|
|
|
|
this.get('/deployment/:id');
|
|
|
|
|
2017-11-29 23:36:34 +00:00
|
|
|
this.get('/job/:id/evaluations', function({ evaluations }, { params }) {
|
|
|
|
return this.serialize(evaluations.where({ jobId: params.id }));
|
|
|
|
});
|
|
|
|
|
|
|
|
this.get('/evaluation/:id');
|
|
|
|
|
2017-09-19 14:47:10 +00:00
|
|
|
this.get('/deployment/allocations/:id', function(schema, { params }) {
|
|
|
|
const job = schema.jobs.find(schema.deployments.find(params.id).jobId);
|
|
|
|
const allocations = schema.allocations.where({ jobId: job.id });
|
|
|
|
|
|
|
|
return this.serialize(allocations.slice(0, 3));
|
|
|
|
});
|
|
|
|
|
|
|
|
this.get('/nodes', function({ nodes }) {
|
|
|
|
const json = this.serialize(nodes.all());
|
|
|
|
return json;
|
|
|
|
});
|
|
|
|
|
|
|
|
this.get('/node/:id');
|
|
|
|
|
|
|
|
this.get('/node/:id/allocations', function({ allocations }, { params }) {
|
|
|
|
return this.serialize(allocations.where({ nodeId: params.id }));
|
|
|
|
});
|
|
|
|
|
|
|
|
this.get('/allocation/:id');
|
|
|
|
|
2017-10-10 18:23:10 +00:00
|
|
|
this.get('/namespaces', function({ namespaces }) {
|
|
|
|
const records = namespaces.all();
|
|
|
|
|
|
|
|
if (records.length) {
|
|
|
|
return this.serialize(records);
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Response(501, {}, null);
|
|
|
|
});
|
|
|
|
|
|
|
|
this.get('/namespace/:id', function({ namespaces }, { params }) {
|
|
|
|
if (namespaces.all().length) {
|
|
|
|
return this.serialize(namespaces.find(params.id));
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Response(501, {}, null);
|
|
|
|
});
|
2017-10-07 01:27:36 +00:00
|
|
|
|
2017-09-19 14:47:10 +00:00
|
|
|
this.get('/agent/members', function({ agents }) {
|
|
|
|
return {
|
|
|
|
Members: this.serialize(agents.all()),
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
this.get('/status/leader', function(schema) {
|
|
|
|
return JSON.stringify(findLeader(schema));
|
|
|
|
});
|
|
|
|
|
2017-10-14 19:42:14 +00:00
|
|
|
this.get('/acl/token/self', function({ tokens }, req) {
|
|
|
|
const secret = req.requestHeaders['X-Nomad-Token'];
|
|
|
|
const tokenForSecret = tokens.findBy({ secretId: secret });
|
|
|
|
|
|
|
|
// Return the token if it exists
|
|
|
|
if (tokenForSecret) {
|
|
|
|
return this.serialize(tokenForSecret);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Client error if it doesn't
|
|
|
|
return new Response(400, {}, null);
|
|
|
|
});
|
|
|
|
|
2017-10-14 18:01:28 +00:00
|
|
|
this.get('/acl/token/:id', function({ tokens }, req) {
|
|
|
|
const token = tokens.find(req.params.id);
|
|
|
|
const secret = req.requestHeaders['X-Nomad-Token'];
|
|
|
|
const tokenForSecret = tokens.findBy({ secretId: secret });
|
|
|
|
|
|
|
|
// Return the token only if the request header matches the token
|
|
|
|
// or the token is of type management
|
|
|
|
if (token.secretId === secret || (tokenForSecret && tokenForSecret.type === 'management')) {
|
|
|
|
return this.serialize(token);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return not authorized otherwise
|
|
|
|
return new Response(403, {}, null);
|
|
|
|
});
|
|
|
|
|
|
|
|
this.get('/acl/policy/:id', function({ policies, tokens }, req) {
|
|
|
|
const policy = policies.find(req.params.id);
|
|
|
|
const secret = req.requestHeaders['X-Nomad-Token'];
|
|
|
|
const tokenForSecret = tokens.findBy({ secretId: secret });
|
|
|
|
|
|
|
|
// Return the policy only if the token that matches the request header
|
|
|
|
// includes the policy or if the token that matches the request header
|
|
|
|
// is of type management
|
|
|
|
if (
|
|
|
|
tokenForSecret &&
|
|
|
|
(tokenForSecret.policies.includes(policy) || tokenForSecret.type === 'management')
|
|
|
|
) {
|
|
|
|
return this.serialize(policy);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return not authorized otherwise
|
|
|
|
return new Response(403, {}, null);
|
|
|
|
});
|
|
|
|
|
2017-09-19 14:47:10 +00:00
|
|
|
// TODO: in the future, this hack may be replaceable with dynamic host name
|
|
|
|
// support in pretender: https://github.com/pretenderjs/pretender/issues/210
|
|
|
|
HOSTS.forEach(host => {
|
|
|
|
this.get(`http://${host}/v1/client/allocation/:id/stats`, function(
|
|
|
|
{ clientAllocationStats },
|
|
|
|
{ params }
|
|
|
|
) {
|
|
|
|
return this.serialize(clientAllocationStats.find(params.id));
|
|
|
|
});
|
|
|
|
|
|
|
|
this.get(`http://${host}/v1/client/stats`, function({ clientStats }) {
|
|
|
|
return this.serialize(clientStats.find(host));
|
|
|
|
});
|
2017-11-16 02:12:16 +00:00
|
|
|
|
|
|
|
this.get(`http://${host}/v1/client/fs/logs/:allocation_id`, function(
|
|
|
|
server,
|
|
|
|
{ params, queryParams }
|
|
|
|
) {
|
|
|
|
const allocation = server.allocations.find(params.allocation_id);
|
2017-11-16 02:13:28 +00:00
|
|
|
const tasks = allocation.taskStateIds.map(id => server.taskStates.find(id));
|
2017-11-16 02:12:16 +00:00
|
|
|
|
2017-11-16 02:13:28 +00:00
|
|
|
if (!tasks.mapBy('name').includes(queryParams.task)) {
|
2017-11-16 02:12:16 +00:00
|
|
|
return new Response(400, {}, 'must include task name');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (queryParams.plain) {
|
|
|
|
return logFrames.join('');
|
|
|
|
}
|
|
|
|
|
|
|
|
return logEncode(logFrames, logFrames.length - 1);
|
|
|
|
});
|
2017-09-19 14:47:10 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function filterKeys(object, ...keys) {
|
|
|
|
const clone = copy(object, true);
|
|
|
|
|
|
|
|
keys.forEach(key => {
|
|
|
|
delete clone[key];
|
|
|
|
});
|
|
|
|
|
|
|
|
return clone;
|
|
|
|
}
|