2018-02-28 19:48:06 +00:00
|
|
|
import Watchable from './watchable';
|
2019-05-17 00:43:00 +00:00
|
|
|
import addToPath from 'nomad-ui/utils/add-to-path';
|
2018-02-28 19:48:06 +00:00
|
|
|
|
2020-06-11 21:23:00 +00:00
|
|
|
export default class AllocationAdapter extends Watchable {
|
|
|
|
stop = adapterAction('/stop');
|
2019-05-20 22:27:20 +00:00
|
|
|
|
|
|
|
restart(allocation, taskName) {
|
|
|
|
const prefix = `${this.host || '/'}${this.urlPrefix()}`;
|
|
|
|
const url = `${prefix}/client/allocation/${allocation.id}/restart`;
|
|
|
|
return this.ajax(url, 'PUT', {
|
|
|
|
data: taskName && { TaskName: taskName },
|
|
|
|
});
|
2020-06-11 21:23:00 +00:00
|
|
|
}
|
2020-06-01 13:15:59 +00:00
|
|
|
|
|
|
|
ls(model, path) {
|
|
|
|
return this.token
|
|
|
|
.authorizedRequest(`/v1/client/fs/ls/${model.id}?path=${encodeURIComponent(path)}`)
|
|
|
|
.then(handleFSResponse);
|
2020-06-11 21:23:00 +00:00
|
|
|
}
|
2020-06-01 13:15:59 +00:00
|
|
|
|
|
|
|
stat(model, path) {
|
|
|
|
return this.token
|
2020-06-11 21:23:00 +00:00
|
|
|
.authorizedRequest(`/v1/client/fs/stat/${model.id}?path=${encodeURIComponent(path)}`)
|
2020-06-01 13:15:59 +00:00
|
|
|
.then(handleFSResponse);
|
2020-06-11 21:23:00 +00:00
|
|
|
}
|
|
|
|
}
|
2019-05-17 00:43:00 +00:00
|
|
|
|
2020-06-01 13:15:59 +00:00
|
|
|
async function handleFSResponse(response) {
|
|
|
|
if (response.ok) {
|
|
|
|
return response.json();
|
|
|
|
} else {
|
|
|
|
const body = await response.text();
|
|
|
|
|
|
|
|
// TODO update this if/when endpoint returns 404 as expected
|
|
|
|
const statusIs500 = response.status === 500;
|
|
|
|
const bodyIncludes404Text = body.includes('no such file or directory');
|
|
|
|
|
|
|
|
const translatedCode = statusIs500 && bodyIncludes404Text ? 404 : response.status;
|
|
|
|
|
|
|
|
throw {
|
|
|
|
code: translatedCode,
|
|
|
|
toString: () => body,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-17 00:43:00 +00:00
|
|
|
function adapterAction(path, verb = 'POST') {
|
|
|
|
return function(allocation) {
|
2019-05-20 22:27:20 +00:00
|
|
|
const url = addToPath(this.urlForFindRecord(allocation.id, 'allocation'), path);
|
2019-05-17 00:43:00 +00:00
|
|
|
return this.ajax(url, verb);
|
|
|
|
};
|
|
|
|
}
|