github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/ui/app/adapters/allocation.js (about)

     1  import Watchable from './watchable';
     2  import addToPath from 'nomad-ui/utils/add-to-path';
     3  import classic from 'ember-classic-decorator';
     4  
     5  @classic
     6  export default class AllocationAdapter extends Watchable {
     7    stop = adapterAction('/stop');
     8  
     9    restart(allocation, taskName) {
    10      const prefix = `${this.host || '/'}${this.urlPrefix()}`;
    11      const url = `${prefix}/client/allocation/${allocation.id}/restart`;
    12      return this.ajax(url, 'PUT', {
    13        data: taskName && { TaskName: taskName },
    14      });
    15    }
    16  
    17    restartAll(allocation) {
    18      const prefix = `${this.host || '/'}${this.urlPrefix()}`;
    19      const url = `${prefix}/client/allocation/${allocation.id}/restart`;
    20      return this.ajax(url, 'PUT', { data: { AllTasks: true } });
    21    }
    22  
    23    ls(model, path) {
    24      return this.token
    25        .authorizedRequest(
    26          `/v1/client/fs/ls/${model.id}?path=${encodeURIComponent(path)}`
    27        )
    28        .then(handleFSResponse);
    29    }
    30  
    31    stat(model, path) {
    32      return this.token
    33        .authorizedRequest(
    34          `/v1/client/fs/stat/${model.id}?path=${encodeURIComponent(path)}`
    35        )
    36        .then(handleFSResponse);
    37    }
    38  
    39    async check(model) {
    40      const res = await this.token.authorizedRequest(
    41        `/v1/client/allocation/${model.id}/checks`
    42      );
    43      const data = await res.json();
    44      // Append allocation ID to each check
    45      Object.values(data).forEach((check) => {
    46        check.Alloc = model.id;
    47        check.Timestamp = check.Timestamp * 1000; // Convert to milliseconds
    48      });
    49      return data;
    50    }
    51  }
    52  
    53  async function handleFSResponse(response) {
    54    if (response.ok) {
    55      return response.json();
    56    } else {
    57      const body = await response.text();
    58  
    59      throw {
    60        code: response.status,
    61        toString: () => body,
    62      };
    63    }
    64  }
    65  
    66  function adapterAction(path, verb = 'POST') {
    67    return function (allocation) {
    68      const url = addToPath(
    69        this.urlForFindRecord(allocation.id, 'allocation'),
    70        path
    71      );
    72      return this.ajax(url, verb);
    73    };
    74  }