github.com/outbrain/consul@v1.4.5/ui-v2/app/services/repository/token.js (about) 1 import Service, { inject as service } from '@ember/service'; 2 import { get } from '@ember/object'; 3 import { typeOf } from '@ember/utils'; 4 import { PRIMARY_KEY, SLUG_KEY } from 'consul-ui/models/token'; 5 import { Promise } from 'rsvp'; 6 import statusFactory from 'consul-ui/utils/acls-status'; 7 import isValidServerErrorFactory from 'consul-ui/utils/http/acl/is-valid-server-error'; 8 const isValidServerError = isValidServerErrorFactory(); 9 const status = statusFactory(isValidServerError, Promise); 10 const MODEL_NAME = 'token'; 11 export default Service.extend({ 12 getModelName: function() { 13 return MODEL_NAME; 14 }, 15 getPrimaryKey: function() { 16 return PRIMARY_KEY; 17 }, 18 getSlugKey: function() { 19 return SLUG_KEY; 20 }, 21 status: function(obj) { 22 return status(obj); 23 }, 24 self: function(secret, dc) { 25 return get(this, 'store') 26 .self(this.getModelName(), { 27 secret: secret, 28 dc: dc, 29 }) 30 .catch(e => { 31 // If we get this 500 RPC error, it means we are a legacy ACL cluster 32 // set AccessorID to null - which for the frontend means legacy mode 33 if (isValidServerError(e)) { 34 return { 35 AccessorID: null, 36 SecretID: secret, 37 }; 38 } 39 return Promise.reject(e); 40 }); 41 }, 42 clone: function(item) { 43 return get(this, 'store').clone(this.getModelName(), get(item, PRIMARY_KEY)); 44 }, 45 findByPolicy: function(id, dc) { 46 return get(this, 'store').query(this.getModelName(), { 47 policy: id, 48 dc: dc, 49 }); 50 }, 51 // TODO: RepositoryService 52 store: service('store'), 53 findAllByDatacenter: function(dc) { 54 return get(this, 'store').query(this.getModelName(), { 55 dc: dc, 56 }); 57 }, 58 findBySlug: function(slug, dc) { 59 return get(this, 'store').queryRecord(this.getModelName(), { 60 id: slug, 61 dc: dc, 62 }); 63 }, 64 create: function(obj) { 65 // TODO: This should probably return a Promise 66 return get(this, 'store').createRecord(this.getModelName(), obj); 67 }, 68 persist: function(item) { 69 return item.save(); 70 }, 71 remove: function(obj) { 72 let item = obj; 73 if (typeof obj.destroyRecord === 'undefined') { 74 item = obj.get('data'); 75 } 76 if (typeOf(item) === 'object') { 77 item = get(this, 'store').peekRecord(this.getModelName(), item[this.getPrimaryKey()]); 78 } 79 return item.destroyRecord().then(item => { 80 return get(this, 'store').unloadRecord(item); 81 }); 82 }, 83 invalidate: function() { 84 get(this, 'store').unloadAll(this.getModelName()); 85 }, 86 });