ProfitBricks Client documentation¶
Contents:
Introduction¶
The ProfitBricks client can be used to manage your data centers through ProfitBricks’ public API.
It comes as a command line tool with Bash completion and as a profitbricks_client
Python
module. You can use the profitbricks_client
Python module to write your own scripts or to
interactively use it with tools like ipython.
Installation¶
Installing ProfitBricks client is simple with pip, just run this in your terminal:
pip install git+git://github.com/profitbricks/profitbricks-client
Quick start¶
Call
profitbricks-client --help
to get a help message. Then you probably want to know which API calls are available:
profitbricks-client --list
You will be asked for your username and password. The password will be stored in your keyring (if you have the keyring Python module installed). Then you might want to know how to list all your data centers:
profitbricks-client getAllDataCenters --help
This call takes no arguments. To list all your datacenters, run:
profitbricks-client getAllDataCenters
Then you are interested in getting one of your data centers by specifying one of your data centers:
profitbricks-client getDataCenter --dataCenterId <dataCenterId>
Python Module example¶
Imagine you want to write a small Python script that shows the list of IP
addresses for every server. You can walk through this example step by step with
ipython. First you need to import the profitbricks_client
and create a
client object:
import profitbricks_client
client = profitbricks_client.get_profitbricks_client(api_version='1.3')
Then you can retrieve a list of all data centers by calling
getAllDataCenters()
. You get the ID, name, and version for every data
center. You can iterate over all data center IDs or just select the ones that
you are interested in. To keep this example simple, you just take the first
one. For the following example, you just want to have the unique IDs.
all_datacenters = client.getAllDataCenters()
datacenter_id = all_datacenters[0].dataCenterId
The function getDataCenter()
gives you all information about your data
center including all servers, storages, load balancers and so on. So let’s call
this function for your selected data center:
datacenter = client.getDataCenter(dataCenterId=datacenter_id)
You can print the name of the data center:
print datacenter.dataCenterName + ':'
The returned datacenter object has an attribute servers which contains a list of all servers in that data center. Every server in this list has a serverName and a list of ips if this server has any IP assigned to it. (Shutdown servers may not have a ips property.) You can sort the servers list by the serverName attribute and then print the server name with the IPs belonging to it (excluding servers without IPs):
from operator import attrgetter
servers = sorted(datacenter.servers, key=attrgetter('serverName'))
for server in [s for s in servers if hasattr(s, 'ips')]:
print server.serverName + ' '+ ' '.join(server.ips)
The full script to print all servers and their IP addresses for every data center look like this:
#!/usr/bin/python
# Copyright (C) 2014, ProfitBricks GmbH
# Authors: Benjamin Drung <benjamin.drung@profitbricks.com>
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
from operator import attrgetter
import profitbricks_client
def main():
client = profitbricks_client.get_profitbricks_client(api_version='1.3')
datacenter_ids = [dc.dataCenterId for dc in client.getAllDataCenters()]
for datacenter_id in datacenter_ids:
datacenter = client.getDataCenter(dataCenterId=datacenter_id)
print datacenter.dataCenterName + ':'
servers = sorted(datacenter.servers, key=attrgetter('serverName'))
for server in [s for s in servers if hasattr(s, 'ips')]:
print server.serverName + ' ' + ' '.join(server.ips)
print
if __name__ == '__main__':
main()