github.com/hernad/nomad@v1.6.112/ui/app/controllers/policies/policy.js (about)

     1  /**
     2   * Copyright (c) HashiCorp, Inc.
     3   * SPDX-License-Identifier: MPL-2.0
     4   */
     5  
     6  // @ts-check
     7  import Controller from '@ember/controller';
     8  import { action } from '@ember/object';
     9  import { inject as service } from '@ember/service';
    10  import { tracked } from '@glimmer/tracking';
    11  import { alias } from '@ember/object/computed';
    12  import { task } from 'ember-concurrency';
    13  
    14  export default class PoliciesPolicyController extends Controller {
    15    @service notifications;
    16    @service router;
    17    @service store;
    18  
    19    @alias('model.policy') policy;
    20    @alias('model.tokens') tokens;
    21  
    22    @tracked
    23    error = null;
    24  
    25    @tracked isDeleting = false;
    26  
    27    get newTokenString() {
    28      return `nomad acl token create -name="<TOKEN_NAME>" -policy="${this.policy.name}" -type=client -ttl=<8h>`;
    29    }
    30  
    31    @action
    32    onDeletePrompt() {
    33      this.isDeleting = true;
    34    }
    35  
    36    @action
    37    onDeleteCancel() {
    38      this.isDeleting = false;
    39    }
    40  
    41    @task(function* () {
    42      try {
    43        yield this.policy.deleteRecord();
    44        yield this.policy.save();
    45        this.notifications.add({
    46          title: 'Policy Deleted',
    47          color: 'success',
    48          type: `success`,
    49          destroyOnClick: false,
    50        });
    51        this.router.transitionTo('policies');
    52      } catch (err) {
    53        this.notifications.add({
    54          title: `Error deleting Policy ${this.policy.name}`,
    55          message: err,
    56          color: 'critical',
    57          sticky: true,
    58        });
    59      }
    60    })
    61    deletePolicy;
    62  
    63    async refreshTokens() {
    64      this.tokens = this.store
    65        .peekAll('token')
    66        .filter((token) =>
    67          token.policyNames?.includes(decodeURIComponent(this.policy.name))
    68        );
    69    }
    70  
    71    @task(function* () {
    72      try {
    73        const newToken = this.store.createRecord('token', {
    74          name: `Example Token for ${this.policy.name}`,
    75          policies: [this.policy],
    76          // New date 10 minutes into the future
    77          expirationTime: new Date(Date.now() + 10 * 60 * 1000),
    78          type: 'client',
    79        });
    80        yield newToken.save();
    81        yield this.refreshTokens();
    82        this.notifications.add({
    83          title: 'Example Token Created',
    84          message: `${newToken.secret}`,
    85          color: 'success',
    86          timeout: 30000,
    87          customAction: {
    88            label: 'Copy to Clipboard',
    89            action: () => {
    90              navigator.clipboard.writeText(newToken.secret);
    91            },
    92          },
    93        });
    94      } catch (err) {
    95        this.error = {
    96          title: 'Error creating new token',
    97          description: err,
    98        };
    99  
   100        throw err;
   101      }
   102    })
   103    createTestToken;
   104  
   105    @task(function* (token) {
   106      try {
   107        yield token.deleteRecord();
   108        yield token.save();
   109        yield this.refreshTokens();
   110        this.notifications.add({
   111          title: 'Token successfully deleted',
   112          color: 'success',
   113        });
   114      } catch (err) {
   115        this.error = {
   116          title: 'Error deleting token',
   117          description: err,
   118        };
   119  
   120        throw err;
   121      }
   122    })
   123    deleteToken;
   124  }