You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by kl...@apache.org on 2017/09/26 14:34:33 UTC

mesos git commit: Added CLI utility function to verify addresses.

Repository: mesos
Updated Branches:
  refs/heads/master 1b23456aa -> 016a565f3


Added CLI utility function to verify addresses.

This will be used by future plugins.

Review: https://reviews.apache.org/r/61212/


Project: http://git-wip-us.apache.org/repos/asf/mesos/repo
Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/016a565f
Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/016a565f
Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/016a565f

Branch: refs/heads/master
Commit: 016a565f3c8f0b2028fa6e70816531bc3b13bcc5
Parents: 1b23456
Author: Armand Grillet <ag...@mesosphere.io>
Authored: Tue Sep 26 16:32:28 2017 +0200
Committer: Kevin Klues <kl...@gmail.com>
Committed: Tue Sep 26 16:33:43 2017 +0200

----------------------------------------------------------------------
 src/python/cli_new/lib/cli/util.py | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/016a565f/src/python/cli_new/lib/cli/util.py
----------------------------------------------------------------------
diff --git a/src/python/cli_new/lib/cli/util.py b/src/python/cli_new/lib/cli/util.py
index 7371f83..8c96a6a 100644
--- a/src/python/cli_new/lib/cli/util.py
+++ b/src/python/cli_new/lib/cli/util.py
@@ -22,6 +22,7 @@ import imp
 import importlib
 import os
 import re
+import socket
 import textwrap
 
 from cli.exceptions import CLIException
@@ -152,6 +153,37 @@ def format_subcommands_help(cmd):
     return (arguments, short_help, long_help, flag_string)
 
 
+def verify_address_format(address):
+    """
+    Verify that an address ip and port are correct.
+    """
+    # We use 'basestring' as the type of address because it can be
+    # 'str' or 'unicode' depending on the source of the address (e.g.
+    # a config file or a flag). Both types inherit from basestring.
+    if not isinstance(address, basestring):
+        raise CLIException("The address must be a string")
+
+    address_pattern = re.compile(r'[0-9]+(?:\.[0-9]+){3}:[0-9]+')
+    if not address_pattern.match(address):
+        raise CLIException("The address '{address}' does not match"
+                           " the expected format '<ip>:<port>'"
+                           .format(address=address))
+
+    colon_pos = address.rfind(':')
+    ip = address[:colon_pos]
+    port = int(address[colon_pos+1:])
+
+    try:
+        socket.inet_aton(ip)
+    except socket.error as err:
+        raise CLIException("The IP '{ip}' is not valid: {error}"
+                           .format(ip=ip, error=err))
+
+    # A correct port number is between these two values.
+    if port < 0 or port > 65535:
+        raise CLIException("The port '{port}' is not valid")
+
+
 def join_plugin_paths(settings, config):
     """
     Return all the plugin paths combined