Apache Libcloud

The Libcloud library wraps the IONOS Cloud API, which is a REST API. All API operations are performed over SSL and authenticated using your IONOS portal credentials.

The API can be accessed within an instance running in IONOS or directly over the Internet from any application that can send an HTTPS request and receive an HTTPS response.

On top of the basic Libcloud functionality of listing images or creating nodes, this library also provides support for extended functions such as taking servers online, attaching and detaching volumes, and adding network interfaces.

Given that this is a REST API, entities and their linked resources are fetched using full URLs under the hood. Examples for this will follow later.

Terms and Concepts

Below is a list of entities provided by ProfitBricks. Carrying out basic CRUD operations on these can be done using the core and extended functionality of this driver.

Locations

ProfitBricks provides a list of locations which can be used to provision data centers and servers. There are four locations available:

  • Frankfurt, Germany
  • Karlsruhe, Germany
  • Las Vegas, United States of America
  • Newark, United States of America

Images

ProfitBricks provides a list of publicly available operating system images which can be used to create new server (node) volumes. Each image is specifically linked to a location so different locations can contain a selection of images.

This driver also supports updates and deletes to private images that you have created using other tools. It does not support creating private images.

Virtual Data Centers

Within the ProfitBricks environment, a virtual data center (VDC) is a virtual representation of a physical data center and encompasses a list of Local Area Networks (LANs), Load Balancers (LB), servers (nodes), and volumes all grouped together in a logically isolated environment. Servers within the same virtual data center can talk to each other and connect to volumes and other resources in the same virtual data center.

Servers (Nodes)

Servers are entities within a virtual data center that are configured with a CPU type, a number of CPU cores, an amount of RAM, and optional network interface cards (NICs). Servers can be attached to and detached from volumes and LANs.

Volumes

Like servers, volumes are also entities that reside within a virtual data center and consist of an operating system image, a size, a bus type, and a disk type (SSD, HDD). Volumes can be connected to and disconnected from servers.

Snapshots

A snapshot is a backup of the running state of your servers volume at the time it was backed up. Snapshots can be created from volumes, deleted, and restored to volumes.

Network Interfaces (NICs)

A server can have one or more NICs attached to it. These can then be attached to load balancers and LANs. NICs can be added to servers, updated, and destroyed.

Local Area Networks (LANs)

A virtual data center can have any number of LANs assigned to it. Each LAN in turn can have NICs assigned to them, which are also assigned to servers.

LANs can be added, updated, and destroyed.

Firewall Rules (FW Rules)

A network interface card can have any number of firewall rules attached to it to facilitate opening ports and exposing services. Firewall rules can be added, updated, and deleted.

Load Balancers (LB)

Load balancers can be added to a virtual data center, updated, and deleted. Load balancers can also have NICs attached to and detached from them.

IP Blocks

A virtual data center can have any number of IP blocks assigned to it. IP Blocks are groups of reserved static IP addresses in a particular location that can be assigned to servers and network resources inside a virtual data center provisioned at that same location. IP blocks can be created, updated, and deleted.

Getting Started

Before you begin you will need to have signed-up for a IONOS account. The credentials you setup during sign-up will be used to authenticate against the API.

Installation

Libcloud is available on PyPi. You can install the latest stable version using pip:

$ pip install apache-libcloud

Done! The ProfitBricks Compute driver is included in the standard distribution of libcloud.

Connecting to ProfitBricks and Authenticating

We will be providing code samples throughout this guide. To follow along, open up a text editor and add the following lines of code to get started:

from pprint import pprint as pp

from libcloud.compute.types import Provider
from libcloud.compute.providers import get_driver

cls = get_driver(Provider.PROFIT_BRICKS)
driver = cls('username', 'password')

The above code will import everything needed to get set up, instantiate the driver, and authenticate with ProfitBricks.

Your username is the one you setup during sign-up. Your password is the one you specified during sign-up.

Caution: You will want to ensure you follow security best practices when using credentials within your code.

Usage Examples

Here are two simple examples on how to use the driver.

List all available sizes:

pp(driver.list_sizes())

NOTE: While the libcloud driver exposes a pre-created list of sizes, ProfitBricks allows you to define your own custom size. These options are exposed as extension parameters in the create_node() function of the driver.

List all nodes:

pp(driver.list_nodes())

This will list all nodes across all virtual data centers attached to your account.

Update a node using one of the extension functions:

nodes = driver.list_nodes()
current_node = nodes[0]
updated_node = driver.ex_update_node(
    node=node,
    name='My Updated Node',
    ram=2048,
    cores=3
)
pp(vars(updated_node))

This will update the name of a node, the amount of RAM it uses, and the number of CPU cores.

Note: All core and extension functions that involve listing entities will return an iterable Python list(), while the same functions that describe an entity will return a single instance of that entity.

Additional Documentation and Support

You can reference the official libcloud documentation for the full list of extension functions and supported parameters for the ProfitBricks compute driver.

You can also engage with us in the community and we'll be more than happy to answer any questions you might have.

Exploring core functionality

The following code snippets show how the core Libcloud functionality works for this driver.

List Locations

The following code snippet will list all ProfitBricks locations:

locations = driver.list_locations()
for location in locations:
    pp(vars(location))

List Images

The following code snippet will list all images (public and private):

all_images = driver.list_images()

List images by type (CDROM, HDD):

hdd_images = driver.list_images(type='HDD')
cd_images = driver.list_images(type='CDROM')

List public and private images:

public_images = driver.list_images(is_public=True)
private_images = driver.list_images(is_public=False)

This function returns an iterable list:

for image in all_images:
    print('Image name is: {name}'.format(name=image.name))

You can apply your own filtering if you wish to:

my_images = [image for image in all_images if image.extra['location'] == 'de/fra']

List Sizes

The following code snippet will list all sizes - useful to create a node with a pre-defined size:

sizes = driver.list_sizes()

List Nodes

The following code snippet will list all nodes associated with your account

nodes = driver.list_nodes()

Create a Node

Create a standalone node:

sizes = driver.list_sizes()
size = sizes[0]

new_node = driver.create_node(
    name='Basic node',
    size=size
)

Create a composite node by specifying a size and an image:

sizes = driver.list_sizes()
size = sizes[0]

images = driver.list_images(image_type='HDD')
image = images[0]

new_node = driver.create_node(
    name='Postgres Server',
    size=size,
    image=image,
    ex_password='D5mmyP8ssw0rd'
)

Create a composite node with an Intel Xeon CPU (4 cores), 2gb of RAM, and a 40GB volume:

images = driver.list_images(type='HDD')
image = images[0]

new_node = driver.create_node(
    name='App server one',
    ex_cores=2,
    ex_ram=2048,
    ex_cpu_family='INTEL_XEON',
    image=image,
    ex_password='D5mmyP8ssw0rd'
)

Create a node at a specific location:

location = driver.ex_describe_location(ex_location_id='de/fra')
new_node = driver.create_node(
    location=location,
    ex_cores=2,
    ex_ram=1024
)

Create a node in an existing virtual data center:

sizes = driver.list_sizes()
size = sizes[0]

images = driver.list_images(type='HDD')
image = [image for image in images if image.extra['location'] == 'us/las']

datacenters = driver.ex_list_datacenters()
datacenter = [datacenter for datacenter in datacenters if datacenter.extra['location'] == 'us/las']

new_node = driver.create_node(
    name='My new node',
    size=size,
    image=image,
    ex_datacenter=datacenter,
    ex_password='D5mmyP8ssw0rd'
)

Create a node with a pre-existing volume:

sizes = driver.list_sizes()
size = sizes[0]

volumes = driver.list_volumes()
volume = volumes[0]

new_node = driver.create_node(
    name='My new node',
    volume=volume,
    size=size
)

Create an offline node with no internet access:

new_node = driver.create_node(
    name='Offline node',
    image=image,
    size=size,
    ex_internet_access=False,
    ex_password='D5mmyP8ssw0rd'
)

Create a node that can be remotely accessed via SSH:

new_node = driver.create_node(
    name='My new node with SSH',
    size=size,
    image=image,
    ex_datacenter=datacenter,
    ex_password='D5mmyP8ssw0rd',
    ex_exposed_public_ports=[22]
)

Create a node to act as a web server, exposing ports 80 and 443 publicly:

new_node = driver.create_node(
    name='Demo web server',
    image=image,
    ex_datacenter=datacenter,
    ex_cpu_family='AMD_OPTERON',
    ex_exposed_public_ports=[80, 443],
    ex_exposed_private_ports=[22],
    ex_ram=2048,
    ex_cores=2,
    ex_disk=32,
    ex_disk_type='SSD',
    ex_password='CF98asdjdkaAADjda82348asDASda'
)

Create a node with SSH keys:

new_node = driver.create_node(
    name='SSH Keys Example',
    image=image,
    size=size,
    ex_ssh_keys=['ssh-key-one', 'ssh-key-two']
)

Notes on the create node examples

  • When creating a new node, the ex_password or ex_ssh_keys parameters must be set or the request will fail.

  • By default, all new nodes are taken online when created. To create an offline node, pass in the argument ex_internet_access and set it to False.

  • Within the ProfitBricks environment, a server must always reside in a virtual data center. If a node is created with no data center specified, the best data center will be determined through the image or the volume.

  • When creating a new node, the location of the virtual data center and the image used must be the same or else the request will fail.

  • It is possible to mix and match a size with more fine grained parameters, such as the number of CPU cores and amount of RAM.

  • When specifying the amount of RAM, always do this in multiples of 256.

  • When specifying an image as part of creating a composite server, a new volume will be created in the same request.

Reboot a Node

The following code snippet fetch a single node and then reboot it:

node = driver.ex_describe_node(
    ex_href='https://api.profitbricks.com/cloudapi/v4/datacenters/<data-center-id>/servers/<server-id>'
)
rebooted = driver.reboot_node(node=node)

Destroy a Node

The following code snippet will delete a node:

node = driver.ex_describe_node(
    ex_href='https://api.profitbricks.com/cloudapi/v4/datacenters/<data-center-id>/servers/<server-id>'
)
destroyed = driver.destroy_node(node=node)

The following code snippet will delete a node and all attached volumes:

node = driver.ex_describe_node(
    ex_href='https://api.profitbricks.com/cloudapi/v4/datacenters/<data-center-id>/servers/<server-id>'
)
destroyed = driver.destroy_node(node=node, ex_remove_attached_disks=True)

List Volumes

The following code snippet will list all volumes associated with your account:

volumes = driver.list_volumes()

Create a Volume

The following code will create HDD and SSD volumes - both 100GB in size:

datacenters = driver.ex_list_datacenters()
location    = driver.ex_describe_location('us/las')
datacenter  = [datacenter for datacenter in datacenters if datacenter.extra['location'] == location.id]

images = driver.list_images(type='HDD')
image = [image for image in images if image.extra['location'] == location.id][0]

new_hdd_volume = driver.create_volume(
    name='Example HDD volume',
    size=100,
    ex_type='HDD'
    ex_datacenter=datacenter,
    ex_image=image,
    ex_password='dummy-P8SSw0rd'
)

new_ssd_volume = driver.create_volume(
    name='Example SSD volume',
    size=100,
    ex_type='SSD',
    ex_datacenter=datacenter,
    ex_image=image,
    ex_password='dummy-P8SSw0rd'
)

The following code will create an SSD volume with some SSH keys:

datacenters = driver.ex_list_datacenters()
location    = driver.ex_describe_location('us/las')
datacenter  = [datacenter for datacenter in datacenters if datacenter.extra['location'] == location.id]

images = driver.list_images(type='HDD')
image = [image for image in images if image.extra['location'] == location.id][0]

ssh_keys = ['ssh-key-one', 'ssh-key-two']

new_hdd_volume = driver.create_volume(
    name='Example with keys',
    size=100,
    ex_type='SSD',
    ex_datacenter=datacenter,
    ex_image=image,
    ssh_keys=ssh_keys
)

Notes on the above examples

  • When creating a volume, the location of the virtual data center and the image must be the same, or the request will fail.

  • Volume size is specified in GB.

  • A volume cannot be created with an image of type CDROM. Trying to do this will cause the request to fail.

  • A volume must be created with SSH keys or a password.

  • A volume password may only contain lower and uppercase Latin characters and numbers.

Attach a Volume to a Node

The following code snippet demonstrates how to attach a volume to a node:

node = driver.ex_describe_node(
    ex_href='https://api.profitbricks.com/cloudapi/v4/datacenters/<data-center-id>/servers/<server-id>'
)
volume = driver.ex_describe_volume(
    ex_href='https://api.profitbricks.com/cloudapi/v4/datacenters/<data-center-id>/volumes/<volume-id>'
)

attached_volume = driver.attach_volume(node=node, volume=volume)

Note: Attaching a volume to a node is only possible if they both reside in the same virtual data center.

Detach a Volume from a Node

The following snippet detaches a volume from a node:

node = driver.ex_describe_node(
    ex_href='https://api.profitbricks.com/cloudapi/v4/datacenters/<data-center-id>/servers/<server-id>'
)
volume = driver.ex_describe_volume(
    ex_href='https://api.profitbricks.com/cloudapi/v4/datacenters/<data-center-id>/volumes/<volume-id>'
)

detached = driver.attach_volume(node=node, volume=volume)

Destroy a Volume

The following snippet destroys a volume:

volume = driver.ex_describe_volume(
    ex_href='https://api.profitbricks.com/cloudapi/v4/datacenters/<data-center-id>/volumes/<volume-id>'
)
destroyed = driver.destroy_volume(volume=volume)

Note: This operation is permanent and deleted volumes cannot be recovered.

List Snapshots

The following code will list all volume snapshots:

snapshots = driver.list_snapshots()

Create a Volume Snapshot

The following code will create a snapshot of a volume:

volume = driver.ex_describe_volume(
    ex_href='https://api.profitbricks.com/cloudapi/v4/datacenters/<data-center-id>/volumes/<volume-id>'
)
snapshot = driver.create_volume_snapshot(volume=volume)

Destroy a Volume Snapshot

The following code will destroy a volume snapshot:

snapshot = driver.ex_describe_snapshot(
    ex_href='https://api.profitbricks.com/cloudapi/v4/snapshots/<snapshot-id>'
)
deleted = driver.ex_destroy_snapshot(snapshot=snapshot)

Exploring extended ProfitBricks functionality

Data center extension functions

How to: List Data Centers

The following code will get a list of all data centers associated with your account:

data_centers = driver.ex_list_datacenters()

Create a Data Center

The following code creates a new virtual data center:

karlsruhe = driver.ex_describe_location(ex_location_id='de/fkb')
lasvegas = driver.ex_describe_location(ex_location_id='us/las')

new_datacenter = driver.ex_create_datacenter(
    name='My Data Center',
    location=karlsruhe
)
another_new_datacenter = driver.ex_create_datacenter(
    name='Another Data Center',
    location=lasvegas,
    description='Another new data center.'
)

Fetch a Data Center

The following code fetches a virtual data center by its href attribute along with its metadata, properties and entities:

data_center = driver.ex_describe_datacenter(
    ex_href='https://api.profitbricks.com/cloudapi/v4/datacenters/<data-center-id>'
)

The following code fetches a virtual data center by its ID:

data_center = driver.ex_describe_datacenter(
    ex_datacenter_id=<data-center-id>
)

Rename a Data Center

The following code renames a virtual data center:

data_center = driver.ex_describe_datacenter(
    ex_href='https://api.profitbricks.com/cloudapi/v4/datacenters/<data-center-id>'
)
renamed_data_center = driver.ex_rename_datacenter(name='New Datacenter Name')

Delete a Data Center

The following code deletes a data center:

data_center = driver.ex_describe_datacenter(
    ex_href='https://api.profitbricks.com/cloudapi/v4/datacenters/<data-center-id>'
)
deleted = driver.ex_delete_datacenter(datacenter=datacenter)

Node (server) extension functions

How to: Fetch a server

The following code fetches a single server by its href attribute:

server = driver.ex_describe_node(
    ex_href='https://api.profitbricks.com/cloudapi/v4/datacenters/<data-center-id>/servers/<server-id>'
)

The following code fetches a single server by its ID:

server = driver.ex_describe_node(
    ex_datacenter_id='<data-center-id>',
    ex_node_id='<server-id>'
)

Update a Server

The following code updates a server:

server = driver.ex_describe_node(
    ex_href='https://api.profitbricks.com/cloudapi/v4/datacenters/<data-center-id>/servers/<server-id>'
)

updated_server = driver.ex_update_node(
    node=node,
    name='Updated Node',
    cores=4,
    ram=8192,
    availability_zone='AUTO',
    ex_cpu_family='AMD_OPTERON'
)

Start and Stop a Server

The following code snippets start and stop a server:

server = driver.ex_describe_node(
    ex_href='https://api.profitbricks.com/cloudapi/v4/datacenters/<data-center-id>/servers/<server-id>'
)
started = driver.ex_start_node(node=node)
stopped = driver.ex_stop_node(node=node)

List Attached Volumes

The following code snipped will return a list of volumes attached to a server

server = driver.ex_describe_node(
    ex_href='https://api.profitbricks.com/cloudapi/v4/datacenters/<data-center-id>/servers/<server-id>'
)
volumes = driver.ex_list_attached_volumes(node=node)

Image extension functions

How to: Fetch an image

The following code will fetch a single image by its href attribute:

image = driver.ex_describe_image(
    ex_href='https://api.profitbricks.com/cloudapi/v4/images/<image-id>'
)

The following code will fetch a single image by its ID:

image = driver.ex_describe_image(
    ex_image_id='<image-id>'
)

Update an Image

The following code will update a private image:

image = driver.ex_describe_image(
    ex_href='https://api.profitbricks.com/cloudapi/v4/images/<image-id>'
)
updated_image = driver.ex_update_image(
    image=image,
    name='Updated image name',
    description='Updated image description',
    ram_hot_plug=True,
    disc_scsi_hot_plug=True
)

Delete an Image

The following code will delete a private image:

image = driver.ex_describe_image(
    ex_href='https://api.profitbricks.com/cloudapi/v4/images/<image-id>'
)
deleted = driver.ex_delete_image(image=image)

Notes on the above examples

  • Creating private images will be supported when the API supports this functionality.
  • All operations on editing images can only be carried out on your own private images. You cannot make changes to the public images that ProfitBricks makes available.

Volume extension functions

How to: Fetch a volume

The following code illustrates how to fetch a single volume by its href attribute:

volume = driver.ex_describe_volume(
    ex_href='https://api.profitbricks.com/cloudapi/v4/datacenters/<data-center-id>/volumes/<volume-id>'
)

The following code illustrates how to fetch a single volume by its ID:

volume = driver.ex_describe_volume(
    ex_datacenter_id='<data-center-id>'
    ex_volume_id='<volume-id>'
)

Update a Volume

The following code snippets updates a volumes name and size:

volume = driver.ex_describe_volume(
    ex_href='https://api.profitbricks.com/cloudapi/v4/datacenters/<data-center-id>/volumes/<volume-id>'
)
updated_volume = driver.ex_update_volume(
    volume=volume,
    storage_name='Updated Storage',
    size=120
)

The following code snippet changes a volumes bus type:

second_update = driver.ex_update_volume(
    volume=volume,
    ex_bus_type='IDE'
)

Restore a Snapshot

The following code snippet restores a snapshot to a volume:

snapshot = driver.ex_describe_snapshot(
    ex_href='https://api.profitbricks.com/cloudapi/v4/snapshots/<snapshot-id>'
)
volume = driver.ex_describe_volume(
    ex_href=href='https://api.profitbricks.com/cloudapi/v4/datacenters/<data-center-id>/volumes/<volume-id>'
)
restored = driver.ex_restore_snapshot(
    volume=volume,
    snapshot=snapshot
)

Notes on the above examples

  • Volume sizes cannot be decreased. Attempting to do so will cause the request to fail.
  • Changing a volume may require a reboot of the node it is attached to.

Snapshot extension functions

How to: Fetch a snapshot

The following code snippet fetches an existing snapshot by its href attribute:

snapshot = driver.ex_describe_snapshot(
    ex_href='https://api.profitbricks.com/cloudapi/v4/snapshots/<snapshot-id>'
)

The following code snippet fetches an existing snapshot by its ID:

snapshot = driver.ex_describe_snapshot(
    ex_snapshot_id='<snapshot-id>'
)

Update a Snapshot

The following code snippet updates a snapshot:

snapshot = driver.ex_describe_snapshot(
    ex_href='https://api.profitbricks.com/cloudapi/v4/snapshots/<snapshot-id>'
)
updated_snapshot = driver.ex_update_snapshot(
    snapshot=snapshot,
    name='Updated Snapshot',
    cpu_hot_plug=True
)

Network interface extension functions

How to: List network interfaces

The following code snippet lists all network interfaces associated with your account:

network_interfaces = driver.ex_list_network_interfaces()

Fetch a NIC

This code snippet fetches a single network interface by its href attribute:

network_interface = driver.ex_describe_network_interface(
    ex_href='https://api.profitbricks.com/cloudapi/v4/datacenters/<datacenter-id>/servers/<server-id>/nics/<nic_id>'
)

This code snippet fetches a single network interface by its ID:

network_interface = driver.ex_describe_network_interface(
    ex_datacenter_id='<datacenter-id>',
    ex_server_id='<server-id>',
    ex_nic_id='<nic_id>'
)

Create a NIC

This code snippet creates a basic network interface and attaches it to a node:

server = driver.ex_describe_node(
    ex_href='https://api.profitbricks.com/cloudapi/v4/datacenters/<data-center-id>/servers/<server-id>'
)
network_interface = driver.ex_create_network_interface(node=server)

This code snippet creates a network interface, attaches it to a node and assigns it to a local area network in a data center:

server = driver.ex_describe_node(
    ex_href='https://api.profitbricks.com/cloudapi/v4/datacenters/<data-center-id>/servers/<server-id>'
)
network_interface = driver.ex_create_network_interface(
    node=server,
    lan_id=1
)

Update a NIC

This code snippet will reassign a network interface to a different local area network within a virtual data center:

network_interface = driver.ex_describe_network_interface(
    ex_href='https://api.profitbricks.com/cloudapi/v4/datacenters/<datacenter-id>/servers/<server-id>/nics/<nic_id>'
)
updated_network_interface = driver.ex_update_network_interface(
    network_interface=network_interface,
    lan_id=3
)

This code snippet will set DHCP to false for a NIC:

network_interface = driver.ex_describe_network_interface(
    ex_href='https://api.profitbricks.com/cloudapi/v4/datacenters/<datacenter-id>/servers/<server-id>/nics/<nic_id>'
)
updated_network_interface = driver.ex_update_network_interface(
    network_interface=network_interface,
    dhcp_active=False
)

Delete a NIC

This code snippet will delete a network interface from a node:

network_interface = driver.ex_describe_network_interface(
    ex_href='https://api.profitbricks.com/cloudapi/v4/datacenters/<datacenter-id>/servers/<server-id>/nics/<nic_id>'
)
deleted = driver.ex_destroy_network_interface(
    network_interface=network_interface
)

Firewall rule extension functions

How to: List firewall rules for a network interface

The following code snippet lists firewall rules for a network interface:

network_interface = driver.ex_describe_network_interface(
    ex_href='https://api.profitbricks.com/cloudapi/v4/datacenters/<datacenter-id>/servers/<server-id>/nics/<nic_id>'
)
firewall_rules = driver.ex_list_firewall_rules(network_interface)

Fetch a Firewall Rule

The following code snippet fetches a single firewall rule by its href attribute:

firewall_rule = driver.ex_describe_firewall_rule(
    ex_href='https://api.profitbricks.com/cloudapi/v4/datacenters/<datacenter-id>/servers/<server-id>/nics/<nic_id>/firewallrules/<firewall-rule-id>'
)

The following code snippet fetches a single firewall rule by its ID:

firewall_rule = driver.ex_describe_firewall_rule(
    ex_datacenter_id='<data-center-id>',
    ex_server_id='<server-id>',
    ex_nic_id='<nic-id>',
    ex_firewall_rule_id='<firewall-rule-id>'
)

Create a Firewall Rule

The following code snippet creates a firewall rule to expose port 80 (HTTP) for a network interface:

network_interface = driver.ex_describe_network_interface(
    ex_href='https://api.profitbricks.com/cloudapi/v4/datacenters/<datacenter-id>/servers/<server-id>/nics/<nic_id>'
)

firewall_rule = driver.ex_create_firewall_rule(
    network_interface=network_interface,
    protocol='TCP',
    name='Expose HTTP',
    port_range_start=80,
    port_range_end=80
)

The following code snippet creates a firewall rule to expose port 443 (HTTPS) for a network interface:

network_interface = driver.ex_describe_network_interface(
    ex_href='https://api.profitbricks.com/cloudapi/v4/datacenters/<datacenter-id>/servers/<server-id>/nics/<nic_id>'
)

firewall_rule = driver.ex_create_firewall_rule(
    network_interface=network_interface,
    protocol='TCP',
    name='Expose HTTP',
    port_range_start=443,
    port_range_end=443
)

Update a Firewall Rule

The following code snippet will update a firewall rule:

firewall_rule = driver.ex_describe_firewall_rule(
    ex_href='https://api.profitbricks.com/cloudapi/v4/datacenters/<datacenter-id>/servers/<server-id>/nics/<nic_id>/firewallrules/<firewall-rule-id>'
)
updated = driver.ex_update_firewall_rule(
    firewall_rule=firewall_rule,
    name='Updated firewall rule',
    port_range_start=1024,
    port_range_end=1024
)

Delete a Firewall Rule

The following code snippet will delete a firewall rule:

firewall_rule = driver.ex_describe_firewall_rule(
    ex_href='https://api.profitbricks.com/cloudapi/v4/datacenters/<datacenter-id>/servers/<server-id>/nics/<nic_id>/firewallrules/<firewall-rule-id>'
)
deleted = driver.ex_delete_firewall_rule(
    firewall_rule=firewall_rule
)

Local area network extension functions

How to: List local area networks

This code snippet will list all local area networks within a data center:

data_center = driver.ex_describe_datacenter(
    ex_href='https://api.profitbricks.com/cloudapi/v4/datacenters/<data-center-id>'
)
local_area_networks = driver.ex_list_lans(
    datacenter=data_center
)

Fetch a LAN

This code snippet will fetch a single LAN by its href attribute:

local_area_network = driver.ex_describe_lan(
    ex_href='https://api.profitbricks.com/cloudapi/v4/datacenters/<data-center-id>/lans/<lan-id>'
)

This code snippet will fetch a single LAN by its ID:

local_area_network = driver.ex_describe_lan(
    ex_datacenter_id='<data-center-id>',
    ex_lan_id='<lan-id>'
)

Create a LAN

This code snippet will create a LAN for a virtual data center:

data_center = driver.ex_describe_datacenter(
    ex_href='https://api.profitbricks.com/cloudapi/v4/datacenters/<data-center-id>'
)
created_local_area_network = driver.ex_create_lan(
    datacenter=data_center
)

This code snippet will create a LAN for a virtual data center and configure it with internet access:

data_center = driver.ex_describe_datacenter(
    ex_href='https://api.profitbricks.com/cloudapi/v4/datacenters/<data-center-id>'
)
created_local_area_network = driver.ex_create_lan(
    datacenter=data_center,
    is_public=True
)

This code snippet will create a LAN for a virtual data center and assign it some NICs:

data_center = driver.ex_describe_datacenter(
    ex_href='https://api.profitbricks.com/cloudapi/v4/datacenters/<data-center-id>'
)

network_interfaces = driver.ex_list_network_interfaces()
to_assign = network_interfaces[1:3]

created_local_area_network = driver.ex_create_lan(
    datacenter=data_center,
    is_public=True,
    nics=to_assign
)

Update a LAN

The following code snippet will update the name for a LAN and take it online:

local_area_network = driver.ex_describe_lan(
    ex_href='https://api.profitbricks.com/cloudapi/v4/datacenters/<data-center-id>/lans/<lan-id>'
)
updated = driver.ex_update_lan(
    name='Internet Access',
    is_public=True
)

Delete a LAN

The following code snippet will delete a LAN:

local_area_network = driver.ex_describe_lan(
    ex_href='https://api.profitbricks.com/cloudapi/v4/datacenters/<data-center-id>/lans/<lan-id>'
)
deleted = driver.ex_delete_lan(lan=local_area_network)

Load balancer extension functions

How to: List load balancers

The following code snippet will list all load balancers associated with your account:

load_balancers = driver.ex_list_load_balancers()

Fetch a Load Balancer

The following code snippet will fetch a single load balancer by its href:

load_balancer = driver.ex_describe_load_balancer(
    ex_href='https://api.profitbricks.com/cloudapi/v4/datacenters/<data-center-id>/loadbalancers/<loadbalancer-id>'
)

The following code snippet will fetch a single load balancer by its ID:

load_balancer = driver.ex_describe_load_balancer(
    ex_datacenter_id='<data-center-id>',
    ex_load_balancer_id='<loadbalancer-id>'
)

Create a Load Balancer

The following code snippet will create a new load balancer for a virtual data center:

data_center = driver.ex_describe_datacenter(
    ex_href='https://api.profitbricks.com/cloudapi/v4/datacenters/<data-center-id>'
)
new_load_balancer = driver.ex_create_load_balancer(
    datacenter=data_center,
    name='Load Balancer'
)

The following code snippet will create a new load balancer for a virtual data center, assigning it an IP address and attaching some network interface cards:

data_center = driver.ex_describe_datacenter(
    ex_href='https://api.profitbricks.com/cloudapi/v4/datacenters/<data-center-id>'
)
nics = driver.ex_list_nics()
to_assign = nics[1:3]

new_load_balancer = driver.ex_create_load_balancer(
    datacenter=data_center,
    name='Load Balancer',
    ip='10.2.3.4',
    nics=to_assign
)

Update a Load Balancer

The following code snippet will update a load balancer:

load_balancer = driver.ex_describe_load_balancer(
    ex_href='https://api.profitbricks.com/cloudapi/v4/datacenters/<data-center-id>/loadbalancers/<loadbalancer-id>'
)
updated = driver.ex_update_load_balancer(
    load_balancer=load_balancer,
    name='Updated load balancer',
    dhcp=True
)

List Load Balanced NICs

The following code snippet will list the load balanced NICs that are members of a specific load balancer:

load_balancer = driver.ex_describe_load_balancer(
    ex_href='https://api.profitbricks.com/cloudapi/v4/datacenters/<data-center-id>/loadbalancers/<loadbalancer-id>'
)
load_balanced_nics = driver.ex_list_load_balanced_nics(
    load_balancer=load_balancer
)

Fetch a Load Balanced NIC

See fetching a network interface controller

Attach a NIC to a Load Balancer

The following code snippet demonstrates attaching a NIC to a load balancer:

network_interface = driver.ex_describe_network_interface(
    ex_href='https://api.profitbricks.com/cloudapi/v4/datacenters/<datacenter-id>/servers/<server-id>/nics/<nic_id>'
)
load_balancer = driver.ex_describe_load_balancer(
    ex_href='https://api.profitbricks.com/cloudapi/v4/datacenters/<data-center-id>/loadbalancers/<loadbalancer-id>'
)

attached = driver.ex_attach_nic_to_load_balancer(
    network_interface=network_interface,
    load_balancer=load_balancer
)

Detach a NIC from a Load Balancer

The following code snippet demonstrates detaching a NIC from a load balancer:

network_interface = driver.ex_describe_network_interface(
    ex_href='https://api.profitbricks.com/cloudapi/v4/datacenters/<datacenter-id>/servers/<server-id>/nics/<nic_id>'
)
load_balancer = driver.ex_describe_load_balancer(
    ex_href='https://api.profitbricks.com/cloudapi/v4/datacenters/<data-center-id>/loadbalancers/<loadbalancer-id>'
)

detached = driver.ex_detach_nic_from_load_balancer(
    network_interface=network_interface,
    load_balancer=load_balancer
)

IP Block extension functions

How to: List IP blocks

The following code snippet lists all IP blocks:

ip_blocks = driver.ex_list_ip_blocks()

Fetch an IP Block

The following code snippet fetches a single IP block by its href attribute:

ip_block = driver.ex_describe_ip_block(
    ex_href='https://api.profitbricks.com/cloudapi/v4/ipblocks/<ip-block-id>'
)

The following code snippet fetches a single IP block by its ID:

ip_block = driver.ex_describe_ip_block(
    ex_ip_block_id='<ip-block-id>'
)

Create an IP Block

The following code snippet creates an IP block:

location = driver.ex_describe_location(ex_location_id='us/las')
ip_block = driver.ex_create_ip_block(
    location=location,
    size=2,
    name='New IP block'
)

Delete an IP block

The following code snippet deletes an IP block:

ip_block = driver.ex_describe_ip_block(
    ex_href='https://api.profitbricks.com/cloudapi/v4/ipblocks/<ip-block-id>'
)
deleted = driver.ex_delete_ip_block(ip_block=ip_block)

See also

References and other information

You can also engage with us in the community section of this website and we'll be happy to answer any questions you might have.