github.com/hernad/nomad@v1.6.112/ui/app/models/variable.js (about)

     1  /**
     2   * Copyright (c) HashiCorp, Inc.
     3   * SPDX-License-Identifier: MPL-2.0
     4   */
     5  
     6  // @ts-check
     7  import Model from '@ember-data/model';
     8  import { computed } from '@ember/object';
     9  import classic from 'ember-classic-decorator';
    10  // eslint-disable-next-line no-unused-vars
    11  import MutableArray from '@ember/array/mutable';
    12  import { trimPath } from '../helpers/trim-path';
    13  import { attr } from '@ember-data/model';
    14  
    15  /**
    16   * @typedef KeyValue
    17   * @type {object}
    18   * @property {string} key
    19   * @property {string} value
    20   */
    21  
    22  /**
    23   * @typedef Variable
    24   * @type {object}
    25   */
    26  
    27  /**
    28   * A Variable has a path, namespace, and an array of key-value pairs within the client.
    29   * On the server, these key-value pairs are serialized into object structure.
    30   * @class
    31   * @extends Model
    32   */
    33  @classic
    34  export default class VariableModel extends Model {
    35    /**
    36     * Can be any arbitrary string, but behaves best when used as a slash-delimited file path.
    37     *
    38     * @type {string}
    39     */
    40    @attr('string', { defaultValue: '' }) path;
    41  
    42    /**
    43     * @type {MutableArray<KeyValue>}
    44     */
    45    @attr({
    46      defaultValue() {
    47        return [{ key: '', value: '' }];
    48      },
    49    })
    50    keyValues;
    51  
    52    /** @type {number} */
    53    @attr('number') createIndex;
    54    /** @type {number} */
    55    @attr('number') modifyIndex;
    56    /** @type {Date} */
    57    @attr('date') createTime;
    58    /** @type {Date} */
    59    @attr('date') modifyTime;
    60    /** @type {string} */
    61    @attr('string', { defaultValue: 'default' }) namespace;
    62  
    63    @computed('path')
    64    get parentFolderPath() {
    65      const split = this.path.split('/');
    66      const [, ...folderPath] = [split.pop(), ...split];
    67      return folderPath.join('/');
    68    }
    69  
    70    /**
    71     * Removes starting and trailing slashes, pathLinkedEntitiesand sets the ID property
    72     */
    73    setAndTrimPath() {
    74      this.set('path', trimPath([this.path]));
    75      if (!this.get('id')) {
    76        this.set('id', `${this.get('path')}@${this.get('namespace')}`);
    77      }
    78    }
    79  
    80    /**
    81     * Translates the key-value pairs into an object structure.
    82     */
    83    @computed('keyValues')
    84    get items() {
    85      return this.keyValues.reduce((acc, { key, value }) => {
    86        acc[key] = value;
    87        return acc;
    88      }, {});
    89    }
    90  
    91    // Gets the path of the variable, and if it starts with jobs/, delimits on / and returns each part separately in an array
    92  
    93    /**
    94     * @typedef pathLinkedEntities
    95     * @type {Object}
    96     * @property {string} job
    97     * @property {string} [group]
    98     * @property {string} [task]
    99     */
   100  
   101    /**
   102     * @type {pathLinkedEntities}
   103     */
   104    get pathLinkedEntities() {
   105      const entityTypes = ['job', 'group', 'task'];
   106      const emptyEntities = { job: '', group: '', task: '' };
   107      if (
   108        this.path?.startsWith('nomad/jobs/') &&
   109        this.path?.split('/').length <= 5
   110      ) {
   111        return this.path
   112          .split('/')
   113          .slice(2, 5)
   114          .reduce((acc, pathPart, index) => {
   115            acc[entityTypes[index]] = pathPart;
   116            return acc;
   117          }, emptyEntities);
   118      } else {
   119        return emptyEntities;
   120      }
   121    }
   122  }