github.com/emate/nomad@v0.8.2-wo-binpacking/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 namespace = options.data && options.data.namespace; 51 if (namespace) { 52 return `${url}?namespace=${namespace}`; 53 } 54 return url; 55 }, 56 57 relationshipFallbackLinks: { 58 summary: '/summary', 59 }, 60 61 findAllocations(job) { 62 const url = `${this.buildURL('job', job.get('id'), job, 'findRecord')}/allocations`; 63 return this.ajax(url, 'GET', { data: this.buildQuery() }).then(allocs => { 64 return this.store.pushPayload('allocation', { 65 allocations: allocs, 66 }); 67 }); 68 }, 69 70 fetchRawDefinition(job) { 71 const url = this.buildURL('job', job.get('id'), job, 'findRecord'); 72 return this.ajax(url, 'GET', { data: this.buildQuery() }); 73 }, 74 75 forcePeriodic(job) { 76 if (job.get('periodic')) { 77 const url = addToPath(this.urlForFindRecord(job.get('id'), 'job'), '/periodic/force'); 78 return this.ajax(url, 'POST'); 79 } 80 }, 81 82 stop(job) { 83 const url = this.urlForFindRecord(job.get('id'), 'job'); 84 return this.ajax(url, 'DELETE'); 85 }, 86 }); 87 88 function addToPath(url, extension = '') { 89 const [path, params] = url.split('?'); 90 let newUrl = `${path}${extension}`; 91 92 if (params) { 93 newUrl += `?${params}`; 94 } 95 96 return newUrl; 97 }