Revision 57d8e228

b/lib/rapi/client.py
172 172
  return _AppendIf(container, condition, (_QPARAM_FORCE, 1))
173 173

  
174 174

  
175
def _SetItemIf(container, condition, item, value):
176
  """Sets an item if a condition evaluates to truth.
177

  
178
  """
179
  if condition:
180
    container[item] = value
181

  
182
  return condition
183

  
184

  
175 185
def UsesRapiClient(fn):
176 186
  """Decorator for code using RAPI client to initialize pycURL.
177 187

  
......
791 801

  
792 802
    """
793 803
    body = {}
794

  
795
    if disks is not None:
796
      body["disks"] = disks
797

  
798
    if nodes is not None:
799
      body["nodes"] = nodes
804
    _SetItemIf(body, disks is not None, "disks", disks)
805
    _SetItemIf(body, nodes is not None, "nodes", nodes)
800 806

  
801 807
    return self._SendRequest(HTTP_POST,
802 808
                             ("/%s/instances/%s/recreate-disks" %
......
823 829
      "amount": amount,
824 830
      }
825 831

  
826
    if wait_for_sync is not None:
827
      body["wait_for_sync"] = wait_for_sync
832
    _SetItemIf(body, wait_for_sync is not None, "wait_for_sync", wait_for_sync)
828 833

  
829 834
    return self._SendRequest(HTTP_POST,
830 835
                             ("/%s/instances/%s/disk/%s/grow" %
......
974 979
      body = {
975 980
        "start": not no_startup,
976 981
        }
977
      if os is not None:
978
        body["os"] = os
979
      if osparams is not None:
980
        body["osparams"] = osparams
982
      _SetItemIf(body, os is not None, "os", os)
983
      _SetItemIf(body, osparams is not None, "osparams", osparams)
981 984
      return self._SendRequest(HTTP_POST,
982 985
                               ("/%s/instances/%s/reinstall" %
983 986
                                (GANETI_RAPI_VERSION, instance)), None, body)
......
1067 1070
      "mode": mode,
1068 1071
      }
1069 1072

  
1070
    if shutdown is not None:
1071
      body["shutdown"] = shutdown
1072

  
1073
    if remove_instance is not None:
1074
      body["remove_instance"] = remove_instance
1075

  
1076
    if x509_key_name is not None:
1077
      body["x509_key_name"] = x509_key_name
1078

  
1079
    if destination_x509_ca is not None:
1080
      body["destination_x509_ca"] = destination_x509_ca
1073
    _SetItemIf(body, shutdown is not None, "shutdown", shutdown)
1074
    _SetItemIf(body, remove_instance is not None,
1075
               "remove_instance", remove_instance)
1076
    _SetItemIf(body, x509_key_name is not None, "x509_key_name", x509_key_name)
1077
    _SetItemIf(body, destination_x509_ca is not None,
1078
               "destination_x509_ca", destination_x509_ca)
1081 1079

  
1082 1080
    return self._SendRequest(HTTP_PUT,
1083 1081
                             ("/%s/instances/%s/export" %
......
1097 1095

  
1098 1096
    """
1099 1097
    body = {}
1100

  
1101
    if mode is not None:
1102
      body["mode"] = mode
1103

  
1104
    if cleanup is not None:
1105
      body["cleanup"] = cleanup
1098
    _SetItemIf(body, mode is not None, "mode", mode)
1099
    _SetItemIf(body, cleanup is not None, "cleanup", cleanup)
1106 1100

  
1107 1101
    return self._SendRequest(HTTP_PUT,
1108 1102
                             ("/%s/instances/%s/migrate" %
......
1126 1120

  
1127 1121
    """
1128 1122
    body = {}
1129

  
1130
    if iallocator is not None:
1131
      body["iallocator"] = iallocator
1132

  
1133
    if ignore_consistency is not None:
1134
      body["ignore_consistency"] = ignore_consistency
1135

  
1136
    if target_node is not None:
1137
      body["target_node"] = target_node
1123
    _SetItemIf(body, iallocator is not None, "iallocator", iallocator)
1124
    _SetItemIf(body, ignore_consistency is not None,
1125
               "ignore_consistency", ignore_consistency)
1126
    _SetItemIf(body, target_node is not None, "target_node", target_node)
1138 1127

  
1139 1128
    return self._SendRequest(HTTP_PUT,
1140 1129
                             ("/%s/instances/%s/failover" %
......
1159 1148
      "new_name": new_name,
1160 1149
      }
1161 1150

  
1162
    if ip_check is not None:
1163
      body["ip_check"] = ip_check
1164

  
1165
    if name_check is not None:
1166
      body["name_check"] = name_check
1151
    _SetItemIf(body, ip_check is not None, "ip_check", ip_check)
1152
    _SetItemIf(body, name_check is not None, "name_check", name_check)
1167 1153

  
1168 1154
    return self._SendRequest(HTTP_PUT,
1169 1155
                             ("/%s/instances/%s/rename" %
......
1359 1345
      # Server supports body parameters
1360 1346
      body = {}
1361 1347

  
1362
      if iallocator is not None:
1363
        body["iallocator"] = iallocator
1364
      if remote_node is not None:
1365
        body["remote_node"] = remote_node
1366
      if early_release is not None:
1367
        body["early_release"] = early_release
1368
      if mode is not None:
1369
        body["mode"] = mode
1348
      _SetItemIf(body, iallocator is not None, "iallocator", iallocator)
1349
      _SetItemIf(body, remote_node is not None, "remote_node", remote_node)
1350
      _SetItemIf(body, early_release is not None,
1351
                 "early_release", early_release)
1352
      _SetItemIf(body, mode is not None, "mode", mode)
1370 1353
    else:
1371 1354
      # Pre-2.5 request format
1372 1355
      body = None
......
1414 1397
    if _NODE_MIGRATE_REQV1 in self.GetFeatures():
1415 1398
      body = {}
1416 1399

  
1417
      if mode is not None:
1418
        body["mode"] = mode
1419
      if iallocator is not None:
1420
        body["iallocator"] = iallocator
1421
      if target_node is not None:
1422
        body["target_node"] = target_node
1400
      _SetItemIf(body, mode is not None, "mode", mode)
1401
      _SetItemIf(body, iallocator is not None, "iallocator", iallocator)
1402
      _SetItemIf(body, target_node is not None, "target_node", target_node)
1423 1403

  
1424 1404
      assert len(query) <= 1
1425 1405

  
......
1851 1831
      "fields": fields,
1852 1832
      }
1853 1833

  
1854
    if qfilter is not None:
1855
      body["qfilter"] = qfilter
1856
      # TODO: remove this after 2.7
1857
      body["filter"] = qfilter
1834
    _SetItemIf(body, qfilter is not None, "qfilter", qfilter)
1835
    # TODO: remove "filter" after 2.7
1836
    _SetItemIf(body, qfilter is not None, "filter", qfilter)
1858 1837

  
1859 1838
    return self._SendRequest(HTTP_PUT,
1860 1839
                             ("/%s/query/%s" %

Also available in: Unified diff