github.com/outbrain/consul@v1.4.5/ui-v2/app/routes/dc/acls/tokens/edit.js (about)

     1  import SingleRoute from 'consul-ui/routing/single';
     2  import { inject as service } from '@ember/service';
     3  import { hash } from 'rsvp';
     4  import { set, get } from '@ember/object';
     5  import updateArrayObject from 'consul-ui/utils/update-array-object';
     6  
     7  import WithTokenActions from 'consul-ui/mixins/token/with-actions';
     8  
     9  const ERROR_PARSE_RULES = 'Failed to parse ACL rules';
    10  const ERROR_NAME_EXISTS = 'Invalid Policy: A Policy with Name';
    11  export default SingleRoute.extend(WithTokenActions, {
    12    repo: service('repository/token'),
    13    policyRepo: service('repository/policy'),
    14    datacenterRepo: service('repository/dc'),
    15    settings: service('settings'),
    16    model: function(params, transition) {
    17      const dc = this.modelFor('dc').dc.Name;
    18      const policyRepo = get(this, 'policyRepo');
    19      return this._super(...arguments).then(model => {
    20        return hash({
    21          ...model,
    22          ...{
    23            // TODO: I only need these to create a new policy
    24            datacenters: get(this, 'datacenterRepo').findAll(),
    25            policy: this.getEmptyPolicy(),
    26            token: get(this, 'settings').findBySlug('token'),
    27            items: policyRepo.findAllByDatacenter(dc).catch(function(e) {
    28              switch (get(e, 'errors.firstObject.status')) {
    29                case '403':
    30                case '401':
    31                  // do nothing the SingleRoute will have caught it already
    32                  return;
    33              }
    34              throw e;
    35            }),
    36          },
    37        });
    38      });
    39    },
    40    setupController: function(controller, model) {
    41      this._super(...arguments);
    42      controller.setProperties(model);
    43    },
    44    getEmptyPolicy: function() {
    45      const dc = this.modelFor('dc').dc.Name;
    46      return get(this, 'policyRepo').create({ Datacenter: dc });
    47    },
    48    actions: {
    49      // TODO: Some of this could potentially be moved to the repo services
    50      loadPolicy: function(item, items) {
    51        const repo = get(this, 'policyRepo');
    52        const dc = this.modelFor('dc').dc.Name;
    53        const slug = get(item, repo.getSlugKey());
    54        repo.findBySlug(slug, dc).then(item => {
    55          updateArrayObject(items, item, repo.getSlugKey());
    56        });
    57      },
    58      remove: function(item, items) {
    59        return items.removeObject(item);
    60      },
    61      clearPolicy: function() {
    62        // TODO: I should be able to reset the ember-data object
    63        // back to it original state?
    64        // possibly Forms could know how to create
    65        const controller = get(this, 'controller');
    66        controller.setProperties({
    67          policy: this.getEmptyPolicy(),
    68        });
    69      },
    70      createPolicy: function(item, policies, success) {
    71        get(this, 'policyRepo')
    72          .persist(item)
    73          .then(item => {
    74            set(item, 'CreateTime', new Date().getTime());
    75            policies.pushObject(item);
    76            return item;
    77          })
    78          .then(function() {
    79            success();
    80          })
    81          .catch(err => {
    82            if (typeof err.errors !== 'undefined') {
    83              const error = err.errors[0];
    84              let prop;
    85              let message = error.detail;
    86              switch (true) {
    87                case message.indexOf(ERROR_PARSE_RULES) === 0:
    88                  prop = 'Rules';
    89                  message = error.detail;
    90                  break;
    91                case message.indexOf(ERROR_NAME_EXISTS) === 0:
    92                  prop = 'Name';
    93                  message = message.substr(ERROR_NAME_EXISTS.indexOf(':') + 1);
    94                  break;
    95              }
    96              if (prop) {
    97                item.addError(prop, message);
    98              }
    99            } else {
   100              throw err;
   101            }
   102          });
   103      },
   104    },
   105  });