Reply
Highlighted

Writing a rest client in perl

Community Manager
Community Manager
Posts: 248
4856     0

If you are transitioning from the Infoblox Perl API (PAPI) to the REST Api (WAPI) but want to stick with writing perl code, then you should make use of the REST::Client.pm module. This takes care of just about all the heavy lifting and lets you quickly write all the rest calls you need. Here's how it works:

 
1) You need a few to use a few new modules:
 
#!/usr/bin/perl#use MIME::Base64;use JSON ;use REST::Client;
 
REST::Client is incredibly lightweight, and you only need the JSON module
of you want to read and write the responses into perl data structures.
MIME::Base64 is included in most perl builds by default
 
2) Create a handler that will do all the communication to the REST server
As usual we need an IP address and some login credentials. The
credentials are sent in the headers with the HTTP request
 
# set set up some defaults:my $REST_REV = "v1.0";my $master = '192.168.1.2';my $user = 'admin';my $pass = 'infoblox';# set up the headers and default paramsmy $headers = {    Authorization => 'Basic '.  encode_base64($user . ':' . $pass),};# set up a REST sessionmy $rest = REST::Client->new( {            host => "https://$master/wapi/$REST_REV",} );
 
3) ok, now that we've done the hard stuff, we can send data to the REST
server. Lets start with a simple 'get' request, and list all the networks
in the grid. To do that we just get the 'network' object with no
paramaters
 
$rest->GET( '/network' , $headers );# print the resultprint $rest->responseContent() . "\n";# to convert the data into a hash, use the JSON modulemy $res_data = from_json( $rest->responseContent() );
 
4) Now we want to try something harder, and limit what we are searching
for. To do that we have to send some parameters in the GET request, and
we have to encode them correctly, so we use REST::Client::buildQuery()
 
# create a hash of search params, and convert it into a query stringmy $pdata = {    'network~' => '10',};my $params = $rest->buildQuery( $pdata );
# then send of the new request with some params$rest->GET( "/network".$params , $headers );print $rest->responseContent() . "\n";
 
5) so far so good, but what about other rest methods. With the WAPI, you have 4 options:
 
use GET to set/search
use POST to add
use PUT to modify
use DELETE to remove
 
So we will try a POST to create a new network:
 
# We set up new headers that show we have a POST in the message body$headers = {    Authorization => 'Basic '.  encode_base64($user . ':' . $pass),    'Content-type' => 'application/x-www-form-urlencoded'};# then we have to url encode the params that we want in the bodymy $pdata = {    'network' => '10.10.10.0/24'};my $params = $rest->buildQuery( $pdata );# but buildQuery() prepends a '?' so we strip that out$params =~ s/\?//;# then sent the request:# POST requests have 3 args: URL, BODY, HEADERS$rest->POST( "/network" , $params , $headers );print $rest->responseContent() . "\n";
 
6) what about modifying a network? That requires PUT. But it also
requires us to find the right object in the database
 
# Get the object first, because we need the '_ref' keymy $params = $rest->buildQuery(         'network' => '10.10.10.0/24',    );$rest->GET( "/network".$params , $headers );# get the reference to this object, you always get back an ARRAYREF of# all the objects, so you look at the first one# and grab its '_ref'my $res_data = from_json( $rest->responseContent() );my $key = $res_data->[0]->{_ref};if ( $key ) {    print "Modify : $key\n";    # set up some values we want to change    my $params = $rest->buildQuery(             'comment' => 'A Test Network'        );    # and make the change, use the KEY as the URL    # and don't forget the leading '/' before the key    # PUT requests have 3 args: URL, BODY, HEADERS    $rest->PUT( "/$key".$params ,"", $headers );    print $rest->responseContent() . "\n";}
 
7) lastly, here's how you would delete an object
 
# Get the object first, because we need the '_ref' key# but do it in a single line$rest->GET( "/network" . $rest->buildQuery(             'network' => '10.10.10.0/24',        ), $headers );# get the reference to this object, like beforemy $res_data = from_json( $rest->responseContent() );my $key = $res_data->[0]->{_ref};if ( $key ) {    print "Delete : $key\n";    # and make the change, use the KEY as the URL    # and don't forget the leading '/' before the key    # DELETE requests have 2 args: URL, HEADERS    $rest->DELETE( "/$key" , $headers );    print $rest->responseContent() . "\n";}
 

 

Highlighted

Re: Writing a rest client in perl

Authority
Posts: 25
4857     0
How to add/update EA to the host?
Thank you
Showing results for 
Search instead for 
Did you mean: 

Recommended for You