github.com/hernad/nomad@v1.6.112/ui/app/components/policy-editor.js (about)

     1  /**
     2   * Copyright (c) HashiCorp, Inc.
     3   * SPDX-License-Identifier: MPL-2.0
     4   */
     5  
     6  import Component from '@glimmer/component';
     7  import { action } from '@ember/object';
     8  import { inject as service } from '@ember/service';
     9  import { alias } from '@ember/object/computed';
    10  
    11  export default class PolicyEditorComponent extends Component {
    12    @service notifications;
    13    @service router;
    14    @service store;
    15  
    16    @alias('args.policy') policy;
    17  
    18    @action updatePolicyRules(value) {
    19      this.policy.set('rules', value);
    20    }
    21  
    22    @action async save(e) {
    23      if (e instanceof Event) {
    24        e.preventDefault(); // code-mirror "command+enter" submits the form, but doesnt have a preventDefault()
    25      }
    26      try {
    27        const nameRegex = '^[a-zA-Z0-9-]{1,128}$';
    28        if (!this.policy.name?.match(nameRegex)) {
    29          throw new Error(
    30            `Policy name must be 1-128 characters long and can only contain letters, numbers, and dashes.`
    31          );
    32        }
    33  
    34        const shouldRedirectAfterSave = this.policy.isNew;
    35  
    36        if (
    37          this.policy.isNew &&
    38          this.store.peekRecord('policy', this.policy.name)
    39        ) {
    40          throw new Error(
    41            `A policy with name ${this.policy.name} already exists.`
    42          );
    43        }
    44  
    45        this.policy.id = this.policy.name;
    46  
    47        await this.policy.save();
    48  
    49        this.notifications.add({
    50          title: 'Policy Saved',
    51          color: 'success',
    52        });
    53  
    54        if (shouldRedirectAfterSave) {
    55          this.router.transitionTo('policies.policy', this.policy.id);
    56        }
    57      } catch (error) {
    58        this.notifications.add({
    59          title: `Error creating Policy ${this.policy.name}`,
    60          message: error,
    61          color: 'critical',
    62          sticky: true,
    63        });
    64      }
    65    }
    66  }