From a89151f98b278fa6bfaa3d2e9dd27a49d9d79d5a Mon Sep 17 00:00:00 2001 From: Markos Gogoulos Date: Tue, 17 Feb 2015 17:10:51 +0200 Subject: [PATCH] fixes create node for Host Virtual plus small enhancements return an empty list on list_nodes in case no servers are found, instead of an Exception pass args explicitly in create_node rather than using kwargs fix URL for build server, new url is /cloud/server/build/stub_node_id rather than /cloud/server/build that does not work currently adds a complete example in create_node for node creation with ssh key deployed --- libcloud/compute/drivers/hostvirtual.py | 41 +++++++++++++++++------ libcloud/test/compute/test_hostvirtual.py | 2 +- 2 files changed, 32 insertions(+), 11 deletions(-) diff --git a/libcloud/compute/drivers/hostvirtual.py b/libcloud/compute/drivers/hostvirtual.py index e9fa6c80a5..508e8c7d14 100644 --- a/libcloud/compute/drivers/hostvirtual.py +++ b/libcloud/compute/drivers/hostvirtual.py @@ -79,6 +79,8 @@ def _to_node(self, data): extra['size'] = data['plan_id'] if 'os_id' in data: extra['image'] = data['os_id'] + if 'fqdn' in data: + extra['fqdn'] = data['fqdn'] if 'location_id' in data: extra['location'] = data['location_id'] if 'ip' in data: @@ -133,7 +135,11 @@ def list_images(self): return images def list_nodes(self): - result = self.connection.request(API_ROOT + '/cloud/servers/').object + try: + result = self.connection.request( + API_ROOT + '/cloud/servers/').object + except HostVirtualException: + return [] nodes = [] for value in result: node = self._to_node(value) @@ -161,11 +167,26 @@ def _wait_for_node(self, node_id, timeout=30, interval=5.0): raise HostVirtualException(412, 'Timedout on getting node details') - def create_node(self, **kwargs): - dc = None + def create_node(self, name, image, size, **kwargs): + """Creates a node + + Example of node creation with ssh key deployed + + >>> from libcloud.compute.base import NodeAuthSSHKey + >>> key = open('/home/user/.ssh/id_rsa.pub').read() + >>> auth = NodeAuthSSHKey(pubkey=key) + >>> from libcloud.compute.providers import get_driver; + >>> driver = get_driver('hostvirtual') + >>> conn = driver('API_KEY') + >>> image = conn.list_images()[1] + >>> size = conn.list_sizes()[0] + >>> location = conn.list_locations()[1] + >>> name = 'markos-dev' + >>> node = conn.create_node(name, image, size, auth=auth, + location=location) + """ - size = kwargs['size'] - image = kwargs['image'] + dc = None auth = self._get_and_check_auth(kwargs.get('auth')) @@ -179,12 +200,13 @@ def create_node(self, **kwargs): result = self.connection.request(API_ROOT + '/cloud/buy/', data=json.dumps(params), method='POST').object + fqdn = kwargs.get('fqdn', name+'.test.com') # create a stub node stub_node = self._to_node({ 'mbpkgid': result['id'], 'status': 'PENDING', - 'fqdn': kwargs['name'], + 'fqdn': fqdn, 'plan_id': size.id, 'os_id': image.id, 'location_id': dc @@ -298,9 +320,8 @@ def ex_provision_node(self, **kwargs): image = node.extra['image'] params = { - 'mbpkgid': node.id, 'image': image, - 'fqdn': node.name, + 'fqdn': node.extra['fqdn'], 'location': node.extra['location'], } @@ -317,8 +338,8 @@ def ex_provision_node(self, **kwargs): if not ssh_key and not password: raise HostVirtualException(500, "Need SSH key or Root password") - - result = self.connection.request(API_ROOT + '/cloud/server/build', + result = self.connection.request(API_ROOT + '/cloud/server/build/%s' % + node.id, data=json.dumps(params), method='POST').object return bool(result) diff --git a/libcloud/test/compute/test_hostvirtual.py b/libcloud/test/compute/test_hostvirtual.py index 7098a637e5..62b8a0e9a0 100644 --- a/libcloud/test/compute/test_hostvirtual.py +++ b/libcloud/test/compute/test_hostvirtual.py @@ -174,7 +174,7 @@ def _cloud_buy(self, method, url, body, headers): body = self.fixtures.load('create_node.json') return (httplib.OK, body, {}, httplib.responses[httplib.OK]) - def _cloud_server_build(self, method, url, body, headers): + def _cloud_server_build_62291(self, method, url, body, headers): body = self.fixtures.load('create_node.json') return (httplib.OK, body, {}, httplib.responses[httplib.OK])