Wednesday, June 18, 2008

Making use of map()

The map() function (perldoc -f map) is a surprisingly useful and not well publicised function in perl. It "Evaluates the BLOCK or EXPR for each element of LIST (locally setting $_ to each element) and returns the list value composed of the results of each such evaluation."

So it is a shortcut method for things like :


my @chars
foreach my $_ ( @nums ) {
push @chars , chr ;
}


You can just use map to do the same thing


@chars = map(chr, @nums);


Now, when you are working with all the methods and imbedded objects in the API, map is a very quick way to pull out the values you want :



# get a list of all the DNS zones
my @zones = $session->search(
"object" => "Infoblox::DNS::Zone",
"name" => ".*",
"view" => $data{'view'},
) ;

# get just the list of zone names
my @names = map ( $_->name() , @zones ) ;

Getting members

There are quite a few places that mention members :

Infoblox::DHCP::Member - DHCP Member object.
Infoblox::DNS::Member - DNS Member object
Infoblox::Grid::Member - Grid Member object.
Infoblox::Grid::Member::DHCP - DHCP member object.
Infoblox::Grid::Member::DNS - Grid Member DNS object.
Infoblox::Grid::Member::Interface - Interface member object.
Infoblox::Grid::Member::License - License object.
Infoblox::Grid::Member::OSPF - OSPF (Open Shortest Path First) member
Infoblox::Grid::Member::QIP - Enable Lucent VitalQIP on a member node.
Infoblox::Grid::Member::RADIUS - Manages the Remote Authentication
Infoblox::RADIUS::Member - RADIUS Member object.

So it can be confusing to work out where you get actual info about the GRID members. You should use this one :

Infoblox::Grid::Member - Grid Member object.

If you wanted to get all the members in the grid, try something like this :


# get all the members :
my @members = $Session->search (
"object" => "Infoblox::Grid::Member",
"name" => "Infoblox" ,
)

# get a list of the ip addrs
foreach my $mem ( @members ) {
my $ip = $mem->ipv4addr();
my $name = $mem->name();

print "member : $ip , $name\n";

}


All the rest are just special references to menu items that appear in various places in the workflow, such as adding nameservers to a DNS zone, or setting the member for a DHCP range. These other objects are used so you can tell the server how to find the relevant information that relates to that component of the configuration.

Synopsis

The Infoblox API doesn't have a Synopsis section, and a lot of users seem to have trouble getting a basic script built. Here are the essential components



use Infoblox;

# connect to the grid

my $session = Infoblox::Session->new(
"master" => "205.158.171.216", # VIP address of master
"username" => "admin", # Username of valid account
"password" => "infoblox" ); # Password for above

# create a reference to a member in the Grid

my $member1ns = Infoblox::DNS::Member->new(
"name" => "ns1.infoblox.com",
"ipv4addr" => "10.128.64.6" );

# create the zone, asisgned to a member

my $zone = Infoblox::DNS::Zone->new(
"name" => "test.com",
"email" => "hostmaster\@infoblox.com",
"primary" => $member1ns,
);

# add the zone

$session->add( $zone ) or
printf STDERR "Add Zone failure %d: %s\n",
$session->status_code(), $session->status_detail();