Skip to content

Instantly share code, notes, and snippets.

@jmatt
Created September 7, 2012 01:56
Show Gist options
  • Select an option

  • Save jmatt/3662528 to your computer and use it in GitHub Desktop.

Select an option

Save jmatt/3662528 to your computer and use it in GitHub Desktop.
OpenStack_1_1_NodeDriver extended to improve IP handling and add suspend and resume.
def _to_node(self, api_node):
"""
Extends OpenStack_1_1_NodeDriver._to_node adding support for public and private ips.
"""
def _set_ips(self):
"""
Set up ips in the api_node so _to_node may call it's super.
"""
try:
api_node['addresses']['public'] = []
api_node['addresses']['private'] = []
ips = None
all_ips = self.connection.request('/os-floating-ips').object
try:
ips = [f for f in all_ips['floating_ips'] if f['instance_id'] == api_node['id']]
except Exception as e:
logger.warn("%s %s %s" % (e, str(e), e.message))
if ips:
ips = ips.pop()
api_node['addresses']['private'].append({'version': 4, 'addr': ips['fixed_ip']})
api_node['addresses']['public'].append({'version': 4, 'addr': ips['ip']})
else:
guess_private_ip = api_node['addresses']['private_0'][0]['addr']
api_node['addresses']['private'].append({'version': 4, 'addr': guess_private_ip})
except Exception as e:
logger.warn("%s %s %s" % (e, str(e), e.message))
_set_ips()
node = super(OpenStack_Esh_NodeDriver,self)._to_node(api_node)
node.extra.update({'addresses': api_node['addresses'],
'keypair': api_node['key_name']})
# Removed these for now - I'm not sure if libcloud expects the
# collection even for private_ip and public_ip... or if it
# only uses private_ips and public_ips
# node.private_ip = node.private_ips[0]
# node.public_ip = node.public_ips[0]
return node
def suspend_node(self, node):
"""
Suspend a node.
"""
resp = self._node_action(node, 'suspend')
return resp.status == httplib.ACCEPTED
def resume_node(self, node):
"""
Resume a node.
"""
resp = self._node_action(node, 'resume')
return resp.status == httplib.ACCEPTED
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment