Apache Libcloud

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

The Cloud API can be accessed using an instance running in a ProfitBricks virtual data center or directly from a local client over the Internet from any application that can send a HTTPS request and receive a 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 create, read, update, and delete (CRUD) operations on these entities 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 virtual data centers and servers. Currently there are three locations available:

  • Frankfurt, Germany
  • Karlsruhe, Germany
  • Las Vegas, 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 different selection of images.

This driver also supports updates and deletes, but only to private images. It does not support creating private images.

Virtual Data Centers

Within the ProfitBricks environment, a virtual data center is a virtual representation of a physical data center and encompasses a list of local area networks, load balancers, servers, and volumes all grouped togther in a walled environment. Servers within the same virtual data center can talk to each other and connect to volumes.

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 network interface card(s). Servers can be attached to and detached from volumes and local area networks.

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 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

A server can have one or more network interface cards attached to it. These can then be attached to load balancers and local area networks in turn. Network interface cards can be added to servers, updated, and destroyed.

Local area networks

A virtual data center can have any number of local area networks assigned to it. Each local area network in turn can have network interface cards assigned to them, which are also assigned to servers.

Local area networks can be added, updated, and destroyed.

Firewall 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

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

IP blocks

A virtual data center can have any number of IP blocks assigned to it. An IP block is a list of reserved static IP addresses that can be assigned to resources inside the virtual data center. IP blocks can be created, updated, and deleted.

Getting Started

Before you begin you will need to have signed-up for a ProfitBricks account. The credentials you setup during sign-up will be used to authenticate against the Cloud 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 configured during sign-up. It is generally an e-mail address. Your 'password' is the one you specified during sign-up.

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

Examples of using the driver

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.

Core Functionality

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

How to: List locations

The following code snippet will list all ProfitBricks locations:

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

How to: 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']

How to: List sizes

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

sizes = driver.list_sizes()

How to: List nodes

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

nodes = driver.list_nodes()

How to: 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 above 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 virtual data center location will be determined based on the image or the volume specified.

  • 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.

How to: 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/v3/datacenters/<data-center-id>/servers/<server-id>'
)
rebooted = driver.reboot_node(node=node)

How to: Destroy a node

The following code snippet will delete a node:

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

How to: List volumes

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

volumes = driver.list_volumes()

How to: 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 a 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 gigabytes (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 either SSH keys or a password. It may be created with both SSH keys and a password.

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

How to: 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/v3/datacenters/<data-center-id>/servers/<server-id>'
)
volume = driver.ex_describe_volume(
    ex_href='https://api.profitbricks.com/cloudapi/v3/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.

How to: 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/v3/datacenters/<data-center-id>/servers/<server-id>'
)
volume = driver.ex_describe_volume(
    ex_href='https://api.profitbricks.com/cloudapi/v3/datacenters/<data-center-id>/volumes/<volume-id>'
)

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

How to: Destroy a volume

The following snippet destroys a volume:

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

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

How to: List snapshots

The following code will list all volume snapshots:

snapshots = driver.list_snapshots()

How to: Create a volume snapshot

The following code will create a snap shot of a volume:

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

How to: Destroy a volume snapshot

The following code will destroy a volume snapshot:

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

Extended ProfitBricks functionality

Virtual 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()

How to: Create a Data Center

The following code creates a new 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.'
)

How to: Fetch a Data Center

The following code fetches a 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/v3/datacenters/<data-center-id>'
)

The following code fetches a data center by its ID:

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

How to: Rename a Data Center

The following code renames a data center:

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

How to: Delete a Data Center

The following code deletes a data center:

data_center = driver.ex_describe_datacenter(
    ex_href='https://api.profitbricks.com/cloudapi/v3/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/v3/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>'
)

How to: Update server cores, memory, and disk

The following code updates a server:

server = driver.ex_describe_node(
    ex_href='https://api.profitbricks.com/cloudapi/v3/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'
)

How to: 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/v3/datacenters/<data-center-id>/servers/<server-id>'
)
started = driver.ex_start_node(node=node)
stopped = driver.ex_stop_node(node=node)

How to: List volumes attached to a server

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/v3/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/v3/images/<image-id>'
)

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

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

How to: Update a private image

The following code will update an image

image = driver.ex_describe_image(
    ex_href='https://api.profitbricks.com/cloudapi/v3/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
)

How to: Delete a private image

image = driver.ex_describe_image(
    ex_href='https://api.profitbricks.com/cloudapi/v3/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.

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/v3/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>'
)

How to: 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/v3/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'
)

How to: Restore a volume snapshot

The following code snippet restores a snapshot to a volume:

snapshot = driver.ex_describe_snapshot(
    ex_href='https://api.profitbricks.com/cloudapi/v3/snapshots/<snapshot-id>'
)
volume = driver.ex_describe_volume(
    ex_href=href='https://api.profitbricks.com/cloudapi/v3/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 an attached volume may result in needing to reboot the node.

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/v3/snapshots/<snapshot-id>'
)

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

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

How to: Update a snapshot

The following code snippet updates a snapshot:

snapshot = driver.ex_describe_snapshot(
    ex_href='https://api.profitbricks.com/cloudapi/v3/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()

How to: Fetch a network interface

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/v3/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>'
)

How to: Create a network interface

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/v3/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/v3/datacenters/<data-center-id>/servers/<server-id>'
)
network_interface = driver.ex_create_network_interface(
    node=server,
    lan_id=1
)

How to: Update a network interface

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

network_interface = driver.ex_describe_network_interface(
    ex_href='https://api.profitbricks.com/cloudapi/v3/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 network interface:

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

How to: Delete a network interface

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/v3/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/v3/datacenters/<datacenter-id>/servers/<server-id>/nics/<nic_id>'
)
firewall_rules = driver.ex_list_firewall_rules(network_interface)

How to: 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/v3/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>'
)

How to: 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/v3/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/v3/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
)

How to: 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/v3/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
)

How to: 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/v3/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/v3/datacenters/<data-center-id>'
)
local_area_networks = driver.ex_list_lans(
    datacenter=data_center
)

How to: Fetch a local area network

This code snippet will fetch a single local area network by its href attribute:

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

This code snippet will fetch a single local area network by its ID:

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

How to: Create a local area network

This code snippet will create a local area network for a data center:

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

This code snippet will create a local area network for a data center and set it up to have internet access:

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

This code snippet will create a local area network for a data center and assign it some network interface controllers:

data_center = driver.ex_describe_datacenter(
    ex_href='https://api.profitbricks.com/cloudapi/v3/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
)

How to: Update a local area network

The following code snippet will update the name for a local area network and take it online:

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

How to: Delete a local area network

The following code snippet will delete a local area network:

local_area_network = driver.ex_describe_lan(
    ex_href='https://api.profitbricks.com/cloudapi/v3/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()

How to: 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/v3/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>'
)

How to: Create a load balancer

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

data_center = driver.ex_describe_datacenter(
    ex_href='https://api.profitbricks.com/cloudapi/v3/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 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/v3/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
)

How to: 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/v3/datacenters/<data-center-id>/loadbalancers/<loadbalancer-id>'
)
updated = driver.ex_update_load_balancer(
    load_balancer=load_balancer,
    name='Updated load balancer',
    dhcp=True
)

How to: List load balanced network interface controllers

The following code snippet will list load balanced network interface controllers for a load balancer:

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

How to: Fetch a load balanced network interface controller

See fetching a network interface controller

How to: Attach a network interface controller to a load balancer

The following code snippet demonstrates attaching a network interface to a load balancer:

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

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

How to: Detach a network interface controller from a load balancer

The following code snippet demonstrates detaching a network interface from a load balancer:

network_interface = driver.ex_describe_network_interface(
    ex_href='https://api.profitbricks.com/cloudapi/v3/datacenters/<datacenter-id>/servers/<server-id>/nics/<nic_id>'
)
load_balancer = driver.ex_describe_load_balancer(
    ex_href='https://api.profitbricks.com/cloudapi/v3/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()

How to: 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/v3/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>'
)

How to: 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'
)

How to: 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/v3/ipblocks/<ip-block-id>'
)
deleted = driver.ex_delete_ip_block(ip_block=ip_block)

References