/
OptionalPortsAndServiceDiscoveryComponent.jsx
290 lines (241 loc) · 8.67 KB
/
OptionalPortsAndServiceDiscoveryComponent.jsx
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
import classNames from "classnames";
import React from "react/addons";
import ContainerConstants from "../constants/ContainerConstants";
import DuplicableRowControls from "../components/DuplicableRowControls";
import DuplicableRowsMixin from "../mixins/DuplicableRowsMixin";
import FormActions from "../actions/FormActions";
import FormGroupComponent from "../components/FormGroupComponent";
import PortInputAttributes from "../constants/PortInputAttributes";
import PluginComponentStore from "../stores/PluginComponentStore";
import PluginMountPointComponent from "../components/PluginMountPointComponent";
import PluginMountPoints from "../plugin/shared/PluginMountPoints";
import Util from "../helpers/Util";
const fieldsetId = "portDefinitions";
function determinePortDefinitionsType(fields) {
if (fields.dockerNetwork === ContainerConstants.NETWORK.BRIDGE) {
return ContainerConstants.NETWORK.BRIDGE;
}
if ((fields.dockerImage != null && fields.dockerImage !== "") ||
fields.dockerNetwork === ContainerConstants.NETWORK.HOST) {
return ContainerConstants.NETWORK.HOST;
}
return ContainerConstants.TYPE.MESOS;
}
function isTooComplexStructure(fields) {
return fields.portDefinitions != null &&
fields.portDefinitions.some(portDefinition =>
portDefinition.labels != null &&
!!Object.keys(portDefinition.labels).length
);
}
var OptionalPortsAndServiceDiscoveryComponent = React.createClass({
displayName: "OptionalPortsAndServiceDiscoveryComponent",
mixins: [DuplicableRowsMixin],
duplicableRowsScheme: {
portDefinitions: {
port: null,
protocol: ContainerConstants.PORTMAPPINGS.PROTOCOL.TCP,
name: null,
labels: null,
isRandomPort: true
}
},
propTypes: {
fields: React.PropTypes.object.isRequired,
getErrorMessage: React.PropTypes.func.isRequired,
handleModeToggle: React.PropTypes.func.isRequired
},
handleAddRow: function (fieldId, position, event) {
event.target.blur();
event.preventDefault();
var type = determinePortDefinitionsType(this.props.fields);
var isBridgeNetwork = type === ContainerConstants.NETWORK.BRIDGE;
var scheme = Util.extendObject(this.duplicableRowsScheme[fieldId], {
consecutiveKey: Util.getNewConsecutiveKey()
});
if (isBridgeNetwork) {
scheme.isRandomPort = false;
}
FormActions.insert(fieldId,
scheme,
position
);
},
handleChangeRow: function (fieldId, position) {
this.updateRow(fieldId, position);
},
handleRemoveRow: function (fieldId, position, event) {
event.target.blur();
event.preventDefault();
this.removeRow(fieldId, position);
},
getHelpText: function () {
var rows = this.state.rows[fieldsetId];
if (rows == null) {
return null;
}
var partialRandomText = !rows.every(row => row.isRandomPort)
? "partially "
: "";
var dynamicPortText = !rows.every(row => !row.isRandomPort)
? ` which will be ${partialRandomText}assigned dynamically`
: "";
var type = determinePortDefinitionsType(this.props.fields);
var portIdentifiers = Array(rows.length)
.fill()
.map((_, i) => "$PORT" + i)
.join(", ")
.replace(/(.*), (.*)$/, "$1 and $2");
var message = "Your application will need to be configured to listen to" +
` ${portIdentifiers}${dynamicPortText}.`;
if (type === ContainerConstants.NETWORK.HOST) {
message = "Your Docker container will need to be configured to listen" +
` to ${portIdentifiers}${dynamicPortText}.`;
} else if (type === ContainerConstants.NETWORK.BRIDGE) {
message = "Your Docker container network is in bridged mode." +
" The container will bind to the requested ports and" +
` they will be dynamically mapped to ${portIdentifiers} on the host.`;
}
return <div>{message}</div>;
},
getRandomPortCheckbox: function (row, i, hidden = false) {
var classSet = classNames({
"hidden": hidden,
}, "checkbox-form-group port-input-field");
var value = hidden
? false
: row.isRandomPort;
return (
<FormGroupComponent className={classSet}
fieldId={`${fieldsetId}.${i}.isRandomPort`}
label="Assign a random port"
value={value}>
<input ref={`isRandomPort${i}`} type="checkbox" />
</FormGroupComponent>
);
},
getPortInputField: function (row, i) {
var type = determinePortDefinitionsType(this.props.fields);
var isBridgeNetwork = type === ContainerConstants.NETWORK.BRIDGE;
var fieldLabel = isBridgeNetwork
? "Container Port"
: "Port";
var randomPortField = null;
if (!isBridgeNetwork && row.isRandomPort) {
randomPortField = (
<FormGroupComponent
label={fieldLabel}
value={"$PORT" + i}>
<input disabled={true} />
</FormGroupComponent>
);
}
let portFieldClassSet = classNames({
"hidden": !!randomPortField
});
return (
<div className="col-sm-4">
<FormGroupComponent className={portFieldClassSet}
fieldId={`${fieldsetId}.${i}.port`}
label={fieldLabel}
value={row.port}>
<input ref={`port${i}`} {...PortInputAttributes} />
</FormGroupComponent>
{randomPortField}
{this.getRandomPortCheckbox(row, i, isBridgeNetwork)}
</div>
);
},
getPortDefinitionRow: function (row, i, disableRemoveButton = false) {
var error = this.getError(fieldsetId, row.consecutiveKey);
var getErrorMessage = this.props.getErrorMessage;
var rowClassSet = classNames({
"has-error": !!error,
"duplicable-row": true
});
return (
<div key={row.consecutiveKey+i} className={rowClassSet}>
<fieldset className="row duplicable-row"
onChange={this.handleChangeRow.bind(null, fieldsetId, i)}>
{this.getPortInputField(row, i)}
<div className="col-sm-2">
<FormGroupComponent
errorMessage={
getErrorMessage(`${fieldsetId}.${i}.protocol`)
}
fieldId={`${fieldsetId}.${i}.protocol`}
label="Protocol"
value={row.protocol}>
<select defaultValue={row.protocol} ref={`protocol${i}`}>
<option value={ContainerConstants.PORTMAPPINGS.PROTOCOL.TCP}>
{ContainerConstants.PORTMAPPINGS.PROTOCOL.TCP}
</option>
<option value={ContainerConstants.PORTMAPPINGS.PROTOCOL.UDP}>
{ContainerConstants.PORTMAPPINGS.PROTOCOL.UDP}
</option>
</select>
</FormGroupComponent>
</div>
<div className="col-sm-6">
<FormGroupComponent
fieldId={`${fieldsetId}.${i}.name`}
label="Name"
value={row.name}>
<input ref={`name${i}`} />
</FormGroupComponent>
<DuplicableRowControls
disableRemoveButton={disableRemoveButton}
handleAddRow={this.handleAddRow.bind(null, fieldsetId, i + 1)}
handleRemoveRow=
{this.handleRemoveRow.bind(null, fieldsetId, i)} />
</div>
</fieldset>
{error}
</div>
);
},
getPortDefinitionRows: function (hasPluginComponent = false) {
if (hasPluginComponent) {
return (
<PluginMountPointComponent
placeId={PluginMountPoints.OPTIONAL_PORTS_AND_SERVICE_DISCOVERY} />
);
}
let rows = this.state.rows[fieldsetId];
if (rows == null) {
return null;
}
let disableRemoveButton = this.hasOnlyOneSingleEmptyRow(fieldsetId,
["protocol", "isRandomPort"]);
return rows.map((row, i) => {
return this.getPortDefinitionRow(row, i, disableRemoveButton);
});
},
render: function () {
var hasPluginComponent = PluginComponentStore.hasComponentAtPlaceId(
PluginMountPoints.OPTIONAL_PORTS_AND_SERVICE_DISCOVERY
);
if (!hasPluginComponent && isTooComplexStructure(this.props.fields)) {
return (
<div>
Looks like your ports & service discovery configuration
is a little bit too sophisticated for our form to handle.
Please switch
to <a className="json-link clickable"
onClick={this.props.handleModeToggle}>JSON mode</a> if
you want to view or modify.
</div>
);
}
return (
<div>
<div className="duplicable-list">
{this.getPortDefinitionRows(hasPluginComponent)}
</div>
{this.getGeneralErrorBlock(fieldsetId)}
{this.getHelpText()}
</div>
);
}
});
export default OptionalPortsAndServiceDiscoveryComponent;