github.com/Ilhicas/nomad@v1.0.4-0.20210304152020-e86851182bc3/ui/app/adapters/allocation.js (about) 1 import Watchable from './watchable'; 2 import addToPath from 'nomad-ui/utils/add-to-path'; 3 4 export default class AllocationAdapter extends Watchable { 5 stop = adapterAction('/stop'); 6 7 restart(allocation, taskName) { 8 const prefix = `${this.host || '/'}${this.urlPrefix()}`; 9 const url = `${prefix}/client/allocation/${allocation.id}/restart`; 10 return this.ajax(url, 'PUT', { 11 data: taskName && { TaskName: taskName }, 12 }); 13 } 14 15 ls(model, path) { 16 return this.token 17 .authorizedRequest(`/v1/client/fs/ls/${model.id}?path=${encodeURIComponent(path)}`) 18 .then(handleFSResponse); 19 } 20 21 stat(model, path) { 22 return this.token 23 .authorizedRequest(`/v1/client/fs/stat/${model.id}?path=${encodeURIComponent(path)}`) 24 .then(handleFSResponse); 25 } 26 } 27 28 async function handleFSResponse(response) { 29 if (response.ok) { 30 return response.json(); 31 } else { 32 const body = await response.text(); 33 34 // TODO update this if/when endpoint returns 404 as expected 35 const statusIs500 = response.status === 500; 36 const bodyIncludes404Text = body.includes('no such file or directory'); 37 38 const translatedCode = statusIs500 && bodyIncludes404Text ? 404 : response.status; 39 40 throw { 41 code: translatedCode, 42 toString: () => body, 43 }; 44 } 45 } 46 47 function adapterAction(path, verb = 'POST') { 48 return function(allocation) { 49 const url = addToPath(this.urlForFindRecord(allocation.id, 'allocation'), path); 50 return this.ajax(url, verb); 51 }; 52 }