Reply
Highlighted
Accepted Solution

Snapin Workflow - Host-request.pl (Add MAC Address)

vscanas
Techie
Posts: 8
4618     0

I have customized the workflow snapin. I want to add the ability to add a MAC adress to a host by the host-request perl script. The following is my sample code.

 

1. UI-Custom.js (Front-end customization)  This removes the delete and request tool (since in the request workflow is being bypass) . It only leaves the host, host allias, and fixed request option. Adds a MAC and comment fields to the host form. Sets field values for zone and for fix address. 

 

Located: snapin-worflow/htdocs/javascript

 

/*

This is where you make customisations to the Workfow snapin

See the customization guide for details

If you want to make standalone widgets, do not edit this file, instead
consult the customization guide

*/

// THESE are examples that you could start from:

//console.log ( "begin custom modifications" );

// disable the login prompt
//IB.controlPanel.disableLogin();

//enables a custom title
IB.controlPanel.setTitle( "DNS NOW" );

// add input fields to a form 
IB.form.HostPanel.addInput( {
        fieldLabel: 'MAC Address',
        value: '00:00:00:00:00:00',
        allowBlank: true,
        itemId: 'mac',
        name: 'mac'
    });
	
IB.form.HostPanel.addInput( {
        fieldLabel: 'Comment',
        value: ' ',
        allowBlank: true,
        itemId: 'comment',
        name: 'comment'
    });

// add the EAs to ALL the forms	
/* IB.controlPanel.setEATitle( "Required Info" );
IB.controlPanel.setEAFields([ "Owner" ]); */

// collapse some components
//IB.controlPanel.collapseEAFields();
//IB.controlPanel.collapseSchedule();

//removes forms
IB.controlPanel.removeForm( IB.form.ZonePanel );
IB.controlPanel.removeForm( IB.form.NetworkPanel );
/* IB.controlPanel.removeForm( IB.form.FixedAddrPanel );*/
IB.controlPanel.removeForm( IB.form.LeasePanel );

//Add defualt values 
IB.form.HostPanel.setFieldValue('zone', "dominionnet.com" );

IB.form.FixedAddrPanel.setFieldValue('mac', "00:00:00:00:00:00" );

// disable 'schedule tasks' fields
IB.controlPanel.hideSchedule();



// hide some components
//IB.controlPanel.hideNextNetwork();

// Other interesting stuff

//IB.controlPanel.addSubmitPrompt('This is a Global Submit message');

//IB.controlPanel.setToolText( 'requests' , 'Refresh Requests' );

IB.controlPanel.removeTool( 'delete');
IB.controlPanel.removeTool( 'requests');

//IB.controlPanel.setMenuText('zones','DNS Zone');

//IB.form.RequestPanel.setTitle( "Your Request Queue" );

 

 

2. The forms run with PERL scripts

Located: snapin-worflow/cgi-bin/

 

Host: host-request.pl & host-search.pl

Host alias: alias-request & alias-search.pl

Fixed Adress: fixed-request & fixed-search.pl

 

I have modified the host-request pl. Currently I am able to add a comment by relating to the $hobj

#Creates comment IT WORKS! 
$hobj->comment( $comment ) if $comment ;

 

 

However I get an error message for a MAC address

#Error because it can't locate mac in hobj 
#$hobj->mac( $mac ) if $mac ;  

ERROR MESSAGE: Can't locate object method "mac" via package "Infoblox:Smiley Very HappyNS:Host"...

 

 

So far I understand the every perl has an object method that it runs within the pre-defined API that is the reason I can add comment but not a MAC.

 

For the package "Infoblox:Smiley Very HappyHCP::FixedAddr" you can add a MAC address by relating to the $fobj but the type of network will be a fixed address. I want the ability to add a MAC Address and have it as a host type.

# if we are flipping from a reservation to a fixed address, we have to 
# workaround a bug with match_client()
if ( $mac ) {
    $fobj->mac( $mac ) ;
    if ( $mac ne '00:00:00:00:00:00' ) {
        $fobj->match_client( 'MAC' ) ;
    }
}

 

What should I do?

I think the best scenerio for the host-request.pl is to have a get function that gets the IP address and then relates it to mac address.  (I try doing this in the comments of the script)

 

Host-request.pl

#!/usr/bin/perl
#
# Copyright (c) 2008, Infoblox Inc.
#
# 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.
#
use strict ;

# load the WebUI common libraries
use Infoblox::WebUI ;

# initialise the WebUI interface
my $sdb=Infoblox::WebUI->new( );

# get the CGI fields
my $name = $sdb->CGI->param('name') || "" ;
my $zone = $sdb->CGI->param('zone') || "" ;
my $view = $sdb->CGI->param('view') || "" ;
my $ipaddrs = $sdb->CGI->param('ipaddrs') || "" ;
my $aliases = $sdb->CGI->param('aliases') || "" ;
my $mac = $sdb->CGI->param('mac') || "00:00:00:00:00:00" ;
my $comment = $sdb->CGI->param('comment') || "" ;


my $mode = $sdb->CGI->param('mode') || "" ;

# check for defaults
unless ( $name and $ipaddrs ) {
    print $sdb->informerror("No name or ipaddrs specified");
    exit ;
}

# override the approval process, and do a direct submit
if ( $mode eq 'na' ) {
    $sdb->config( noapproval => 1 );
}

# get an FQDN from the name(s)
my $fqdn = $sdb->fqdn( $name , $zone ) ;

my $hobj ;
my $action = 'add';
# create a host or get one
if ( $mode eq 'modify' or $mode eq 'delete') {
    if ( $mode eq 'delete' ) {
        $action = 'delete';
    }
    else {
        $action = 'modify';
    }

    ( $hobj ) = $sdb->get(
        object => 'Infoblox::DNS::Host',
        name => $fqdn,
        view => $view,
        zone => $zone,
		#mac => $mac, 
    );
    unless ( $hobj ) {
        print $sdb->informerror( $sdb->status_detail() ) ;
        exit ;
    }
}
else {
    $hobj = Infoblox::DNS::Host->new (
           "name" => $fqdn,
		   #"comment" => $mac
    );
	
}

# my ( $fobj ) = $sdb->get(
        # object => 'Infoblox::DHCP::Host',
        # ipv4addr => $ipaddr,
# );

# my $fobj = Infoblox::DHCP::FixedAddr->new (
           # ipv4addr => $ipaddr,
           # mac => $mac,
# );

# $fobj = Infoblox::DHCP::FixedAddr->get (
            # ipv4addr => $ipaddrs
     # );
	 # $fobj = $fobj[0];
	 # $fobj->mac($mac);
	 # my $response = $session->modify($fobj);

# # # Adds a MAC Adress 
# if ( $mac ) {
    # $fobj->mac( $mac ) ;
    # if ( $mac ne '00:00:00:00:00:00' ) {
        # $fobj->match_client( 'MAC' ) ;
    # }
# }

#Error because it can't locate mac in hobj 
#$hobj->mac( $mac ) if $mac ;  
	
#Creates comment IT WORKS! 
$hobj->comment( $comment ) if $comment ;

# [ ] call an custom handlers for the params validation
#   [ ] and any other modifications
$sdb->call_handlers( $hobj ) ;

# set v4 or v6 addresses
$sdb->set_addrs( $hobj , $ipaddrs,) ;

# set mac address? 
#$sdb->mac_addr( $hobj , $mac ) ;

# set any aliases
my @allist = split ( /[,\s]+/ , $aliases );
if ( @allist ) { $hobj->aliases ( \@allist ) }


# add in any common params that we can add to ANY object
$sdb->set_param_common( $hobj ) ;

# add any extensible attribute params
$sdb->set_param_EA( $hobj ) ;

# store any scheduling info
# this is a generic (non CGI function), so we have to pass the date/time
my $sdate = $sdb->CGI->param('publish_date') || "" ;
my $stime = $sdb->CGI->param('publish_time') || "" ;

# then schedule the request
# and pass an optional DATE/TIME for the request 
# we need to add $fobj if it runs 
my ( $res ) = $sdb->workflow_request( $action , $hobj ,  $sdate , $stime ) ;

if ( $res ) {
    # inform the results 
    print $sdb->informtext("Requested : $name");
}
else {
    print $sdb->informerror( $sdb->status_detail() ) ;
}
  
  

 

fixed-request.pl

#!/usr/bin/perl
#
# Copyright (c) 2008, Infoblox Inc.
#
# 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.
#
use strict ;

# load the WebUI common libraries
use Infoblox::WebUI ;

# initialise the WebUI interface
my $sdb=Infoblox::WebUI->new( );

# get the CGI fields
my $ipaddr = $sdb->CGI->param('ipaddr') || "" ;
my $comment = $sdb->CGI->param('comment') || "" ;
my $mac = $sdb->CGI->param('mac') || "00:00:00:00:00:00" ;

my $mode = $sdb->CGI->param('mode') || "" ;


# check for defaults
unless ( $ipaddr ) {
    print $sdb->informerror("No ipaddr specified");
    exit ;
}

my $fobj ;
my $action = 'add';
# create a host or get one
if ( $mode eq 'modify' or $mode eq 'delete') {
    if ( $mode eq 'delete' ) {
        $action = 'delete';
    }
    else {
        $action = 'modify';
    }

    ( $fobj ) = $sdb->get(
        object => 'Infoblox::DHCP::FixedAddr',
        ipv4addr => $ipaddr,
#         comment => $comment,
#         mac => $mac,
    );
    unless ( $fobj ) {
        print $sdb->informerror( $sdb->status_detail() ) ;
        exit ;
    }
}
else {
    $fobj = Infoblox::DHCP::FixedAddr->new (
           ipv4addr => $ipaddr
    );
}

# [ ] call an custom handlers for the params validation
#   [ ] and any other modifications
$sdb->call_handlers( $fobj ) ;

# [ ] set v4 or v6 addresses ??
# $sdb->set_addrs( $fobj , $ipaddrs ) ;

# if we are flipping from a reservation to a fixed address, we have to 
# workaround a bug with match_client()
if ( $mac ) {
    $fobj->mac( $mac ) ;
    if ( $mac ne '00:00:00:00:00:00' ) {
        $fobj->match_client( 'MAC' ) ;
    }
}

$fobj->comment( $comment ) if $comment ;

# add any extensible attribute params
$sdb->set_param_EA( $fobj ) ;

# store any scheduling info
# this is a generic (non CGI function), so we have to pass the date/time
my $sdate = $sdb->CGI->param('publish_date') || "" ;
my $stime = $sdb->CGI->param('publish_time') || "" ;

# then schedule the request
# and pass an optional DATE/TIME for the request
my ( $res ) = $sdb->workflow_request( $action , $fobj , $sdate , $stime ) ;

if ( $res ) {
    # inform the results 
    print $sdb->informtext("Requested : $ipaddr");
}
else {
    print $sdb->informerror( $sdb->status_detail() ) ;
}
    

 

I included the converted lease as the scripts does some interesting things by having the $lobj (object method for converted lease) and $fobj (oject method for fixed address) both included in the perl script

#!/usr/bin/perl
#
# Copyright (c) 2008, Infoblox Inc.
#
# 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.
#
use strict ;

# load the WebUI common libraries
use Infoblox::WebUI ;

# initialise the WebUI interface
my $sdb=Infoblox::WebUI->new( );

# get the CGI fields
my $ipaddr = $sdb->CGI->param('ipaddr') || "" ;
my $comment = $sdb->CGI->param('comment') || "" ;
my $mac = $sdb->CGI->param('mac') || "00:00:00:00:00:00" ;

my $mode = $sdb->CGI->param('mode') || "" ;

# check for defaults
unless ( $ipaddr ) {
    print $sdb->informerror("No ipaddr specified");
    exit ;
}

# lease conversion ALWAYS gets a lease first
# add and modify are actually the same thing
# 'delete', just trashes the lease

# create a host or get one
#     if ( $mode eq 'delete' ) {
#         $action = 'delete';
#     }
#     else {
#         $action = 'add';
#     }

my ( $lobj ) = $sdb->get(
        object => 'Infoblox::DHCP::Lease',
        ipv4addr => $ipaddr,
);
unless ( $lobj ) {
    print $sdb->informerror( $sdb->status_detail() ) ;
    exit ;
}

# [ ] call an custom handlers for the params validation
#   [ ] and any other modifications
$sdb->call_handlers( $lobj ) ;

# to convert a lease, we just add a fixed address
# [ ] do we need to delete the lease first ??

my $fobj = Infoblox::DHCP::FixedAddr->new (
           ipv4addr => $ipaddr,
           mac => $mac,
);

$fobj->comment( $comment ) if $comment ;

# add any extensible attribute params
$sdb->set_param_EA( $fobj ) ;

# store any scheduling info
# this is a generic (non CGI function), so we have to pass the date/time
my $sdate = $sdb->CGI->param('publish_date') || "" ;
my $stime = $sdb->CGI->param('publish_time') || "" ;

my $res ;

# if we are in delete mode, we just delete the lease
if ( $mode eq 'delete' ) {
    ( $res ) = $sdb->workflow_request( 'delete' , $lobj , $sdate , $stime ) ;
}
else {
    ( $res ) = $sdb->workflow_request( 'add' , $fobj , $sdate , $stime ) ;
}

if ( $res ) {
    # inform the results 
    print $sdb->informtext("Requested : $ipaddr");
}
else {
    print $sdb->informerror( $sdb->status_detail() ) ;
}
    

Re: Snapin Workflow - Host-request.pl (Add MAC Address)

GHorne Community Manager
Community Manager
Posts: 248
4619     0

If you want to add a MAC address you have to attach it to an ip address. On a HOST the list of addresses is a struct of either ipaddresses or Infoblox:Smiley Very HappyHCP::FixedAddr objects.

 

The magic here is in the call to 'set_addrs()'.

 

This is a helper function (in Simple.pm) that works out if the address is v4 or v6, and 'does the right thing'. Fortunately you can short circuit the process, since the method will should happily accept a FixedAddr object without complaining. 

 

I haven't tested any of this, but digging into the source code, you should be able to do something like this,

 

# set v4 or v6 addresses
$sdb->set_addrs( $hobj , $ipaddrs ) ;

# if you want to add a fixed address, you need to create it here and add
# it to the set_addrs list.
# in this example we will splice the first address from the list
if ( $mac ) {
    my $fixed_addr = Infoblox::DHCP::FixedAddr->new(
           "ipv4addr"          => @$ipaddrs[0],
           "match_client"      => "MAC",
        #    "mac"               => $mac,
           "mac"               => "CA:FE:C0:FF:EE", #hardcoded, as an example
    );

    # set_addrs expects an ARRAYREF of ipaddreses, so we send as new list
    $sdb->set_addrs( $hobj , [ $fixed_addr ] ) ;
}

Note this will only accept the first ip address, if you want to pass multiple IP addresses you will have to do some extra work on the GUI side to accept a list of MAC:IP pairs

 

Re: Snapin Workflow - Host-request.pl (Add MAC Address)

vscanas
Techie
Posts: 8
4619     0

It gave me the following error when I try it.

 

cgi-bin/host-request.pl:

Software error:

Can't use string ("10.1.0.3") as an ARRAY ref while "strict refs" in use at /portal/snapin-workflow/cgi-bin/host-request.pl line 125.

For help, please send mail to the webmaster ([no address given]), giving this error message and the time and date of the error. 

 

Host-request pl script

 

 

#!/usr/bin/perl
#
# Copyright (c) 2008, Infoblox Inc.
#
# 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.
#
use strict ;

# load the WebUI common libraries
use Infoblox::WebUI ;

# initialise the WebUI interface
my $sdb=Infoblox::WebUI->new( );

# get the CGI fields
my $name = $sdb->CGI->param('name') || "" ;
my $zone = $sdb->CGI->param('zone') || "" ;
my $view = $sdb->CGI->param('view') || "" ;
my $ipaddrs = $sdb->CGI->param('ipaddrs') || "" ;
my $aliases = $sdb->CGI->param('aliases') || "" ;
my $mac = $sdb->CGI->param('mac') || "00:00:00:00:00:00" ;
my $comment = $sdb->CGI->param('comment') || "" ;


my $mode = $sdb->CGI->param('mode') || "" ;

# check for defaults
unless ( $name and $ipaddrs ) {
    print $sdb->informerror("No name or ipaddrs specified");
    exit ;
}

# override the approval process, and do a direct submit
if ( $mode eq 'na' ) {
    $sdb->config( noapproval => 1 );
}

# get an FQDN from the name(s)
my $fqdn = $sdb->fqdn( $name , $zone ) ;

my $hobj ;
my $action = 'add';
# create a host or get one
if ( $mode eq 'modify' or $mode eq 'delete') {
    if ( $mode eq 'delete' ) {
        $action = 'delete';
    }
    else {
        $action = 'modify';
    }

    ( $hobj ) = $sdb->get(
        object => 'Infoblox::DNS::Host',
        name => $fqdn,
        view => $view,
        zone => $zone,
		#mac => $mac, 
    );
    unless ( $hobj ) {
        print $sdb->informerror( $sdb->status_detail() ) ;
        exit ;
    }
}
else {
    $hobj = Infoblox::DNS::Host->new (
           "name" => $fqdn,
		   #"comment" => $mac
    );
	
}

 # my ( $fobj ) = $sdb->get(
         # object => 'Infoblox::DHCP::Host',
         # ipv4addr => $ipaddr,
 # );

 # my $fobj = Infoblox::DHCP::FixedAddr->new (
            # ipv4addr => $ipaddr,
            # mac => $mac,
 # );

# $fobj = Infoblox::DHCP::FixedAddr->get (
            # ipv4addr => $ipaddrs
     # );
	 # $fobj = $fobj[0];
	 # $fobj->mac($mac);
	 # my $response = $session->modify($fobj);

# # # Adds a MAC Adress 
# if ( $mac ) {
    # $fobj->mac( $mac ) ;
    # if ( $mac ne '00:00:00:00:00:00' ) {
        # $fobj->match_client( 'MAC' ) ;
    # }
# }

#Error because it can't locate mac in hobj 
#$hobj->mac( $mac ) if $mac ;  
	
#Creates comment IT WORKS! 
$hobj->comment( $comment ) if $comment ;

# [ ] call an custom handlers for the params validation
#   [ ] and any other modifications
$sdb->call_handlers( $hobj ) ;

# set v4 or v6 addresses
$sdb->set_addrs( $hobj , $ipaddrs,) ;


# if you want to add a fixed address, you need to create it here and add
# it to the set_addrs list.
# in this example we will splice the first address from the list
if ( $mac ) {
    my $fixed_addr = Infoblox::DHCP::FixedAddr->new(
           "ipv4addr"          => @$ipaddrs[0],
           "match_client"      => "MAC",
           "mac"               => $mac,
        #   "mac"               => "CA:FE:C0:FF:EE", #hardcoded, as an example
    );
	
	# set_addrs expects an ARRAYREF of ipaddreses, so we send as new list
    $sdb->set_addrs( $hobj , [ $fixed_addr ] ) ;
}

# set mac address? 
#$sdb->mac_addr( $hobj , $mac ) ;

# set any aliases
my @allist = split ( /[,\s]+/ , $aliases );
if ( @allist ) { $hobj->aliases ( \@allist ) }


# add in any common params that we can add to ANY object
$sdb->set_param_common( $hobj ) ;

# add any extensible attribute params
$sdb->set_param_EA( $hobj ) ;

# store any scheduling info
# this is a generic (non CGI function), so we have to pass the date/time
my $sdate = $sdb->CGI->param('publish_date') || "" ;
my $stime = $sdb->CGI->param('publish_time') || "" ;

# then schedule the request
# and pass an optional DATE/TIME for the request 
# we need to add $fobj if it runs 
my ( $res ) = $sdb->workflow_request( $action , $hobj , $sdate , $stime ) ;

if ( $res ) {
    # inform the results 
    print $sdb->informtext("Requested : $name");
}
else {
    print $sdb->informerror( $sdb->status_detail() ) ;
}
  
  

 

I try other ways but I'm curios if the error has to do with stric refs and which way can be fixed.

Re: Snapin Workflow - Host-request.pl (Add MAC Address)

GHorne Community Manager
Community Manager
Posts: 248
4619     0

you may just need to tweak the syntax of this line :

 

    "ipv4addr"          => @$ipaddrs[0],

 

Try something like  @{ $ipaddrs } [0],

 

it all depends on which version of perl you are using

 

The error usually points you to the problem, have a look at line 125

attaching the script inseratd of pasting it inline will also help

Re: Snapin Workflow - Host-request.pl (Add MAC Address)

vscanas
Techie
Posts: 8
4619     0

So after a month of trial and error I finally got it to work.

 

I first want to say thank you for your responses. It guided me to the right direction. Another error that I encounter was this:  "A host record requires at least one IP address."  when I search it gave me an error with the ipv6address since I try to use [ $fixed_addr ] and according to documentation when creating an Infoblox:Smiley Very HappyNS::Host object, setting this method to [ ] is not supported. I guess is something with simple.pm or the # $sdb->set_addrs( $hobj , [ $fixed_addr ]. So I went another similar route not using the set_addrs

 

The magic happens here:

 

my $fixed_addr = Infoblox::DHCP::FixedAddr -> new(
         ipv4addr                        => $ipaddrs,
         mac                             => $mac
      );
	    

    $hobj = Infoblox::DNS::Host->new (
           "name" => $fqdn,
'ipv4addrs' => [ $fixed_addr ], #"comment" => $mac );

Here is my whole host-request.pl script with errors as comments.

#!/usr/bin/perl
#
# Copyright (c) 2008, Infoblox Inc.
#
# 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.
#
use strict ;

# load the WebUI common libraries
use Infoblox::WebUI ;

# initialise the WebUI interface
my $sdb=Infoblox::WebUI->new( );

# get the CGI fields
my $name = $sdb->CGI->param('name') || "" ;
my $zone = $sdb->CGI->param('zone') || "" ;
my $view = $sdb->CGI->param('view') || "" ;
my $ipaddrs = $sdb->CGI->param('ipaddrs') || "" ;
my $aliases = $sdb->CGI->param('aliases') || "" ;
my $mac = $sdb->CGI->param('mac') || "00:00:00:00:00:00" ;
my $comment = $sdb->CGI->param('comment') || "" ;


my $mode = $sdb->CGI->param('mode') || "" ;

# check for defaults
unless ( $name and $ipaddrs ) {
    print $sdb->informerror("No name or ipaddrs specified");
    exit ;
}

# override the approval process, and do a direct submit
if ( $mode eq 'na' ) {
    $sdb->config( noapproval => 1 );
}

# get an FQDN from the name(s)
my $fqdn = $sdb->fqdn( $name , $zone ) ;

my $hobj ;
my $action = 'add';
# create a host or get one

#if mode equals does the following or delete otherwsie it crates a host 
if ( $mode eq 'modify' or $mode eq 'delete') {
    if ( $mode eq 'delete' ) {
        $action = 'delete';
    }
    else {
        $action = 'modify';
    }

    ( $hobj ) = $sdb->get(
        object => 'Infoblox::DNS::Host',
        name => $fqdn,
        view => $view,
        zone => $zone,
		#mac => $mac, 
    );
    unless ( $hobj ) {
        print $sdb->informerror( $sdb->status_detail() ) ;
        exit ;
    }
}
else {

	my $fixed_addr = Infoblox::DHCP::FixedAddr -> new(
         ipv4addr                        => $ipaddrs,
         mac                             => $mac
      );
	    

    $hobj = Infoblox::DNS::Host->new (
           "name" => $fqdn,
		   'ipv4addrs' => [ $fixed_addr ],
		   #"comment" => $mac
    );
	
}

 # my ( $fobj ) = $sdb->get(
         # object => 'Infoblox::DHCP::Host',
         # ipv4addr => $ipaddr,
 # );

 # my $fobj = Infoblox::DHCP::FixedAddr->new (
            # ipv4addr => $ipaddr,
            # mac => $mac,
 # );

# $fobj = Infoblox::DHCP::FixedAddr->get (
            # ipv4addr => $ipaddrs
     # );
	 # $fobj = $fobj[0];
	 # $fobj->mac($mac);
	 # my $response = $session->modify($fobj);

# # # Adds a MAC Adress 
# if ( $mac ) {
    # $fobj->mac( $mac ) ;
    # if ( $mac ne '00:00:00:00:00:00' ) {
        # $fobj->match_client( 'MAC' ) ;
    # }
# }

#Error because it can't locate mac in hobj 
#$hobj->mac( $mac ) if $mac ;  
	
#Creates comment IT WORKS! 
$hobj->comment( $comment ) if $comment ;

# [ ] call an custom handlers for the params validation
#   [ ] and any other modifications
$sdb->call_handlers( $hobj ) ;

# if you want to add a fixed address, you need to create it here and add
# it to the set_addrs list.
# # in this example we will splice the first address from the list
# if ( $mac ) {
    # my $fixed_addr = Infoblox::DHCP::FixedAddr->new(
           # "ipv4addr"          => $ipaddrs,
           # "match_client"      => "MAC",
           # "mac"               => $mac,
        # #   "mac"               => "CA:FE:C0:FF:EE", #hardcoded, as an example
    # );
	
	# # set_addrs expects an ARRAYREF of ipaddreses, so we send as new list
    # $sdb->set_addrs( $hobj , [ $fixed_addr ] ) ;
# }

# set mac address? 
#$sdb->mac_addr( $hobj , $mac ) ;

# set any aliases
my @allist = split ( /[,\s]+/ , $aliases );
if ( @allist ) { $hobj->aliases ( \@allist ) }


# add in any common params that we can add to ANY object
$sdb->set_param_common( $hobj ) ;

# add any extensible attribute params
$sdb->set_param_EA( $hobj ) ;

# store any scheduling info
# this is a generic (non CGI function), so we have to pass the date/time
my $sdate = $sdb->CGI->param('publish_date') || "" ;
my $stime = $sdb->CGI->param('publish_time') || "" ;

# then schedule the request
# and pass an optional DATE/TIME for the request 
# we need to add $fobj if it runs 
my ( $res ) = $sdb->workflow_request( $action , $hobj , $sdate , $stime ) ;

if ( $res ) {
    # inform the results 
    print $sdb->informtext("Requested : $name");
}
else {
    print $sdb->informerror( $sdb->status_detail() ) ;
}

 

 

-

Re: Snapin Workflow - Host-request.pl (Add MAC Address)

vscanas
Techie
Posts: 8
4619     0

It's me again! I thought I had this solved but the code above only worked halfway properly. The error that I encountered came when I wanted to modify a record say I wanted to change the IP, Mac address, or the comment of an existing Host record. Well, that did not work. The Perl script will run successful but in the grid, the changes were not done. So back to the drawing board and now the Host perl script you see below works like a charm when you want to modify a host record. 

 

   Host-request.pl

 

#!/usr/bin/perl
#
# Copyright (c) 2008, Infoblox Inc.
#
# 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.
#

use strict ;
use POSIX qw(strftime);

# load the WebUI common libraries
use Infoblox::WebUI ;

# initialise the WebUI interface
my $sdb=Infoblox::WebUI->new( );

# get the CGI fields
my $name = $sdb->CGI->param('name') || "" ;
my $zone = $sdb->CGI->param('zone') || "" ;
my $view = $sdb->CGI->param('view') || "" ;
my $ipaddrs = $sdb->CGI->param('ipaddrs') || "" ;
my $aliases = $sdb->CGI->param('aliases') || "" ;
my $mac = $sdb->CGI->param('mac') || "" ;
my $comment = $sdb->CGI->param('comment') || "" ;

my $myTime=strftime "%Y %a %b %d %H:%M:%S", localtime();

my $mode = $sdb->CGI->param('mode') || "" ;

# check for defaults
unless ( $name and $ipaddrs ) {
    print $sdb->informerror("No name or ipaddrs specified");
    exit ;
}

# override the approval process, and do a direct submit
if ( $mode eq 'na' ) {
    $sdb->config( noapproval => 1 );
}

# get an FQDN from the name(s)
my $fqdn = $sdb->fqdn( $name , $zone ) ;

my $hobj ;
my $action = 'add';
# create a host or get one
if ( $mode eq 'modify' or $mode eq 'delete') {
    if ( $mode eq 'delete' ) {
        $action = 'delete';
    }
    else {
        $action = 'modify';
    }

    ( $hobj ) = $sdb->get(
        object => 'Infoblox::DNS::Host',
        name => $fqdn,
        view => $view,
        zone => $zone,
		
		
		
		
    );
    unless ( $hobj ) {
        print $sdb->informerror( $sdb->status_detail() ) ;
        exit ;
    }
}
else {

	#my $info = "locked by".$sdb->config('username');	


    $hobj = Infoblox::DNS::Host->new (
           "name" => $fqdn,
		   "comment" => $comment,
		  #extensible_attributes => { IB_workflow_lock => $info }
		  extensible_attributes => { 'Owner' => ${sdb}->config('username'),'Group' => ${sdb}->config('gid')}
		   
    );
}	

# set v4 or v6 addresses
#$sdb->set_addrs( $hobj , $ipaddrs ) ;

if ( $mac ) {

	
   my $fixed_addr = Infoblox::DHCP::FixedAddr->new(
           "ipv4addr"          => $ipaddrs,
           "match_client"      => "MAC",
           "mac"               => $mac,
           #"mac"               => "CA:FE:C0:FF:EE", #hardcoded, as an example
    );

	$hobj->ipv4addrs([$fixed_addr]);
	
    # set_addrs expects an ARRAYREF of ipaddreses, so we send as new list
	#    $sdb->set_addrs( $hobj , [ $fixed_addr ] ) ;
	#$sdb->set_addrs( $hobj , "152.325.2.36, 14.256.14.2, 51.2.32.1" ) ;
}
# if you want to add a fixed address, you need to create it here and add
# it to the set_addrs list.
# in this example we will splice the first address from the list

$sdb->set_param_EA( $hobj );

# [ ] call an custom handlers for the params validation
#   [ ] and any other modifications
$sdb->call_handlers( $hobj ) ;


#Failed (Original)
#A host record requires at least one IP address.

# set v4 or v6 addresses
#$sdb->set_addrs( $hobj , $ipaddrs ) ;

# set any aliases
my @allist = split ( /[,\s]+/ , $aliases );
if ( @allist ) { $hobj->aliases ( \@allist ) }

# add in any common params that we can add to ANY object
$sdb->set_param_common( $hobj ) ;

# add any extensible attribute params
#$sdb->set_param_EA( $hobj ) ;

# store any scheduling info
# this is a generic (non CGI function), so we have to pass the date/time
my $sdate = $sdb->CGI->param('publish_date') || "" ;
my $stime = $sdb->CGI->param('publish_time') || "" ;

# then schedule the request
# and pass an optional DATE/TIME for the request
my ( $res ) = $sdb->workflow_request( $action , $hobj , $sdate , $stime ) ;

if ( $res ) {
    # inform the results 
    print $sdb->informtext("Requested : $name");
}
else {
    print $sdb->informerror( $sdb->status_detail() ) ;
}
    

   UI-custom.js

// add input fields to a form 
IB.form.HostPanel.insertInput( 4, {
        fieldLabel: 'MAC Address',
        value: '',
        allowBlank: true,
        itemId: 'mac',
        name: 'mac'
    });
	
IB.form.HostPanel.addInput( {
        fieldLabel: 'Comment',
        value:'',
        allowBlank: true,
        itemId: 'comment',
        name: 'comment'
    });
	

 

If you are trying to add this functionality to the workflow snapin here is a few things to keep in mind:

1. Edit the UI-custom.js file to add MAC field & Comment

location: /snapin-workflow/htdocs/javascript/

2. Edit the host-request.pl

location: /snapin-workflow/cgi-bin/

 

 

 

Showing results for 
Search instead for 
Do you mean 

Recommended for You