1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
# |
3
|
|
|
# Copyright (c) 2014-2015 Online SAS and Contributors. All Rights Reserved. |
4
|
|
|
# Edouard Bonlieu <[email protected]> |
5
|
|
|
# Julien Castets <[email protected]> |
6
|
|
|
# Manfred Touron <[email protected]> |
7
|
|
|
# Kevin Deldycke <[email protected]> |
8
|
|
|
# |
9
|
|
|
# Licensed under the BSD 2-Clause License (the "License"); you may not use this |
10
|
|
|
# file except in compliance with the License. You may obtain a copy of the |
11
|
|
|
# License at http://opensource.org/licenses/BSD-2-Clause |
12
|
|
|
|
13
|
|
|
import os |
14
|
|
|
|
15
|
|
|
from juju_scaleway.exceptions import ProviderAPIError |
16
|
|
|
|
17
|
|
|
import json |
18
|
|
|
import requests |
19
|
|
|
|
20
|
|
|
|
21
|
|
|
class Entity(object): |
22
|
|
|
|
23
|
|
|
@classmethod |
24
|
|
|
def from_dict(cls, data): |
25
|
|
|
i = cls() |
26
|
|
|
i.__dict__.update(data) |
27
|
|
|
return i |
28
|
|
|
|
29
|
|
|
|
30
|
|
|
class Server(Entity): |
31
|
|
|
""" |
32
|
|
|
Attributes: id, name, image, state, public_ip, creation_date |
33
|
|
|
""" |
34
|
|
|
|
35
|
|
|
|
36
|
|
|
class Image(Entity): |
37
|
|
|
""" |
38
|
|
|
Attributes:, id, name, arch, public |
39
|
|
|
""" |
40
|
|
|
|
41
|
|
|
|
42
|
|
|
class Client(object): |
43
|
|
|
|
44
|
|
|
def __init__(self, access_key, secret_key): |
45
|
|
|
self.access_key = access_key |
46
|
|
|
self.secret_key = secret_key |
47
|
|
|
self.api_url_base = 'https://api.scaleway.com' |
48
|
|
|
|
49
|
|
|
def get_images(self): |
50
|
|
|
data = self.request("/images") |
51
|
|
|
return [Image.from_dict(image) for image in data.get("images", [])] |
52
|
|
|
|
53
|
|
|
def get_url(self, target): |
54
|
|
|
return "%s%s" % (self.api_url_base, target) |
55
|
|
|
|
56
|
|
|
def get_servers(self): |
57
|
|
|
data = self.request("/servers") |
58
|
|
|
return [Server.from_dict(server) for server in data.get('servers', [])] |
59
|
|
|
|
60
|
|
|
def get_server(self, server_id): |
61
|
|
|
data = self.request("/servers/%s" % (server_id)) |
62
|
|
|
return Server.from_dict(data.get('server', {})) |
63
|
|
|
|
64
|
|
|
def create_server(self, name, image): |
65
|
|
|
params = dict( |
66
|
|
|
name=name, |
67
|
|
|
image=image, |
68
|
|
|
organization=self.access_key) |
69
|
|
|
|
70
|
|
|
data = self.request('/servers', method='POST', params=params) |
71
|
|
|
server = Server.from_dict(data.get('server', {})) |
72
|
|
|
# Execute poweron action |
73
|
|
|
self.request('/servers/%s/action' % (server.id), |
74
|
|
|
method='POST', params={'action': 'poweron'}) |
75
|
|
|
|
76
|
|
|
return server |
77
|
|
|
|
78
|
|
|
def destroy_server(self, server_id): |
79
|
|
|
data = self.request('/servers/%s/action' % (server_id), |
80
|
|
|
method='POST', params={'action': 'terminate'}) |
81
|
|
|
return data.get('task') |
82
|
|
|
|
83
|
|
|
def request(self, target, method='GET', params=None): |
84
|
|
|
params = params and dict(params) or {} |
85
|
|
|
|
86
|
|
|
headers = {'User-Agent': 'juju/client'} |
87
|
|
|
headers = {'X-Auth-Token': self.secret_key} |
88
|
|
|
url = self.get_url(target) |
89
|
|
|
|
90
|
|
|
if method == 'POST': |
91
|
|
|
headers['Content-Type'] = "application/json" |
92
|
|
|
response = requests.post( |
93
|
|
|
url, headers=headers, data=json.dumps(params) |
94
|
|
|
) |
95
|
|
|
else: |
96
|
|
|
response = requests.get(url, headers=headers, params=params) |
97
|
|
|
|
98
|
|
|
data = response.json() |
99
|
|
|
if not data: |
100
|
|
|
raise ProviderAPIError(response, 'No json result found') |
101
|
|
|
if response.status_code >= 400: |
102
|
|
|
raise ProviderAPIError(response, data['message']) |
103
|
|
|
|
104
|
|
|
return data |
105
|
|
|
|
106
|
|
|
@classmethod |
107
|
|
|
def connect(cls): |
108
|
|
|
access_key = os.environ.get('SCALEWAY_ACCESS_KEY') |
109
|
|
|
secret_key = os.environ.get('SCALEWAY_SECRET_KEY') |
110
|
|
|
if not access_key or not secret_key: |
111
|
|
|
raise KeyError("Missing api credentials") |
112
|
|
|
return cls(access_key, secret_key) |
113
|
|
|
|
114
|
|
|
|
115
|
|
|
def main(): |
116
|
|
|
import code |
117
|
|
|
client = Client.connect() |
118
|
|
|
code.interact(local={'client': client}) |
119
|
|
|
|
120
|
|
|
|
121
|
|
|
if __name__ == '__main__': |
122
|
|
|
main() |
123
|
|
|
|