-
Notifications
You must be signed in to change notification settings - Fork 81
/
PodDetail.js
291 lines (252 loc) · 7.55 KB
/
PodDetail.js
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
import mixin from "reactjs-mixin";
import PropTypes from "prop-types";
import React from "react";
import { routerShape } from "react-router";
import { Hooks } from "PluginSDK";
import Page from "#SRC/js/components/Page";
import RouterUtil from "#SRC/js/utils/RouterUtil";
import StringUtil from "#SRC/js/utils/StringUtil";
import TabsMixin from "#SRC/js/mixins/TabsMixin";
import UserActions from "#SRC/js/constants/UserActions";
import { isSDKService } from "#SRC/js/utils/ServiceUtil";
import Pod from "../../structs/Pod";
import PodHeader from "./PodHeader";
import Service from "../../structs/Service";
import ServiceActionDisabledModal from "../../components/modals/ServiceActionDisabledModal";
import ServiceActionLabels from "../../constants/ServiceActionLabels";
import ServiceBreadcrumbs from "../../components/ServiceBreadcrumbs";
import ServiceModals from "../../components/modals/ServiceModals";
import ServiceTree from "../../structs/ServiceTree";
import {
DELETE,
EDIT,
OPEN,
RESTART,
RESUME,
SCALE,
STOP
} from "../../constants/ServiceActionItem";
const METHODS_TO_BIND = [
"handleActionDisabledModalOpen",
"handleActionDisabledModalClose"
];
class PodDetail extends mixin(TabsMixin) {
constructor() {
super(...arguments);
this.tabs_tabs = {
"/services/overview/:id/tasks": "Tasks",
"/services/overview/:id/configuration": "Configuration",
"/services/overview/:id/debug": "Debug"
};
this.state = {
actionDisabledID: null,
actionDisabledModalOpen: false,
currentTab: Object.keys(this.tabs_tabs).shift()
};
METHODS_TO_BIND.forEach(method => {
this[method] = this[method].bind(this);
});
}
componentWillMount() {
super.componentWillMount(...arguments);
this.updateCurrentTab();
}
componentWillReceiveProps(nextProps) {
super.componentWillReceiveProps(...arguments);
this.updateCurrentTab(nextProps);
}
updateCurrentTab(props = this.props) {
const { routes } = props;
const currentTab = RouterUtil.reconstructPathFromRoutes(routes);
if (currentTab != null) {
this.setState({ currentTab });
}
}
onActionsItemSelection(actionID) {
const { pod } = this.props;
const isGroup = pod instanceof ServiceTree;
let containsSDKService = false;
if (isGroup) {
containsSDKService =
pod.findItem(function(item) {
return item instanceof Service && isSDKService(item);
}) != null;
}
// We still want to support the `open` action to display the web view
if (
actionID !== DELETE &&
(containsSDKService || isSDKService(pod)) &&
!Hooks.applyFilter(
"isEnabledSDKAction",
actionID === EDIT || actionID === OPEN,
actionID
)
) {
this.handleActionDisabledModalOpen(actionID);
} else {
this.handleServiceAction(actionID);
}
}
handleServiceAction(actionID) {
const { modalHandlers, router } = this.context;
const { pod } = this.props;
switch (actionID) {
case EDIT:
router.push(
`/services/detail/${encodeURIComponent(pod.getId())}/edit/`
);
break;
case SCALE:
modalHandlers.scaleService({ service: pod });
break;
case OPEN:
modalHandlers.openServiceUI({ service: pod });
break;
case RESTART:
modalHandlers.restartService({ service: pod });
break;
case RESUME:
modalHandlers.resumeService({ service: pod });
break;
case STOP:
modalHandlers.stopService({ service: pod });
break;
case DELETE:
modalHandlers.deleteService({ service: pod });
break;
}
}
handleActionDisabledModalOpen(actionDisabledID) {
this.setState({ actionDisabledModalOpen: true, actionDisabledID });
}
handleActionDisabledModalClose() {
this.setState({ actionDisabledModalOpen: false, actionDisabledID: null });
}
getActions() {
const { pod } = this.props;
const instanceCount = pod.getInstancesCount();
const actions = [];
if (
pod instanceof Service &&
pod.getWebURL() != null &&
pod.getWebURL() !== ""
) {
actions.push({
label: this.props.intl.formatMessage({
id: ServiceActionLabels.open
}),
onItemSelect: this.onActionsItemSelection.bind(this, OPEN)
});
}
actions.push({
label: "Edit",
onItemSelect: this.onActionsItemSelection.bind(this, EDIT)
});
if (instanceCount > 0) {
actions.push({
label: "Restart",
onItemSelect: this.onActionsItemSelection.bind(this, RESTART)
});
}
if (!pod.getLabels().MARATHON_SINGLE_INSTANCE_APP) {
actions.push({
label: "Scale",
onItemSelect: this.onActionsItemSelection.bind(this, SCALE)
});
}
if (instanceCount > 0) {
actions.push({
label: "Stop",
onItemSelect: this.onActionsItemSelection.bind(this, STOP)
});
}
if (instanceCount === 0) {
actions.push({
label: "Resume",
onItemSelect: this.onActionsItemSelection.bind(this, RESUME)
});
}
actions.push({
className: "text-danger",
label: StringUtil.capitalize(UserActions.DELETE),
onItemSelect: this.onActionsItemSelection.bind(this, DELETE)
});
return actions;
}
hasVolumes() {
return (
!!this.props.pod && this.props.pod.getVolumesData().getItems().length > 0
);
}
getTabs() {
const {
pod: { id }
} = this.props;
const routePrefix = `/services/detail/${encodeURIComponent(id)}`;
const tabs = [
{ label: "Tasks", routePath: `${routePrefix}/tasks` },
{ label: "Configuration", routePath: `${routePrefix}/configuration` },
{ label: "Debug", routePath: `${routePrefix}/debug` },
{ label: "Endpoints", routePath: `${routePrefix}/endpoints` }
];
if (this.hasVolumes()) {
tabs.push({
label: "Volumes",
routePath: `${routePrefix}/podvolumes`
});
}
return tabs;
}
render() {
const { children, pod } = this.props;
const { actionDisabledModalOpen, actionDisabledID } = this.state;
const breadcrumbs = <ServiceBreadcrumbs serviceID={pod.id} />;
// TODO (DCOS_OSS-1038): Move cloned props to route parameters
const clonedProps = { service: pod };
const clonedChildren = React.Children.map(children, function(child) {
// Only add props to children that are not ServiceModals
if (child.type === ServiceModals) {
return child;
}
return React.cloneElement(child, clonedProps);
});
return (
<Page>
<Page.Header
actions={this.getActions()}
breadcrumbs={breadcrumbs}
tabs={this.getTabs()}
>
<PodHeader
onDestroy={this.handleActionDestroy}
onEdit={this.handleActionEdit}
onScale={this.handleActionScale}
onStop={this.handleActionSuspend}
pod={pod}
tabs={this.getTabs()}
/>
</Page.Header>
{clonedChildren}
<ServiceActionDisabledModal
actionID={actionDisabledID}
open={actionDisabledModalOpen}
onClose={this.handleActionDisabledModalClose}
service={pod}
/>
</Page>
);
}
}
PodDetail.contextTypes = {
modalHandlers: PropTypes.shape({
scaleService: PropTypes.func,
stopService: PropTypes.func,
deleteService: PropTypes.func
}).isRequired,
router: routerShape
};
PodDetail.propTypes = {
children: PropTypes.node,
pod: PropTypes.instanceOf(Pod)
};
module.exports = PodDetail;