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