Rename OpRedistributeConfig and LURedistributeConfig
[ganeti-local] / lib / opcodes.py
index 4211c71..dcb46e3 100644 (file)
@@ -1,7 +1,7 @@
 #
 #
 
-# Copyright (C) 2006, 2007, 2008, 2009, 2010 Google Inc.
+# Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011 Google Inc.
 #
 # This program is free software; you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
@@ -33,6 +33,8 @@ opcodes.
 # few public methods:
 # pylint: disable-msg=R0903
 
+import logging
+
 from ganeti import constants
 from ganeti import errors
 from ganeti import ht
@@ -236,6 +238,43 @@ class BaseOpCode(object):
       slots.extend(getattr(parent, "OP_PARAMS", []))
     return slots
 
+  def Validate(self, set_defaults):
+    """Validate opcode parameters, optionally setting default values.
+
+    @type set_defaults: bool
+    @param set_defaults: Whether to set default values
+    @raise errors.OpPrereqError: When a parameter value doesn't match
+                                 requirements
+
+    """
+    for (attr_name, default, test) in self.GetAllParams():
+      assert test == ht.NoType or callable(test)
+
+      if not hasattr(self, attr_name):
+        if default == ht.NoDefault:
+          raise errors.OpPrereqError("Required parameter '%s.%s' missing" %
+                                     (self.OP_ID, attr_name),
+                                     errors.ECODE_INVAL)
+        elif set_defaults:
+          if callable(default):
+            dval = default()
+          else:
+            dval = default
+          setattr(self, attr_name, dval)
+
+      if test == ht.NoType:
+        # no tests here
+        continue
+
+      if set_defaults or hasattr(self, attr_name):
+        attr_val = getattr(self, attr_name)
+        if not test(attr_val):
+          logging.error("OpCode %s, parameter %s, has invalid type %s/value %s",
+                        self.OP_ID, attr_name, type(attr_val), attr_val)
+          raise errors.OpPrereqError("Parameter '%s.%s' fails validation" %
+                                     (self.OP_ID, attr_name),
+                                     errors.ECODE_INVAL)
+
 
 class OpCode(BaseOpCode):
   """Abstract OpCode.
@@ -250,12 +289,15 @@ class OpCode(BaseOpCode):
                       method for details).
   @cvar OP_PARAMS: List of opcode attributes, the default values they should
                    get if not already defined, and types they must match.
+  @cvar WITH_LU: Boolean that specifies whether this should be included in
+      mcpu's dispatch table
   @ivar dry_run: Whether the LU should be run in dry-run mode, i.e. just
                  the check steps
   @ivar priority: Opcode priority for queue
 
   """
   OP_ID = "OP_ABSTRACT"
+  WITH_LU = True
   OP_PARAMS = [
     ("dry_run", None, ht.TMaybeBool),
     ("debug_level", None, ht.TOr(ht.TNone, ht.TPositiveInt)),
@@ -329,7 +371,7 @@ class OpCode(BaseOpCode):
 
 # cluster opcodes
 
-class OpPostInitCluster(OpCode):
+class OpClusterPostInit(OpCode):
   """Post cluster initialization.
 
   This opcode does not touch the cluster at all. Its purpose is to run hooks
@@ -339,7 +381,7 @@ class OpPostInitCluster(OpCode):
   OP_ID = "OP_CLUSTER_POST_INIT"
 
 
-class OpDestroyCluster(OpCode):
+class OpClusterDestroy(OpCode):
   """Destroy the cluster.
 
   This opcode has no other parameters. All the state is irreversibly
@@ -349,7 +391,7 @@ class OpDestroyCluster(OpCode):
   OP_ID = "OP_CLUSTER_DESTROY"
 
 
-class OpQueryClusterInfo(OpCode):
+class OpClusterQuery(OpCode):
   """Query cluster information."""
   OP_ID = "OP_CLUSTER_QUERY"
 
@@ -420,7 +462,7 @@ class OpRepairDiskSizes(OpCode):
     ]
 
 
-class OpQueryConfigValues(OpCode):
+class OpClusterConfigQuery(OpCode):
   """Query cluster configuration values."""
   OP_ID = "OP_CLUSTER_CONFIG_QUERY"
   OP_PARAMS = [
@@ -489,7 +531,7 @@ class OpSetClusterParams(OpCode):
     ]
 
 
-class OpRedistributeConfig(OpCode):
+class OpClusterRedistConf(OpCode):
   """Force a full push of the cluster configuration.
 
   """
@@ -951,7 +993,7 @@ class OpSetInstanceParams(OpCode):
     ("disks", ht.EmptyList, ht.TList),
     ("beparams", ht.EmptyDict, ht.TDict),
     ("hvparams", ht.EmptyDict, ht.TDict),
-    ("disk_template", None, _CheckDiskTemplate),
+    ("disk_template", None, ht.TOr(ht.TNone, _CheckDiskTemplate)),
     ("remote_node", None, ht.TMaybeString),
     ("os_name", None, ht.TMaybeString),
     ("force_variant", False, ht.TBool),
@@ -985,6 +1027,17 @@ class OpAddGroup(OpCode):
     ]
 
 
+class OpAssignGroupNodes(OpCode):
+  """Assign nodes to a node group."""
+  OP_ID = "OP_GROUP_ASSIGN_NODES"
+  OP_DSC_FIELD = "group_name"
+  OP_PARAMS = [
+    _PGroupName,
+    _PForce,
+    ("nodes", ht.NoDefault, ht.TListOf(ht.TNonEmptyString)),
+    ]
+
+
 class OpQueryGroups(OpCode):
   """Compute the list of node groups."""
   OP_ID = "OP_GROUP_QUERY"
@@ -1036,7 +1089,7 @@ class OpDiagnoseOS(OpCode):
 
 
 # Exports opcodes
-class OpQueryExports(OpCode):
+class OpBackupQuery(OpCode):
   """Compute the list of exported images."""
   OP_ID = "OP_BACKUP_QUERY"
   OP_PARAMS = [
@@ -1045,7 +1098,7 @@ class OpQueryExports(OpCode):
     ]
 
 
-class OpPrepareExport(OpCode):
+class OpBackupPrepare(OpCode):
   """Prepares an instance export.
 
   @ivar instance_name: Instance name
@@ -1060,7 +1113,7 @@ class OpPrepareExport(OpCode):
     ]
 
 
-class OpExportInstance(OpCode):
+class OpBackupExport(OpCode):
   """Export an instance.
 
   For local exports, the export destination is the node name. For remote
@@ -1093,7 +1146,7 @@ class OpExportInstance(OpCode):
     ]
 
 
-class OpRemoveExport(OpCode):
+class OpBackupRemove(OpCode):
   """Remove an instance's export."""
   OP_ID = "OP_BACKUP_REMOVE"
   OP_DSC_FIELD = "instance_name"
@@ -1232,6 +1285,7 @@ class OpTestDummy(OpCode):
     ("messages", ht.NoDefault, ht.NoType),
     ("fail", ht.NoDefault, ht.NoType),
     ]
+  WITH_LU = False
 
 
 def _GetOpList():
@@ -1242,7 +1296,7 @@ def _GetOpList():
   """
   return [v for v in globals().values()
           if (isinstance(v, type) and issubclass(v, OpCode) and
-              hasattr(v, "OP_ID"))]
+              hasattr(v, "OP_ID") and v is not OpCode)]
 
 
 OP_MAPPING = dict((v.OP_ID, v) for v in _GetOpList())