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/05/29 13:04:24 UTC

mesos git commit: Added HTTP utility functions to the new Mesos CLI.

Repository: mesos
Updated Branches:
  refs/heads/master 8740ce8d3 -> bf9715a9b


Added HTTP utility functions to the new Mesos CLI.

These will be used by future plugins and tests.

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


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

Branch: refs/heads/master
Commit: bf9715a9bdd688cfddcf9ad72b528ff6f7bdb6ef
Parents: 8740ce8
Author: Armand Grillet <ag...@mesosphere.io>
Authored: Mon May 29 05:57:42 2017 -0700
Committer: Kevin Klues <kl...@gmail.com>
Committed: Mon May 29 06:02:23 2017 -0700

----------------------------------------------------------------------
 src/cli_new/lib/cli/http.py | 77 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 77 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/bf9715a9/src/cli_new/lib/cli/http.py
----------------------------------------------------------------------
diff --git a/src/cli_new/lib/cli/http.py b/src/cli_new/lib/cli/http.py
new file mode 100644
index 0000000..03d6031
--- /dev/null
+++ b/src/cli_new/lib/cli/http.py
@@ -0,0 +1,77 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+"""
+A collection of http related functions used by the CLI and its Plugins.
+"""
+
+import json
+import urllib2
+import time
+
+from cli.exceptions import CLIException
+
+
+def read_endpoint(addr, endpoint):
+    """
+    Read the specified endpoint and return the results.
+    """
+    try:
+        url = "http://{addr}/{endpoint}".format(addr=addr, endpoint=endpoint)
+
+        http_response = urllib2.urlopen(url).read().decode("utf-8")
+    except Exception as exception:
+        raise CLIException("{error}".format(error=str(exception)))
+
+    return http_response
+
+
+def get_json(addr, endpoint, condition=None, timeout=5):
+    """
+    Return the contents of the 'endpoint' at 'addr' as JSON data
+    subject to the condition specified in 'condition'. If we are
+    unable to read the data or unable to meet the condition within
+    'timeout' seconds we throw an error.
+    """
+    start_time = time.time()
+
+    while True:
+        data = None
+
+        try:
+            data = read_endpoint(addr, endpoint)
+        except Exception as exception:
+            pass
+
+        if data:
+            try:
+                data = json.loads(data)
+            except Exception as exception:
+                raise CLIException("Could not load JSON from '{data}': {error}"
+                                   .format(data=data, error=str(exception)))
+
+            if not condition:
+                return data
+
+            if condition(data):
+                return data
+
+        if time.time() - start_time > timeout:
+            raise CLIException("Failed to get data within {seconds} seconds:"
+                               " {error}"
+                               .format(seconds=str(timeout), error=exception))
+
+        time.sleep(0.1)