github.com/hernad/nomad@v1.6.112/ui/app/routes/jobs/run/index.js (about) 1 /** 2 * Copyright (c) HashiCorp, Inc. 3 * SPDX-License-Identifier: MPL-2.0 4 */ 5 6 // @ts-check 7 import Route from '@ember/routing/route'; 8 import { inject as service } from '@ember/service'; 9 import classic from 'ember-classic-decorator'; 10 import notifyForbidden from 'nomad-ui/utils/notify-forbidden'; 11 12 @classic 13 export default class JobsRunIndexRoute extends Route { 14 @service can; 15 @service notifications; 16 @service router; 17 @service store; 18 @service system; 19 20 queryParams = { 21 template: { 22 refreshModel: true, 23 }, 24 }; 25 26 beforeModel(transition) { 27 if ( 28 this.can.cannot('run job', null, { 29 namespace: transition.to.queryParams.namespace, 30 }) 31 ) { 32 this.router.transitionTo('jobs'); 33 } 34 } 35 36 async model({ template }) { 37 try { 38 // When jobs are created with a namespace attribute, it is verified against 39 // available namespaces to prevent redirecting to a non-existent namespace. 40 await this.store.findAll('namespace'); 41 42 if (template) { 43 const VariableAdapter = this.store.adapterFor('variable'); 44 const templateRecord = await VariableAdapter.getJobTemplate(template); 45 return this.store.createRecord('job', { 46 _newDefinition: templateRecord.items.template, 47 }); 48 } else { 49 return this.store.createRecord('job'); 50 } 51 } catch (e) { 52 this.handle404(e); 53 } 54 } 55 56 handle404(e) { 57 const error404 = e.errors?.find((err) => err.status === 404); 58 if (error404) { 59 this.notifications.add({ 60 title: `Error loading job template`, 61 message: error404.detail, 62 color: 'critical', 63 sticky: true, 64 }); 65 66 return; 67 } 68 notifyForbidden(this)(e); 69 } 70 71 resetController(controller, isExiting) { 72 if (isExiting) { 73 controller.model?.deleteRecord(); 74 controller.set('template', null); 75 } 76 } 77 }