Created
April 16, 2015 12:00
-
-
Save NoKriK/f972b4d51db924011ccf to your computer and use it in GitHub Desktop.
Scaleway Libcloud driver
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
try: | |
import simplejson as json | |
except: | |
import json | |
from libcloud.utils.py3 import httplib | |
from libcloud.common.base import ConnectionUserAndKey, JsonResponse | |
from libcloud.compute.types import Provider, NodeState, InvalidCredsError | |
from libcloud.compute.base import NodeDriver | |
from libcloud.compute.base import Node | |
from libcloud.compute.base import StorageVolume, VolumeSnapshot, NodeImage | |
class OnlineLabsResponse(JsonResponse): | |
valid_response_codes = [httplib.OK, httplib.ACCEPTED, httplib.CREATED, | |
httplib.NO_CONTENT] | |
def parse_error(self): | |
print self.status | |
if self.status == httplib.UNAUTHORIZED: | |
data = self.parse_body() | |
raise InvalidCredsError(data['code'] + ': ' + data['message']) | |
return self.body | |
def success(self): | |
return self.status in self.valid_response_codes | |
class OnlineLabsConnection(ConnectionUserAndKey): | |
host = 'api.cloud.online.net' | |
responseCls = OnlineLabsResponse | |
allow_insecure = False | |
def add_default_headers(self, headers): | |
headers['Accept'] = 'application/json' | |
headers['Content-Type'] = 'application/json; charset=UTF-8' | |
headers['X-Auth-Token'] = self.key | |
return headers | |
class OnlineLabsNodeDriver(NodeDriver): | |
""" | |
OnlineLabs node driver node driver. | |
""" | |
connectionCls = OnlineLabsConnection | |
type = Provider.ONLINELABS | |
name = "Online Labs" | |
website = 'http://labs.online.net' | |
NODE_STATE_MAP = {'running': NodeState.PENDING, | |
'stopped': NodeState.STOPPED, | |
'starting': NodeState.RUNNING, | |
'stopping': NodeState.UNKNOWN | |
} | |
def list_nodes(self): | |
result = self.connection.request('/servers').object['servers'] | |
return list(map(self._to_node, result)) | |
def list_volumes(self): | |
result = self.connection.request('/volumes').object['volumes'] | |
return list(map(self._to_volume, result)) | |
def list_snapshots(self): | |
result = self.connection.request('/snapshots').object['snapshots'] | |
return list(map(self._to_snapshot, result)) | |
def list_images(self): | |
result = self.connection.request('/images').object['images'] | |
return list(map(self._to_image, result)) | |
def reboot_node(self, node): | |
data = json.dumps({'action': 'reboot'}) | |
result = self.connection.request('/servers/%s/action' % (node.id), | |
data=data, method='POST') | |
return result.status == httplib.ACCEPTED | |
def destroy_node(self, node): | |
result = self.connection.request('/servers/%s' % (node.id), | |
method='DELETE') | |
return result.status == httplib.NO_CONTENT | |
def ex_stop_node(self, node): | |
data = json.dumps({'action': 'poweroff'}) | |
result = self.connection.request('/servers/%s/action' % (node.id), | |
data=data, method='POST') | |
return result.status == httplib.ACCEPTED | |
def ex_start_node(self, node): | |
data = json.dumps({'action': 'poweron'}) | |
result = self.connection.request('/servers/%s/action' % (node.id), | |
data=data, method='POST') | |
return result.status == httplib.ACCEPTED | |
def ex_get_node(self, node): | |
result = self.connection.request('/servers/%s' % (node.id)) | |
return self._to_node(result.object) | |
def create_node(self, **kwargs): | |
data = json.dumps({'name': kwargs['name'], | |
'image': kwargs['image'].id, | |
'organization': self.key, | |
'tags': [], | |
'volumes': {} | |
}) | |
result = self.connection.request('/servers', | |
data=data, | |
method='POST').object | |
return self._to_node(result=result['server']) | |
def _to_volume(self, result): | |
server = {} | |
if 'server' in result and result['server'] is not None: | |
server = [att for att in result['server'] if att] | |
volume = StorageVolume( | |
id=result['id'], | |
name=result['name'], | |
size=result['size'], | |
driver=self, | |
extra={ | |
'server': server, | |
'creation_date': result['creation_date'], | |
'modification_date': result['modification_date'], | |
'volume_type': result['volume_type'], | |
'export_uri': result['export_uri'] | |
} | |
) | |
return volume | |
def _to_node(self, result): | |
private_ip = [] | |
public_ips = [] | |
extra_keys = ['creation_date', 'hostname', 'tags', 'size_id'] | |
if 'state' in result: | |
state = self.NODE_STATE_MAP.get(result['state'], NodeState.UNKNOWN) | |
else: | |
state = NodeState.UNKNOWN | |
if 'public_ip' in result and result['public_ip'] is not None: | |
public_ips = result['public_ip']['address'] | |
if 'private_ip' in result and result['private_ip'] is not None: | |
private_ip = result['private_ip'] | |
extra = {} | |
for key in extra_keys: | |
if key in result: | |
extra[key] = result[key] | |
node = Node(id=result['id'], name=result['name'], state=state, | |
public_ips=public_ips, private_ips=private_ip, extra=extra, | |
driver=self) | |
return node | |
def _to_snapshot(self, result): | |
base_volume = {} | |
if 'base_volume' in result and result['base_volume'] is not None: | |
base_volume = [att for att in result['base_volume'] if att] | |
extra = {'base_volume': base_volume, | |
'name': result['name'], | |
'created_date': result['creation_date'], | |
'modification_date': result['modification_date'], | |
'volume_type': result['volume_type'], | |
'state': result['state'] | |
} | |
snapshot = VolumeSnapshot(id=result['id'], driver=self, | |
size=result['size'], extra=extra) | |
return snapshot | |
def _to_image(self, result): | |
extra = {'arch': result['arch'], 'public': result['public'], | |
'creation_date': result['creation_date'], | |
'default_bootscript': result['default_bootscript']} | |
return NodeImage(id=result['id'], name=result['name'], | |
extra=extra, driver=self) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment