Skip to content

Commit 58847c1

Browse files
committed
Move byon config parsing to machine location
1 parent f919a39 commit 58847c1

File tree

2 files changed

+78
-56
lines changed

2 files changed

+78
-56
lines changed

core/src/main/java/org/apache/brooklyn/location/byon/ByonLocationResolver.java

+3-55
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ protected ConfigBag extractConfig(Map<?,?> locationFlags, String spec, LocationR
134134
if (host instanceof String) {
135135
machineSpec = parseMachine((String)host, locationClass, defaultProps, spec);
136136
} else if (host instanceof Map) {
137-
machineSpec = parseMachine((Map<String, ?>)host, locationClass, defaultProps, spec);
137+
machineSpec = parseMachine((Map<String, ?>)host, locationClass, defaultProps);
138138
} else {
139139
throw new IllegalArgumentException("Expected machine to be String or Map, but was "+host.getClass().getName()+" ("+host+")");
140140
}
@@ -147,68 +147,16 @@ protected ConfigBag extractConfig(Map<?,?> locationFlags, String spec, LocationR
147147
return config;
148148
}
149149

150-
protected LocationSpec<? extends MachineLocation> parseMachine(Map<String, ?> vals, Class<? extends MachineLocation> locationClass, Map<String, ?> defaults, String specForErrMsg) {
151-
Map<String, Object> valSanitized = Sanitizer.sanitize(vals);
150+
protected LocationSpec<? extends MachineLocation> parseMachine(Map<String, ?> vals, Class<? extends MachineLocation> locationClass, Map<String, ?> defaults) {
152151
Map<String, Object> machineConfig = MutableMap.copyOf(vals);
153152

154153
String osFamily = (String) machineConfig.remove(OS_FAMILY.getName());
155-
String ssh = (String) machineConfig.remove("ssh");
156-
if (machineConfig.containsKey("winrm") && !(machineConfig.get("winrm") instanceof String)) {
157-
machineConfig.put("address", machineConfig.get("winrm"));
158-
machineConfig.remove("winrm");
159-
} else {
160-
String winrm = (String) machineConfig.remove("winrm");
161-
162-
Map<Integer, String> tcpPortMappings = (Map<Integer, String>) machineConfig.get("tcpPortMappings");
163-
164-
checkArgument(ssh != null ^ winrm != null, "Must specify exactly one of 'ssh' or 'winrm' for machine: %s", valSanitized);
165-
166-
UserAndHostAndPort userAndHostAndPort;
167-
String host;
168-
int port;
169-
if (ssh != null) {
170-
userAndHostAndPort = parseUserAndHostAndPort(ssh, 22);
171-
} else {
172-
// TODO set to null and rely on the MachineLocation. If not then make a dependency to WinRmMachineLocation and use its config key name.
173-
userAndHostAndPort = parseUserAndHostAndPort(winrm, vals.get("winrm.useHttps") != null && (Boolean)vals.get("winrm.useHttps") ? 5986 : 5985);
174-
}
175-
176-
// If there is a tcpPortMapping defined for the connection-port, then use that for ssh/winrm machine
177-
port = userAndHostAndPort.getHostAndPort().getPort();
178-
if (tcpPortMappings != null && tcpPortMappings.containsKey(port)) {
179-
String override = tcpPortMappings.get(port);
180-
HostAndPort hostAndPortOverride = HostAndPort.fromString(override);
181-
if (!hostAndPortOverride.hasPort()) {
182-
throw new IllegalArgumentException("Invalid portMapping ('"+override+"') for port "+port+" in "+specForErrMsg);
183-
}
184-
port = hostAndPortOverride.getPort();
185-
host = hostAndPortOverride.getHostText().trim();
186-
} else {
187-
host = userAndHostAndPort.getHostAndPort().getHostText().trim();
188-
}
189-
190-
machineConfig.put("address", host);
191-
try {
192-
InetAddress.getByName(host);
193-
} catch (Exception e) {
194-
throw new IllegalArgumentException("Invalid host '"+host+"' specified in '"+specForErrMsg+"': "+e);
195-
}
196-
197-
if (userAndHostAndPort.getUser() != null) {
198-
checkArgument(!vals.containsKey("user"), "Must not specify user twice for machine: %s", valSanitized);
199-
machineConfig.put("user", userAndHostAndPort.getUser());
200-
}
201-
if (userAndHostAndPort.getHostAndPort().hasPort()) {
202-
checkArgument(!vals.containsKey("port"), "Must not specify port twice for machine: %s", valSanitized);
203-
machineConfig.put("port", port);
204-
}
205-
}
206154
for (Map.Entry<String, ?> entry : defaults.entrySet()) {
207155
if (!machineConfig.containsKey(entry.getKey())) {
208156
machineConfig.put(entry.getKey(), entry.getValue());
209157
}
210158
}
211-
159+
212160
Class<? extends MachineLocation> locationClassHere = locationClass;
213161
if (osFamily != null) {
214162
locationClassHere = getLocationClass(osFamily);

core/src/main/java/org/apache/brooklyn/location/byon/FixedListMachineProvisioningLocation.java

+75-1
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@
2222

2323
import java.io.Closeable;
2424
import java.io.File;
25+
import java.net.InetAddress;
2526
import java.util.ArrayList;
2627
import java.util.Collection;
2728
import java.util.List;
2829
import java.util.Map;
2930
import java.util.Set;
3031

32+
import com.google.common.net.HostAndPort;
3133
import org.apache.brooklyn.api.location.Location;
3234
import org.apache.brooklyn.api.location.LocationSpec;
3335
import org.apache.brooklyn.api.location.MachineLocation;
@@ -45,6 +47,7 @@
4547
import org.apache.brooklyn.util.collections.MutableSet;
4648
import org.apache.brooklyn.util.core.config.ConfigBag;
4749
import org.apache.brooklyn.util.core.flags.SetFromFlag;
50+
import org.apache.brooklyn.util.net.UserAndHostAndPort;
4851
import org.apache.brooklyn.util.stream.Streams;
4952
import org.apache.brooklyn.util.text.WildcardGlobs;
5053
import org.apache.brooklyn.util.text.WildcardGlobs.PhraseTreatment;
@@ -317,7 +320,7 @@ public T obtain(Map<?,?> flags) throws NoMachinesAvailableException {
317320
T desiredMachine = (T) flags.get("desiredMachine");
318321
ConfigBag allflags = ConfigBag.newInstanceExtending(config().getBag()).putAll(flags);
319322
Function<Iterable<? extends MachineLocation>, MachineLocation> chooser = allflags.get(MACHINE_CHOOSER);
320-
323+
321324
synchronized (lock) {
322325
Set<T> a = getAvailable();
323326
if (a.isEmpty()) {
@@ -381,13 +384,84 @@ protected void updateMachineConfig(T machine, Map<?, ?> flags) {
381384
// For backwards compatibility, where peristed state did not have this.
382385
origConfigs = Maps.newLinkedHashMap();
383386
}
387+
parseMachineConfig((AbstractLocation) machine);
384388
Map<String, Object> strFlags = ConfigBag.newInstance(flags).getAllConfig();
385389
Map<String, Object> origConfig = ((ConfigurationSupportInternal)machine.config()).getLocalBag().getAllConfig();
386390
origConfigs.put(machine, origConfig);
387391
requestPersist();
388392

389393
((ConfigurationSupportInternal)machine.config()).putAll(strFlags);
390394
}
395+
396+
private void parseMachineConfig(AbstractLocation machine) {
397+
String ssh = machine.config().get(ConfigKeys.newStringConfigKey("ssh"));
398+
machine.config().removeKey(ConfigKeys.newStringConfigKey("ssh"));
399+
String winrm = machine.config().get(ConfigKeys.newStringConfigKey("winrm"));
400+
machine.config().removeKey(ConfigKeys.newStringConfigKey("winrm"));
401+
402+
Map<Integer, String> tcpPortMappings = (Map<Integer, String>) machine.config().get(ConfigKeys.newConfigKey(Map.class, "tcpPortMappings"));
403+
404+
if (ssh == null && winrm == null) {
405+
throw new IllegalArgumentException("Must specify exactly one of 'ssh' or 'winrm' for machine: "+machine);
406+
}
407+
408+
UserAndHostAndPort userAndHostAndPort;
409+
String host;
410+
int port;
411+
if (ssh != null) {
412+
userAndHostAndPort = parseUserAndHostAndPort(ssh, 22);
413+
} else {
414+
// TODO set to null and rely on the MachineLocation. If not then make a dependency to WinRmMachineLocation and use its config key name.
415+
Boolean useHttps = machine.config().get(ConfigKeys.newConfigKey(Boolean.class,"winrm.useHttps"));
416+
userAndHostAndPort = parseUserAndHostAndPort(winrm, useHttps != null && useHttps ? 5986 : 5985);
417+
}
418+
419+
// If there is a tcpPortMapping defined for the connection-port, then use that for ssh/winrm machine
420+
port = userAndHostAndPort.getHostAndPort().getPort();
421+
if (tcpPortMappings != null && tcpPortMappings.containsKey(port)) {
422+
String override = tcpPortMappings.get(port);
423+
HostAndPort hostAndPortOverride = HostAndPort.fromString(override);
424+
if (!hostAndPortOverride.hasPort()) {
425+
throw new IllegalArgumentException("Invalid portMapping ('"+override+"') for port "+port+" in machine configuration "+machine.config());
426+
}
427+
port = hostAndPortOverride.getPort();
428+
host = hostAndPortOverride.getHostText().trim();
429+
} else {
430+
host = userAndHostAndPort.getHostAndPort().getHostText().trim();
431+
}
432+
433+
machine.config().set(ConfigKeys.newStringConfigKey("address"), host);
434+
try {
435+
InetAddress.getByName(host);
436+
} catch (Exception e) {
437+
throw new IllegalArgumentException("Invalid host '"+host+"' specified in '"+machine+"': "+e);
438+
}
439+
440+
if (userAndHostAndPort.getUser() != null) {
441+
machine.config().set(ConfigKeys.newStringConfigKey("user"), userAndHostAndPort.getUser());
442+
}
443+
if (userAndHostAndPort.getHostAndPort().hasPort()) {
444+
machine.config().set(ConfigKeys.newConfigKey(Integer.class,"port"), port);
445+
}
446+
}
447+
448+
private UserAndHostAndPort parseUserAndHostAndPort(String val) {
449+
String userPart = null;
450+
String hostPart = val;
451+
if (val.contains("@")) {
452+
userPart = val.substring(0, val.indexOf("@"));
453+
hostPart = val.substring(val.indexOf("@")+1);
454+
}
455+
return UserAndHostAndPort.fromParts(userPart, HostAndPort.fromString(hostPart));
456+
}
457+
458+
private UserAndHostAndPort parseUserAndHostAndPort(String val, int defaultPort) {
459+
UserAndHostAndPort result = parseUserAndHostAndPort(val);
460+
if (!result.getHostAndPort().hasPort()) {
461+
result = UserAndHostAndPort.fromParts(result.getUser(), result.getHostAndPort().getHostText(), defaultPort);
462+
}
463+
return result;
464+
}
391465

392466
protected void restoreMachineConfig(MachineLocation machine) {
393467
if (origConfigs == null) {

0 commit comments

Comments
 (0)