Rename OpAddNode and LUAddNode
[ganeti-local] / lib / opcodes.py
index 4211c71..e7b05f1 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,12 +391,12 @@ class OpDestroyCluster(OpCode):
   OP_ID = "OP_CLUSTER_DESTROY"
 
 
-class OpQueryClusterInfo(OpCode):
+class OpClusterQuery(OpCode):
   """Query cluster information."""
   OP_ID = "OP_CLUSTER_QUERY"
 
 
-class OpVerifyCluster(OpCode):
+class OpClusterVerify(OpCode):
   """Verify the cluster state.
 
   @type skip_checks: C{list}
@@ -374,7 +416,7 @@ class OpVerifyCluster(OpCode):
     ]
 
 
-class OpVerifyDisks(OpCode):
+class OpClusterVerifyDisks(OpCode):
   """Verify the cluster disks.
 
   Parameters: none
@@ -398,7 +440,7 @@ class OpVerifyDisks(OpCode):
   OP_ID = "OP_CLUSTER_VERIFY_DISKS"
 
 
-class OpRepairDiskSizes(OpCode):
+class OpClusterRepairDiskSizes(OpCode):
   """Verify the disk sizes of the instances and fixes configuration
   mimatches.
 
@@ -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 = [
@@ -428,7 +470,7 @@ class OpQueryConfigValues(OpCode):
     ]
 
 
-class OpRenameCluster(OpCode):
+class OpClusterRename(OpCode):
   """Rename the cluster.
 
   @type name: C{str}
@@ -444,7 +486,7 @@ class OpRenameCluster(OpCode):
     ]
 
 
-class OpSetClusterParams(OpCode):
+class OpClusterSetParams(OpCode):
   """Change the parameters of the cluster.
 
   @type vg_name: C{str} or C{None}
@@ -489,7 +531,7 @@ class OpSetClusterParams(OpCode):
     ]
 
 
-class OpRedistributeConfig(OpCode):
+class OpClusterRedistConf(OpCode):
   """Force a full push of the cluster configuration.
 
   """
@@ -554,7 +596,7 @@ class OpRemoveNode(OpCode):
     ]
 
 
-class OpAddNode(OpCode):
+class OpNodeAdd(OpCode):
   """Add a node to the cluster.
 
   @type node_name: C{str}
@@ -702,7 +744,7 @@ class OpNodeEvacuationStrategy(OpCode):
 
 # instance opcodes
 
-class OpCreateInstance(OpCode):
+class OpInstanceCreate(OpCode):
   """Create an instance.
 
   @ivar instance_name: Instance name
@@ -749,7 +791,7 @@ class OpCreateInstance(OpCode):
     ]
 
 
-class OpReinstallInstance(OpCode):
+class OpInstanceReinstall(OpCode):
   """Reinstall an instance's OS."""
   OP_ID = "OP_INSTANCE_REINSTALL"
   OP_DSC_FIELD = "instance_name"
@@ -761,7 +803,7 @@ class OpReinstallInstance(OpCode):
     ]
 
 
-class OpRemoveInstance(OpCode):
+class OpInstanceRemove(OpCode):
   """Remove an instance."""
   OP_ID = "OP_INSTANCE_REMOVE"
   OP_DSC_FIELD = "instance_name"
@@ -772,7 +814,7 @@ class OpRemoveInstance(OpCode):
     ]
 
 
-class OpRenameInstance(OpCode):
+class OpInstanceRename(OpCode):
   """Rename an instance."""
   OP_ID = "OP_INSTANCE_RENAME"
   OP_PARAMS = [
@@ -783,7 +825,7 @@ class OpRenameInstance(OpCode):
     ]
 
 
-class OpStartupInstance(OpCode):
+class OpInstanceStartup(OpCode):
   """Startup an instance."""
   OP_ID = "OP_INSTANCE_STARTUP"
   OP_DSC_FIELD = "instance_name"
@@ -796,7 +838,7 @@ class OpStartupInstance(OpCode):
     ]
 
 
-class OpShutdownInstance(OpCode):
+class OpInstanceShutdown(OpCode):
   """Shutdown an instance."""
   OP_ID = "OP_INSTANCE_SHUTDOWN"
   OP_DSC_FIELD = "instance_name"
@@ -807,7 +849,7 @@ class OpShutdownInstance(OpCode):
     ]
 
 
-class OpRebootInstance(OpCode):
+class OpInstanceReboot(OpCode):
   """Reboot an instance."""
   OP_ID = "OP_INSTANCE_REBOOT"
   OP_DSC_FIELD = "instance_name"
@@ -819,7 +861,7 @@ class OpRebootInstance(OpCode):
     ]
 
 
-class OpReplaceDisks(OpCode):
+class OpInstanceReplaceDisks(OpCode):
   """Replace the disks of an instance."""
   OP_ID = "OP_INSTANCE_REPLACE_DISKS"
   OP_DSC_FIELD = "instance_name"
@@ -833,7 +875,7 @@ class OpReplaceDisks(OpCode):
     ]
 
 
-class OpFailoverInstance(OpCode):
+class OpInstanceFailover(OpCode):
   """Failover an instance."""
   OP_ID = "OP_INSTANCE_FAILOVER"
   OP_DSC_FIELD = "instance_name"
@@ -844,7 +886,7 @@ class OpFailoverInstance(OpCode):
     ]
 
 
-class OpMigrateInstance(OpCode):
+class OpInstanceMigrate(OpCode):
   """Migrate an instance.
 
   This migrates (without shutting down an instance) to its secondary
@@ -864,7 +906,7 @@ class OpMigrateInstance(OpCode):
     ]
 
 
-class OpMoveInstance(OpCode):
+class OpInstanceMove(OpCode):
   """Move an instance.
 
   This move (with shutting down an instance and data copying) to an
@@ -883,7 +925,7 @@ class OpMoveInstance(OpCode):
     ]
 
 
-class OpConnectConsole(OpCode):
+class OpInstanceConsole(OpCode):
   """Connect to an instance's console."""
   OP_ID = "OP_INSTANCE_CONSOLE"
   OP_DSC_FIELD = "instance_name"
@@ -892,7 +934,7 @@ class OpConnectConsole(OpCode):
     ]
 
 
-class OpActivateInstanceDisks(OpCode):
+class OpInstanceActivateDisks(OpCode):
   """Activate an instance's disks."""
   OP_ID = "OP_INSTANCE_ACTIVATE_DISKS"
   OP_DSC_FIELD = "instance_name"
@@ -902,7 +944,7 @@ class OpActivateInstanceDisks(OpCode):
     ]
 
 
-class OpDeactivateInstanceDisks(OpCode):
+class OpInstanceDeactivateDisks(OpCode):
   """Deactivate an instance's disks."""
   OP_ID = "OP_INSTANCE_DEACTIVATE_DISKS"
   OP_DSC_FIELD = "instance_name"
@@ -911,7 +953,7 @@ class OpDeactivateInstanceDisks(OpCode):
     ]
 
 
-class OpRecreateInstanceDisks(OpCode):
+class OpInstanceRecreateDisks(OpCode):
   """Deactivate an instance's disks."""
   OP_ID = "OP_INSTANCE_RECREATE_DISKS"
   OP_DSC_FIELD = "instance_name"
@@ -921,7 +963,7 @@ class OpRecreateInstanceDisks(OpCode):
     ]
 
 
-class OpQueryInstances(OpCode):
+class OpInstanceQuery(OpCode):
   """Compute the list of instances."""
   OP_ID = "OP_INSTANCE_QUERY"
   OP_PARAMS = [
@@ -931,7 +973,7 @@ class OpQueryInstances(OpCode):
     ]
 
 
-class OpQueryInstanceData(OpCode):
+class OpInstanceQueryData(OpCode):
   """Compute the run-time status of instances."""
   OP_ID = "OP_INSTANCE_QUERY_DATA"
   OP_PARAMS = [
@@ -940,7 +982,7 @@ class OpQueryInstanceData(OpCode):
     ]
 
 
-class OpSetInstanceParams(OpCode):
+class OpInstanceSetParams(OpCode):
   """Change the parameters of an instance."""
   OP_ID = "OP_INSTANCE_SET_PARAMS"
   OP_DSC_FIELD = "instance_name"
@@ -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),
@@ -959,7 +1001,7 @@ class OpSetInstanceParams(OpCode):
     ]
 
 
-class OpGrowDisk(OpCode):
+class OpInstanceGrowDisk(OpCode):
   """Grow a disk of an instance."""
   OP_ID = "OP_INSTANCE_GROW_DISK"
   OP_DSC_FIELD = "instance_name"
@@ -973,7 +1015,7 @@ class OpGrowDisk(OpCode):
 
 # Node group opcodes
 
-class OpAddGroup(OpCode):
+class OpGroupAdd(OpCode):
   """Add a node group to the cluster."""
   OP_ID = "OP_GROUP_ADD"
   OP_DSC_FIELD = "group_name"
@@ -985,7 +1027,18 @@ class OpAddGroup(OpCode):
     ]
 
 
-class OpQueryGroups(OpCode):
+class OpGroupAssignNodes(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 OpGroupQuery(OpCode):
   """Compute the list of node groups."""
   OP_ID = "OP_GROUP_QUERY"
   OP_PARAMS = [
@@ -994,7 +1047,7 @@ class OpQueryGroups(OpCode):
     ]
 
 
-class OpSetGroupParams(OpCode):
+class OpGroupSetParams(OpCode):
   """Change the parameters of a node group."""
   OP_ID = "OP_GROUP_SET_PARAMS"
   OP_DSC_FIELD = "group_name"
@@ -1006,7 +1059,7 @@ class OpSetGroupParams(OpCode):
     ]
 
 
-class OpRemoveGroup(OpCode):
+class OpGroupRemove(OpCode):
   """Remove a node group from the cluster."""
   OP_ID = "OP_GROUP_REMOVE"
   OP_DSC_FIELD = "group_name"
@@ -1015,7 +1068,7 @@ class OpRemoveGroup(OpCode):
     ]
 
 
-class OpRenameGroup(OpCode):
+class OpGroupRename(OpCode):
   """Rename a node group in the cluster."""
   OP_ID = "OP_GROUP_RENAME"
   OP_DSC_FIELD = "old_name"
@@ -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())