Use split_port for ports format check
Rather than implement the logic a second time, use docker-py split_port function to test if the ports is valid. Signed-off-by: Mazz Mosley <mazz@houseofmnowster.com>
This commit is contained in:
parent
2e428f94ca
commit
df74b131ff
3 changed files with 12 additions and 29 deletions
|
|
@ -1,5 +1,6 @@
|
|||
import os
|
||||
|
||||
from docker.utils.ports import split_port
|
||||
import json
|
||||
from jsonschema import Draft4Validator, FormatChecker, ValidationError
|
||||
|
||||
|
|
@ -26,26 +27,13 @@ DOCKER_CONFIG_HINTS = {
|
|||
VALID_NAME_CHARS = '[a-zA-Z0-9\._\-]'
|
||||
|
||||
|
||||
@FormatChecker.cls_checks(format="ports", raises=ValidationError("Ports is incorrectly formatted."))
|
||||
@FormatChecker.cls_checks(format="ports", raises=ValidationError("Invalid port formatting, it should be '[[remote_ip:]remote_port:]port[/protocol]'"))
|
||||
def format_ports(instance):
|
||||
def _is_valid(port):
|
||||
if ':' in port or '/' in port:
|
||||
return True
|
||||
try:
|
||||
int(port)
|
||||
return True
|
||||
except ValueError:
|
||||
return False
|
||||
try:
|
||||
split_port(instance)
|
||||
except ValueError:
|
||||
return False
|
||||
|
||||
if isinstance(instance, list):
|
||||
for port in instance:
|
||||
if not _is_valid(port):
|
||||
return False
|
||||
return True
|
||||
elif isinstance(instance, str):
|
||||
return _is_valid(instance)
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def get_unsupported_config_msg(service_name, error_key):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue