open-nomad/ui/app/adapters/allocation.js

54 lines
1.2 KiB
JavaScript
Raw Normal View History

import Watchable from './watchable';
import addToPath from 'nomad-ui/utils/add-to-path';
export default class AllocationAdapter extends Watchable {
stop = adapterAction('/stop');
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 },
});
}
ls(model, path) {
return this.token
2021-12-28 16:08:12 +00:00
.authorizedRequest(
`/v1/client/fs/ls/${model.id}?path=${encodeURIComponent(path)}`
)
.then(handleFSResponse);
}
stat(model, path) {
return this.token
2021-12-28 16:08:12 +00:00
.authorizedRequest(
`/v1/client/fs/stat/${model.id}?path=${encodeURIComponent(path)}`
)
.then(handleFSResponse);
}
}
async function handleFSResponse(response) {
if (response.ok) {
return response.json();
} else {
const body = await response.text();
throw {
code: response.status,
toString: () => body,
};
}
}
function adapterAction(path, verb = 'POST') {
2021-12-28 14:45:20 +00:00
return function (allocation) {
2021-12-28 16:08:12 +00:00
const url = addToPath(
this.urlForFindRecord(allocation.id, 'allocation'),
path
);
return this.ajax(url, verb);
};
}