Revision 6998aefe

b/qa/qa_utils.py
29 29
import subprocess
30 30
import random
31 31
import tempfile
32
import operator
32 33

  
33 34
try:
34 35
  import functools
......
129 130
    raise qa_error.Error("%r doesn't match /%r/" % (string, pattern))
130 131

  
131 132

  
132
def _GetName(entity, key):
133
def _GetName(entity, fn):
133 134
  """Tries to get name of an entity.
134 135

  
135 136
  @type entity: string or dict
136
  @type key: string
137
  @param key: Dictionary key containing name
137
  @param fn: Function retrieving name from entity
138 138

  
139 139
  """
140 140
  if isinstance(entity, basestring):
141 141
    result = entity
142
  elif isinstance(entity, dict) or hasattr(entity, "__getitem__"):
143
    result = entity[key]
144 142
  else:
145
    raise qa_error.Error("Expected string or dictionary, got %s: %s" %
146
                         (type(entity), entity))
143
    result = fn(entity)
147 144

  
148 145
  if not ht.TNonEmptyString(result):
149 146
    raise Exception("Invalid name '%s'" % result)
......
182 179
  if node is None:
183 180
    node = qa_config.GetMasterNode()
184 181

  
185
  nodename = _GetName(node, "primary")
182
  nodename = _GetName(node, operator.attrgetter("primary"))
186 183

  
187 184
  if isinstance(cmd, basestring):
188 185
    cmdstr = cmd
......
614 611
  """Check if instance is running or not.
615 612

  
616 613
  """
617
  instance_name = _GetName(instance, "name")
614
  instance_name = _GetName(instance, operator.attrgetter("name"))
618 615

  
619 616
  script = qa_config.GetInstanceCheckScript()
620 617
  if not script:

Also available in: Unified diff