github.com/zoomfoo/nomad@v0.8.5-0.20180907175415-f28fd3a1a056/ui/app/services/system.js (about) 1 import Service, { inject as service } from '@ember/service'; 2 import { computed } from '@ember/object'; 3 import { copy } from '@ember/object/internals'; 4 import PromiseObject from '../utils/classes/promise-object'; 5 import PromiseArray from '../utils/classes/promise-array'; 6 import { namespace } from '../adapters/application'; 7 8 // When the request isn't ok (e.g., forbidden) handle gracefully 9 const jsonWithDefault = defaultResponse => res => 10 res.ok ? res.json() : copy(defaultResponse, true); 11 12 export default Service.extend({ 13 token: service(), 14 store: service(), 15 16 leader: computed('activeRegion', function() { 17 const token = this.get('token'); 18 19 return PromiseObject.create({ 20 promise: token 21 .authorizedRequest(`/${namespace}/status/leader`) 22 .then(res => res.json()) 23 .then(rpcAddr => ({ rpcAddr })) 24 .then(leader => { 25 // Dirty self so leader can be used as a dependent key 26 this.notifyPropertyChange('leader.rpcAddr'); 27 return leader; 28 }), 29 }); 30 }), 31 32 defaultRegion: computed(function() { 33 const token = this.get('token'); 34 return PromiseObject.create({ 35 promise: token 36 .authorizedRawRequest(`/${namespace}/agent/members`) 37 .then(jsonWithDefault({})) 38 .then(json => { 39 return { region: json.ServerRegion }; 40 }), 41 }); 42 }), 43 44 regions: computed(function() { 45 const token = this.get('token'); 46 47 return PromiseArray.create({ 48 promise: token.authorizedRawRequest(`/${namespace}/regions`).then(jsonWithDefault([])), 49 }); 50 }), 51 52 activeRegion: computed('regions.[]', { 53 get() { 54 const regions = this.get('regions'); 55 const region = window.localStorage.nomadActiveRegion; 56 57 if (regions.includes(region)) { 58 return region; 59 } 60 61 return null; 62 }, 63 set(key, value) { 64 if (value == null) { 65 window.localStorage.removeItem('nomadActiveRegion'); 66 } else { 67 // All localStorage values are strings. Stringify first so 68 // the return value is consistent with what is persisted. 69 const strValue = value + ''; 70 window.localStorage.nomadActiveRegion = strValue; 71 return strValue; 72 } 73 }, 74 }), 75 76 shouldShowRegions: computed('regions.[]', function() { 77 return this.get('regions.length') > 1; 78 }), 79 80 shouldIncludeRegion: computed( 81 'activeRegion', 82 'defaultRegion.region', 83 'shouldShowRegions', 84 function() { 85 return ( 86 this.get('shouldShowRegions') && 87 this.get('activeRegion') !== this.get('defaultRegion.region') 88 ); 89 } 90 ), 91 92 namespaces: computed('activeRegion', function() { 93 return PromiseArray.create({ 94 promise: this.get('store') 95 .findAll('namespace') 96 .then(namespaces => namespaces.compact()), 97 }); 98 }), 99 100 shouldShowNamespaces: computed('namespaces.[]', function() { 101 const namespaces = this.get('namespaces').toArray(); 102 return namespaces.length && namespaces.some(namespace => namespace.get('id') !== 'default'); 103 }), 104 105 activeNamespace: computed('namespaces.[]', { 106 get() { 107 const namespaceId = window.localStorage.nomadActiveNamespace || 'default'; 108 const namespace = this.get('namespaces').findBy('id', namespaceId); 109 110 if (namespace) { 111 return namespace; 112 } 113 114 // If the namespace in localStorage is no longer in the cluster, it needs to 115 // be cleared from localStorage 116 this.set('activeNamespace', null); 117 return this.get('namespaces').findBy('id', 'default'); 118 }, 119 set(key, value) { 120 if (value == null) { 121 window.localStorage.removeItem('nomadActiveNamespace'); 122 } else if (typeof value === 'string') { 123 window.localStorage.nomadActiveNamespace = value; 124 return this.get('namespaces').findBy('id', value); 125 } else { 126 window.localStorage.nomadActiveNamespace = value.get('name'); 127 return value; 128 } 129 }, 130 }), 131 132 reset() { 133 this.set('activeNamespace', null); 134 }, 135 });