/
namespace.ts
136 lines (120 loc) · 4.02 KB
/
namespace.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import { ThunkAction } from "redux-thunk";
import { Kube } from "shared/Kube";
import { get } from "lodash";
import { ActionType, deprecated } from "typesafe-actions";
import Namespace, { setStoredNamespace } from "../shared/Namespace";
import { IResource, IStoreState } from "../shared/types";
const { createAction } = deprecated;
export const requestNamespace = createAction("REQUEST_NAMESPACE", resolve => {
return (cluster: string, namespace: string) => resolve({ cluster, namespace });
});
export const receiveNamespace = createAction("RECEIVE_NAMESPACE", resolve => {
return (cluster: string, namespace: IResource) => resolve({ cluster, namespace });
});
export const setNamespaceState = createAction("SET_NAMESPACE", resolve => {
return (cluster: string, namespace: string) => resolve({ cluster, namespace });
});
export const postNamespace = createAction("CREATE_NAMESPACE", resolve => {
return (cluster: string, namespace: string) => resolve({ cluster, namespace });
});
export const receiveNamespaces = createAction("RECEIVE_NAMESPACES", resolve => {
return (cluster: string, namespaces: string[]) => resolve({ cluster, namespaces });
});
export const errorNamespaces = createAction("ERROR_NAMESPACES", resolve => {
return (cluster: string, err: Error, op: string) => resolve({ cluster, err, op });
});
export const setAllowCreate = createAction("ALLOW_CREATE_NAMESPACE", resolve => {
return (cluster: string, allowed: boolean) => resolve({ cluster, allowed });
});
export const clearClusters = createAction("CLEAR_CLUSTERS");
const allActions = [
requestNamespace,
receiveNamespace,
setNamespaceState,
receiveNamespaces,
errorNamespaces,
clearClusters,
postNamespace,
setAllowCreate,
];
export type NamespaceAction = ActionType<typeof allActions[number]>;
export function fetchNamespaces(
cluster: string,
): ThunkAction<Promise<string[]>, IStoreState, null, NamespaceAction> {
return async dispatch => {
try {
const namespaceList = await Namespace.list(cluster);
const namespaceStrings = get(namespaceList, "namespaces", []).map(
(n: IResource) => n.metadata.name,
);
if (namespaceStrings.length === 0) {
dispatch(
errorNamespaces(
cluster,
new Error("The current account does not have access to any namespaces"),
"list",
),
);
return [];
}
dispatch(receiveNamespaces(cluster, namespaceStrings));
return namespaceStrings;
} catch (e) {
dispatch(errorNamespaces(cluster, e, "list"));
return [];
}
};
}
export function createNamespace(
cluster: string,
ns: string,
): ThunkAction<Promise<boolean>, IStoreState, null, NamespaceAction> {
return async dispatch => {
try {
await Namespace.create(cluster, ns);
dispatch(postNamespace(cluster, ns));
dispatch(fetchNamespaces(cluster));
return true;
} catch (e) {
dispatch(errorNamespaces(cluster, e, "create"));
return false;
}
};
}
export function getNamespace(
cluster: string,
ns: string,
): ThunkAction<Promise<boolean>, IStoreState, null, NamespaceAction> {
return async dispatch => {
try {
dispatch(requestNamespace(cluster, ns));
const namespace = await Namespace.get(cluster, ns);
dispatch(receiveNamespace(cluster, namespace));
return true;
} catch (e) {
dispatch(errorNamespaces(cluster, e, "get"));
return false;
}
};
}
export function setNamespace(
cluster: string,
ns: string,
): ThunkAction<Promise<void>, IStoreState, null, NamespaceAction> {
return async dispatch => {
setStoredNamespace(cluster, ns);
dispatch(setNamespaceState(cluster, ns));
};
}
export function canCreate(
cluster: string,
): ThunkAction<Promise<void>, IStoreState, null, NamespaceAction> {
return async dispatch => {
try {
const allowed = await Kube.canI(cluster, "", "namespaces", "create", "");
dispatch(setAllowCreate(cluster, allowed));
} catch (e) {
dispatch(errorNamespaces(cluster, e, "get"));
}
};
}