Skip to content

Commit 14a6084

Browse files
committed
merge @samdobson pull request and minor changes
2 parents 011a86c + ff81a88 commit 14a6084

2 files changed

Lines changed: 42 additions & 12 deletions

File tree

README.rst

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,21 @@ To install dop, simply: ::
2222

2323
Example
2424
-------
25-
It is pretty easy to use: ::
25+
It is pretty easy to use:
26+
27+
.. code-block:: python
2628
2729
from dop.client import Client
2830
2931
client = Client('client_id', 'api_key')
32+
33+
# Print regions.
3034
regions = client.regions()
3135
for region in regions:
32-
print region.to_json()
33-
36+
print(region.to_json())
37+
38+
# Create a 512Mb droplet (Debian 7.0 x32) in the Amsterdam region.
39+
client.create_droplet(name='test', size_id=66, image_id=303619, region_id=2)
3440
3541
3642
Contribute

dop/client.py

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818

1919
class Droplet(object):
2020
def __init__(self, id, name, size_id, image_id, region_id, event_id,
21-
backups_active, status, ip_address, created_at, private_ip_address):
21+
backups_active, status, ip_address, created_at,
22+
private_ip_address):
2223
self.id = id
2324
self.name = name
2425
self.size_id = size_id
@@ -53,6 +54,22 @@ def from_json(json):
5354
return droplet
5455

5556

57+
class Status(object):
58+
def __init__(self, action_status, percentage):
59+
self.action_status = action_status
60+
self.percentage = percentage
61+
62+
def to_json(self):
63+
return self.__dict__
64+
65+
@staticmethod
66+
def from_json(json):
67+
action_status = json.get('action_status', '')
68+
percentage = json.get('percentage', -1)
69+
status = Status(action_status, percentage)
70+
return status
71+
72+
5673
class Snapshot(object):
5774
def __init__(self, name):
5875
self.name = name
@@ -157,12 +174,12 @@ def regions(self):
157174
regions = [Region.from_json(r) for r in regions_json]
158175
return regions
159176

160-
def images(self, show_all=True):
177+
def images(self, my_images=False):
161178
params = {}
162-
if show_all:
163-
params['filter'] = 'global'
164-
else:
179+
if my_images:
165180
params['filter'] = 'my_images'
181+
else:
182+
params['filter'] = 'global'
166183

167184
json = self.request('/images', method='GET', params=params)
168185
images_json = json.get('images', [])
@@ -176,7 +193,7 @@ def sizes(self):
176193
sizes = [Size.from_json(s) for s in sizes_json]
177194
return sizes
178195

179-
def all_ssh_keys(self):
196+
def ssh_keys(self):
180197
params = {}
181198
json = self.request('/ssh_keys', method='GET', params=params)
182199
ssh_keys_json = json.get('ssh_keys', [])
@@ -279,15 +296,15 @@ def destroy_droplet(self, id, scrub_data=False):
279296
if scrub_data:
280297
params['scrub_data'] = True
281298

282-
json = self.request('/droplets/%s/destroy' % (id), method='GET',
299+
json = self.request('/droplets/%s/destroy' % id, method='GET',
283300
params=params)
284301
return json.get('event_id', None)
285302

286303
def show_image(self, image_id):
287304
params = {
288305
'image_id': image_id,
289306
}
290-
json = self.request('/images/%s' % (image_id), method='GET',
307+
json = self.request('/images/%s' % image_id, method='GET',
291308
params=params)
292309
image_json = json.get('image', None)
293310
image = Image.from_json(image_json)
@@ -299,7 +316,14 @@ def shutdown_droplet(self, id):
299316
params=params)
300317
return json.get('event_id', None)
301318

302-
# WEIRD BEHAVIOUR METHODS
319+
def get_status(self, event_id):
320+
params = {}
321+
json = self.request('/events/%s' % event_id, method='GET',
322+
params=params)
323+
status_json = json.get('event', None)
324+
status = Status.from_json(status_json)
325+
return status
326+
303327
def reset_root_password(self, id):
304328
params = {}
305329
json = self.request('/droplets/%s/reset_root_password' % id,

0 commit comments

Comments
 (0)