File: FloatingIP.py

package info (click to toggle)
python-digitalocean 1.16.0-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid
  • size: 912 kB
  • sloc: python: 4,961; makefile: 46
file content (112 lines) | stat: -rw-r--r-- 3,095 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# -*- coding: utf-8 -*-
from .baseapi import BaseAPI, GET, POST, DELETE


class FloatingIP(BaseAPI):
    def __init__(self, *args, **kwargs):
        self.ip = None
        self.droplet = []
        self.region = []

        super(FloatingIP, self).__init__(*args, **kwargs)

    @classmethod
    def get_object(cls, api_token, ip):
        """
            Class method that will return a FloatingIP object by its IP.

            Args:
                api_token: str - token
                ip: str - floating ip address
        """
        floating_ip = cls(token=api_token, ip=ip)
        floating_ip.load()
        return floating_ip

    def load(self):
        """
            Load the FloatingIP object from DigitalOcean.

            Requires self.ip to be set.
        """
        data = self.get_data('floating_ips/%s' % self.ip, type=GET)
        floating_ip = data['floating_ip']

        # Setting the attribute values
        for attr in floating_ip.keys():
            setattr(self, attr, floating_ip[attr])

        return self

    def create(self, *args, **kwargs):
        """
            Creates a FloatingIP and assigns it to a Droplet.

            Note: Every argument and parameter given to this method will be
            assigned to the object.

            Args:
                droplet_id: int - droplet id
        """
        data = self.get_data('floating_ips/',
                             type=POST,
                             params={'droplet_id': self.droplet_id})

        if data:
            self.ip = data['floating_ip']['ip']
            self.region = data['floating_ip']['region']

        return self

    def reserve(self, *args, **kwargs):
        """
            Creates a FloatingIP in a region without assigning
            it to a specific Droplet.

            Note: Every argument and parameter given to this method will be
            assigned to the object.

            Args:
                region_slug: str - region's slug (e.g. 'nyc3')
        """
        data = self.get_data('floating_ips/',
                             type=POST,
                             params={'region': self.region_slug})

        if data:
            self.ip = data['floating_ip']['ip']
            self.region = data['floating_ip']['region']

        return self

    def destroy(self):
        """
            Destroy the FloatingIP
        """
        return self.get_data('floating_ips/%s/' % self.ip, type=DELETE)

    def assign(self, droplet_id):
        """
            Assign a FloatingIP to a Droplet.

            Args:
                droplet_id: int - droplet id
        """
        return self.get_data(
            "floating_ips/%s/actions/" % self.ip,
            type=POST,
            params={"type": "assign", "droplet_id": droplet_id}
        )

    def unassign(self):
        """
            Unassign a FloatingIP.
        """
        return self.get_data(
            "floating_ips/%s/actions/" % self.ip,
            type=POST,
            params={"type": "unassign"}
        )

    def __str__(self):
        return "%s" % (self.ip)