You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This Groovy SDK provides a convenient way to use the ProfitBricks Cloud API from JVM based applications.
This guide will show you how to programmatically perform infrastructure management operations with it.
Design Objective
There is essentially only one usage pattern: Create an entity, then invoke one of the CRUD methods on it:
create(): Effectively send a resource creation REST request (HTTP CREATE).
read(): Fetch an existing REST resource by its UUID (HTTP GET).
update(): Send an update to an existing REST resource (HTTP PUT).
delete(): Delete an existing resource (HTTP DELETE).
all: This property implements a list operation to fetch UUIDs of existing REST resources.
An entity must be sufficiently populated in order for the associated REST request to succeed. No extra validation will take place. If there is an error, you will see an exception with the cause.
For resources that exist within the context of another resource, that other resource must also be properly instantiated and injected.
For example: to successfully operate on a network interface, the entity (NIC) must also contain proper Server and LAN instances, while in turn the Server and LAN instances must contain a proper DataCenter instance (which needs just a valid id).
No extra modeling was made (e.g. no parent links and in total ignorance about the depth API parameter). The entities just enclose the transported JSON representations.
On a successfully-delivered POST, PUT or DELETE request, the Profitbricks API might send a Location header instead of the final resulting response to the requested action. This SDK will block on such a response, then continue polling for the final result. If a successful result does not come in time, an exception will be thrown.
This was made to allow for easy and agile API scripting, as the result matters most of the time, and such a behavior would be needed anyway.
For API resources which do not fit into the CRUD scheme, an extra command facade is provided. This provides functionality like attaching and detaching storage volumes to virtual servers or associating network interfaces with load balancers.
This implementation is based on Groovy 2, so it technically runs on any JVM version 7 or later.
Before you begin you will need to have signed up for a ProfitBricks account. The credentials you set up during the sign-up process must be used to authenticate against the Cloud API.
One way to configure the API client is to use system properties. The following table lists all those properties along with their default value (if any).
System Properties
name
default
notes
com.profitbricks.sdk.verifySSL
true
set to false to ignore SSL certificate verification issues, see also note below
com.profitbricks.sdk.user
-
the API user name for basic authentication. required
com.profitbricks.sdk.password
-
the API password for basic authentication. required
com.profitbricks.sdk.wait.init.milliseconds
100
if waiting for success, this is the initial time period between two checks.
com.profitbricks.sdk.wait.timeout.seconds
120
if waiting for success, this is the timeout.
com.profitbricks.sdk.wait.max.milliseconds
1500
if waiting for success, this is the maximum time period between two checks.
com.profitbricks.sdk.wait.factor
1.87
if waiting for success, this is the factor by which the current time period value is multiplied.
Individual configuration values can also be overridden with each individual request.
All CRUD (and list) methods as well as all commands can be invoked with an optional map as the last parameter. The keys in this map are expected to be named like the corresponding system property minus the prefix com.profitbricks.sdk..
Note:verifySSL can only be configured via system property before making the first API call and not be overridden.
// for example
datacenter.create(user: 'otheruser', password: 'otherpassword', 'wait.factor': Math.PI/2)
// or for read operationsassert userID :'user id missing'newUser().read(userID, [password: 'mypassword'])
SDK Reference
This section provides details on all the available operations and the parameters they accept. Also included are brief code snippets illustrating its usage.
datacenters
Virtual data centers (VDCs) are the foundation of the ProfitBricks platform. VDCs act as logical containers for all other objects you will be creating, e.g., servers. You can provision as many VDCs as you want. VDCs have their own private network and are logically isolated from each other.
list datacenters
Lists the ids of all currently provisioned datacenters that are accessible for the current user.
assert datacenter :'datacenter missing!'assert serverId :'server id missing!'Server server =newServer(id: serverId, dataCenter: datacenter).read()
assert server :'no such server!'
Create a Server
Argument
Required
Description
DataCenter::id
yes
Server::name
yes
Server::cores
yes
number of cores
Server::ram
yes
amount of memory in MB, size must be specified in multiples of 256
Server::availabilityZone
no
fire zone (see table below)
Server::cpuFamily
no
CPU family: AMD_OPTERON or INTEL_XEON default: AMD_OPTERON
Removes a server from a data center. NOTE: This will not automatically remove the storage volume(s) attached to a server. A separate operation is required to delete a storage volume.
Argument
Required
DataCenter::id
yes
Server::id
yes
assert server :'server missing!'assert server.delete() :'server deletion failed!'
list attached volumes
Retrieves a list of ids of all volumes currently attached to a given server.
Forces a hard reboot of the server. Do not use this method if you want to gracefully reboot the machine. This is the equivalent of powering off the machine and turning it back on.
Argument
Required
DataCenter::id
yes
Server::id
yes
assert server :'server missing!'assertCommands.reboot(server) :'server reboot failed!'
start a server
Starts a server. If the server's public IP address was deallocated then a new IP address will be assigned.
Argument
Required
DataCenter::id
yes
Server::id
yes
assert server :'server missing!'assertCommands.start(s) :'server start failed!'
stop a server
Stops a server. The machine will be forcefully powered off, billing will stop, and the public IP address, if one is allocated, will be deallocated.
Argument
Required
DataCenter::id
yes
Server::id
yes
assert server :'server missing!'assertCommands.stop(s) :'server stop failed!'
Images
list images
Retrieves a list of ids of all available images (CDROMs).
assert dc :'datacenter missing!'Volume volume =newVolume(dataCenter: dc, id: volumeId).read()
assert volume :'no such volume!'
create a volume
Creates a volume within the virtual data center. This will not attach the volume to a server. Please see the Attach a Volume entry in the Server section for details on how to attach storage volumes.
Argument
Required
Description
DataCenter::id
yes
The ID of the VDC.
Volume::name
yes
The name of the volume.
Volume::size
yes
The size of the volume in GB.
Volume::bus
no
The bus type of the volume (VIRTIO or IDE). Default: VIRTIO.
Volume::image
no
The image or snapshot ID.
Volume::imageAlias
no
The alias of the image.
Volume::type
no
The volume type, HDD or SSD.
Volume::licenceType
no
The licence type of the volume. see table below
Volume::imagePassword
no
password for the root or administrative account, must consist of 8-50 alphanumeric characters
Volume::sshKeys
no
collection of SSH public keys
Volume::availabilityZone
no
availability zone the volume should reside in, see table below
Licence Type
Comment
WINDOWS2016
for Microsoft Windows Server 2016
WINDOWS
for Microsoft Windows Server 2008 and 2012
LINUX
for Linux
OTHER
for any volumes that do not match one of the other licence types
UNKNOWN
default value when you've uploaded an image and haven't set the license type
assert lan :'LAN missing!'assert lan.delete() :'LAN deletion failed!'
Network Interfaces
list NICs
Retrieves a list of NIC ids for a given server. This needs a valid Server instance.
Argument
Required
DataCenter::id
yes
Server::id
yes
assert server :'server missing!'List<String> nicIDs =new NIC(server: server).all
assert nicIDs :'no NICs for this server!'
retrieve a NIC
Argument
Required
DataCenter::id
yes
Server::id
yes
NIC::id
yes
assert server :'server missing!'assert nicID :'NIC id missing!'NIC nic =new NIC(server: s, id: nicID).read()
assert nic :'no such NIC for this server!'
create a NIC
Argument
Required
Description
DataCenter::id
yes
Server::id
yes
NIC::lan
yes
id of the LAN the NIC will be attached on (if that LAN does not exist it will be created)
NIC::name
no
NIC::ips
no
all IPs assigned to the NIC
NIC::dhcp
no
DHCP enabled? default: true
NIC::nat
no
if address translation should be performed
NIC::firewallActive
no
if there are active firewall rules for this NIC
assert server :'server missing!'assert lan :'LAN missing!'NIC nic =new NIC(server: server, lan: lan, nat: true).create()
assert nic.id :'NIC creation failed!'
assert nic :'NIC missing!'assert nic.delete() :'NIC deletion failed!'
Firewall Rules
list firewall rules
Retrieves a list of firewall rules associated with a particular NIC. Needs a valid NIC instance.
Argument
Required
DataCenter::id
yes
Server::id
yes
NIC::id
yes
assert nic :'NIC missing!'List<String> fwRuleIDs =newFirewallRule(nic: nic).all
assert fwRuleIDs :'no firewall rules for the given NIC!'
Retrieve a firewall rule
Argument
Required
DataCenter::id
yes
Server::id
yes
NIC::id
yes
FirewallRule::id
yes
assert nic :'NIC missing!'assert ruleID :'firewall rule id missing!'FirewallRule rule =newFirewallRule(nic: nic, id: ruleID).read()
assert rule :'no such firewall rule for this NIC!'
Create a firewall rule
Argument
Required
Description
DataCenter::id
yes
Server::id
yes
NIC::id
yes
FirewallRule::protocol
yes
one of TCP, UDP, ICMP or ANY
FirewallRule::name
no
FirewallRule::sourceMac
no
only allow connections from this MAC
FirewallRule::sourceIp
no
only allow connections from this IPv4 address
FirewallRule::targetIp
no
only allow connections to this IPv4 address
FirewallRule::portRangeStart
no
only allow connections for ports in the given range, this defines the range start
FirewallRule::portRangeEnd
no
only allow connections for ports in the given range, this defines the range end
Retrieves a list of ids of NICs associated with the load balancer. Needs valid DataCenter and LoadBalancer instances.
Argument
Required
DataCenter::id
yes
LoadBalancer::id
yes
assert loadbalancer :'load balancer missing!'List<String> nicIDs =Commands.associatedNICs(loadbalancer)
assert nicIDs :'no NICs associated with this load balancer!'
associate a NIC with a load balancer
Argument
Required
DataCenter::id
yes
LoadBalancer::id
yes
NIC::id
yes
assert loadbalancer :'load balancer missing!'assert nic :'NIC missing!'assertCommands.associate(loadbalancer, nic) :'nic to load balancer association failed!'
remove a NIC association
Argument
Required
DataCenter::id
yes
LoadBalancer::id
yes
NIC::id
yes
assert loadbalancer :'load balancer missing!'assert nic :'NIC missing!'assertCommands.dissociate(loadbalancer, nic) :'nic from load balancer dissociation failed!'
Users
list users
Retrieves a list of ids of all users for the current contract.
assert userGroup :'user group missing!'
userGroup.createDatacenter =falseassert userGroup.update() :'user group update failed!'
delete a user group
Argument
Required
UserGroup::id
yes
assert userGroup :'user group missing!'assert group.delete() :'user group deletion failed!'
list group users
Retrieves a list of ids of all users that are members of a group.
Argument
Required
UserGroup::id
yes
assert userGroup :'user group missing!'List<String> userIDs =Commands.userIDs(group)
assert userIDs :'no users in this group!'
add a user to a group
Argument
Required
UserGroup::id
yes
User::id
yes
assert userGroup :'user group missing!'assert user :'user missing!'assertCommands.assign(userGroup, user) :'assigning user to user group failed!'
remove a user from a group
Argument
Required
UserGroup::id
yes
User::id
yes
assert userGroup :'user group missing!'assert user :'user missing!'assertCommands.unassign(group, user) :'assigning user to user group failed!'
Contract Resources
Retrieve the current resource allocation statistics for this contract.
ContractStats stats =newContractStats().read()
assert stats :'retrieval of contract stats failed!'
A Concise Example
importcom.profitbricks.sdk.model.*import staticcom.profitbricks.sdk.Commands.*// create a datacenterDataCenter dc =newDataCenter(name: "Example DC", location: 'de/fkb', description: 'desc').create()
// create a LANLAN lan =new LAN(dataCenter: dc, name: "public lan", _public: true).create()
// create a serverServer server =newServer(dataCenter: dc, name: "Example server", cores: 1, ram: 1024).create()
// add a NIC to serverNIC nic =new NIC(server: server, lan: lan, name: "example nic").create()
assert nic.ips
// find a linux image to attach to serverImage image =newImage().all.collect{image.read(it) asImage}.findAll {
it._public &&
it.location == dc.location &&
it.licenceType =~/(?i)linux/&&
it.imageType =~/(?i)hdd/
}.first()
// create a volume from that imageVolume volume =newVolume(dataCenter: dc, name: "OS volume", image: image.id, imagePassword: 'test1234').create()
// attach volume to server
attach server, volume
// delete datacenter
dc.delete()
TODO
Having proper life cycle control over your entities is helpful in the long run. The current approach to query for /request resources should be replaced by a future/promise based mechanism.
To circumvent hitting the rate limit there should be a way to indicate slowing down depending on the value of the X-RateLimit-Remaining and X-RateLimit-Burst response headers.
Support
You can engage with us in the ProfitBricks DevOps Central community, there we'll gladly answer any questions you might have about this SDK or the Profitbricks Cloud API.