Add userdata support.

Based on Jesse first shot at it. Make it accept file like object as well
like we do for file injection on server creation.
This commit is contained in:
Chmouel Boudjnah 2011-09-01 16:37:30 -05:00
parent be53d30569
commit 186b4689b5
3 changed files with 29 additions and 5 deletions
novaclient/v1_1
tests/v1_1

@ -15,14 +15,15 @@
# License for the specific language governing permissions and limitations
# under the License.
import base64
from novaclient import base
from novaclient import exceptions
class BootingManagerWithFind(base.ManagerWithFind):
"""Like a `ManagerWithFind`, but has the ability to boot servers."""
def _boot(self, resource_url, response_key, name, image, flavor,
meta=None, files=None, zone_blob=None,
meta=None, files=None, zone_blob=None, userdata=None,
reservation_id=None, return_raw=False, min_count=None,
max_count=None, security_groups=None):
"""
@ -52,6 +53,10 @@ class BootingManagerWithFind(base.ManagerWithFind):
"imageRef": base.getid(image),
"flavorRef": base.getid(flavor),
}}
if userdata:
if hasattr(userdata, 'read'):
userdata = userdata.read()
body["server"]["user_data"] = base64.b64encode(userdata)
if meta:
body["server"]["metadata"] = meta
if reservation_id:

@ -326,7 +326,7 @@ class ServerManager(local_base.BootingManagerWithFind):
def create(self, name, image, flavor, meta=None, files=None,
zone_blob=None, reservation_id=None, min_count=None,
max_count=None, security_groups=None):
max_count=None, security_groups=None, userdata=None):
"""
Create (boot) a new server.
@ -344,6 +344,9 @@ class ServerManager(local_base.BootingManagerWithFind):
:param zone_blob: a single (encrypted) string which is used internally
by Nova for routing between Zones. Users cannot populate
this field.
:param userdata: user data to pass to be exposed by the metadata
server this can be a file type object as well or a
string.
:param reservation_id: a UUID for the set of servers being requested.
"""
if not min_count:
@ -353,7 +356,7 @@ class ServerManager(local_base.BootingManagerWithFind):
if min_count > max_count:
min_count = max_count
return self._boot("/servers", "server", name, image, flavor,
meta=meta, files=files,
meta=meta, files=files, userdata=userdata,
zone_blob=zone_blob, reservation_id=reservation_id,
min_count=min_count, max_count=max_count,
security_groups=security_groups)

@ -33,14 +33,30 @@ class ServersTest(utils.TestCase):
image=1,
flavor=1,
meta={'foo': 'bar'},
userdata="hello moto",
files={
'/etc/passwd': 'some data', # a file
'/tmp/foo.txt': StringIO.StringIO('data') # a stream
'/tmp/foo.txt': StringIO.StringIO('data'), # a stream
}
)
cs.assert_called('POST', '/servers')
self.assertTrue(isinstance(s, servers.Server))
def test_create_server_userdata_file_object(self):
s = cs.servers.create(
name="My server",
image=1,
flavor=1,
meta={'foo': 'bar'},
userdata=StringIO.StringIO('hello moto'),
files={
'/etc/passwd': 'some data', # a file
'/tmp/foo.txt': StringIO.StringIO('data'), # a stream
},
)
cs.assert_called('POST', '/servers')
self.assertTrue(isinstance(s, servers.Server))
def test_update_server(self):
s = cs.servers.get(1234)