Commit bfc2002f authored by Michael Hanselmann's avatar Michael Hanselmann
Browse files

RAPI client: Update ReplaceInstanceDisks



- The RAPI client shouldn't check the replacement mode as only
  the server knows which ones are valid (constants are still
  provided for convenience)
- Disks shouldn't be a list of names, but of indexes
Signed-off-by: default avatarMichael Hanselmann <hansmi@google.com>
Reviewed-by: default avatarGuido Trotter <ultrotter@google.com>
parent fde28316
......@@ -45,12 +45,6 @@ REPLACE_DISK_PRI = "replace_on_primary"
REPLACE_DISK_SECONDARY = "replace_on_secondary"
REPLACE_DISK_CHG = "replace_new_secondary"
REPLACE_DISK_AUTO = "replace_auto"
VALID_REPLACEMENT_MODES = frozenset([
REPLACE_DISK_PRI,
REPLACE_DISK_SECONDARY,
REPLACE_DISK_CHG,
REPLACE_DISK_AUTO,
])
VALID_NODE_ROLES = frozenset([
"drained", "master", "master-candidate", "offline", "regular",
])
......@@ -79,13 +73,6 @@ class GanetiApiError(Error):
self.code = code
class InvalidReplacementMode(Error):
"""Raised when an invalid disk replacement mode is attempted.
"""
pass
class InvalidNodeRole(Error):
"""Raised when an invalid node role is used.
......@@ -721,14 +708,14 @@ class GanetiRapiClient(object):
("/%s/instances/%s/reinstall" %
(GANETI_RAPI_VERSION, instance)), query, None)
def ReplaceInstanceDisks(self, instance, disks, mode=REPLACE_DISK_AUTO,
def ReplaceInstanceDisks(self, instance, disks=None, mode=REPLACE_DISK_AUTO,
remote_node=None, iallocator=None, dry_run=False):
"""Replaces disks on an instance.
@type instance: str
@param instance: instance whose disks to replace
@type disks: list of str
@param disks: disks to replace
@type disks: list of ints
@param disks: Indexes of disks to replace
@type mode: str
@param mode: replacement mode to use (defaults to replace_auto)
@type remote_node: str or None
......@@ -743,27 +730,20 @@ class GanetiRapiClient(object):
@rtype: int
@return: job id
@raises InvalidReplacementMode: If an invalid disk replacement mode is given
@raises GanetiApiError: If no secondary node is given with a non-auto
replacement mode is requested.
"""
if mode not in VALID_REPLACEMENT_MODES:
raise InvalidReplacementMode("%s is not a valid disk replacement mode" %
mode)
query = [
("mode", mode),
("disks", ",".join(disks)),
]
if mode == REPLACE_DISK_AUTO:
query.append(("iallocator", iallocator))
elif mode == REPLACE_DISK_SECONDARY:
if remote_node is None:
raise GanetiApiError("Missing secondary node")
if disks:
query.append(("disks", ",".join(str(idx) for idx in disks)))
if remote_node:
query.append(("remote_node", remote_node))
if iallocator:
query.append(("iallocator", iallocator))
if dry_run:
query.append(("dry-run", 1))
......
......@@ -309,32 +309,30 @@ class GanetiRapiClientTests(testutils.GanetiTestCase):
def testReplaceInstanceDisks(self):
self.rapi.AddResponse("999")
job_id = self.client.ReplaceInstanceDisks("instance-name",
["hda", "hdc"], dry_run=True, iallocator="hail")
disks=[0, 1], dry_run=True, iallocator="hail")
self.assertEqual(999, job_id)
self.assertHandler(rlib2.R_2_instances_name_replace_disks)
self.assertItems(["instance-name"])
self.assertQuery("disks", ["hda,hdc"])
self.assertQuery("disks", ["0,1"])
self.assertQuery("mode", ["replace_auto"])
self.assertQuery("iallocator", ["hail"])
self.assertDryRun()
self.assertRaises(client.InvalidReplacementMode,
self.client.ReplaceInstanceDisks,
"instance_a", ["hda"], mode="invalid_mode")
self.assertRaises(client.GanetiApiError,
self.client.ReplaceInstanceDisks,
"instance-foo", ["hda"], mode="replace_on_secondary")
self.rapi.AddResponse("1000")
job_id = self.client.ReplaceInstanceDisks("instance-bar",
["hda"], mode="replace_on_secondary", remote_node="foo-node",
disks=[1], mode="replace_on_secondary", remote_node="foo-node",
dry_run=True)
self.assertEqual(1000, job_id)
self.assertItems(["instance-bar"])
self.assertQuery("disks", ["hda"])
self.assertQuery("disks", ["1"])
self.assertQuery("remote_node", ["foo-node"])
self.assertDryRun()
self.rapi.AddResponse("5175")
self.assertEqual(5175, self.client.ReplaceInstanceDisks("instance-moo"))
self.assertItems(["instance-moo"])
self.assertQuery("disks", None)
def testGetJobs(self):
self.rapi.AddResponse('[ { "id": "123", "uri": "\\/2\\/jobs\\/123" },'
' { "id": "124", "uri": "\\/2\\/jobs\\/124" } ]')
......
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