From e015b5548401465a2c14f0237e0787489a03ef4a Mon Sep 17 00:00:00 2001
From: Iustin Pop <iustin@google.com>
Date: Wed, 20 May 2009 23:02:45 +0100
Subject: [PATCH] Allow overriding the RAPI port/scheme
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

This patch adds a very dumb way to override the port and scheme for
RAPI: if the master string doesn't contain a colon, we consider it a
simple hostname specification, and thus prepend β€˜https://’ and append
β€˜:5080’, (the default RAPI port); otherwise, we consider it a fully
specified URL, and don't do any mangling to it.

This allows to use saved RAPI responses in testing, with the β€œfile://”
scheme, or to use another port or non https mode with an actual RAPI
instance.
---
 Ganeti/HTools/Rapi.hs | 11 +++++++++--
 hbal.1                |  6 ++++--
 hn1.1                 |  6 ++++--
 3 files changed, 17 insertions(+), 6 deletions(-)

diff --git a/Ganeti/HTools/Rapi.hs b/Ganeti/HTools/Rapi.hs
index 88b5fe35c..aade23a6f 100644
--- a/Ganeti/HTools/Rapi.hs
+++ b/Ganeti/HTools/Rapi.hs
@@ -13,6 +13,7 @@ import Network.Curl.Types ()
 import Network.Curl.Code
 import Data.Either ()
 import Data.Maybe
+import Data.List
 import Control.Monad
 import Text.JSON (JSObject, JSValue)
 import Text.Printf (printf)
@@ -28,9 +29,15 @@ getUrl url = do
             _ -> fail $ printf "Curl error for '%s', error %s"
                  url (show code))
 
+-- | Append the default port if not passed in
+formatHost :: String -> String
+formatHost master =
+    if elem ':' master then  master
+    else "https://" ++ master ++ ":5080"
+
 getInstances :: String -> IO (Result String)
 getInstances master = do
-  let url2 = printf "https://%s:5080/2/instances?bulk=1" master
+  let url2 = printf "%s/2/instances?bulk=1" (formatHost master)
   body <- getUrl url2
   return $ (body >>= \x -> do
               arr <- loadJSArray x
@@ -39,7 +46,7 @@ getInstances master = do
 
 getNodes :: String -> IO (Result String)
 getNodes master = do
-  let url2 = printf "https://%s:5080/2/nodes?bulk=1" master
+  let url2 = printf "%s/2/nodes?bulk=1" (formatHost master)
   body <- getUrl url2
   return $ (body >>= \x -> do
              arr <- loadJSArray x
diff --git a/hbal.1 b/hbal.1
index cfa401cf9..c7a8e794f 100644
--- a/hbal.1
+++ b/hbal.1
@@ -287,8 +287,10 @@ how to customize the default value via the environment).
 .BI "-m" cluster
 Collect data not from files but directly from the
 .I cluster
-given as an argument via RAPI. This work for both Ganeti 1.2 and
-Ganeti 2.0.
+given as an argument via RAPI. If the argument doesn't contain a colon
+(:), then it is converted into a fully-built URL via prepending
+https:// and appending the default RAPI port, otherwise it's
+considered a fully-specified URL and is used unchanged.
 
 .TP
 .BI "-l" N ", --max-length=" N
diff --git a/hn1.1 b/hn1.1
index e683e2fbb..08621d207 100644
--- a/hn1.1
+++ b/hn1.1
@@ -232,8 +232,10 @@ how to customize the default value via the environment).
 .BI "-m" cluster
 Collect data not from files but directly from the
 .I cluster
-given as an argument via RAPI. This work for both Ganeti 1.2 and
-Ganeti 2.0.
+given as an argument via RAPI. If the argument doesn't contain a colon
+(:), then it is converted into a fully-built URL via prepending
+https:// and appending the default RAPI port, otherwise it's
+considered a fully-specified URL and is used unchanged.
 
 .TP
 .BI "-d" DEPTH ", --depth=" DEPTH
-- 
GitLab