Author: tomaz
Date: Wed Jun 15 18:47:31 2011
New Revision: 1136145

URL: http://svn.apache.org/viewvc?rev=1136145&view=rev
Log:
Allow users to pass in file like object to the ScriptDeployment constructor. Do 
the same with the SSHKeyDeployment constructur. Closes LIBCLOUD-90.

Modified:
    libcloud/trunk/libcloud/compute/deployment.py
    libcloud/trunk/test/compute/test_deployment.py

Modified: libcloud/trunk/libcloud/compute/deployment.py
URL: 
http://svn.apache.org/viewvc/libcloud/trunk/libcloud/compute/deployment.py?rev=1136145&r1=1136144&r2=1136145&view=diff
==============================================================================
--- libcloud/trunk/libcloud/compute/deployment.py (original)
+++ libcloud/trunk/libcloud/compute/deployment.py Wed Jun 15 18:47:31 2011
@@ -38,6 +38,17 @@ class Deployment(object):
         raise NotImplementedError, \
             'run not implemented for this deployment'
 
+    def _get_string_value(self, argument_name, argument_value):
+        if not isinstance(argument_value, basestring) and \
+           not hasattr(argument_value, 'read'):
+            raise TypeError('%s argument must be a string or a file-like '
+                            'object' % (argument_name))
+
+        if hasattr(argument_value, 'read'):
+            argument_value = argument_value.read()
+
+        return argument_value
+
 
 class SSHKeyDeployment(Deployment):
     """
@@ -49,7 +60,8 @@ class SSHKeyDeployment(Deployment):
         @type key: C{str}
         @keyword key: Contents of the public key write
         """
-        self.key = key
+        self.key = self._get_string_value(argument_name='key',
+                                          argument_value=key)
 
     def run(self, node, client):
         """
@@ -76,8 +88,8 @@ class ScriptDeployment(Deployment):
         @type delete: C{bool}
         @keyword delete: Whether to delete the script on completion.
         """
-        if not isinstance(script, basestring):
-            raise TypeError('script argument must be a string')
+        script = self._get_string_value(argument_name='script',
+                                        argument_value=script)
 
         self.script = script
         self.stdout = None

Modified: libcloud/trunk/test/compute/test_deployment.py
URL: 
http://svn.apache.org/viewvc/libcloud/trunk/test/compute/test_deployment.py?rev=1136145&r1=1136144&r2=1136145&view=diff
==============================================================================
--- libcloud/trunk/test/compute/test_deployment.py (original)
+++ libcloud/trunk/test/compute/test_deployment.py Wed Jun 15 18:47:31 2011
@@ -78,9 +78,21 @@ class DeploymentTests(unittest.TestCase)
         self.assertEqual(self.node, sd2.run(node=self.node,
                         client=MockClient(hostname='localhost')))
 
-    def test_script_deployment_argument_types(self):
+    def test_script_deployment_and_sshkey_deployment_argument_types(self):
+        class FileObject(object):
+            def __init__(self, name):
+                self.name = name
+
+            def read(self):
+                return 'bar'
+
         ScriptDeployment(script='foobar')
         ScriptDeployment(script=unicode('foobar'))
+        ScriptDeployment(script=FileObject('test'))
+
+        SSHKeyDeployment(key='foobar')
+        SSHKeyDeployment(key=unicode('foobar'))
+        SSHKeyDeployment(key=FileObject('test'))
 
         try:
             ScriptDeployment(script=[])
@@ -89,5 +101,13 @@ class DeploymentTests(unittest.TestCase)
         else:
             self.fail('TypeError was not thrown')
 
+        try:
+            SSHKeyDeployment(key={})
+        except TypeError:
+            pass
+        else:
+            self.fail('TypeError was not thrown')
+
+
 if __name__ == '__main__':
     sys.exit(unittest.main())


Reply via email to