github.com/outbrain/consul@v1.4.5/ui-v2/app/adapters/acl.js (about) 1 import Adapter, { 2 REQUEST_CREATE, 3 REQUEST_UPDATE, 4 REQUEST_DELETE, 5 DATACENTER_QUERY_PARAM as API_DATACENTER_KEY, 6 } from './application'; 7 import EmberError from '@ember/error'; 8 import { PRIMARY_KEY, SLUG_KEY } from 'consul-ui/models/acl'; 9 import { FOREIGN_KEY as DATACENTER_KEY } from 'consul-ui/models/dc'; 10 import { PUT as HTTP_PUT } from 'consul-ui/utils/http/method'; 11 import { OK as HTTP_OK, UNAUTHORIZED as HTTP_UNAUTHORIZED } from 'consul-ui/utils/http/status'; 12 13 import makeAttrable from 'consul-ui/utils/makeAttrable'; 14 const REQUEST_CLONE = 'cloneRecord'; 15 export default Adapter.extend({ 16 urlForQuery: function(query, modelName) { 17 // https://www.consul.io/api/acl.html#list-acls 18 return this.appendURL('acl/list', [], this.cleanQuery(query)); 19 }, 20 urlForQueryRecord: function(query, modelName) { 21 // https://www.consul.io/api/acl.html#read-acl-token 22 if (typeof query.id === 'undefined') { 23 throw new Error('You must specify an id'); 24 } 25 return this.appendURL('acl/info', [query.id], this.cleanQuery(query)); 26 }, 27 urlForCreateRecord: function(modelName, snapshot) { 28 // https://www.consul.io/api/acl.html#create-acl-token 29 return this.appendURL('acl/create', [], { 30 [API_DATACENTER_KEY]: snapshot.attr(DATACENTER_KEY), 31 }); 32 }, 33 urlForUpdateRecord: function(id, modelName, snapshot) { 34 // the id is in the payload, don't add it in here 35 // https://www.consul.io/api/acl.html#update-acl-token 36 return this.appendURL('acl/update', [], { 37 [API_DATACENTER_KEY]: snapshot.attr(DATACENTER_KEY), 38 }); 39 }, 40 urlForDeleteRecord: function(id, modelName, snapshot) { 41 // https://www.consul.io/api/acl.html#delete-acl-token 42 return this.appendURL('acl/destroy', [snapshot.attr(SLUG_KEY)], { 43 [API_DATACENTER_KEY]: snapshot.attr(DATACENTER_KEY), 44 }); 45 }, 46 urlForCloneRecord: function(modelName, snapshot) { 47 // https://www.consul.io/api/acl.html#clone-acl-token 48 return this.appendURL('acl/clone', [snapshot.attr(SLUG_KEY)], { 49 [API_DATACENTER_KEY]: snapshot.attr(DATACENTER_KEY), 50 }); 51 }, 52 urlForRequest: function({ type, snapshot, requestType }) { 53 switch (requestType) { 54 case 'cloneRecord': 55 return this.urlForCloneRecord(type.modelName, snapshot); 56 } 57 return this._super(...arguments); 58 }, 59 clone: function(store, modelClass, id, snapshot) { 60 const params = { 61 store: store, 62 type: modelClass, 63 id: id, 64 snapshot: snapshot, 65 requestType: 'cloneRecord', 66 }; 67 // _requestFor is private... but these methods aren't, until they disappear.. 68 const request = { 69 method: this.methodForRequest(params), 70 url: this.urlForRequest(params), 71 headers: this.headersForRequest(params), 72 data: this.dataForRequest(params), 73 }; 74 // TODO: private.. 75 return this._makeRequest(request); 76 }, 77 dataForRequest: function(params) { 78 const data = this._super(...arguments); 79 switch (params.requestType) { 80 case REQUEST_UPDATE: 81 case REQUEST_CREATE: 82 return data.acl; 83 } 84 return data; 85 }, 86 methodForRequest: function(params) { 87 switch (params.requestType) { 88 case REQUEST_DELETE: 89 case REQUEST_CREATE: 90 case REQUEST_CLONE: 91 return HTTP_PUT; 92 } 93 return this._super(...arguments); 94 }, 95 isCreateRecord: function(url, method) { 96 return ( 97 url.pathname === 98 this.parseURL(this.urlForCreateRecord('acl', makeAttrable({ [DATACENTER_KEY]: '' }))).pathname 99 ); 100 }, 101 isCloneRecord: function(url, method) { 102 return ( 103 url.pathname === 104 this.parseURL( 105 this.urlForCloneRecord( 106 'acl', 107 makeAttrable({ [SLUG_KEY]: this.slugFromURL(url), [DATACENTER_KEY]: '' }) 108 ) 109 ).pathname 110 ); 111 }, 112 isUpdateRecord: function(url, method) { 113 return ( 114 url.pathname === 115 this.parseURL(this.urlForUpdateRecord(null, 'acl', makeAttrable({ [DATACENTER_KEY]: '' }))) 116 .pathname 117 ); 118 }, 119 handleResponse: function(status, headers, payload, requestData) { 120 let response = payload; 121 const method = requestData.method; 122 if (status === HTTP_OK) { 123 const url = this.parseURL(requestData.url); 124 switch (true) { 125 case response === true: 126 response = this.handleBooleanResponse(url, response, PRIMARY_KEY, SLUG_KEY); 127 break; 128 case this.isQueryRecord(url): 129 response = this.handleSingleResponse(url, response[0], PRIMARY_KEY, SLUG_KEY); 130 break; 131 case this.isUpdateRecord(url, method): 132 case this.isCreateRecord(url, method): 133 case this.isCloneRecord(url, method): 134 response = this.handleSingleResponse(url, response, PRIMARY_KEY, SLUG_KEY); 135 break; 136 default: 137 response = this.handleBatchResponse(url, response, PRIMARY_KEY, SLUG_KEY); 138 } 139 } else if (status === HTTP_UNAUTHORIZED) { 140 const e = new EmberError(); 141 e.code = status; 142 e.message = payload; 143 throw e; 144 } 145 return this._super(status, headers, response, requestData); 146 }, 147 });