github.com/Ilhicas/nomad@v1.0.4-0.20210304152020-e86851182bc3/ui/app/services/system.js (about)

     1  import Service, { inject as service } from '@ember/service';
     2  import { computed } from '@ember/object';
     3  import { alias } from '@ember/object/computed';
     4  import PromiseObject from '../utils/classes/promise-object';
     5  import PromiseArray from '../utils/classes/promise-array';
     6  import { namespace } from '../adapters/application';
     7  import jsonWithDefault from '../utils/json-with-default';
     8  import classic from 'ember-classic-decorator';
     9  import { task } from 'ember-concurrency';
    10  
    11  @classic
    12  export default class SystemService extends Service {
    13    @service token;
    14    @service store;
    15  
    16    @computed('activeRegion')
    17    get leader() {
    18      const token = this.token;
    19  
    20      return PromiseObject.create({
    21        promise: token
    22          .authorizedRequest(`/${namespace}/status/leader`)
    23          .then(res => res.json())
    24          .then(rpcAddr => ({ rpcAddr }))
    25          .then(leader => {
    26            // Dirty self so leader can be used as a dependent key
    27            this.notifyPropertyChange('leader.rpcAddr');
    28            return leader;
    29          }),
    30      });
    31    }
    32  
    33    @computed
    34    get agent() {
    35      const token = this.token;
    36      return PromiseObject.create({
    37        promise: token
    38          .authorizedRawRequest(`/${namespace}/agent/self`)
    39          .then(jsonWithDefault({}))
    40          .then(agent => {
    41            agent.version = agent.member?.Tags?.build || 'Unknown';
    42            return agent;
    43          }),
    44      });
    45    }
    46  
    47    @computed
    48    get defaultRegion() {
    49      const token = this.token;
    50      return PromiseObject.create({
    51        promise: token
    52          .authorizedRawRequest(`/${namespace}/agent/members`)
    53          .then(jsonWithDefault({}))
    54          .then(json => {
    55            return { region: json.ServerRegion };
    56          }),
    57      });
    58    }
    59  
    60    @computed
    61    get regions() {
    62      const token = this.token;
    63  
    64      return PromiseArray.create({
    65        promise: token.authorizedRawRequest(`/${namespace}/regions`).then(jsonWithDefault([])),
    66      });
    67    }
    68  
    69    @computed('regions.[]')
    70    get activeRegion() {
    71      const regions = this.regions;
    72      const region = window.localStorage.nomadActiveRegion;
    73  
    74      if (regions.includes(region)) {
    75        return region;
    76      }
    77  
    78      return null;
    79    }
    80  
    81    set activeRegion(value) {
    82      if (value == null) {
    83        window.localStorage.removeItem('nomadActiveRegion');
    84        return;
    85      } else {
    86        // All localStorage values are strings. Stringify first so
    87        // the return value is consistent with what is persisted.
    88        const strValue = value + '';
    89        window.localStorage.nomadActiveRegion = strValue;
    90      }
    91    }
    92  
    93    @computed('regions.[]')
    94    get shouldShowRegions() {
    95      return this.get('regions.length') > 1;
    96    }
    97  
    98    @computed('activeRegion', 'defaultRegion.region', 'shouldShowRegions')
    99    get shouldIncludeRegion() {
   100      return this.shouldShowRegions && this.activeRegion !== this.get('defaultRegion.region');
   101    }
   102  
   103    @computed('activeRegion')
   104    get namespaces() {
   105      return PromiseArray.create({
   106        promise: this.store.findAll('namespace').then(namespaces => namespaces.compact()),
   107      });
   108    }
   109  
   110    @computed('namespaces.[]')
   111    get shouldShowNamespaces() {
   112      const namespaces = this.namespaces.toArray();
   113      return namespaces.length && namespaces.some(namespace => namespace.get('id') !== 'default');
   114    }
   115  
   116    @computed('namespaces.[]')
   117    get activeNamespace() {
   118      const namespaceId = window.localStorage.nomadActiveNamespace || 'default';
   119      const namespace = this.namespaces.findBy('id', namespaceId);
   120  
   121      if (namespace) {
   122        return namespace;
   123      }
   124  
   125      // If the namespace in localStorage is no longer in the cluster, it needs to
   126      // be cleared from localStorage
   127      window.localStorage.removeItem('nomadActiveNamespace');
   128      return this.namespaces.findBy('id', 'default');
   129    }
   130  
   131    set activeNamespace(value) {
   132      if (value == null) {
   133        window.localStorage.removeItem('nomadActiveNamespace');
   134        return;
   135      } else if (typeof value === 'string') {
   136        window.localStorage.nomadActiveNamespace = value;
   137      } else {
   138        window.localStorage.nomadActiveNamespace = value.get('name');
   139      }
   140    }
   141  
   142    reset() {
   143      this.set('activeNamespace', null);
   144      this.notifyPropertyChange('namespaces');
   145    }
   146  
   147    @task(function*() {
   148      const emptyLicense = { License: { Features: [] } };
   149  
   150      try {
   151        return yield this.token
   152          .authorizedRawRequest(`/${namespace}/operator/license`)
   153          .then(jsonWithDefault(emptyLicense));
   154      } catch (e) {
   155        return emptyLicense;
   156      }
   157    })
   158    fetchLicense;
   159  
   160    @alias('fetchLicense.lastSuccessful.value') license;
   161  
   162    @computed('license.License.Features.[]')
   163    get features() {
   164      return this.get('license.License.Features') || [];
   165    }
   166  }