|
| 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) |
0 commit comments