Commit 21bcb9aa authored by Michael Hanselmann's avatar Michael Hanselmann
Browse files

Fix off-by-one error when modifying instance NIC



For an instance with exactly one NIC:

$ gnt-instance modify --net 1:ip=1.2.3.4 inst1
Failure: prerequisites not met for this operation:
error type: wrong_input, error details:
Invalid NIC index 1, valid values are 0 to 1

For an instance with no NIC at all, it fails with “Invalid NIC index 0, valid
values are 0 to 0”. This is fixed by this patch.
Signed-off-by: default avatarMichael Hanselmann <hansmi@google.com>
Reviewed-by: default avatarGuido Trotter <ultrotter@google.com>
parent b8716596
......@@ -1396,7 +1396,7 @@ def GenericInstanceCreate(mode, opts, args):
if opts.nics:
try:
nic_max = max(int(nidx[0])+1 for nidx in opts.nics)
nic_max = max(int(nidx[0]) + 1 for nidx in opts.nics)
except ValueError, err:
raise errors.OpPrereqError("Invalid NIC index passed: %s" % str(err))
nics = [{}] * nic_max
......@@ -1427,7 +1427,7 @@ def GenericInstanceCreate(mode, opts, args):
if opts.sd_size is not None:
opts.disks = [(0, {"size": opts.sd_size})]
try:
disk_max = max(int(didx[0])+1 for didx in opts.disks)
disk_max = max(int(didx[0]) + 1 for didx in opts.disks)
except ValueError, err:
raise errors.OpPrereqError("Invalid disk index passed: %s" % str(err))
disks = [{}] * disk_max
......
......@@ -7671,10 +7671,14 @@ class LUSetInstanceParams(LogicalUnit):
continue
if nic_op != constants.DDM_ADD:
# an existing nic
if not instance.nics:
raise errors.OpPrereqError("Invalid NIC index %s, instance has"
" no NICs" % nic_op,
errors.ECODE_INVAL)
if nic_op < 0 or nic_op >= len(instance.nics):
raise errors.OpPrereqError("Invalid NIC index %s, valid values"
" are 0 to %d" %
(nic_op, len(instance.nics)),
(nic_op, len(instance.nics) - 1),
errors.ECODE_INVAL)
old_nic_params = instance.nics[nic_op].nicparams
old_nic_ip = instance.nics[nic_op].ip
......
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