digitaloceanobjects, represents all digital ocean services as objects, hiding all those horrible api calls.
Project description
digitalocean objects (pip3 install digitaloceanobjects)
everyone: I wish, for once, to just have a simple object oriented experience with the api.
digitaloceanobjects:
Please visit GitHub page for documentation that has navigation that works.
Table of Contents
- How to install
- Configurations
- OMG OMG SHOW ME SHOW ME HOW... NOW!!!
- Blocking Wonderful Blocking
- Account
- Sizes
- Regions
- SSH Keys
- Droplets
- Block Storage (Volumes)
- Volume Manager
- Create New Volume
- Retrieve All Volumes
- Retrieve All Volumes By Name
- Retrieve Volume By ID
- Retrieve Volume By Name And Region
- Retrieve Volumes With ANY Tags
- Retrieve Volumes With ALL Tags
- Retrieve Volumes With ONLY Tags
- Delete Volume By ID
- Delete Volume By Name And Region
- Delete Volumes With ANY Tags
- Delete Volumes With ALL Tags
- Delete Volumes With ONLY Tags
- Volume Object
- Volume Manager
- Snapshots
- Floating IPs
- Actions
- Exceptions
How to install
Here are your options.
Install from pypi repository
The most popular way is to install the latest package available on pypi.
You can install digitaloceanobjects using pip3
pip3 install -U digitaloceanobjects
You can uninstall if you like using,
pip3 uninstall digitaloceanobjects
Install from the cloned git hub repo
There are a few ways to install this python package from a clone of its github repo. Check out a copy and try the following...
Build a .tar.gz install package
From the root of the repo build a repo, and check the repo.
python3 setup.py sdist
twine check dist/*
Check the newly created dist directory for newly created .tar.gz files. This is your .tar.gz package and you can install using...
pip3 install ./dist/digitaloceanobjects-0.0.17.tar.gz
You can still uninstall using the same commands,
pip3 uninstall digitaloceanobjects
Install using the setup.py file
!WARNING! Install does not track which files, and where they are placed. So, you need to keep a record of there python3 does this.
This is how... from the github repo root directory.
sudo python3 setup.py install --record files.txt
You can uninstall using by playing back that files.txt file,
sudo xargs rm -rf < files.txt
Local interactive install
Using this method you can modify this packages code and have changes immediately available. Perfect for if you want to tinker with the library, poke around and contribute to the project.
From the cloned repository root directory.
pip3 install -e ./
You can uninstall using the usual command,
pip3 uninstall digitaloceanobjects
Configurations
Token
Set the DIGITALOCEAN_ACCESS_TOKEN environment variable with your api key.
export DIGITALOCEAN_ACCESS_TOKEN='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
api connection settings
You don't need to look too deeply here, this is for information only.
digitaloceanobjects is powered by a baserestapi class from the following project.
https://github.com/zorani/cloudapi/blob/main/cloudapi/baserestapi.py
digitaloceanobjects/digitaloceanapi/digitaloceanapiconnection.py inherits baserestapi, baseresapi takes care of all the tricky rate limiting.
Inside /digitaloceanapiconnection.py you will find a 'callrateperhour' variable set to the current digital ocean limit of 5000. digitalocean-objects converts 'callrateperhour' to seconds between requests.
You will also see the following variables.
geometric_delay_multiplier: If a request fails, the 'seconds between requests' is increased by multiplying by this number.
maximum_geometric_delay_multiplicaiton: How many times should you increase the 'seconds between requests' before considering it a fail.
maximum_failed_attempts: a failed attempt is put to the back of an internal queue for a retry. how many failed attempts are allowed before returning the response with failure codes and content.
BaseRESTAPI.__init__(
self,
baseurl="https://api.digitalocean.com",
callrateperhour=5000,
geometric_delay_multiplier=2,
maximum_geometric_delay_multiplications=6,
maximum_failed_attempts=3,
)
OMG OMG SHOW ME SHOW ME HOW NOW
Okay!! Okay!! Here is a quick start example!
Look how easy it is to work with digitaloceanobjects...
...read the code comments...
#!/usr/bin/env python3
from digitaloceanobjects import Droplet, DropletManager
from digitaloceanobjects import Volume, VolumeManager
#Create a droplet manager, to you know... manage your droplets.
droplet_manager = DropletManager()
#Create a new droplet.
my_brand_new_droplet = droplet_manager.create_new_droplet(
name="test-droplet",
region="ams3",
size="s-1vcpu-1gb",
image="ubuntu-16-04-x64",
tags=["digitalocean", "objects", "are", "great"],
)
#What? Done already?
#Yup... now output the droplet details.
print(type(my_brand_new_droplet))
print(my_brand_new_droplet.attributes)
#Want to attache a volume? No problem...
#Create a volume manager.
volume_manager = VolumeManager()
#You'll need a volume, so lets create a new volume.
my_brand_new_volume = volume_manager.create_new_volume(
size_gigabytes=10,
name="test-volume",
region="ams3",
description="BlockStoreFor Examples",
filesystem_type="ext4",
filesystem_label="example",
tags=["is", "it", "really", "this", "easy"],
)
#Well... damn that was easy. Peek at the volume object and the attributes. Nice.
print(type(my_brand_new_volume))
print(my_brand_new_volume.attributes)
#So, now just ask your droplet to attach your new volume.
my_brand_new_droplet.attach_a_volume(my_brand_new_volume)
#Still don't beleive how easy this was? Check the droplet attributes, you will now have a volume id attached to it.
print(my_brand_new_droplet.attributes.volume_ids)
Hope you're happy...
... now read the rest of the documentation to see what other amazing things you can do!
Blocking Wonderful Blocking
Did you notice in the quick start example above we didn't at any point check to see if the droplet, or the volume were ready and available?
Well... that's because digitaloceanobjects is 'blocking', it waits for an operation on digital ocean to complete before returning.
You can code away, without any worries.
For example, digitaloceanobjects will wait for a droplet to be ready before any further actions are applied to it. You can't attach a volume to a droplet that hasn't finished setting up.
If you want to setup multiple droplets concurrently you should thread your droplet set up script so you're not waiting on independent services.
Account
Retrieve information about your current digital ocean account.
from digitaloceanobjects import AccountManager
Account Manager
account_manager=AccountManager()
Retrieve Account Information
droplet_limit = account_manager.droplet_limit()
floating_ip_limit = account_manager.floating_ip_limit()
volume_limit = account_manager.volume_limit()
email = account_manager.email()
email_verified = account_manager.email_verified()
uuid = account_manager.uuid()
status = account_manager.status()
status_message = account_manager.status_message()
Sizes
The sizes objects represent different packages of hardware resources that can be used for Droplets. When a Droplet is created, a size must be selected so that the correct resources can be allocated.
Each size represents a plan that bundles together specific sets of resources. This includes the amount of RAM, the number of virtual CPUs, disk space, and transfer. The size object also includes the pricing details and the regions that the size is available in.
Import the size object, and the sizemanager.
from digitaloceanobjects import Size, SizeManager
Size Manager
The size manager contains methods to query information about available sizes on digital ocean.
size_manager=SizeManager()
Retrieve Sizes
list_of_size_objects=size_manager.retrieve_sizes()
Size Object
Size objects contains an attributes data class with the standard digital ocean size attributes.
The size objects worked on by the above manager contain a data class with attributes describing the available digital ocean sizes.
class Size:
def __init__(self):
self.attributes = SizeAttributes()
@dataclass
class SizeAttributes:
slug: str = None
available: bool = None
transfer: float = None
price_monthly: float = None
price_hourly: float = None
memory: int = None
vcpus: int = None
disk: int = None
regions: list = field(default_factory=list)
description: str = None
Regions
A region in DigitalOcean represents a datacenter where Droplets can be deployed and images can be transferred.
Each region represents a specific datacenter in a geographic location. Some geographical locations may have multiple "regions" available. This means that there are multiple datacenters available within that area.
Import the digitaloceanobject Region and RegionManger to interact with Regions.
from digitaloceanobjects import Region, RegionManager
Region Manager
Create a region manager.
region_manager = RegionManager()
Retrieve All Regions
Retrieve a list of region objects.
list_of_region_objects = region_manager.retrieve_all_regions()
Region Object
Region objects contains an attributes data class with the standard digital ocean region attributes.
class Region:
def __init__(self):
self.attributes = RegionAttributes()
...
@dataclass
class RegionAttributes:
slug: str = None
name: str = None
sizes: list = field(default_factory=list)
available: bool = None
features: list = field(default_factory=list)
SSH Keys
SSH Keys. DigitalOcean allows you to add SSH public keys to the interface so that you can embed your public key into a Droplet at the time of creation. Only the public key is required to take advantage of this functionality.
Import the digitaloceanobject SSHKey and SSHKeyManger to interact with SSH Keys.
from digitaloceanobjects import SSHKey, SSHKeyManager
SSH Key Manager
Create an SSH key manager.
sshkey_manager = SSHKeyManager()
Retrieve All SSH Keys
Returns a list of SSHKey objects each containing details of existing keys.
list_of_sshkey_objects=sshkey_manager.retrieve_all_sshkeys()
Create New Key
'upload' a new key to digital ocean by providing your public key, and an easy to remember name.
Returns an SSHKey object with details of your new key stored in it's attribute data class.
sshkey_object=sshkey_manager.create_new_key(name:str, public_key:str)
Retrieve SSH Key Using ID
Each SSH Key has an ID. Using this ID you can retrieve information on an existing SSH Key.
Returns an SSHKey object with details of your existing key stored in it's attribute data class.
sshkey_object=sshkey_manager.retrieve_sshkey_with_id(id:int)
SSH Key Object
SSH Key objects contains an attributes data class with the standard digital ocean SSH Key attributes.
sshkey_object=SSHKey()
class SSHKey:
def __init__(self):
self.attributes = SSHKeyAttributes()
...
@dataclass
class SSHKeyAttributes:
id: str = None
fingerprint: str = None
public_key: str = None
name: str = None
To work on your digital ocean ssh keys first retrieve your ssh key objects using the ssh key object manager. Then apply the objects following methods.
Update SSH Key Name
Then call the objects update name method with your new ssh key name.
sshkey_object.update_name(name:str)
Delete SSH Key
You can delete an ssh key by calling the objects delete method.
sshkey_object.delete()
Droplets
A Droplet is a DigitalOcean virtual machine.
Import the digitaloceanobject Droplet and DropletManger to interact with or create new droplets.
from digitaloceanobjects import Droplet, DropletManager
Droplet Manager
Create a droplet manager.
droplet_manager = DropletManager()
Create New Droplet
To create a new droplet use the following method supplying your desired droplet Attribute Values.
droplet_object = droplet_manager.create_new_droplet(
name="example.com",
region="nyc3",
size="s-1vcpu-1gb",
image="ubuntu-16-04-x64",
ssh_keys=[],
backups=False,
ipv6=True,
user_data=None,
private_networking=None,
volumes=None,
tags=["bananas"],
)
Retrieve Droplet By ID
You can retreive an existing droplet by calling the following method and supplying the droplets id.
A droplet object will be returned. The droplet object will contain the standard digital ocean droplet attributes.
droplet_object = droplet_manager.retrieve_droplet_by_id(id:int)
Retrieve Droplets By Name
Many droplets can have the same name.
To retrieve a list of droplet objects that have a particular name use the following method supplying your name.
list_of_droplet_objects = droplet_manager.retrieve_droplet_by_name(name:str)
Retrieve All Droplets
To retrieve all droplets in your account apply the following method.
A list of droplet objects will be returned.
list_of_droplet_objects = droplet_manager.retrieve_all_droplets()
Retrieve Droplets With ANY tags
You can tag droplets with as many tags as you like.
This method will return a list of digital ocean objects that contain any of the tags you provide in a list.
list_of_droplet_objects = droplet_manager.retrieve_droplets_with_any_tags(tag:list)
Retrieve Droplets With ALL tags
This method will return a list of droplet objects, but only throse droplets that have at least all of the tags that you specify in a list of tags.
list_of_droplet_objects = droplet_manager.retrieve_droplets_with_all_tags(tag:list)
Retrieve Droplets With ONLY tags
This method will return a list of droplet objects that exactly match a list of tags that you specify.
list_of_droplet_objects = droplet_manager.retrieve_droplets_with_only_tags(tag:list)
Delete Droplets With ANY tags
You can also delete droplets by tags.
This method allows you to delete all droplets that contain any, one or more, of a list of tags that you specify.
droplet_manager.delete_droplets_with_any_tags(tag:list)
Delete Droplets With ALL tags
This method will delete any droplets that contain all, at least all, of the tags that you specify.
droplet_manager.delete_droplets_with_all_tags(tag:list)
Delete Droplets With ONLY tags
This method will delete droplets that match exactly a tag list that you specify.
droplet_manager.delete_droplets_with_only_tags(tag:list)
Delete Droplet By ID
The droplet manager allowes you to request droplet deletion by supplying a droplet id.
You might prefer to delete a droplet by calling a droplet objects delete method directly though.
droplet_manager.delete_droplet_by_id(id:int)
Droplet Object
Droplet objects contains an attributes data class with the standard digital ocean droplet attributes.
Some of the droplet attributes such as region
and image
can be further inspected by retrieving their relative digitaloceanobject objects.
droplet_object=Droplet()
class Droplet:
def __init__(self, status=None):
self.attributes = DropletAttributes()
self.attributes.status = status
self.deleted=False
...
@dataclass
class DropletAttributes:
id: int = None
name: str = None
memory: int = None
vcpus: int = None
disk: int = None
locked: bool = None
created_at: str = None
status: str = None
backup_ids: list = field(default_factory=list)
snapshot_ids: list = field(default_factory=list)
features: list = field(default_factory=list)
region: object = field(default_factory=list)
image: object = field(default_factory=list)
size: object = field(default_factory=list)
size_slug: str = None
networks: object = field(default_factory=list)
kernel: object = field(default_factory=list)
next_backup_window: object = field(default_factory=list)
tags: list = field(default_factory=list)
volume_ids: list = field(default_factory=list)
vpc_uuid: list = field(default_factory=list)
Reboot
A reboot action is an attempt to reboot the Droplet in a graceful way, similar to using the reboot command from the console.
You can reboot a droplet by calling the following method.
droplet_object.reboot()
Power Cycle
A power cycle action is similar to pushing the reset button on a physical machine, it's similar to booting from scratch.
You can power cycle a droplet by calling the following method.
droplet_object.powercycle()
Shutdown
A shutdown action is an attempt to shutdown the Droplet in a graceful way, similar to using the shutdown command from the console. Since a shutdown command can fail, this action guarantees that the command is issued, not that it succeeds. The preferred way to turn off a Droplet is to attempt a shutdown, with a reasonable timeout, followed by a power off action to ensure the Droplet is off.
You can shutdown a droplet by calling the following method.
droplet_object.shutdown()
Power Off
A power_off event is a hard shutdown and should only be used if the shutdown action is not successful. It is similar to cutting the power on a server and could lead to complications.
You can power off a droplet by calling the following method.
droplet_object.poweroff()
Power On
You can power on a droplet by calling the following method.
droplet_object.poweron()
Rebuild
A rebuild action functions just like a new create.
You can supply an image slug, or an image id. Your droplet will be rebuilt, and you object attributes will be updated to reflect the changed this method makes.
You can rebuild a droplet by calling the following method.
droplet_object.rebuild(img:str)
Rename
You can rename a droplet by calling the following method.
droplet_object.rename(name:str)
Create Snapshot
You can create a droplet snapshot using the following method supplying a name to be use for your snapshot.
dropletsnapshot_object = droplet_object.createsnapshot(name:str)
A droplet snapshot object is returned. This type of snapshot, droplet snapshot, has more details than the usual snapshot covered later.
Here are the DropletSnapshot details.
class DropletSnapshot:
def __init__(self):
self.attributes = DropletSnapshotAttributes()
@dataclass
class DropletSnapshotAttributes:
id: int = None
name: str = None
distribution: str = None
slug: str = None
public: bool = None
regions: list = field(default_factory=list)
created_at: str = None
min_disk_size: int = None
type: str = None
size_gigabytes: float = None
Retrieve Snapshots
You can retrieve all the DropletSnapshots for a droplet by calling the following method.
list_of_dropletsnapshot_objects = droplet_object.retrieve_snapshots()
Retrieve Snapshot By ID
You can retrieve a DropletSnapshot for a droplet by calling the following method and supplying an id.
This method will only search snapshots associated to the droplet.
dropletsnapshot_object = droplet_object.retrieve_snapshot_by_id(id:int)
Retrieve Associated Volumes
A droplet may have block storage attached to it. You can retrieve a list of volume objects associated to your droplet by calling the following method.
list_of_volume_objects = droplet_object.retrieve_associated_volumes()
Retrieve Associated Volume Snapshots
You can retrieve all associated volume snapshots by calling the following method.
list_of_volume_snapshot_objects = droplet_object.retrieve_associated_volume_snapshots()
!WARNING! These snapshot objects are slightly different to the droplet snapshots we just looked at.
The snapshots object created here can actually detail saved instances of both Droplet or a block storage volume, which is reflected in the 'resource_type' attribute.
Attach A Volume
You can attach a volume to a droplet by calling the following method and supplying a volume object.
droplet_object.attach_a_volume(target_volume:Volume)
Detach A Volume
You can similarly detach the volume by calling the following.
droplet_object.detach_a_volume(target_volume:Volume)
Restore Droplet
A Droplet restoration will rebuild an image using a backup image. The image ID that is passed in must be a backup of the current Droplet instance. The operation will leave any embedded SSH keys intact.
You can restore a droplet by calling the following method.
droplet_object.restore_droplet(image_id:int)
Resize Droplet
If you set disk=False
only the RAM will be increased.
If you set disk=True
the disk size will be upgraded, and you will not be able to shrink your droplet down to it's previous RAM size.
droplet_object.resize_droplet(
slug_size='s-1vcpu-2gb',
disk_resize=False
)
A Droplet must be powered off prior to resizing, digitaloceandroplets does this for you.
Delete Droplet
You can delete a droplet from your account by applying the following method.
droplet_object.delete()
This method sets your objects droplet_object.deleted=True
so object methods will no longer work.
Block Storage (Volumes)
DigitalOcean Block Storage Volumes provide expanded storage capacity for your Droplets and can be moved between Droplets within a specific region. Volumes function as raw block devices, meaning they appear to the operating system as locally attached storage which can be formatted using any file system supported by the OS. They may be created in sizes from 1GiB to 16TiB.
Import the digitaloceanobject Volume and VolumeManger to interact with or create new volumes.
from digitaloceanobjects import Volume, VolumeManager
Volume Manager
Create a volume manager.
volume_manager = VolumeManager()
Create New Volume
To create a new volume use the following method supplying your desired volume Attribute Values.
volume_object = volume_manager.create_new_volume(
size_gigabytes=10,
name="testingavolume",
region="ams3",
description="BlockStoreExample",
filesystem_type="ext4",
filesystem_label="example",
tags=["banana"],
)
Note: You can only create one volume per region with the same name.
Retrieve All Volumes
To list all available volumes on your account apply the following method.
A list of volume objects will be returned.
list_of_volume_objects=volume_manager.retrieve_all_volumes()
Retrieve All Volumes By Name
To list volumes on your account that match a specified name use the following method.
list_of_volume_objects=volume_manager.retrieve_all_volumes_by_name(name:str)
Retrieve Volume By ID
To retrieve a volume by id, apply the following method.
volume_object=volume_manager.retrieve_volume_by_id(id:int)
Retrieve Volume By Name And Region
You can also pick out a specific existing volume by name and region, but calling the following method.
volume_object=volume_manager.retrieve_volume_by_name_region(
name:str,
region:str
)
Retrieve Volumes With ANY Tags
To retrieve a list of volumes that match any, at least one, of your specified tags use the following method and supply a list of tag strings.
list_of_volume_objects=volume_manager.retrieve_volumes_with_any_tags(tag:list)
Retrieve Volumes With ALL Tags
To retrieve a list of volumes that are tagged with all, at least all, of your specified tags use the following command.
list_of_volume_objects=volume_manager.retrieve_volumes_with_all_tags(tag:list)
Retrieve Volumes With ONLY Tags
To retrieve a list of volumes that are tagged with only, exactly matching, your list of specified tags, apply the following method supplying a list of tag strings.
list_of_volume_objects=volume_manager.retrieve_volumes_with_only_tags(tag:list)
Delete Volume By ID
The volume manager allows you to delete a volume by supplying its ID.
Though you may prefer to delete a volume by directly calling delete on its volume object.
volume_manager.delete_volumes_by_id(id:int)
Delete Volume By Name And Region
The volume manager allows you to delete a volume by supplying its name and region.
Though you may prefer to delete a volume by directly calling delete on its volume object.
volume_manager.delete_volume_by_name_region(
name:str,
region:str
)
Delete Volumes With ANY Tags
You can delete all volumes that match any of, at least one, of your specified tags by applying the following method and supplying a list of tag strings.
volume_manager.delete_volumes_with_any_tags(tag:list)
Delete Volumes With ALL Tags
You can delete all volumes that contain, at least have all, of your specified tags by applying the following method and supplying a list of tag strings.
volume_manager.delete_volumes_with_all_tags(tag:list)
Delete Volumes With ONLY Tags
You can also delete all volume that only contain, that match exactly, your list of specified tag strings by applying the following method.
volume_manager.delete_volumes_with_only_tags(tag:list)
Volume Object
Volume objects contain an attributes data class with the standard digital ocean volume attributes.
volume_object=Volume()
class Volume:
def __init__(self):
self.attributes = VolumeAttributes()
self.deleted=False
...
@dataclass
class VolumeAttributes:
id: str = None
region: object = field(default_factory=list)
droplet_ids: list = field(default_factory=list)
name: str = None
description: str = None
size_gigabytes: int = None
created_at: str = None
filesystem_type: str = None
filesystem_label: str = None
tags: list = field(default_factory=list)
Create Snapshot
You can create a snapshot from your volume using the following method.
A snapshot will be returned.
snapshot_object=volume_object.create_snapshot(
name:str,
tags:list
)
Retrieve Snapshots
You can retrieve a list of snapshots for your object using the following method.
A list of snapshot objects will be returned.
list_of_snapshot_objects=volume_object.retrieve_snapshots()
Detach From Droplets
You can detach a volume from a droplet by calling the following method. No need to supply details of a droplet as volumes can only be attached to one droplet at a time. This method just makes sure the volume object detaches.
volume_object.detach_from_droplets()
Resize Volume
To resize a volume to a new size in GiB (1024^3), call the following method.
Volumes may only be resized upwards. The maximum size for a volume is 16TiB.
volume_object.resize(size_gigabytes:int)
Snapshots
Snapshots are saved instances of a Droplet or a block storage volume, which is reflected in the resource_type
attribute. In order to avoid problems with compressing filesystems, each defines a min_disk_size
attribute which is the minimum size of the Droplet or volume disk when creating a new resource from the saved snapshot.
Import the digitaloceanobject Snapshot and SnapshotManger to interact with or create new snapshots.
from digitaloceanobjects import Snapshot, SnapshotManager
Snapshot Manager
Create a snapshot manager.
snapshot_manager=SnapshotManager()
Retrieve All Snapshots
To retrieve all existing snapshots call the following method.
A list of Snapshot objects will be returned.
list_of_snapshot_objects=snapshot_manager.retrieve_all_snapshots()
Retrieve All Droplet Snapshots
To retrieve a list of only droplet snapshots call the following method. Snapshot objects will be returned, remember to not confiuse this with DropletSnapshot objects.
list_of_snapshot_objects=snapshot_manager.retrieve_all_droplet_snapshots()
Retrieve All Volume Snapshots
To retrieve a list of only volume snapshots call the following method.
list_of_snapshot_objects=snapshot_manager.retrieve_all_volume_snapshots()
Retrieve Snapshot By ID
You can retrieve a snapshot object by ID.
snapshot_object=snapshot_manager.retrieve_snapshots_id(id:int)
Snapshot Object
Snapshot objects contains an attributes data class with the standard digital ocean snapshot attributes.
snapshot_object=Snapshot()
class Snapshot:
def __init__(self):
self.attributes = SnapshotAttributes()
...
@dataclass
class SnapshotAttributes:
id: str = None
name: str = None
created_at: str = None
regions: list = field(default_factory=list)
resource_id: str = None
resource_type: str = None
min_disk_size: int = None
size_gigabytes: float = None
tags: list = field(default_factory=list)
Delete Snapshot
You can delete a snapshot from your account by calling the snapshot object delete method.
snapshot_object.delete()
Floating IPs
DigitalOcean Floating IPs are publicly-accessible static IP addresses that can be mapped to one of your Droplets. They can be used to create highly available setups or other configurations requiring movable addresses.
Floating IPs are bound to a specific region.
Import the digitaloceanobject FloatingIP and FloatingIPManger to interact with or create new floating ips.
from digitaloceanobjects import FloatingIP, FloatingIPManager
Floating IP Manager
Create a floating IP manager.
floatingip_manager=FloatingIPManager()
Retrieve All Floating IPs
To retrieve all floating IPs on your account, call the following method.
A list of floating ip objects will be returned.
list_of_floatingip_objects=floatingip_manager.retrieve_all_floating_ips()
Create New Floating IP
Creates a floating IP and attaches it straight to the specified droplet object.
A floating ip object is returned with details of your new floating ip.
floatingip_object=floatingip_manager.create_new_floating_ip(droplet_object:Droplet)
Create Region Reserve IP
You can reserve a floating ip in a specified region by calling the following method and specifying a region slug.
floatingip_object=floatingip_manager.reserve_ip_for_region(region_slug:str)
Retrieve Floating IP
You can retrieve details of floating ip by using the ip address itself. Call the following method and specifying the ip address.
floatingip_object=floatingip_manager.retrieve_floating_ip(ip:str)
Floating IP Object
Floating IP objects contains an attributes data class with the standard digital ocean floating IP attributes.
floatingip_object=FloatingIP()
class FloatingIP:
def __init__(self):
self.attributes = FloatingIPAttributes()
...
@dataclass
class FloatingIPAttributes:
ip: str = None
region: object = None
droplet: object = None
locked: bool = None
Delete Floating IP
You can delete a floating ip from your account by calling the following.
floatingip_object.delete()
Unassign Floating IP]
You can unasign a floating ip from a droplet by calling the following. Specifying a droplet is not necessary.
floatingip_object.unassign()
Retrieve All IP Actions
You can retrieve all the actions applied to an ip by calling the following method.
A list of action objects will be returned.
list_of_action_objects=floatingip_object.retrieve_all_actions()
Retrieve Existing IP Action
You can retrieve a specific action by specifying its id and using the following method.
action_object=floatingip_object.retrieve_existing_actions(action_id:int)
Actions
Actions are records of events that have occurred on the resources in your account. These can be things like rebooting a Droplet, or transferring an image to a new region.
An action object is created every time one of these actions is initiated. The action object contains information about the current status of the action, start and complete timestamps, and the associated resource type and ID.
Every action that creates an action object is available through this endpoint. Completed actions are not removed from this list and are always available for querying.
Import the digitaloceanobject Action and ActionManger to interact with actions.
from digitaloceanobjects import Action, ActionManager
Action Manager
Create an action manager.
action_manager=ActionManager()
Retrieve All Actions
This will be the entire list of actions taken on your account, so it will be quite large. As with any large collection returned by the API, the results will be paginated with only 20 on each page by default but the following method collects them all, and returns all of them in a list of action objects.
If you want to have direct access to the api call please check the actions.py
. You can paginate using the api call but still benefit from the rate limit management of digitaloceanobjects.
A list of Action Objectsis returned.
list_of_action_objects=action_manager.retrieve_all_actions()
Retrieve Action
You can retreive one action at a time if you like, by specifying the action id.
action_object=action_manager.retrieve_action(action_id:int)
Action Object
Action objects contains an attributes data class with the standard digital ocean action attributes.
action_object=Action()
class Action:
def __init__(self, action_attributes: ActionAttributes):
self.attributes = action_attributes
'''
@dataclass
class ActionAttributes:
id: int = None
status: str = None
type: str = None
started_at: str = None
completed_at: str = None
resource_id: int = None
resource_type: str = None
region: object = None
region_slug: str = None
Exceptions
Some exceptions for you to catch, to help your code run smoother.
Droplet Exceptions
ErrorDropletNotFound
ErrorDropletNameContainsInvalidChars
ErrorDropletSlugSizeNotFound
ErrorDropletResizeDiskError
ErrorAccountDropletLimitReached
ErrorDropletAttachedVolumeCountAlreadAtLimit
Volume Exceptions
ErrorVolumeAlreadyExists
ErrorVolumeNotFound
ErrorVolumeResizeValueTooLarge
ErrorVolumeResizeDirection
ErrorAccountVolumeLimitReached
Snapshot Exceptions
ErrorSnapshotNotFound
Action Exceptions
ErrorActionDoesNotExists
ErrorActionFailed
Region Exceptions
ErrorNotSameRegion
ErrorRegionDoesNotExist
Floating IP Exceptions
ErrorAccountFloatingIPLimitReached
ErrorFloatingIPDoesNotExists
ErrorDropletAlreadyHasFloatingIP
SSH Key Exceptions
ErrorSSHkeyDoesNotExists
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file digitaloceanobjects-0.0.24.tar.gz
.
File metadata
- Download URL: digitaloceanobjects-0.0.24.tar.gz
- Upload date:
- Size: 50.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a46edb6bf952865dc50c872fd5ebba83c82ebb85c00ba883e3c96afef2316ea0 |
|
MD5 | 994b489ad88e079b6b89cc86ef479a57 |
|
BLAKE2b-256 | aada064842d81d44259164155cd169c3a22b3931657a3528d418a26037a02115 |