github.com/blixtra/nomad@v0.7.2-0.20171221000451-da9a1d7bb050/ui/mirage/config.js (about) 1 import Ember from 'ember'; 2 import Response from 'ember-cli-mirage/response'; 3 import { HOSTS } from './common'; 4 import { logFrames, logEncode } from './data/logs'; 5 6 const { copy } = Ember; 7 8 export function findLeader(schema) { 9 const agent = schema.agents.first(); 10 return `${agent.address}:${agent.tags.port}`; 11 } 12 13 export default function() { 14 this.timing = 0; // delay for each request, automatically set to 0 during testing 15 16 this.namespace = 'v1'; 17 18 this.get('/jobs', function({ jobs }, { queryParams }) { 19 const json = this.serialize(jobs.all()); 20 const namespace = queryParams.namespace || 'default'; 21 return json 22 .filter( 23 job => 24 namespace === 'default' 25 ? !job.NamespaceID || job.NamespaceID === namespace 26 : job.NamespaceID === namespace 27 ) 28 .map(job => filterKeys(job, 'TaskGroups', 'NamespaceID')); 29 }); 30 31 this.get('/job/:id', function({ jobs }, { params, queryParams }) { 32 const job = jobs.all().models.find(job => { 33 const jobIsDefault = !job.namespaceId || job.namespaceId === 'default'; 34 const qpIsDefault = !queryParams.namespace || queryParams.namespace === 'default'; 35 return ( 36 job.id === params.id && 37 (job.namespaceId === queryParams.namespace || (jobIsDefault && qpIsDefault)) 38 ); 39 }); 40 41 return job ? this.serialize(job) : new Response(404, {}, null); 42 }); 43 44 this.get('/job/:id/summary', function({ jobSummaries }, { params }) { 45 return this.serialize(jobSummaries.findBy({ jobId: params.id })); 46 }); 47 48 this.get('/job/:id/allocations', function({ allocations }, { params }) { 49 return this.serialize(allocations.where({ jobId: params.id })); 50 }); 51 52 this.get('/job/:id/versions', function({ jobVersions }, { params }) { 53 return this.serialize(jobVersions.where({ jobId: params.id })); 54 }); 55 56 this.get('/job/:id/deployments', function({ deployments }, { params }) { 57 return this.serialize(deployments.where({ jobId: params.id })); 58 }); 59 60 this.get('/deployment/:id'); 61 62 this.get('/job/:id/evaluations', function({ evaluations }, { params }) { 63 return this.serialize(evaluations.where({ jobId: params.id })); 64 }); 65 66 this.get('/evaluation/:id'); 67 68 this.get('/deployment/allocations/:id', function(schema, { params }) { 69 const job = schema.jobs.find(schema.deployments.find(params.id).jobId); 70 const allocations = schema.allocations.where({ jobId: job.id }); 71 72 return this.serialize(allocations.slice(0, 3)); 73 }); 74 75 this.get('/nodes', function({ nodes }) { 76 const json = this.serialize(nodes.all()); 77 return json; 78 }); 79 80 this.get('/node/:id'); 81 82 this.get('/node/:id/allocations', function({ allocations }, { params }) { 83 return this.serialize(allocations.where({ nodeId: params.id })); 84 }); 85 86 this.get('/allocation/:id'); 87 88 this.get('/namespaces', function({ namespaces }) { 89 const records = namespaces.all(); 90 91 if (records.length) { 92 return this.serialize(records); 93 } 94 95 return new Response(501, {}, null); 96 }); 97 98 this.get('/namespace/:id', function({ namespaces }, { params }) { 99 if (namespaces.all().length) { 100 return this.serialize(namespaces.find(params.id)); 101 } 102 103 return new Response(501, {}, null); 104 }); 105 106 this.get('/agent/members', function({ agents }) { 107 return { 108 Members: this.serialize(agents.all()), 109 }; 110 }); 111 112 this.get('/status/leader', function(schema) { 113 return JSON.stringify(findLeader(schema)); 114 }); 115 116 this.get('/acl/token/self', function({ tokens }, req) { 117 const secret = req.requestHeaders['X-Nomad-Token']; 118 const tokenForSecret = tokens.findBy({ secretId: secret }); 119 120 // Return the token if it exists 121 if (tokenForSecret) { 122 return this.serialize(tokenForSecret); 123 } 124 125 // Client error if it doesn't 126 return new Response(400, {}, null); 127 }); 128 129 this.get('/acl/token/:id', function({ tokens }, req) { 130 const token = tokens.find(req.params.id); 131 const secret = req.requestHeaders['X-Nomad-Token']; 132 const tokenForSecret = tokens.findBy({ secretId: secret }); 133 134 // Return the token only if the request header matches the token 135 // or the token is of type management 136 if (token.secretId === secret || (tokenForSecret && tokenForSecret.type === 'management')) { 137 return this.serialize(token); 138 } 139 140 // Return not authorized otherwise 141 return new Response(403, {}, null); 142 }); 143 144 this.get('/acl/policy/:id', function({ policies, tokens }, req) { 145 const policy = policies.find(req.params.id); 146 const secret = req.requestHeaders['X-Nomad-Token']; 147 const tokenForSecret = tokens.findBy({ secretId: secret }); 148 149 // Return the policy only if the token that matches the request header 150 // includes the policy or if the token that matches the request header 151 // is of type management 152 if ( 153 tokenForSecret && 154 (tokenForSecret.policies.includes(policy) || tokenForSecret.type === 'management') 155 ) { 156 return this.serialize(policy); 157 } 158 159 // Return not authorized otherwise 160 return new Response(403, {}, null); 161 }); 162 163 // TODO: in the future, this hack may be replaceable with dynamic host name 164 // support in pretender: https://github.com/pretenderjs/pretender/issues/210 165 HOSTS.forEach(host => { 166 this.get(`http://${host}/v1/client/allocation/:id/stats`, function( 167 { clientAllocationStats }, 168 { params } 169 ) { 170 return this.serialize(clientAllocationStats.find(params.id)); 171 }); 172 173 this.get(`http://${host}/v1/client/stats`, function({ clientStats }) { 174 return this.serialize(clientStats.find(host)); 175 }); 176 177 this.get(`http://${host}/v1/client/fs/logs/:allocation_id`, function( 178 server, 179 { params, queryParams } 180 ) { 181 const allocation = server.allocations.find(params.allocation_id); 182 const tasks = allocation.taskStateIds.map(id => server.taskStates.find(id)); 183 184 if (!tasks.mapBy('name').includes(queryParams.task)) { 185 return new Response(400, {}, 'must include task name'); 186 } 187 188 if (queryParams.plain) { 189 return logFrames.join(''); 190 } 191 192 return logEncode(logFrames, logFrames.length - 1); 193 }); 194 }); 195 } 196 197 function filterKeys(object, ...keys) { 198 const clone = copy(object, true); 199 200 keys.forEach(key => { 201 delete clone[key]; 202 }); 203 204 return clone; 205 }