Skip to content

Commit e4801db

Browse files
authored
Merge pull request #149 from andrewsomething/storage
Add support for Block Storage volumes.
2 parents d97e50d + ab74335 commit e4801db

10 files changed

Lines changed: 488 additions & 1 deletion

File tree

digitalocean/Manager.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from .Action import Action
1616
from .Account import Account
1717
from .FloatingIP import FloatingIP
18-
18+
from .Volume import Volume
1919

2020
class Manager(BaseAPI):
2121
def __init__(self, *args, **kwargs):
@@ -268,5 +268,23 @@ def get_floating_ip(self, ip):
268268
"""
269269
return FloatingIP.get_object(api_token=self.token, ip=ip)
270270

271+
def get_all_volumes(self):
272+
"""
273+
This function returns a list of Volume objects.
274+
"""
275+
data = self.get_data("volumes")
276+
volumes = list()
277+
for jsoned in data['volumes']:
278+
volume = Volume(**jsoned)
279+
volume.token = self.token
280+
volumes.append(volume)
281+
return volumes
282+
283+
def get_volume(self, volume_id):
284+
"""
285+
Returns a Volume object by its ID.
286+
"""
287+
return Volume.get_object(api_token=self.token, volume_id=volume_id)
288+
271289
def __str__(self):
272290
return "%s" % (self.token)

digitalocean/Volume.py

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
# -*- coding: utf-8 -*-
2+
from .baseapi import BaseAPI, POST, DELETE
3+
4+
5+
class Volume(BaseAPI):
6+
def __init__(self, *args, **kwargs):
7+
self.id = None
8+
self.name = None
9+
self.droplet_ids = []
10+
self.region = None
11+
self.description = None
12+
self.size_gigabytes = None
13+
self.created_at = None
14+
15+
super(Volume, self).__init__(*args, **kwargs)
16+
17+
@classmethod
18+
def get_object(cls, api_token, volume_id):
19+
"""
20+
Class method that will return an Volume object by ID.
21+
"""
22+
volume = cls(token=api_token, id=volume_id)
23+
volume.load()
24+
return volume
25+
26+
def load(self):
27+
data = self.get_data("volumes/%s" % self.id)
28+
volume_dict = data['volume']
29+
30+
# Setting the attribute values
31+
for attr in volume_dict.keys():
32+
setattr(self, attr, volume_dict[attr])
33+
34+
return self
35+
36+
def create(self, *args, **kwargs):
37+
"""
38+
Creates a Block Storage volume
39+
40+
Note: Every argument and parameter given to this method will be
41+
assigned to the object.
42+
43+
Args:
44+
name: string - a name for the volume
45+
region: string - slug identifier for the region
46+
size_gigabytes: int - size of the Block Storage volume in GiB
47+
48+
Optional Args:
49+
description: string - text field to describe a volume
50+
"""
51+
data = self.get_data('volumes/',
52+
type=POST,
53+
params={'name': self.name,
54+
'region': self.region,
55+
'size_gigabytes': self.size_gigabytes,
56+
'description': self.description})
57+
58+
if data:
59+
self.id = data['volume']['id']
60+
self.created_at = data['volume']['created_at']
61+
62+
return self
63+
64+
def destroy(self):
65+
"""
66+
Destroy a volume
67+
"""
68+
return self.get_data("volumes/%s/" % self.id, type=DELETE)
69+
70+
def attach(self, droplet_id, region):
71+
"""
72+
Attach a Volume to a Droplet.
73+
74+
Args:
75+
droplet_id: int - droplet id
76+
region: string - slug identifier for the region
77+
"""
78+
return self.get_data(
79+
"volumes/%s/actions/" % self.id,
80+
type=POST,
81+
params={"type": "attach",
82+
"droplet_id": droplet_id,
83+
"region": region}
84+
)
85+
86+
def detach(self, droplet_id, region):
87+
"""
88+
Detach a Volume to a Droplet.
89+
90+
Args:
91+
droplet_id: int - droplet id
92+
region: string - slug identifier for the region
93+
"""
94+
return self.get_data(
95+
"volumes/%s/actions/" % self.id,
96+
type=POST,
97+
params={"type": "detach",
98+
"droplet_id": droplet_id,
99+
"region": region}
100+
)
101+
102+
def resize(self, size_gigabytes, region):
103+
"""
104+
Detach a Volume to a Droplet.
105+
106+
Args:
107+
size_gigabytes: int - size of the Block Storage volume in GiB
108+
region: string - slug identifier for the region
109+
"""
110+
return self.get_data(
111+
"volumes/%s/actions/" % self.id,
112+
type=POST,
113+
params={"type": "resize",
114+
"size_gigabytes": size_gigabytes,
115+
"region": region}
116+
)
117+
118+
def __str__(self):
119+
return "%s %s %s" % (self.id, self.name, self.size_gigabytes)
120+
121+
def __repr__(self):
122+
return "< %s %s %s >" % (self.id, self.name, self.size_gigabytes)

digitalocean/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@
1919
from .SSHKey import SSHKey
2020
from .Kernel import Kernel
2121
from .FloatingIP import FloatingIP
22+
from .Volume import Volume
2223
from .baseapi import Error, TokenError, DataReadError
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
{
2+
"volumes": [
3+
{
4+
"id": "506f78a4-e098-11e5-ad9f-000f53306ae1",
5+
"region": {
6+
"name": "New York 1",
7+
"slug": "nyc1",
8+
"sizes": [
9+
"512mb",
10+
"1gb",
11+
"2gb",
12+
"4gb",
13+
"8gb",
14+
"16gb",
15+
"32gb",
16+
"48gb",
17+
"64gb"
18+
],
19+
"features": [
20+
"private_networking",
21+
"backups",
22+
"ipv6",
23+
"metadata"
24+
],
25+
"available": true
26+
},
27+
"droplet_ids": [
28+
29+
],
30+
"name": "example",
31+
"description": "Block store for examples",
32+
"size_gigabytes": 100,
33+
"created_at": "2016-03-02T17:00:49Z"
34+
},
35+
{
36+
"id": "2d2967ff-491d-11e6-860c-000f53315870",
37+
"region": {
38+
"name": "New York 1",
39+
"slug": "nyc1",
40+
"sizes": [
41+
"512mb",
42+
"1gb",
43+
"2gb",
44+
"4gb",
45+
"8gb",
46+
"16gb",
47+
"32gb",
48+
"48gb",
49+
"64gb"
50+
],
51+
"features": [
52+
"private_networking",
53+
"backups",
54+
"ipv6",
55+
"metadata"
56+
],
57+
"available": true
58+
},
59+
"droplet_ids": [
60+
19486237
61+
],
62+
"name": "another-example",
63+
"description": "A bigger example volume",
64+
"size_gigabytes": 500,
65+
"created_at": "2016-03-05T17:00:49Z"
66+
}
67+
],
68+
"links": {
69+
},
70+
"meta": {
71+
"total": 2
72+
}
73+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"action": {
3+
"id": 72531856,
4+
"status": "completed",
5+
"type": "attach_volume",
6+
"started_at": "2015-11-12T17:51:03Z",
7+
"completed_at": "2015-11-12T17:51:14Z",
8+
"resource_id": null,
9+
"resource_type": "volume",
10+
"region": {
11+
"name": "New York 1",
12+
"slug": "nyc1",
13+
"sizes": [
14+
"1gb",
15+
"2gb",
16+
"4gb",
17+
"8gb",
18+
"32gb",
19+
"64gb",
20+
"512mb",
21+
"48gb",
22+
"16gb"
23+
],
24+
"features": [
25+
"private_networking",
26+
"backups",
27+
"ipv6",
28+
"metadata"
29+
],
30+
"available": true
31+
},
32+
"region_slug": "nyc1"
33+
}
34+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"action": {
3+
"id": 68212773,
4+
"status": "in-progress",
5+
"type": "detach_volume",
6+
"started_at": "2015-10-15T17:46:15Z",
7+
"completed_at": null,
8+
"resource_id": null,
9+
"resource_type": "backend",
10+
"region": {
11+
"name": "New York 1",
12+
"slug": "nyc1",
13+
"sizes": [
14+
"512mb",
15+
"1gb",
16+
"2gb",
17+
"4gb",
18+
"8gb",
19+
"16gb",
20+
"32gb",
21+
"48gb",
22+
"64gb"
23+
],
24+
"features": [
25+
"private_networking",
26+
"backups",
27+
"ipv6",
28+
"metadata"
29+
],
30+
"available": true
31+
},
32+
"region_slug": "nyc1"
33+
}
34+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"action": {
3+
"id": 72531856,
4+
"status": "in-progress",
5+
"type": "resize_volume",
6+
"started_at": "2015-11-12T17:51:03Z",
7+
"completed_at": "2015-11-12T17:51:14Z",
8+
"resource_id": null,
9+
"resource_type": "volume",
10+
"region": {
11+
"name": "New York 1",
12+
"slug": "nyc1",
13+
"sizes": [
14+
"1gb",
15+
"2gb",
16+
"4gb",
17+
"8gb",
18+
"32gb",
19+
"64gb",
20+
"512mb",
21+
"48gb",
22+
"16gb"
23+
],
24+
"features": [
25+
"private_networking",
26+
"backups",
27+
"ipv6",
28+
"metadata"
29+
],
30+
"available": true
31+
},
32+
"region_slug": "nyc1"
33+
}
34+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"volume": {
3+
"id": "506f78a4-e098-11e5-ad9f-000f53306ae1",
4+
"region": {
5+
"name": "New York 1",
6+
"slug": "nyc1",
7+
"sizes": [
8+
"512mb",
9+
"1gb",
10+
"2gb",
11+
"4gb",
12+
"8gb",
13+
"16gb",
14+
"32gb",
15+
"48gb",
16+
"64gb"
17+
],
18+
"features": [
19+
"private_networking",
20+
"backups",
21+
"ipv6",
22+
"metadata"
23+
],
24+
"available": true
25+
},
26+
"droplet_ids": [
27+
28+
],
29+
"name": "example",
30+
"description": "Block store for examples",
31+
"size_gigabytes": 100,
32+
"created_at": "2016-03-02T17:00:49Z"
33+
}
34+
}

0 commit comments

Comments
 (0)