Check if remote files exist before uploading
[snf-image-creator] / image_creator / kamaki_wrapper.py
index f5a40c7..a722c7b 100644 (file)
@@ -46,19 +46,22 @@ class Kamaki(object):
 
     @staticmethod
     def get_token():
+        """Get the saved token"""
         config = Config()
         return config.get('global', 'token')
 
     @staticmethod
     def save_token(token):
+        """Save this token to the configuration file"""
         config = Config()
         config.set('global', 'token', token)
         config.write()
 
     @staticmethod
     def get_account(token):
+        """Return the account corresponding to this token"""
         config = Config()
-        astakos = AstakosClient(config.get('astakos', 'url'), token)
+        astakos = AstakosClient(config.get('user', 'url'), token)
         try:
             account = astakos.info()
         except ClientError as e:
@@ -69,12 +72,13 @@ class Kamaki(object):
         return account
 
     def __init__(self, account, output):
+        """Create a Kamaki instance"""
         self.account = account
         self.out = output
 
         config = Config()
 
-        pithos_url = config.get('store', 'url')
+        pithos_url = config.get('file', 'url')
         self.pithos_client = PithosClient(
             pithos_url, self.account['auth_token'], self.account['uuid'],
             self.CONTAINER)
@@ -113,4 +117,16 @@ class Kamaki(object):
         params = {'is_public': is_public, 'disk_format': 'diskdump'}
         self.image_client.register(name, location, params, str_metadata)
 
+    def object_exists(self, location):
+        """Check if an object exists in pythos"""
+
+        try:
+            self.pithos_client.get_object_info(location)
+        except ClientError as e:
+            if e.status == 404:  # Object not found error
+                return False
+            else:
+                raise
+        return True
+
 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :