Commit 10f5ab6c authored by Michael Hanselmann's avatar Michael Hanselmann
Browse files

RAPI client: Encode boolean and None query values



Boolean values must be passed as 0 or 1. None should be an empty
value ("").
Signed-off-by: default avatarMichael Hanselmann <hansmi@google.com>
Reviewed-by: default avatarGuido Trotter <ultrotter@google.com>
parent 75f53ffe
......@@ -351,6 +351,34 @@ class GanetiRapiClient(object):
"User-Agent": self.USER_AGENT,
}
@staticmethod
def _EncodeQuery(query):
"""Encode query values for RAPI URL.
@type query: list of two-tuples
@param query: Query arguments
@rtype: list
@return: Query list with encoded values
"""
result = []
for name, value in query:
if value is None:
result.append((name, ""))
elif isinstance(value, bool):
# Boolean values must be encoded as 0 or 1
result.append((name, int(value)))
elif isinstance(value, (list, tuple, dict)):
raise ValueError("Invalid query data type %r" % type(value).__name__)
else:
result.append((name, value))
return result
def _SendRequest(self, method, path, query, content):
"""Sends an HTTP request.
......@@ -384,7 +412,7 @@ class GanetiRapiClient(object):
url = [self._base_url, path]
if query:
url.append("?")
url.append(urllib.urlencode(query))
url.append(urllib.urlencode(self._EncodeQuery(query)))
req = _RapiRequest(method, "".join(url), self._headers, encoded_content)
......
......@@ -151,6 +151,30 @@ class GanetiRapiClientTests(testutils.GanetiTestCase):
def assertDryRun(self):
self.assertTrue(self.rapi.GetLastHandler().dryRun())
def testEncodeQuery(self):
query = [
("a", None),
("b", 1),
("c", 2),
("d", "Foo"),
("e", True),
]
expected = [
("a", ""),
("b", 1),
("c", 2),
("d", "Foo"),
("e", 1),
]
self.assertEqualValues(self.client._EncodeQuery(query),
expected)
# invalid types
for i in [[1, 2, 3], {"moo": "boo"}, (1, 2, 3)]:
self.assertRaises(ValueError, self.client._EncodeQuery, [("x", i)])
def testHttpError(self):
self.rapi.AddResponse(None, code=404)
try:
......@@ -255,7 +279,7 @@ class GanetiRapiClientTests(testutils.GanetiTestCase):
self.assertItems(["i-bar"])
self.assertDryRun()
self.assertQuery("type", ["hard"])
self.assertQuery("ignore_secondaries", ["True"])
self.assertQuery("ignore_secondaries", ["1"])
def testShutdownInstance(self):
self.rapi.AddResponse("1487")
......@@ -403,7 +427,7 @@ class GanetiRapiClientTests(testutils.GanetiTestCase):
self.client.SetNodeRole("node-foo", "master-candidate", force=True))
self.assertHandler(rlib2.R_2_nodes_name_role)
self.assertItems(["node-foo"])
self.assertQuery("force", ["True"])
self.assertQuery("force", ["1"])
self.assertEqual("\"master-candidate\"", self.http.last_request.data)
self.assertRaises(client.InvalidNodeRole,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment