From 0cfeba49d73facb666640426a461a14aa23b653a Mon Sep 17 00:00:00 2001 From: Michael Lange Date: Wed, 8 Aug 2018 16:10:22 -0700 Subject: [PATCH] Add the region qp to all requests made through the token service --- ui/app/services/system.js | 2 +- ui/app/services/token.js | 28 ++++++++++++++++++++++++++-- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/ui/app/services/system.js b/ui/app/services/system.js index 9f7f74c5e..8b6be9d9b 100644 --- a/ui/app/services/system.js +++ b/ui/app/services/system.js @@ -28,7 +28,7 @@ export default Service.extend({ const token = this.get('token'); return PromiseArray.create({ - promise: token.authorizedRequest(`/${namespace}/regions`).then(res => res.json()), + promise: token.authorizedRawRequest(`/${namespace}/regions`).then(res => res.json()), }); }), diff --git a/ui/app/services/token.js b/ui/app/services/token.js index 4ce27d9c3..b4e536e2f 100644 --- a/ui/app/services/token.js +++ b/ui/app/services/token.js @@ -1,9 +1,12 @@ -import Service from '@ember/service'; +import Service, { inject as service } from '@ember/service'; import { computed } from '@ember/object'; import { assign } from '@ember/polyfills'; +import queryString from 'query-string'; import fetch from 'nomad-ui/utils/fetch'; export default Service.extend({ + system: service(), + secret: computed({ get() { return window.sessionStorage.nomadTokenSecret; @@ -19,7 +22,12 @@ export default Service.extend({ }, }), - authorizedRequest(url, options = { credentials: 'include' }) { + // All non Ember Data requests should go through authorizedRequest. + // However, the request that gets regions falls into that category. + // This authorizedRawRequest is necessary in order to fetch data + // with the guarantee of a token but without the automatic region + // param since the region cannot be known at this point. + authorizedRawRequest(url, options = { credentials: 'include' }) { const headers = {}; const token = this.get('secret'); @@ -29,4 +37,20 @@ export default Service.extend({ return fetch(url, assign(options, { headers })); }, + + authorizedRequest(url, options) { + const region = this.get('system.activeRegion'); + + if (region) { + url = addParams(url, { region }); + } + + return this.authorizedRawRequest(url, options); + }, }); + +function addParams(url, params) { + const paramsStr = queryString.stringify(params); + const delimiter = url.includes('?') ? '&' : '?'; + return `${url}${delimiter}${paramsStr}`; +}