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