|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +from .baseapi import BaseAPI, PATCH, POST, DELETE |
| 3 | + |
| 4 | + |
| 5 | +class VPC(BaseAPI): |
| 6 | + """ |
| 7 | + An object representing a DigitalOcean VPC. |
| 8 | +
|
| 9 | + Attributes accepted at creation time: |
| 10 | +
|
| 11 | + Args: |
| 12 | + name (str): A name for the VPC |
| 13 | + region (str): The slug for the region where the VPC will be created |
| 14 | + description(str): A free-form text field for describing the VPC |
| 15 | + ip_range (str): The requested range of IP addresses for the VPC in \ |
| 16 | + CIDR notation |
| 17 | +
|
| 18 | +
|
| 19 | + Attributes returned by API: |
| 20 | + * id (str): A unique identifier for the VPC |
| 21 | + * name (str): The name of the VPC |
| 22 | + * region (str): The slug for the region where the VPC is located |
| 23 | + * description(str): A free-form text field for describing the VPC |
| 24 | + * ip_range (str): The requested range of IP addresses for the VPC in \ |
| 25 | + CIDR notation |
| 26 | + * urn (str): The uniform resource name (URN) for the VPC |
| 27 | + * created_at (str): A string that represents when the VPC was created |
| 28 | + * default (bool): A boolen representing whether or not the VPC is the \ |
| 29 | + user's default VPC for the region |
| 30 | + """ |
| 31 | + def __init__(self, *args, **kwargs): |
| 32 | + self.id = "" |
| 33 | + self.name = None |
| 34 | + self.region = None |
| 35 | + self.description = None |
| 36 | + self.ip_range = None |
| 37 | + self.urn = None |
| 38 | + self.created_at = None |
| 39 | + self.default = False |
| 40 | + |
| 41 | + super(VPC, self).__init__(*args, **kwargs) |
| 42 | + |
| 43 | + @classmethod |
| 44 | + def get_object(cls, api_token, vpc_id): |
| 45 | + """ |
| 46 | + Class method that will return a VPC object by its ID. |
| 47 | + """ |
| 48 | + vpc = cls(token=api_token, id=vpc_id) |
| 49 | + vpc.load() |
| 50 | + return vpc |
| 51 | + |
| 52 | + def load(self): |
| 53 | + """ |
| 54 | + Load the VPC object from DigitalOcean. |
| 55 | +
|
| 56 | + Requires self.id to be set. |
| 57 | + """ |
| 58 | + data = self.get_data("vpcs/%s" % self.id) |
| 59 | + vpc = data["vpc"] |
| 60 | + |
| 61 | + for attr in vpc.keys(): |
| 62 | + setattr(self, attr, vpc[attr]) |
| 63 | + |
| 64 | + return self |
| 65 | + |
| 66 | + def create(self): |
| 67 | + """ |
| 68 | + Create the VPC |
| 69 | + """ |
| 70 | + params = { |
| 71 | + "name": self.name, |
| 72 | + "region": self.region, |
| 73 | + "description": self.description, |
| 74 | + "ip_range": self.ip_range |
| 75 | + } |
| 76 | + |
| 77 | + data = self.get_data("vpcs", type=POST, params=params) |
| 78 | + |
| 79 | + if data: |
| 80 | + self.id = data['vpc']['id'] |
| 81 | + self.name = data['vpc']['name'] |
| 82 | + self.region = data['vpc']['region'] |
| 83 | + self.description = data['vpc']['description'] |
| 84 | + self.ip_range = data['vpc']['ip_range'] |
| 85 | + self.urn = data['vpc']['urn'] |
| 86 | + self.created_at = data['vpc']['created_at'] |
| 87 | + self.default = data['vpc']['default'] |
| 88 | + |
| 89 | + return self |
| 90 | + |
| 91 | + def rename(self, new_name): |
| 92 | + """ |
| 93 | + Rename a VPC |
| 94 | +
|
| 95 | + Args: |
| 96 | + name (str): The new name for the VPC |
| 97 | + """ |
| 98 | + data = self.get_data("vpcs/%s" % self.id, |
| 99 | + type=PATCH, |
| 100 | + params={"name": new_name}) |
| 101 | + |
| 102 | + vpc = data["vpc"] |
| 103 | + |
| 104 | + for attr in vpc.keys(): |
| 105 | + setattr(self, attr, vpc[attr]) |
| 106 | + |
| 107 | + return self |
| 108 | + |
| 109 | + def rename(self, new_name): |
| 110 | + """ |
| 111 | + Rename a VPC |
| 112 | +
|
| 113 | + Args: |
| 114 | + name (str): The new name for the VPC |
| 115 | + """ |
| 116 | + data = self.get_data("vpcs/%s" % self.id, |
| 117 | + type=PATCH, |
| 118 | + params={"name": new_name}) |
| 119 | + |
| 120 | + vpc = data["vpc"] |
| 121 | + |
| 122 | + for attr in vpc.keys(): |
| 123 | + setattr(self, attr, vpc[attr]) |
| 124 | + |
| 125 | + return self |
| 126 | + |
| 127 | + def destroy(self): |
| 128 | + """ |
| 129 | + Delete the VPC |
| 130 | + """ |
| 131 | + return self.get_data("vpcs/%s" % self.id, type=DELETE) |
| 132 | + |
| 133 | + def __str__(self): |
| 134 | + return "<VPC: %s %s>" % (self.id, self.name) |
0 commit comments