github.com/anuvu/nomad@v0.8.7-atom1/ui/app/adapters/job.js (about)

     1  import { inject as service } from '@ember/service';
     2  import { assign } from '@ember/polyfills';
     3  import Watchable from './watchable';
     4  
     5  export default Watchable.extend({
     6    system: service(),
     7  
     8    buildQuery() {
     9      const namespace = this.get('system.activeNamespace.id');
    10  
    11      if (namespace && namespace !== 'default') {
    12        return { namespace };
    13      }
    14      return {};
    15    },
    16  
    17    findAll() {
    18      const namespace = this.get('system.activeNamespace');
    19      return this._super(...arguments).then(data => {
    20        data.forEach(job => {
    21          job.Namespace = namespace ? namespace.get('id') : 'default';
    22        });
    23        return data;
    24      });
    25    },
    26  
    27    findRecordSummary(modelName, name, snapshot, namespaceQuery) {
    28      return this.ajax(`${this.buildURL(modelName, name, snapshot, 'findRecord')}/summary`, 'GET', {
    29        data: assign(this.buildQuery() || {}, namespaceQuery),
    30      });
    31    },
    32  
    33    findRecord(store, type, id, snapshot) {
    34      const [, namespace] = JSON.parse(id);
    35      const namespaceQuery = namespace && namespace !== 'default' ? { namespace } : {};
    36  
    37      return this._super(store, type, id, snapshot, namespaceQuery);
    38    },
    39  
    40    urlForFindRecord(id, type, hash) {
    41      const [name, namespace] = JSON.parse(id);
    42      let url = this._super(name, type, hash);
    43      if (namespace && namespace !== 'default') {
    44        url += `?namespace=${namespace}`;
    45      }
    46      return url;
    47    },
    48  
    49    xhrKey(url, method, options = {}) {
    50      const plainKey = this._super(...arguments);
    51      const namespace = options.data && options.data.namespace;
    52      if (namespace) {
    53        return `${plainKey}?namespace=${namespace}`;
    54      }
    55      return plainKey;
    56    },
    57  
    58    relationshipFallbackLinks: {
    59      summary: '/summary',
    60    },
    61  
    62    findAllocations(job) {
    63      const url = `${this.buildURL('job', job.get('id'), job, 'findRecord')}/allocations`;
    64      return this.ajax(url, 'GET', { data: this.buildQuery() }).then(allocs => {
    65        return this.store.pushPayload('allocation', {
    66          allocations: allocs,
    67        });
    68      });
    69    },
    70  
    71    fetchRawDefinition(job) {
    72      const url = this.buildURL('job', job.get('id'), job, 'findRecord');
    73      return this.ajax(url, 'GET', { data: this.buildQuery() });
    74    },
    75  
    76    forcePeriodic(job) {
    77      if (job.get('periodic')) {
    78        const url = addToPath(this.urlForFindRecord(job.get('id'), 'job'), '/periodic/force');
    79        return this.ajax(url, 'POST');
    80      }
    81    },
    82  
    83    stop(job) {
    84      const url = this.urlForFindRecord(job.get('id'), 'job');
    85      return this.ajax(url, 'DELETE');
    86    },
    87  });
    88  
    89  function addToPath(url, extension = '') {
    90    const [path, params] = url.split('?');
    91    let newUrl = `${path}${extension}`;
    92  
    93    if (params) {
    94      newUrl += `?${params}`;
    95    }
    96  
    97    return newUrl;
    98  }