From 41241e7b8b7b5869044172f76c2ed490cc525570 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Toma=C5=BE=20Muraus?= <kami@k5-storitve.net>
Date: Sat, 25 Dec 2010 04:01:58 +0100
Subject: [PATCH 2/2] Destroy created drive and throw an exception if the node creation fails because of insufficient funds.

---
 libcloud/drivers/cloudsigma.py |   21 +++++++++++++++++----
 1 files changed, 17 insertions(+), 4 deletions(-)

diff --git a/libcloud/drivers/cloudsigma.py b/libcloud/drivers/cloudsigma.py
index 999a38e..161e8e6 100644
--- a/libcloud/drivers/cloudsigma.py
+++ b/libcloud/drivers/cloudsigma.py
@@ -142,6 +142,10 @@ class CloudSigmaException(Exception):
     def __repr__(self):
         return "<CloudSigmaException '%s'>" % (self.args[0])
 
+class CloudSigmaInsufficientFundsException(Exception):
+    def __repr__(self):
+        return "<CloudSigmaInsufficientFundsException '%s'>" % (self.args[0])
+
 class CloudSigmaResponse(Response):
     def success(self):
         if self.status == 401:
@@ -335,10 +339,14 @@ class CloudSigmaBaseNodeDriver(NodeDriver):
         response = self.connection.request(action = '/servers/create', data = dict2str(node_data),
                                            method = 'POST').object
 
-        if isinstance(response, list):
-            nodes = [self._to_node(node) for node in response]
-        else:
-            nodes = [self._to_node(response)]
+        if not isinstance(response, list):
+            response = [ response ]
+
+        node = self._to_node(response[0])
+        if node is None:
+            # Insufficient funds, destroy created drive
+            self.ex_drive_destroy(drive_uuid)
+            raise CloudSigmaInsufficientFundsException('Insufficient funds, node creation failed')
 
         # Start the node after it has been created
         node = nodes[0]
@@ -484,6 +492,11 @@ class CloudSigmaBaseNodeDriver(NodeDriver):
             except KeyError:
                 state = NodeState.UNKNOWN
 
+            if not data.has_key('server'):
+              # Response does not contain server UUID if the server creation
+              # failed because of insufficient funds.
+              return None
+
             public_ip = []
             if data.has_key('nic:0:dhcp'):
                 if isinstance(data['nic:0:dhcp'], list):
-- 
1.7.3.4

