github.com/hernad/nomad@v1.6.112/ui/app/adapters/allocation.js (about)

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