Revision a71f835e lib/client/gnt_instance.py

b/lib/client/gnt_instance.py
65 65
  ]
66 66

  
67 67

  
68
_MISSING = object()
68 69
_ENV_OVERRIDE = frozenset(["list"])
69 70

  
70 71

  
......
1267 1268
  return retcode
1268 1269

  
1269 1270

  
1271
def _ConvertNicDiskModifications(mods):
1272
  """Converts NIC/disk modifications from CLI to opcode.
1273

  
1274
  When L{opcodes.OpInstanceSetParams} was changed to support adding/removing
1275
  disks at arbitrary indices, its parameter format changed. This function
1276
  converts legacy requests (e.g. "--net add" or "--disk add:size=4G") to the
1277
  newer format and adds support for new-style requests (e.g. "--new 4:add").
1278

  
1279
  @type mods: list of tuples
1280
  @param mods: Modifications as given by command line parser
1281
  @rtype: list of tuples
1282
  @return: Modifications as understood by L{opcodes.OpInstanceSetParams}
1283

  
1284
  """
1285
  result = []
1286

  
1287
  for (idx, params) in mods:
1288
    if idx == constants.DDM_ADD:
1289
      # Add item as last item (legacy interface)
1290
      action = constants.DDM_ADD
1291
      idxno = -1
1292
    elif idx == constants.DDM_REMOVE:
1293
      # Remove last item (legacy interface)
1294
      action = constants.DDM_REMOVE
1295
      idxno = -1
1296
    else:
1297
      # Modifications and adding/removing at arbitrary indices
1298
      try:
1299
        idxno = int(idx)
1300
      except (TypeError, ValueError):
1301
        raise errors.OpPrereqError("Non-numeric index '%s'" % idx,
1302
                                   errors.ECODE_INVAL)
1303

  
1304
      add = params.pop(constants.DDM_ADD, _MISSING)
1305
      remove = params.pop(constants.DDM_REMOVE, _MISSING)
1306

  
1307
      if not (add is _MISSING or remove is _MISSING):
1308
        raise errors.OpPrereqError("Cannot add and remove at the same time",
1309
                                   errors.ECODE_INVAL)
1310
      elif add is not _MISSING:
1311
        action = constants.DDM_ADD
1312
      elif remove is not _MISSING:
1313
        action = constants.DDM_REMOVE
1314
      else:
1315
        action = constants.DDM_MODIFY
1316

  
1317
      assert not (constants.DDMS_VALUES_WITH_MODIFY & set(params.keys()))
1318

  
1319
    if action == constants.DDM_REMOVE and params:
1320
      raise errors.OpPrereqError("Not accepting parameters on removal",
1321
                                 errors.ECODE_INVAL)
1322

  
1323
    result.append((action, idxno, params))
1324

  
1325
  return result
1326

  
1327

  
1328
def _ParseDiskSizes(mods):
1329
  """Parses disk sizes in parameters.
1330

  
1331
  """
1332
  for (action, _, params) in mods:
1333
    if params and constants.IDISK_SIZE in params:
1334
      params[constants.IDISK_SIZE] = \
1335
        utils.ParseUnit(params[constants.IDISK_SIZE])
1336
    elif action == constants.DDM_ADD:
1337
      raise errors.OpPrereqError("Missing required parameter 'size'",
1338
                                 errors.ECODE_INVAL)
1339

  
1340
  return mods
1341

  
1342

  
1270 1343
def SetInstanceParams(opts, args):
1271 1344
  """Modifies an instance.
1272 1345

  
......
1301 1374
  utils.ForceDictType(opts.hvparams, constants.HVS_PARAMETER_TYPES,
1302 1375
                      allowed_values=[constants.VALUE_DEFAULT])
1303 1376

  
1304
  for idx, (nic_op, nic_dict) in enumerate(opts.nics):
1305
    try:
1306
      nic_op = int(nic_op)
1307
      opts.nics[idx] = (nic_op, nic_dict)
1308
    except (TypeError, ValueError):
1309
      pass
1310

  
1311
  for idx, (disk_op, disk_dict) in enumerate(opts.disks):
1312
    try:
1313
      disk_op = int(disk_op)
1314
      opts.disks[idx] = (disk_op, disk_dict)
1315
    except (TypeError, ValueError):
1316
      pass
1317
    if disk_op == constants.DDM_ADD:
1318
      if "size" not in disk_dict:
1319
        raise errors.OpPrereqError("Missing required parameter 'size'",
1320
                                   errors.ECODE_INVAL)
1321
      disk_dict["size"] = utils.ParseUnit(disk_dict["size"])
1377
  nics = _ConvertNicDiskModifications(opts.nics)
1378
  disks = _ParseDiskSizes(_ConvertNicDiskModifications(opts.disks))
1322 1379

  
1323 1380
  if (opts.disk_template and
1324 1381
      opts.disk_template in constants.DTS_INT_MIRROR and
......
1335 1392
    offline = None
1336 1393

  
1337 1394
  op = opcodes.OpInstanceSetParams(instance_name=args[0],
1338
                                   nics=opts.nics,
1339
                                   disks=opts.disks,
1395
                                   nics=nics,
1396
                                   disks=disks,
1340 1397
                                   disk_template=opts.disk_template,
1341 1398
                                   remote_node=opts.node,
1342 1399
                                   hvparams=opts.hvparams,

Also available in: Unified diff