commit 3201717461bc6e55f2deab7ad426ebfbb7b1e943
Author: Agata Murawska <agatamurawska@google.com>
Date:   Tue Dec 6 16:13:31 2011 +0100

    Instance policy command line support
    
    Signed-off-by: Agata Murawska <agatamurawska@google.com>
    Reviewed-by: Iustin Pop <iustin@google.com>

diff --git a/lib/cli.py b/lib/cli.py
index 5cceb7b..27edc89 100644
--- a/lib/cli.py
+++ b/lib/cli.py
@@ -166,6 +166,11 @@ __all__ = [
   "SHOWCMD_OPT",
   "SHUTDOWN_TIMEOUT_OPT",
   "SINGLE_NODE_OPT",
+  "SPECS_CPU_COUNT_OPT",
+  "SPECS_DISK_COUNT_OPT",
+  "SPECS_DISK_SIZE_OPT",
+  "SPECS_MEM_SIZE_OPT",
+  "SPECS_NIC_COUNT_OPT",
   "SPICE_CACERT_OPT",
   "SPICE_CERT_OPT",
   "SRC_DIR_OPT",
@@ -760,6 +765,28 @@ DISK_PARAMS_OPT = cli_option("-D", "--disk-parameters", dest="diskparams",
                              " template:option=value,option=value,...",
                              type="identkeyval", action="append", default=[])
 
+SPECS_MEM_SIZE_OPT = cli_option("--specs-mem-size", dest="ispecs_mem_size",
+                                 type="keyval", default={},
+                                 help="Memory count specs: min, max, std"
+                                 " (in MB)")
+
+SPECS_CPU_COUNT_OPT = cli_option("--specs-cpu-count", dest="ispecs_cpu_count",
+                                 type="keyval", default={},
+                                 help="CPU count specs: min, max, std")
+
+SPECS_DISK_COUNT_OPT = cli_option("--specs-disk-count",
+                                  dest="ispecs_disk_count",
+                                  type="keyval", default={},
+                                  help="Disk count specs: min, max, std")
+
+SPECS_DISK_SIZE_OPT = cli_option("--specs-disk-size", dest="ispecs_disk_size",
+                                 type="keyval", default={},
+                                 help="Disk size specs: min, max, std (in MB)")
+
+SPECS_NIC_COUNT_OPT = cli_option("--specs-nic-count", dest="ispecs_nic_count",
+                                 type="keyval", default={},
+                                 help="NIC count specs: min, max, std")
+
 HYPERVISOR_OPT = cli_option("-H", "--hypervisor-parameters", dest="hypervisor",
                             help="Hypervisor and hypervisor options, in the"
                             " format hypervisor:option=value,option=value,...",
diff --git a/lib/client/gnt_cluster.py b/lib/client/gnt_cluster.py
index 7c7f071..5c95207 100644
--- a/lib/client/gnt_cluster.py
+++ b/lib/client/gnt_cluster.py
@@ -1403,6 +1403,13 @@ def Epo(opts, args):
   else:
     return _EpoOff(opts, node_list, inst_map)
 
+INSTANCE_POLICY_OPTS = [
+  SPECS_CPU_COUNT_OPT,
+  SPECS_DISK_COUNT_OPT,
+  SPECS_DISK_SIZE_OPT,
+  SPECS_MEM_SIZE_OPT,
+  SPECS_NIC_COUNT_OPT,
+  ]
 
 commands = {
   "init": (
@@ -1414,7 +1421,7 @@ commands = {
      MAINTAIN_NODE_HEALTH_OPT, UIDPOOL_OPT, DRBD_HELPER_OPT, NODRBD_STORAGE_OPT,
      DEFAULT_IALLOCATOR_OPT, PRIMARY_IP_VERSION_OPT, PREALLOC_WIPE_DISKS_OPT,
      NODE_PARAMS_OPT, GLOBAL_SHARED_FILEDIR_OPT, USE_EXTERNAL_MIP_SCRIPT,
-     DISK_PARAMS_OPT],
+     DISK_PARAMS_OPT] + INSTANCE_POLICY_OPTS,
     "[opts...] <cluster_name>", "Initialises a new cluster configuration"),
   "destroy": (
     DestroyCluster, ARGS_NONE, [YES_DOIT_OPT],
@@ -1492,7 +1499,8 @@ commands = {
      DRBD_HELPER_OPT, NODRBD_STORAGE_OPT, DEFAULT_IALLOCATOR_OPT,
      RESERVED_LVS_OPT, DRY_RUN_OPT, PRIORITY_OPT, PREALLOC_WIPE_DISKS_OPT,
      NODE_PARAMS_OPT, USE_EXTERNAL_MIP_SCRIPT, DISK_PARAMS_OPT, HV_STATE_OPT,
-     DISK_STATE_OPT],
+     DISK_STATE_OPT] +
+    INSTANCE_POLICY_OPTS,
     "[opts...]",
     "Alters the parameters of the cluster"),
   "renew-crypto": (
diff --git a/lib/objects.py b/lib/objects.py
index f0bd2bb..7967059 100644
--- a/lib/objects.py
+++ b/lib/objects.py
@@ -44,6 +44,7 @@ from cStringIO import StringIO
 from ganeti import errors
 from ganeti import constants
 from ganeti import netutils
+from ganeti import utils
 
 from socket import AF_INET
 
@@ -54,6 +55,18 @@ __all__ = ["ConfigObject", "ConfigData", "NIC", "Disk", "Instance",
 _TIMESTAMPS = ["ctime", "mtime"]
 _UUID = ["uuid"]
 
+# constants used to create InstancePolicy dictionary
+TISPECS_GROUP_TYPES = {
+  constants.MIN_ISPECS: constants.VTYPE_INT,
+  constants.MAX_ISPECS: constants.VTYPE_INT,
+}
+
+TISPECS_CLUSTER_TYPES = {
+  constants.MIN_ISPECS: constants.VTYPE_INT,
+  constants.MAX_ISPECS: constants.VTYPE_INT,
+  constants.STD_ISPECS: constants.VTYPE_INT,
+  }
+
 
 def FillDict(defaults_dict, custom_dict, skip_keys=None):
   """Basic function to apply settings on top a default dict.
@@ -159,6 +172,45 @@ def MakeEmptyIPolicy():
     ])
 
 
+def CreateIPolicyFromOpts(ispecs_mem_size=None,
+                          ispecs_cpu_count=None,
+                          ispecs_disk_count=None,
+                          ispecs_disk_size=None,
+                          ispecs_nic_count=None,
+                          group_ipolicy=False,
+                          allowed_values=None):
+  """Creation of instane policy based on command line options.
+
+
+  """
+  # prepare ipolicy dict
+  ipolicy_transposed = {
+    constants.MEM_SIZE_SPEC: ispecs_mem_size,
+    constants.CPU_COUNT_SPEC: ispecs_cpu_count,
+    constants.DISK_COUNT_SPEC: ispecs_disk_count,
+    constants.DISK_SIZE_SPEC: ispecs_disk_size,
+    constants.NIC_COUNT_SPEC: ispecs_nic_count,
+    }
+
+  # first, check that the values given are correct
+  if group_ipolicy:
+    forced_type = TISPECS_GROUP_TYPES
+  else:
+    forced_type = TISPECS_CLUSTER_TYPES
+
+  for specs in ipolicy_transposed.values():
+    utils.ForceDictType(specs, forced_type, allowed_values=allowed_values)
+
+  # then transpose
+  ipolicy_out = MakeEmptyIPolicy()
+  for name, specs in ipolicy_transposed.iteritems():
+    assert name in constants.ISPECS_PARAMETERS
+    for key, val in specs.items(): # {min: .. ,max: .., std: ..}
+      ipolicy_out[key][name] = val
+
+  return ipolicy_out
+
+
 class ConfigObject(object):
   """A generic config object.
 
