Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
this.post('/jobs/parse', function(schema, req) {
const body = JSON.parse(req.requestBody);
if (!body.JobHCL)
return new Response(400, {}, 'JobHCL is a required field on the request payload');
if (!body.Canonicalize) return new Response(400, {}, 'Expected Canonicalize to be true');
// Parse the name out of the first real line of HCL to match IDs in the new job record
// Regex expectation:
// in: job "job-name" {
// out: job-name
const nameFromHCLBlock = /.+?"(.+?)"/;
const jobName = body.JobHCL.trim()
.split('\n')[0]
.match(nameFromHCLBlock)[1];
const job = server.create('job', { id: jobName });
return new Response(200, {}, this.serialize(job));
});
this.post('/jobs/parse', function(schema, req) {
const body = JSON.parse(req.requestBody);
if (!body.JobHCL)
return new Response(400, {}, 'JobHCL is a required field on the request payload');
if (!body.Canonicalize) return new Response(400, {}, 'Expected Canonicalize to be true');
// Parse the name out of the first real line of HCL to match IDs in the new job record
// Regex expectation:
// in: job "job-name" {
// out: job-name
const nameFromHCLBlock = /.+?"(.+?)"/;
const jobName = body.JobHCL.trim()
.split('\n')[0]
.match(nameFromHCLBlock)[1];
const job = server.create('job', { id: jobName });
return new Response(200, {}, this.serialize(job));
});
return new Response(404, {}, null);
}
}
// 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);
});
this.post('/attachments/upload', (_, request) => {
const response = uploadResponse(request.requestBody, {
directUploadURL: '/api/attachments/direct-upload'
});
return new Response(200, response.headers, response.body);
});
metadata.then((metadata) => {
request.requestBody = Object.assign({
[file.key]: metadata
}, data);
if (timedOut) {
resolve(new Response(408));
return;
}
resolve(fn.call(this, db, request));
}).catch((error) => {
resolve(new Response(500, {}, { error: error.message }));
this.get('/job/:id/deployment', function({ deployments }, { params }) {
const deployment = deployments.where({ jobId: params.id }).models[0];
return deployment ? this.serialize(deployment) : new Response(200, {}, 'null');
});
this.get('/client/stats', function({ clientStats }, { queryParams }) {
const seed = Math.random();
if (seed > 0.8) {
const stats = clientStats.find(queryParams.node_id);
stats.update({
timestamp: Date.now() * 1000000,
CPUTicksConsumed: stats.CPUTicksConsumed + (Math.random() * 20 - 10),
});
return this.serialize(stats);
} else {
return new Response(500, {}, null);
}
});
function okEmpty() {
return new Response(200, {}, '{}');
}
this.post('/job/:id/plan', function(schema, req) {
const body = JSON.parse(req.requestBody);
if (!body.Job) return new Response(400, {}, 'Job is a required field on the request payload');
if (!body.Diff) return new Response(400, {}, 'Expected Diff to be true');
const FailedTGAllocs = body.Job.Unschedulable && generateFailedTGAllocs(body.Job);
return new Response(
200,
{},
JSON.stringify({ FailedTGAllocs, Diff: generateDiff(req.params.id) })
);
});
function okEmpty() {
return new Response(200, {}, '{}');
}