This repository has been archived by the owner on May 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
apis.store.ts
96 lines (82 loc) · 2.54 KB
/
apis.store.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import {Restangular} from "ng2-restangular";
import {KUBERNETES_RESTANGULAR} from "./../service/kubernetes.restangular";
import {Inject, Injectable} from "@angular/core";
import {BehaviorSubject, Observable} from "rxjs";
import {Http} from "@angular/http";
/**
* Lets keep around the singleton results to avoid doing too many requests for this static data
*/
var _latestAPIs: APIs = null;
let _currentAPIs: BehaviorSubject<APIs> = new BehaviorSubject(_latestAPIs);
let _loadingAPIs: BehaviorSubject<boolean> = new BehaviorSubject(true);
// TODO lets default to OpenShift as a hack ;)
let _defaultIsOpenShift = true;
export class APIs {
constructor(public isOpenShift: boolean) {
}
}
export function isOpenShift(): boolean {
let config = window["Fabric8UIEnv"];
if (config) {
let flag = config["kubernetesMode"];
if (flag === "true") {
return false;
}
}
if (_latestAPIs != null) {
return _latestAPIs.isOpenShift;
}
return _defaultIsOpenShift;
}
@Injectable()
export class APIsStore {
constructor(@Inject(KUBERNETES_RESTANGULAR) private kubernetesRestangular: Restangular, private http: Http) {
this.load();
}
get resource(): Observable<APIs> {
return _currentAPIs.asObservable();
}
get loading(): Observable<boolean> {
return _loadingAPIs.asObservable();
}
/**
* Returns whether we are running against openshift.
*
* NOTE this is intended to be invoked after the APIsStore has finished loading via the .loading() Observable<boolean>!
*
* @return {boolean} true if this cluster is using openshift
*/
isOpenShift(): boolean {
let apis = _latestAPIs;
if (!apis) {
console.log("WARNING: invoked the isOpenShift() method before the APIsStore has loaded!");
return true;
}
return apis.isOpenShift;
}
load() {
/*
// we only need to load once really on startup
if (_startedLoadingAPIs) {
return;
}
_startedLoadingAPIs = true;
if (!_latestAPIs) {
this.kubernetesRestangular.one('oapi').get()
.subscribe(
() => {
_latestAPIs = new APIs(true);
_currentAPIs.next(_latestAPIs);
_loadingAPIs.next(false);
},
(error) => {
console.log('Could not find /oapi but for now going to assume its openshift!');
//console.log('Could not find /oapi so not OpenShift and must be Kubernetes: ' + error);
_latestAPIs = new APIs(_defaultIsOpenShift);
_currentAPIs.next(_latestAPIs);
_loadingAPIs.next(false);
});
}
*/
}
}