github.com/outbrain/consul@v1.4.5/ui-v2/app/adapters/token.js (about) 1 import { inject as service } from '@ember/service'; 2 import Adapter, { 3 REQUEST_CREATE, 4 REQUEST_UPDATE, 5 DATACENTER_QUERY_PARAM as API_DATACENTER_KEY, 6 } from './application'; 7 8 import { PRIMARY_KEY, SLUG_KEY } from 'consul-ui/models/token'; 9 import { FOREIGN_KEY as DATACENTER_KEY } from 'consul-ui/models/dc'; 10 import { OK as HTTP_OK } from 'consul-ui/utils/http/status'; 11 import { PUT as HTTP_PUT } from 'consul-ui/utils/http/method'; 12 13 import { get } from '@ember/object'; 14 15 const REQUEST_CLONE = 'cloneRecord'; 16 const REQUEST_SELF = 'querySelf'; 17 18 export default Adapter.extend({ 19 store: service('store'), 20 cleanQuery: function(_query) { 21 const query = this._super(...arguments); 22 // TODO: Make sure policy is being passed through 23 delete _query.policy; 24 // take off the secret for /self 25 delete query.secret; 26 return query; 27 }, 28 urlForQuery: function(query, modelName) { 29 return this.appendURL('acl/tokens', [], this.cleanQuery(query)); 30 }, 31 urlForQueryRecord: function(query, modelName) { 32 if (typeof query.id === 'undefined') { 33 throw new Error('You must specify an id'); 34 } 35 return this.appendURL('acl/token', [query.id], this.cleanQuery(query)); 36 }, 37 urlForQuerySelf: function(query, modelName) { 38 return this.appendURL('acl/token/self', [], this.cleanQuery(query)); 39 }, 40 urlForCreateRecord: function(modelName, snapshot) { 41 return this.appendURL('acl/token', [], { 42 [API_DATACENTER_KEY]: snapshot.attr(DATACENTER_KEY), 43 }); 44 }, 45 urlForUpdateRecord: function(id, modelName, snapshot) { 46 // If a token has Rules, use the old API 47 if (typeof snapshot.attr('Rules') !== 'undefined') { 48 return this.appendURL('acl/update', [], { 49 [API_DATACENTER_KEY]: snapshot.attr(DATACENTER_KEY), 50 }); 51 } 52 return this.appendURL('acl/token', [snapshot.attr(SLUG_KEY)], { 53 [API_DATACENTER_KEY]: snapshot.attr(DATACENTER_KEY), 54 }); 55 }, 56 urlForDeleteRecord: function(id, modelName, snapshot) { 57 return this.appendURL('acl/token', [snapshot.attr(SLUG_KEY)], { 58 [API_DATACENTER_KEY]: snapshot.attr(DATACENTER_KEY), 59 }); 60 }, 61 urlForRequest: function({ type, snapshot, requestType }) { 62 switch (requestType) { 63 case 'cloneRecord': 64 return this.urlForCloneRecord(type.modelName, snapshot); 65 case 'querySelf': 66 return this.urlForQuerySelf(snapshot, type.modelName); 67 } 68 return this._super(...arguments); 69 }, 70 urlForCloneRecord: function(modelName, snapshot) { 71 return this.appendURL('acl/token', [snapshot.attr(SLUG_KEY), 'clone'], { 72 [API_DATACENTER_KEY]: snapshot.attr(DATACENTER_KEY), 73 }); 74 }, 75 self: function(store, modelClass, snapshot) { 76 const params = { 77 store: store, 78 type: modelClass, 79 snapshot: snapshot, 80 requestType: 'querySelf', 81 }; 82 // _requestFor is private... but these methods aren't, until they disappear.. 83 const request = { 84 method: this.methodForRequest(params), 85 url: this.urlForRequest(params), 86 headers: this.headersForRequest(params), 87 data: this.dataForRequest(params), 88 }; 89 // TODO: private.. 90 return this._makeRequest(request); 91 }, 92 clone: function(store, modelClass, id, snapshot) { 93 const params = { 94 store: store, 95 type: modelClass, 96 id: id, 97 snapshot: snapshot, 98 requestType: 'cloneRecord', 99 }; 100 // _requestFor is private... but these methods aren't, until they disappear.. 101 const request = { 102 method: this.methodForRequest(params), 103 url: this.urlForRequest(params), 104 headers: this.headersForRequest(params), 105 data: this.dataForRequest(params), 106 }; 107 // TODO: private.. 108 return this._makeRequest(request); 109 }, 110 handleSingleResponse: function(url, response, primary, slug) { 111 // Sometimes we get `Policies: null`, make null equal an empty array 112 if (typeof response.Policies === 'undefined' || response.Policies === null) { 113 response.Policies = []; 114 } 115 // Convert an old style update response to a new style 116 if (typeof response['ID'] !== 'undefined') { 117 const item = get(this, 'store') 118 .peekAll('token') 119 .findBy('SecretID', response['ID']); 120 if (item) { 121 response['SecretID'] = response['ID']; 122 response['AccessorID'] = get(item, 'AccessorID'); 123 } 124 } 125 return this._super(url, response, primary, slug); 126 }, 127 handleResponse: function(status, headers, payload, requestData) { 128 let response = payload; 129 if (status === HTTP_OK) { 130 const url = this.parseURL(requestData.url); 131 switch (true) { 132 case response === true: 133 response = this.handleBooleanResponse(url, response, PRIMARY_KEY, SLUG_KEY); 134 break; 135 case Array.isArray(response): 136 response = this.handleBatchResponse(url, response, PRIMARY_KEY, SLUG_KEY); 137 break; 138 default: 139 response = this.handleSingleResponse(url, response, PRIMARY_KEY, SLUG_KEY); 140 } 141 } 142 return this._super(status, headers, response, requestData); 143 }, 144 methodForRequest: function(params) { 145 switch (params.requestType) { 146 case REQUEST_CLONE: 147 case REQUEST_CREATE: 148 return HTTP_PUT; 149 } 150 return this._super(...arguments); 151 }, 152 headersForRequest: function(params) { 153 switch (params.requestType) { 154 case REQUEST_SELF: 155 return { 156 'X-Consul-Token': params.snapshot.secret, 157 }; 158 } 159 return this._super(...arguments); 160 }, 161 dataForRequest: function(params) { 162 let data = this._super(...arguments); 163 switch (params.requestType) { 164 case REQUEST_UPDATE: 165 // If a token has Rules, use the old API 166 if (typeof data.token['Rules'] !== 'undefined') { 167 data.token['ID'] = data.token['SecretID']; 168 data.token['Name'] = data.token['Description']; 169 } 170 // falls through 171 case REQUEST_CREATE: 172 if (Array.isArray(data.token.Policies)) { 173 data.token.Policies = data.token.Policies.filter(function(item) { 174 // Just incase, don't save any policies that aren't saved 175 return !get(item, 'isNew'); 176 }).map(function(item) { 177 return { 178 ID: get(item, 'ID'), 179 Name: get(item, 'Name'), 180 }; 181 }); 182 } else { 183 delete data.token.Policies; 184 } 185 data = data.token; 186 break; 187 case REQUEST_SELF: 188 return {}; 189 case REQUEST_CLONE: 190 data = {}; 191 break; 192 } 193 // make sure we never send the SecretID 194 if (data && typeof data['SecretID'] !== 'undefined') { 195 delete data['SecretID']; 196 } 197 return data; 198 }, 199 });