github.com/manicqin/nomad@v0.9.5/ui/app/adapters/job.js (about)

     1  import { inject as service } from '@ember/service';
     2  import Watchable from './watchable';
     3  import addToPath from 'nomad-ui/utils/add-to-path';
     4  
     5  export default Watchable.extend({
     6    system: service(),
     7  
     8    findAll() {
     9      const namespace = this.get('system.activeNamespace');
    10      return this._super(...arguments).then(data => {
    11        data.forEach(job => {
    12          job.Namespace = namespace ? namespace.get('id') : 'default';
    13        });
    14        return data;
    15      });
    16    },
    17  
    18    findRecord(store, type, id, snapshot) {
    19      const [, namespace] = JSON.parse(id);
    20      const namespaceQuery = namespace && namespace !== 'default' ? { namespace } : {};
    21  
    22      return this._super(store, type, id, snapshot, namespaceQuery);
    23    },
    24  
    25    urlForFindAll() {
    26      const url = this._super(...arguments);
    27      const namespace = this.get('system.activeNamespace.id');
    28      return associateNamespace(url, namespace);
    29    },
    30  
    31    urlForFindRecord(id, type, hash) {
    32      const [name, namespace] = JSON.parse(id);
    33      let url = this._super(name, type, hash);
    34      return associateNamespace(url, namespace);
    35    },
    36  
    37    urlForUpdateRecord(id, type, hash) {
    38      const [name, namespace] = JSON.parse(id);
    39      let url = this._super(name, type, hash);
    40      return associateNamespace(url, namespace);
    41    },
    42  
    43    xhrKey(url, method, options = {}) {
    44      const plainKey = this._super(...arguments);
    45      const namespace = options.data && options.data.namespace;
    46      return associateNamespace(plainKey, namespace);
    47    },
    48  
    49    relationshipFallbackLinks: {
    50      summary: '/summary',
    51    },
    52  
    53    fetchRawDefinition(job) {
    54      const url = this.urlForFindRecord(job.get('id'), 'job');
    55      return this.ajax(url, 'GET');
    56    },
    57  
    58    forcePeriodic(job) {
    59      if (job.get('periodic')) {
    60        const url = addToPath(this.urlForFindRecord(job.get('id'), 'job'), '/periodic/force');
    61        return this.ajax(url, 'POST');
    62      }
    63    },
    64  
    65    stop(job) {
    66      const url = this.urlForFindRecord(job.get('id'), 'job');
    67      return this.ajax(url, 'DELETE');
    68    },
    69  
    70    parse(spec) {
    71      const url = addToPath(this.urlForFindAll('job'), '/parse');
    72      return this.ajax(url, 'POST', {
    73        data: {
    74          JobHCL: spec,
    75          Canonicalize: true,
    76        },
    77      });
    78    },
    79  
    80    plan(job) {
    81      const jobId = job.get('id') || job.get('_idBeforeSaving');
    82      const store = this.store;
    83      const url = addToPath(this.urlForFindRecord(jobId, 'job'), '/plan');
    84  
    85      return this.ajax(url, 'POST', {
    86        data: {
    87          Job: job.get('_newDefinitionJSON'),
    88          Diff: true,
    89        },
    90      }).then(json => {
    91        json.ID = jobId;
    92        store.pushPayload('job-plan', { jobPlans: [json] });
    93        return store.peekRecord('job-plan', jobId);
    94      });
    95    },
    96  
    97    // Running a job doesn't follow REST create semantics so it's easier to
    98    // treat it as an action.
    99    run(job) {
   100      return this.ajax(this.urlForCreateRecord('job'), 'POST', {
   101        data: {
   102          Job: job.get('_newDefinitionJSON'),
   103        },
   104      });
   105    },
   106  
   107    update(job) {
   108      const jobId = job.get('id') || job.get('_idBeforeSaving');
   109      return this.ajax(this.urlForUpdateRecord(jobId, 'job'), 'POST', {
   110        data: {
   111          Job: job.get('_newDefinitionJSON'),
   112        },
   113      });
   114    },
   115  });
   116  
   117  function associateNamespace(url, namespace) {
   118    if (namespace && namespace !== 'default') {
   119      url += `?namespace=${namespace}`;
   120    }
   121    return url;
   122  }