Revision b21d488b lib/cmdlib.py

b/lib/cmdlib.py
13123 13123
  return [(op, idx, params, fn()) for (op, idx, params) in mods]
13124 13124

  
13125 13125

  
13126
def GetItemFromContainer(identifier, kind, container):
13127
  """Return the item refered by the identifier.
13128

  
13129
  @type identifier: string
13130
  @param identifier: Item index or name or UUID
13131
  @type kind: string
13132
  @param kind: One-word item description
13133
  @type container: list
13134
  @param container: Container to get the item from
13135

  
13136
  """
13137
  # Index
13138
  try:
13139
    idx = int(identifier)
13140
    if idx == -1:
13141
      # Append
13142
      absidx = len(container) - 1
13143
    elif idx < 0:
13144
      raise IndexError("Not accepting negative indices other than -1")
13145
    elif idx > len(container):
13146
      raise IndexError("Got %s index %s, but there are only %s" %
13147
                       (kind, idx, len(container)))
13148
    else:
13149
      absidx = idx
13150
    return (absidx, container[idx])
13151
  except ValueError:
13152
    pass
13153

  
13154
  for idx, item in enumerate(container):
13155
    if item.uuid == identifier or item.name == identifier:
13156
      return (idx, item)
13157

  
13158
  raise errors.OpPrereqError("Cannot find %s with identifier %s" %
13159
                             (kind, identifier), errors.ECODE_NOENT)
13160

  
13161

  
13126 13162
#: Type description for changes as returned by L{ApplyContainerMods}'s
13127 13163
#: callbacks
13128 13164
_TApplyContModsCbChanges = \
......
13159 13195
    item and private data object as added by L{PrepareContainerMods}
13160 13196

  
13161 13197
  """
13162
  for (op, idx, params, private) in mods:
13163
    if idx == -1:
13164
      # Append
13165
      absidx = len(container) - 1
13166
    elif idx < 0:
13167
      raise IndexError("Not accepting negative indices other than -1")
13168
    elif idx > len(container):
13169
      raise IndexError("Got %s index %s, but there are only %s" %
13170
                       (kind, idx, len(container)))
13171
    else:
13172
      absidx = idx
13173

  
13198
  for (op, identifier, params, private) in mods:
13174 13199
    changes = None
13175 13200

  
13176 13201
    if op == constants.DDM_ADD:
13177 13202
      # Calculate where item will be added
13203
      # When adding an item, identifier can only be an index
13204
      try:
13205
        idx = int(identifier)
13206
      except ValueError:
13207
        raise errors.OpPrereqError("Only possitive integer or -1 is accepted as"
13208
                                   " identifier for %s" % constants.DDM_ADD,
13209
                                   errors.ECODE_INVAL)
13178 13210
      if idx == -1:
13179 13211
        addidx = len(container)
13180 13212
      else:
13213
        if idx < 0:
13214
          raise IndexError("Not accepting negative indices other than -1")
13215
        elif idx > len(container):
13216
          raise IndexError("Got %s index %s, but there are only %s" %
13217
                           (kind, idx, len(container)))
13181 13218
        addidx = idx
13182 13219

  
13183 13220
      if create_fn is None:
......
13194 13231
        container.insert(idx, item)
13195 13232
    else:
13196 13233
      # Retrieve existing item
13197
      try:
13198
        item = container[absidx]
13199
      except IndexError:
13200
        raise IndexError("Invalid %s index %s" % (kind, idx))
13234
      (absidx, item) = GetItemFromContainer(identifier, kind, container)
13201 13235

  
13202 13236
      if op == constants.DDM_REMOVE:
13203 13237
        assert not params

Also available in: Unified diff