The ProfitBricks Client Library for Node.js provides you with access to the libprofitbricks REST API. The client library supports both simple and complex requests. It is designed for developers who are building applications in Node.js.
This guide will walk you through getting setup with the library and performing various actions against the API.
- Concepts
- Getting Started
- Installation
- Authenticating
- Using the Module
- Making JSON Objects (The Easy Way)
- Fallback Callbacks
- Reserve an Ipblock
- Create a Server with Two Nics.
- Build a datacenter and quickly add 37 servers
- How to: Create a Datacenter
- How to: Delete a Datacenter
- How to: Update Cores, Memory, and Disk
- How to: List Servers, Volumes, and Data Centers
- How to: Create Additional Network Interfaces
- Datacenter functions
- Firewall Rule functions
- Image functions
- Lan functions
- Loadbalancer functions
- Location functions
- Nic functions
- Request functions
- Server functions
- Server Commands
- Snapshot functions
- Volume functions
- Additional Documentation and Support
- Conclusion
The Node.js Client Library , libprofitbricks , wraps the latest version of the ProfitBricks REST API. All API operations are performed over SSL and authenticated using your libprofitbricks portal credentials. The API can be accessed within an instance running in libprofitbricks or directly over the Internet from any application that can send an HTTPS request and receive an HTTPS response.
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 API.
The Node.js Client Library is available on npm. You can install the latest stable version using npm:
npm install -g libprofitbricks
Done!
Connecting to ProfitBricks is handled by first setting up your authentication.
var libpb=require('libprofitbricks')
libpb.setauth('username','password')
- All of the functions are available in the top level libprofitbricks namespace
libpb=require('libprofitbricks')
libpb.setauth('username','password')
var srv= new libpb.server()
- Change the endpoint.
libprofitbricks.endpoint='http://example.com/rest'
- libprofitbricks exposes the options object from the request module,
>libpb.options
{ headers: {} }
- depth is used to control the amount of json returned, the scale is 1 to 5.
libpb.depth= 1,
libpb.setdepth(5)
- authenticate
libpb.setauth: function(username,password)
- pbauth takes a pre-encoded string for http basic authentication
libpb.pbauth: function(sixfourstring)
- There are several functions you can use to assemble ProfitBricks json objects.
- These functions work as object factories, they return json objects.
- The json objects can then be used to create resources at ProfitBricks.
For example,If you want to create a ProfitBricks data center, you can use the libprofitsbricks.datacenter function to generate a json object with the properties of your new data center.
libpb=require('libprofitbricks')
libpb.setauth('username','password')
var props={"name":"UltraMega Data Center","location":"us/las","description":"UltraMega description"}
var dc= new libpb.datacenter(props)// <--- dc is a json object
> dc.show() //<--- show what dc properties are set
{
"properties":
{"name":"UltraMega Data Center",
"location":"us/las",
"description":"UltraMega description"},
"entities":
{"servers":{"items":[]},
"lans":{"items":[]},
"loadbalancers":{"items":[]},
"volumes":{"items":[]}}
}
You can set individual properties on the object by calling it's set function.
dc.set('name', 'BrickHouse') // <--- Sets name for the data center dc
Many of the json objects, like dc, our datacenter json object, can have subobjects. These subobjects are listed as "entities" when you call an objects show() function.
> dc.show() //<--- show what dc properties are set
{
"properties":
{"name":"UltraMega Data Center",
"location":"us/las",
"description":"UltraMega description"},
"entities": // <--- Subobjects
{"servers":{"items":[]},
"lans":{"items":[]},
"loadbalancers":{"items":[]},
"volumes":{"items":[]}}
}
- Entities can be added to the json object through the json objects functions.
var srv=new libpb.server({name:srvname,cores:"5",ram:16384})
dc.addServer(srv)
>dc.show()
{
"properties":
{"name":"goat","location":"us/las","description":"a good one"},
"entities":
{"servers":{"items":
[{"properties":{"name":"srvname","ram":16384,"cores":"5"}]
},
,"lans":{"items":[]},
"loadbalancers":{"items":[]},
"volumes":{"items":[]}}
}
These are the object factory functions.
- Calling them with "new" is not required. I call then with "new" in these docs to indicate object creation
datacenter: function(props)
server: function(props)
nic: function(props)
firewallrule: function(props) //props is required
image: function(props)
ipblock: function(props)
loadbalancer: function(props)
lan: function(props)
snapshot: function(props)
volume: function(props)
Another Example
libpb=require('libprofitbricks')
libpb.setauth('username','password')
var srv= new libpb.server({"name":"f",
"ram":"8192",
"cores":"1"}) // <--- props passed in
> srv.show()
{
"properties":
{"name":"f","ram":8192,"cores":1}, // <--- props passed out
"entities":
{"cdroms":{"items":[]},
"nics":{"items":[]},
"volumes":{"items":[]}}
}
- "props" replaces the objects properties
Show() is handy for listing the properties available to an object.
> libpb=require('libprofitbricks')
> new libpb.volume().show()
{
"properties": {
"name":"v is for volume",
"size":80,
"bus":"VIRTIO", "image":"",
"type":"HDD",
"licenceType":"UNKNOWN",
},
"entities":undefined
}
- Use the json objects to allocate resources at ProfitBricks.
libpb=require('libprofitbricks')
libpb.setauth('username','password')
var dc= new libpb.datacenter() // <--- Makes the json object dc
dc.set('name', 'BrickHouse') // <--- Sets name for the data center dc
libpb.createDatacenter(dc) // <--- Creates a data center at ProfitBricks
libpb=require('libprofitbricks')
libpb.setauth('username','password')
var srv= new libpb.server() // <--- Makes the server json object srv
srv.set('name','srvfu57')// <--- Sets a property for the server srv
srvfu57
> srv.show() // <--- Shows the json object
{
"properties":
{"name":"srvfu57","ram":2048,"cores":4},
"entities":
{"cdroms":{"items":[]},"nics":{"items":[]},"volumes":{"items":[]}}
}
var libpb=require('libprofitbricks')
libpb.setauth('username','password')
libpb.listDatacenters(myOptionalCallback)
- if a callback is not included, pbreq.fallback is used as the callback.
// pbreq.fallback
function fallback(error, response, body) {
if (error){ console.log("error", error) }
if (response){ console.log('Status:', response.statusCode) }
if (body){console.log("body", body) }
}
var libpb=require('libprofitbricks')
libpb.setauth('username','password')
var ipblk = new libpb.ipblock()
ipblk.set('location','de/fkb')
ipblk.set('size', 5)
libpb.reserveIpblock(ipblk)
- Simple Creation
var libpb=require('libprofitbricks')
libpb.setauth('username','password')
var datacenter_id = '700e1cab-99b2-4c30-ba8c-1d273ddba022'
var srv = new libpb.server(
{name:'server1',// <--- Inits with json
ram:4096,
cores:4}
)
libpb.createServer(datacenter_id, srv)
- Complex Creation
var libpb=require('libprofitbricks')
libpb.setauth('username','password')
var datacenter_id = '700e1cab-99b2-4c30-ba8c-1d273ddba022'
var srv = new libpb.server({name:'server1',
ram:4096,
cores:4} )
var nic7 = libpb.nic( {name:'nic7',
ips:['10.2.2.7'],
dhcp:'',
lan:1,
})
var nic9 = libpb.nic( {name:'nic9',
ips:['10.2.2.9'],
dhcp:'',
lan:1,
})
srv.addNic(nic7)
srv.addNic(nic9)
libpb.createServer(datacenter_id, srv,mycallback)
var libpb=require('libprofitbricks')
libpb.setauth('username','password')
var brickhouse= new libpb.datacenter({
name: 'The Brick House',
location: 'us/las',
description: "She's a brick house" })
var servers=37
// reverse while loop
while(servers--){
var srvname="srvfu"+server
var srv=new libpb.server({name:srvname,cores:"5",ram:16384})
brickhouse.addServer(srv)
}
libpb.createDatacenter(brickhouse, myOptionalCallback )
ProfitBricks introduces the concept of Virtual Datacenters. These are logically separated from one and the other and allow you to have a self-contained environment for all servers, volumes, networking, snapshots, and so forth. The goal is to give you the same experience as you would have if you were running your own physical datacenter.
You will need a datacenter before you can create anything else. Like the server functions, the datacenter functions can be used to create a simple vDC or a complex one.
To create a simple one you would do this:
var libpb=require('libprofitbricks')
libpb.setauth('username','password')
var dc = new libpb.datacenter()
dc.set('name','my datacenter')
libpb.createDatacenter(dc)
var libpb=require('libprofitbricks')
libpb.setauth('username','password')
var srv = new libpb.server({name:'server1',
ram:4096,
cores:4} )
var nic7 = libpb.nic( {name:'nic7',
ips:['10.2.2.7'],
lan:1,
})
srv.addNic(nic7)
var nic9 = libpb.nic( {name:'nic9',
ips:['10.2.2.9'],
lan:1,
})
srv.addNic(nic9)
var dc = new libpb.datacenter()
dc.set('name','my datacenter')
dc.addServer(srv)
libpb.createDatacenter(dc)
You will want to exercise a bit of caution here. Removing a datacenter will destroy all objects contained within that datacenter, servers, volumes, snapshots, and so on.
- The objects -- once removed -- will be unrecoverable.
var libpb=require('libprofitbricks')
libpb.setauth('username','password')
datacenter_id = '700e1cab-99b2-4c30-ba8c-1d273ddba022'
libpb.deleteDatacenter(datacenter_id, myOptionalCallback)
libprofitbricks allows users to dynamically update cores, memory, and disk independently of each other. This removes the restriction of needing to upgrade to the next size up to receive an increase in memory. You can now simply increase the instances memory keeping your costs in-line with your resource needs.
- The following code illustrates how you can update cores and memory:
var libpb=require('libprofitbricks')
libpb.setauth('username','password')
var datacenter_id = '700e1cab-99b2-4c30-ba8c-1d273ddba022'
var server_id = '700e1cab-99b2-4c30-ba8c-1d273ddba023'
var jason={ cores:'16',ram:'2048' } // aka the server update values
libpb.updateServer(datacenter_id,server_id,jason,myOptionalCallback)
Listing resources is fairly straight forward.
- Listing the datacenters:
var libpb=require('libprofitbricks')
libpb.setauth('username','password')
libpb.listDatacenters(myOptionalCallback)
- Listing servers in a data center:
var libpb=require('libprofitbricks')
libpb.setauth('username','password')
var datacenter_id = '700e1cab-99b2-4c30-ba8c-1d273ddba022'
libpb.listServers(datacenter_id,myOptionalCallback)
- Listing your volumes:
var libpb=require('libprofitbricks')
libpb.setauth('username','password')
var datacenter_id = '700e1cab-99b2-4c30-ba8c-1d273ddba022'
libpb.listVolumes(datacenter_id)
- The ProfitBricks platform supports adding multiple NICs to a server.
- These NICs can be used to create different, segmented networks on the platform.
The sample below shows you how to add a second NIC to an existing server:
var libpb=require('libprofitbricks')
libpb.setauth('username','password')
dc_id = '700e1cab-99b2-4c30-ba8c-1d273ddba022'
srv_id = '700e1cab-99b2-4c30-ba8c-1d273ddba023'
var jason={name:'nic11',ips:['10.2.2.11'],lan:1})
libpb.createNic(dc_id,srv_id,jason)
datacenter: function(props)
this.show=function()
this.set=function(property,value)
//this.required=['name','location']
//this.optional=['description']
this.properties={
name : "Data center",
location : "us/las",
}
this.entities= {
servers: { items: []},
lans: {items: []},
loadbalancers: {items: []},
volumes: {items: [] }
}
this.addServer=function(aserver)
this.addLan=function(alan)
this.addLoadbalancer=function(aloadbalancer)
this.addVolume=function(avolume)
listDatacenters: function(callback)
createDatacenter: function(jason,callback)
getDatacenter: function(dc_id,callback)
updateDatacenter: function(dc_id,jason,callback)
patchDatacenter: function(dc_id,jason,callback)
deleteDatacenter: function(dc_id,callback)
//props is required for firewall rules
firewallrule:function(props)
this.set=function(property,value)
this.show=function()
this.properties=props
listFWRules : function(dc_id,srv_id,nic_id,callback)
createFWRule : function(dc_id,srv_id,nic_id,jason,callback)
getFWRule : function(dc_id,srv_id,nic_id,fwrule_id,callback)
updateFWRule : function(dc_id,srv_id,nic_id,fwrule_id,jason,callback)
patchFWRule : function(dc_id,srv_id,nic_id,fwrule_id,jason,callback)
delFWRule : function(dc_id,srv_id,nic_id,fwrule_id,callback)
listImages : function(callback)
getImage : function(image_id,callback)
updateImage : function(image_id,jason,callback)
patchImage : function(image_id,jason,callback)
deleteImage : function(image_id,callback)
ipblock: function(props)
this.show=function()
this.set=function(property,value)
//this.required=['size','location']
//this.optional=[]
this.properties= {
size: 5,
location: "de/fkb"
}
listIpblocks : function(callback)
reserveIpblock : function(jason,callback)
getIpblock : function(ipblock_id,callback)
releaseIpblock : function(ipblock_id,callback)
lan: function(props)
this.show=function()
this.set=function(property,value)
//this.required=[]
//this.optional=['name','public']
this.properties={
}
this.entities={
nics: { items: [] }
}
this.addNic=function(anic)
listLans: function(dc_id,callback)
createLan: function(dc_id,jason,callback)
getLan: function(dc_id,lan_id,callback)
updateLan: function(dc_id,lan_id,jason,callback)
patchLan: function(dc_id,lan_id,jason,callback)
deleteLan: function(dc_id,lan_id,callback)
listLanMembers: function(dc_id,lan_id,callback)
loadbalancer: function (props)
this.show=function()
this.set=function(property,value)
//this.required=['name']
//this.optional=['ip','dhcp']
this.properties={
name: "Load Balancer"
}
this.entities={
balancednics: { items: [] }
}
this.addBalancedNic=function(abalnic)
createLoadbalancer: function(dc_id,jason,callback)
deleteLoadbalancer: function(dc_id,lbal_id,callback)
getLoadbalancer: function(dc_id,lbal_id,callback)
listLoadbalancers: function(dc_id,callback)
patchLoadbalancer: function(dc_id,lbal_id,jason,callback)
updateLoadbalancer: function(dc_id,lbal_id,jason,callback)
getLocation : function(location_id,callback)
listLocations : function(callback)
nic: function(props)
this.show=function()
this.set=function(property,value)
//this.required=["name","lan"]
//this.optional=["mac","ips","dhcp","firewallActive"]
this.properties={
name: "",
ips: [],
dhcp: "",
lan : ""
}
this.entities={
firewallrules: {items: []}
}
this.addFwrule=function(afwrule)
createNic: function(dc_id,srv_id,jason,callback)
deleteNic: function(dc_id,srv_id,nic_id,callback)
getNic: function(dc_id,srv_id,nic_id,callback)
listNics: function(dc_id,srv_id,callback)
patchNic: function(dc_id,srv_id,nic_id,jason,callback)
updateNic: function(dc_id,srv_id,nic_id,jason,callback)
listRequests : function(callback)
getRequest : function(request_id,callback)
statusRequest: function(request_id,callback)
server: function(props)
this.show=function()
this.set=function(property,value)
//this.required=['name','core','ram']
//this.optional=['availabilityzone','licensetype','bootVolume','bootCdrom']
this.properties={
name : "Server",
ram : "8192",
cores : "4"
}
this.entities={
cdroms: { items: []},
nics: {items: []},
volumes: {items: [] }
}
this.addNic=function(anic)
createServer: function(dc_id,jason,callback)
delServer: function(dc_id,srv_id,callback)
getServer: function(dc_id,srv_id,callback)
listServers: function(dc_id,callback)
patchServer: function(dc_id,srv_id,jason,callback)
updateServer: function(dc_id,srv_id,jason,callback)
listAttachedVolumes : function(dc_id,srv_id,callback)
attachVolume : function(dc_id,srv_id,volume_id,callback)
getAttachedVolume : function(dc_id,srv_id,volume_id,callback)
detachVolume : function(dc_id,srv_id,volume_id,callback)
listAttachedCdroms : function(dc_id,srv_id,callback)
attachCdrom : function(dc_id,srv_id,cdrom_id,callback)
getAttachedCdrom : function(dc_id,srv_id,cdrom_id,callback)
detachCdrom : function(dc_id,srv_id,cdrom_id,callback)
rebootServer: function(dc_id,srv_id,callback)
startServer: function(dc_id,srv_id,callback)
stopServer: function(dc_id,srv_id,callback)
deleteSnapshot : function(snapshot_id,callback)
getSnapshot : function(snapshot_id,callback)
listSnapshots : function(callback)
patchSnapshot : function(snapshot_id,jason,callback)
updateSnapshot : function(snapshot_id,jason,callback)
/** createSnapshot jason can have name and/or description **/
createSnapshot : function(dc_id,volume_id,jason,callback)
restoreSnapshot : function(dc_id,volume_id,jason,callback)
volume: function(props)
this.show=function()
this.set=function(property,value)
//this.required=['size']
//this.optional=['name','bus','type','licencetype']
this.properties= {
"name": "",
"size": 80,
"bus": "VIRTIO",
"type": "HDD",
"licenceType": "UNKNOWN"
}
listVolumes : function(dc_id,callback)
createVolume: function(dc_id,jason,callback)
getVolume : function(dc_id,volume_id,callback)
updateVolume : function(dc_id,volume_id,jason,callback)
patchVolume : function(dc_id,volume_id,jason,callback)
deleteVolume : function(dc_id,volume_id,callback)
You can find additional examples in our repo [here]. If you find any issues, please let us know via the DevOps Central community or GitHub's issue system and we'll check it out.
We touched on only a few ways you can interact with the libprofitbricks API using Node.js. If you have any other question, ping us in the community.