use of com.google.cloud.tools.jib.api.buildplan.Port in project jib by GoogleContainerTools.
the class Ports method parse.
/**
* Converts/validates a list of strings representing port ranges to an expanded list of {@link
* Port}s.
*
* <p>For example: ["1000", "2000-2002"] will expand to a list of {@link Port}s with the port
* numbers [1000, 2000, 2001, 2002]
*
* @param ports the list of port numbers/ranges, with an optional protocol separated by a '/'
* (defaults to TCP if missing).
* @return the ports as a list of {@link Port}
* @throws NumberFormatException if any of the ports are in an invalid format or out of range
*/
public static Set<Port> parse(List<String> ports) throws NumberFormatException {
Set<Port> result = new HashSet<>();
for (String port : ports) {
Matcher matcher = portPattern.matcher(port);
if (!matcher.matches()) {
throw new NumberFormatException("Invalid port configuration: '" + port + "'. Make sure the port is a single number or a range of two numbers separated " + "with a '-', with or without protocol specified (e.g. '<portNum>/tcp' or " + "'<portNum>/udp').");
}
// Parse protocol
int min = Integer.parseInt(matcher.group(1));
int max = min;
if (!Strings.isNullOrEmpty(matcher.group(2))) {
max = Integer.parseInt(matcher.group(2));
}
String protocol = matcher.group(3);
// Error if configured as 'max-min' instead of 'min-max'
if (min > max) {
throw new NumberFormatException("Invalid port range '" + port + "'; smaller number must come first.");
}
// Warn for possibly invalid port numbers
if (min < 1 || max > 65535) {
throw new NumberFormatException("Port number '" + port + "' is out of usual range (1-65535).");
}
for (int portNumber = min; portNumber <= max; portNumber++) {
result.add(Port.parseProtocol(portNumber, protocol));
}
}
return result;
}
use of com.google.cloud.tools.jib.api.buildplan.Port in project jib by GoogleContainerTools.
the class JsonToImageTranslator method portMapToSet.
/**
* Converts a map of exposed ports as strings to a set of {@link Port}s (e.g. {@code
* {"1000/tcp":{}}} -> {@code Port(1000, Protocol.TCP)}).
*
* @param portMap the map to convert
* @return a set of {@link Port}s
*/
@VisibleForTesting
static ImmutableSet<Port> portMapToSet(@Nullable Map<String, Map<String, String>> portMap) throws BadContainerConfigurationFormatException {
if (portMap == null) {
return ImmutableSet.of();
}
ImmutableSet.Builder<Port> ports = new ImmutableSet.Builder<>();
for (Map.Entry<String, Map<String, String>> entry : portMap.entrySet()) {
String port = entry.getKey();
Matcher matcher = PORT_PATTERN.matcher(port);
if (!matcher.matches()) {
throw new BadContainerConfigurationFormatException("Invalid port configuration: '" + port + "'.");
}
int portNumber = Integer.parseInt(matcher.group("portNum"));
String protocol = matcher.group("protocol");
ports.add(Port.parseProtocol(portNumber, protocol));
}
return ports.build();
}
Aggregations