github.com/hernad/nomad@v1.6.112/ui/app/routes/jobs/run/templates/new.js (about) 1 /** 2 * Copyright (c) HashiCorp, Inc. 3 * SPDX-License-Identifier: MPL-2.0 4 */ 5 6 import { getOwner } from '@ember/application'; 7 import Route from '@ember/routing/route'; 8 import { inject as service } from '@ember/service'; 9 import notifyForbidden from 'nomad-ui/utils/notify-forbidden'; 10 11 export default class JobsRunTemplatesNewRoute extends Route { 12 @service can; 13 @service router; 14 @service store; 15 @service system; 16 17 beforeModel(transition) { 18 if ( 19 this.can.cannot('write variable', null, { 20 namespace: transition.to.queryParams.namespace, 21 }) 22 ) { 23 this.router.transitionTo('jobs.run'); 24 } 25 } 26 27 async model() { 28 try { 29 // When variables are created with a namespace attribute, it is verified against 30 // available namespaces to prevent redirecting to a non-existent namespace. 31 await Promise.all([ 32 this.store.query('variable', { 33 prefix: 'nomad/job-templates', 34 namespace: '*', 35 }), 36 this.store.findAll('namespace'), 37 ]); 38 39 // When navigating from jobs.run.index using "Save as Template" 40 const json = getOwner(this).lookup('controller:jobs.run').jsonTemplate; 41 if (json) { 42 return this.store.createRecord('variable', { 43 keyValues: [{ key: 'template', value: json }], 44 }); 45 } 46 47 // Override Default Value 48 return this.store.createRecord('variable', { keyValues: [] }); 49 } catch (e) { 50 notifyForbidden(this)(e); 51 } 52 } 53 54 resetController(controller, isExiting) { 55 if ( 56 isExiting && 57 controller.model.isNew && 58 !controller.model.isDestroyed && 59 !controller.model.isDestroying 60 ) { 61 controller.model?.unloadRecord(); 62 } 63 controller.set('templateName', null); 64 controller.set('templateNamespace', 'default'); 65 getOwner(this).lookup('controller:jobs.run').jsonTemplate = null; 66 } 67 }