github.com/hernad/nomad@v1.6.112/ui/app/routes/jobs/job/definition.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  
     9  /**
    10   * Route for fetching and displaying a job's definition and specification.
    11   */
    12  export default class DefinitionRoute extends Route {
    13    /**
    14     * Fetch the job's definition, specification, and variables from the API.
    15     *
    16     * @returns {Promise<Object>} A promise that resolves to an object containing the job, definition, format,
    17     *                            specification, variableFlags, and variableLiteral.
    18     */
    19    async model() {
    20      const job = this.modelFor('jobs.job');
    21      if (!job) return;
    22  
    23      const definition = await job.fetchRawDefinition();
    24  
    25      let format = 'json'; // default to json in network request errors
    26      let specification;
    27      let variableFlags;
    28      let variableLiteral;
    29      try {
    30        const specificationResponse = await job.fetchRawSpecification();
    31        specification = specificationResponse?.Source ?? null;
    32        variableFlags = specificationResponse?.VariableFlags ?? null;
    33        variableLiteral = specificationResponse?.Variables ?? null;
    34        format = specificationResponse?.Format ?? 'json';
    35      } catch (e) {
    36        // Swallow the error because Nomad job pre-1.6 will not have a specification
    37      }
    38  
    39      return {
    40        definition,
    41        format,
    42        job,
    43        specification,
    44        variableFlags,
    45        variableLiteral,
    46      };
    47    }
    48  
    49    /**
    50     * Reset the controller when exiting the route.
    51     *
    52     * @param {Controller} controller - The controller instance.
    53     * @param {boolean} isExiting - A boolean flag indicating if the route is being exited.
    54     */
    55    resetController(controller, isExiting) {
    56      if (isExiting) {
    57        const job = controller.job;
    58        job.rollbackAttributes();
    59        job.resetId();
    60        controller.set('isEditing', false);
    61      }
    62    }
    63  
    64    /**
    65     * Set up the controller with the model data and determine the view type.
    66     *
    67     * @param {Controller} controller - The controller instance.
    68     * @param {Object} model - The model data fetched in the `model` method.
    69     */
    70    setupController(controller, model) {
    71      super.setupController(controller, model);
    72  
    73      const view = controller.view
    74        ? controller.view
    75        : model?.specification
    76        ? 'job-spec'
    77        : 'full-definition';
    78      controller.view = view;
    79    }
    80  }