-
Notifications
You must be signed in to change notification settings - Fork 300
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into create-multiple-droplets
- Loading branch information
Showing
17 changed files
with
556 additions
and
8 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
*.pyc | ||
*__pycache__ |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,3 +37,5 @@ nosetests.xml | |
MANIFEST | ||
|
||
.idea/* | ||
|
||
.cache |
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
# -*- coding: utf-8 -*- | ||
from .baseapi import BaseAPI, POST, DELETE | ||
|
||
|
||
class Volume(BaseAPI): | ||
def __init__(self, *args, **kwargs): | ||
self.id = None | ||
self.name = None | ||
self.droplet_ids = [] | ||
self.region = None | ||
self.description = None | ||
self.size_gigabytes = None | ||
self.created_at = None | ||
|
||
super(Volume, self).__init__(*args, **kwargs) | ||
|
||
@classmethod | ||
def get_object(cls, api_token, volume_id): | ||
""" | ||
Class method that will return an Volume object by ID. | ||
""" | ||
volume = cls(token=api_token, id=volume_id) | ||
volume.load() | ||
return volume | ||
|
||
def load(self): | ||
data = self.get_data("volumes/%s" % self.id) | ||
volume_dict = data['volume'] | ||
|
||
# Setting the attribute values | ||
for attr in volume_dict.keys(): | ||
setattr(self, attr, volume_dict[attr]) | ||
|
||
return self | ||
|
||
def create(self, *args, **kwargs): | ||
""" | ||
Creates a Block Storage volume | ||
Note: Every argument and parameter given to this method will be | ||
assigned to the object. | ||
Args: | ||
name: string - a name for the volume | ||
region: string - slug identifier for the region | ||
size_gigabytes: int - size of the Block Storage volume in GiB | ||
Optional Args: | ||
description: string - text field to describe a volume | ||
""" | ||
data = self.get_data('volumes/', | ||
type=POST, | ||
params={'name': self.name, | ||
'region': self.region, | ||
'size_gigabytes': self.size_gigabytes, | ||
'description': self.description}) | ||
|
||
if data: | ||
self.id = data['volume']['id'] | ||
self.created_at = data['volume']['created_at'] | ||
|
||
return self | ||
|
||
def destroy(self): | ||
""" | ||
Destroy a volume | ||
""" | ||
return self.get_data("volumes/%s/" % self.id, type=DELETE) | ||
|
||
def attach(self, droplet_id, region): | ||
""" | ||
Attach a Volume to a Droplet. | ||
Args: | ||
droplet_id: int - droplet id | ||
region: string - slug identifier for the region | ||
""" | ||
return self.get_data( | ||
"volumes/%s/actions/" % self.id, | ||
type=POST, | ||
params={"type": "attach", | ||
"droplet_id": droplet_id, | ||
"region": region} | ||
) | ||
|
||
def detach(self, droplet_id, region): | ||
""" | ||
Detach a Volume to a Droplet. | ||
Args: | ||
droplet_id: int - droplet id | ||
region: string - slug identifier for the region | ||
""" | ||
return self.get_data( | ||
"volumes/%s/actions/" % self.id, | ||
type=POST, | ||
params={"type": "detach", | ||
"droplet_id": droplet_id, | ||
"region": region} | ||
) | ||
|
||
def resize(self, size_gigabytes, region): | ||
""" | ||
Detach a Volume to a Droplet. | ||
Args: | ||
size_gigabytes: int - size of the Block Storage volume in GiB | ||
region: string - slug identifier for the region | ||
""" | ||
return self.get_data( | ||
"volumes/%s/actions/" % self.id, | ||
type=POST, | ||
params={"type": "resize", | ||
"size_gigabytes": size_gigabytes, | ||
"region": region} | ||
) | ||
|
||
def __str__(self): | ||
return "%s %s %s" % (self.id, self.name, self.size_gigabytes) | ||
|
||
def __repr__(self): | ||
return "< %s %s %s >" % (self.id, self.name, self.size_gigabytes) |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"ssh_key": { | ||
"id": 1234, | ||
"fingerprint": "ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff", | ||
"public_key": "AAAAkey", | ||
"name": "new_key" | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
{ | ||
"volumes": [ | ||
{ | ||
"id": "506f78a4-e098-11e5-ad9f-000f53306ae1", | ||
"region": { | ||
"name": "New York 1", | ||
"slug": "nyc1", | ||
"sizes": [ | ||
"512mb", | ||
"1gb", | ||
"2gb", | ||
"4gb", | ||
"8gb", | ||
"16gb", | ||
"32gb", | ||
"48gb", | ||
"64gb" | ||
], | ||
"features": [ | ||
"private_networking", | ||
"backups", | ||
"ipv6", | ||
"metadata" | ||
], | ||
"available": true | ||
}, | ||
"droplet_ids": [ | ||
|
||
], | ||
"name": "example", | ||
"description": "Block store for examples", | ||
"size_gigabytes": 100, | ||
"created_at": "2016-03-02T17:00:49Z" | ||
}, | ||
{ | ||
"id": "2d2967ff-491d-11e6-860c-000f53315870", | ||
"region": { | ||
"name": "New York 1", | ||
"slug": "nyc1", | ||
"sizes": [ | ||
"512mb", | ||
"1gb", | ||
"2gb", | ||
"4gb", | ||
"8gb", | ||
"16gb", | ||
"32gb", | ||
"48gb", | ||
"64gb" | ||
], | ||
"features": [ | ||
"private_networking", | ||
"backups", | ||
"ipv6", | ||
"metadata" | ||
], | ||
"available": true | ||
}, | ||
"droplet_ids": [ | ||
19486237 | ||
], | ||
"name": "another-example", | ||
"description": "A bigger example volume", | ||
"size_gigabytes": 500, | ||
"created_at": "2016-03-05T17:00:49Z" | ||
} | ||
], | ||
"links": { | ||
}, | ||
"meta": { | ||
"total": 2 | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
{ | ||
"action": { | ||
"id": 72531856, | ||
"status": "completed", | ||
"type": "attach_volume", | ||
"started_at": "2015-11-12T17:51:03Z", | ||
"completed_at": "2015-11-12T17:51:14Z", | ||
"resource_id": null, | ||
"resource_type": "volume", | ||
"region": { | ||
"name": "New York 1", | ||
"slug": "nyc1", | ||
"sizes": [ | ||
"1gb", | ||
"2gb", | ||
"4gb", | ||
"8gb", | ||
"32gb", | ||
"64gb", | ||
"512mb", | ||
"48gb", | ||
"16gb" | ||
], | ||
"features": [ | ||
"private_networking", | ||
"backups", | ||
"ipv6", | ||
"metadata" | ||
], | ||
"available": true | ||
}, | ||
"region_slug": "nyc1" | ||
} | ||
} |
Oops, something went wrong.