github.com/outbrain/consul@v1.4.5/ui-v2/app/adapters/policy.js (about) 1 import Adapter, { 2 REQUEST_CREATE, 3 REQUEST_UPDATE, 4 DATACENTER_QUERY_PARAM as API_DATACENTER_KEY, 5 } from './application'; 6 7 import { PRIMARY_KEY, SLUG_KEY } from 'consul-ui/models/policy'; 8 import { FOREIGN_KEY as DATACENTER_KEY } from 'consul-ui/models/dc'; 9 import { OK as HTTP_OK } from 'consul-ui/utils/http/status'; 10 import { PUT as HTTP_PUT } from 'consul-ui/utils/http/method'; 11 12 export default Adapter.extend({ 13 urlForQuery: function(query, modelName) { 14 return this.appendURL('acl/policies', [], this.cleanQuery(query)); 15 }, 16 urlForQueryRecord: function(query, modelName) { 17 if (typeof query.id === 'undefined') { 18 throw new Error('You must specify an id'); 19 } 20 return this.appendURL('acl/policy', [query.id], this.cleanQuery(query)); 21 }, 22 urlForCreateRecord: function(modelName, snapshot) { 23 return this.appendURL('acl/policy', [], { 24 [API_DATACENTER_KEY]: snapshot.attr(DATACENTER_KEY), 25 }); 26 }, 27 urlForUpdateRecord: function(id, modelName, snapshot) { 28 return this.appendURL('acl/policy', [snapshot.attr(SLUG_KEY)], { 29 [API_DATACENTER_KEY]: snapshot.attr(DATACENTER_KEY), 30 }); 31 }, 32 urlForDeleteRecord: function(id, modelName, snapshot) { 33 return this.appendURL('acl/policy', [snapshot.attr(SLUG_KEY)], { 34 [API_DATACENTER_KEY]: snapshot.attr(DATACENTER_KEY), 35 }); 36 }, 37 urlForTranslateRecord: function(modelName, snapshot) { 38 return this.appendURL('acl/policy/translate', [], {}); 39 }, 40 dataForRequest: function(params) { 41 const data = this._super(...arguments); 42 switch (params.requestType) { 43 case REQUEST_UPDATE: 44 case REQUEST_CREATE: 45 return data.policy; 46 } 47 return data; 48 }, 49 handleResponse: function(status, headers, payload, requestData) { 50 let response = payload; 51 if (status === HTTP_OK) { 52 const url = this.parseURL(requestData.url); 53 switch (true) { 54 case response === true: 55 response = this.handleBooleanResponse(url, response, PRIMARY_KEY, SLUG_KEY); 56 break; 57 case Array.isArray(response): 58 response = this.handleBatchResponse(url, response, PRIMARY_KEY, SLUG_KEY); 59 break; 60 default: 61 response = this.handleSingleResponse(url, response, PRIMARY_KEY, SLUG_KEY); 62 } 63 } 64 return this._super(status, headers, response, requestData); 65 }, 66 methodForRequest: function(params) { 67 switch (params.requestType) { 68 case REQUEST_CREATE: 69 return HTTP_PUT; 70 } 71 return this._super(...arguments); 72 }, 73 });