Added checking to RPC modify_host() to fail if locking/unlocking has
been requested on an already locked/unlocked host. Updated frontend 
doctests.

Visibility: Since this RPC seems to be used only by the CLI (the web 
interface uses modify_hosts() RPC) the change will be visible to the CLI 
when you try to lock/unlock hosts that are already locked/unlocked.

Signed-off-by: Mihai Rusu <dizzy@google.com>


git-svn-id: http://test.kernel.org/svn/autotest/trunk@3705 592f7852-d20e-0410-864c-8624ca9c26a4
diff --git a/frontend/afe/rpc_utils.py b/frontend/afe/rpc_utils.py
index e87553a..601e432 100644
--- a/frontend/afe/rpc_utils.py
+++ b/frontend/afe/rpc_utils.py
@@ -334,6 +334,25 @@
                 'status': 'Host status can not be modified by the frontend.'})
 
 
+def check_modify_host_locking(host, update_data):
+    """
+    Checks when locking/unlocking has been requested if the host is already
+    locked/unlocked.
+
+    @param host: models.Host object to be modified
+    @param update_data: A dictionary with the changes to make to the host.
+    """
+    locked = update_data.get('locked', None)
+    if locked is not None:
+        if locked and host.locked:
+            raise model_logic.ValidationError({
+                    'locked': 'Host already locked by %s on %s.' %
+                    (host.locked_by, host.lock_time)})
+        if not locked and not host.locked:
+            raise model_logic.ValidationError({
+                    'locked': 'Host already unlocked.'})
+
+
 def get_motd():
     dirname = os.path.dirname(__file__)
     filename = os.path.join(dirname, "..", "..", "motd.txt")