github.com/hernad/nomad@v1.6.112/ui/app/controllers/jobs/run/templates/new.js (about) 1 /** 2 * Copyright (c) HashiCorp, Inc. 3 * SPDX-License-Identifier: MPL-2.0 4 */ 5 6 import Controller from '@ember/controller'; 7 import { inject as service } from '@ember/service'; 8 import { action } from '@ember/object'; 9 import { tracked } from '@glimmer/tracking'; 10 import { trimPath } from '../../../../helpers/trim-path'; 11 12 export default class JobsRunTemplatesNewController extends Controller { 13 @service router; 14 @service store; 15 @service system; 16 @tracked templateName = null; 17 @tracked templateNamespace = 'default'; 18 @service notifications; 19 20 get namespaceOptions() { 21 const namespaces = this.store 22 .peekAll('namespace') 23 .map(({ name }) => ({ key: name, label: name })); 24 25 return namespaces; 26 } 27 28 get isDuplicateTemplate() { 29 const templates = this.store.peekAll('variable'); 30 const templateName = trimPath([`nomad/job-templates/${this.templateName}`]); 31 32 return !!templates 33 .without(this.model) 34 .find( 35 (v) => v.path === templateName && v.namespace === this.templateNamespace 36 ); 37 } 38 39 @action 40 updateKeyValue(key, value) { 41 if (this.model.keyValues.find((kv) => kv.key === key)) { 42 this.model.keyValues.find((kv) => kv.key === key).value = value; 43 } else { 44 this.model.keyValues.pushObject({ key, value }); 45 } 46 } 47 48 @action 49 async save(e, overwrite = false) { 50 if (e.type === 'submit') { 51 e.preventDefault(); 52 } 53 54 if (this.model?.isNew) { 55 if (this.namespaceOptions) { 56 this.model.set('namespace', this.templateNamespace); 57 } else { 58 const [namespace] = this.store.peekAll('namespace').toArray(); 59 this.model.set('namespace', namespace.id); 60 } 61 } 62 63 this.model.set('path', `nomad/job-templates/${this.templateName}`); 64 this.model.setAndTrimPath(); 65 66 try { 67 await this.model.save({ adapterOptions: { overwrite } }); 68 69 this.notifications.add({ 70 title: 'Job template saved', 71 message: `${this.templateName} successfully saved`, 72 color: 'success', 73 }); 74 75 this.router.transitionTo('jobs.run.templates'); 76 } catch (e) { 77 this.notifications.add({ 78 title: 'Job template cannot be saved.', 79 message: e, 80 color: 'critical', 81 }); 82 } 83 } 84 }