Source code for pontoon.lib.FloatingIP
# -*- coding: utf-8 -*-
from .baseapi import BaseAPI, GET, POST, DELETE
[docs]class FloatingIP(BaseAPI):
def __init__(self, *args, **kwargs):
self.ip = None
self.droplet = []
self.region = []
super(FloatingIP, self).__init__(*args, **kwargs)
@classmethod
[docs] def get_object(cls, api_token, ip, mocked):
"""
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, mocked=mocked)
floating_ip.mock_data = "floatingip/single.json"
floating_ip.load()
return floating_ip
[docs] 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
[docs] 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
"""
self.mock_data = "floating_ip/create.json"
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
[docs] 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')
"""
self.mock_data = "floatingip/single.json"
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
[docs] def destroy(self):
"""
Destroy the FloatingIP
"""
self.mock_status = 204
return self.get_data('floating_ips/%s/' % self.ip, type=DELETE)
[docs] def assign(self, droplet_id):
"""
Assign a FloatingIP to a Droplet.
Args:
droplet_id: int - droplet id
"""
self.mock_data = "floatingip/assign.json"
self.mock_status = 201
return self.get_data(
"floating_ips/%s/actions/" % self.ip,
type=POST,
params={"type": "assign", "droplet_id": droplet_id}
)
[docs] def unassign(self):
"""
Unassign a FloatingIP.
"""
self.mock_data = "floatingip/unassign.json"
self.mock_status = 201
return self.get_data(
"floating_ips/%s/actions/" % self.ip,
type=POST,
params={"type": "unassign"}
)
def __str__(self):
return "%s" % self.ip