Cisco-ACI

 view release on metacpan or  search on metacpan

lib/Cisco/ACI.pm  view on Meta::CPAN

package Cisco::ACI;

use strict;
use warnings;

use Carp;
use JSON;
use HTTP::Request;
use LWP;
use XML::Simple;
use Cisco::ACI::Rule;
use Cisco::ACI::FvcapRule;
use Cisco::ACI::FvAEPg;
use Cisco::ACI::FvBD;
use Cisco::ACI::FvCEp;
use Cisco::ACI::FvRsBd;
use Cisco::ACI::Leaf;
use Cisco::ACI::Spine;
use Cisco::ACI::Tenant;
use Cisco::ACI::FaultCounts;
use Cisco::ACI::Eqpt::ExtCh;
use Cisco::ACI::Fault::Inst;
use Cisco::ACI::Fabric::Link;
use Cisco::ACI::Fabric::Pod;
use Cisco::ACI::Health::Inst;
use Cisco::ACI::Infra::WiNode;
use Cisco::ACI::Stats::Curr::OverallHealth;

our $VERSION = '0.151';

our @LOGIN_ATTR = qw(
buildTime
creationTime
firstLoginTime
firstName
guiIdleTimeoutSeconds
lastName
maximumLifetimeSeconds
node
refreshTimeoutSeconds
remoteUser
restTimeoutSeconds
sessionId
siteFingerprint
token
unixUserId
userName
version
);

our %OBJ_MAPPING = (
	tenant		=> 'fvTenant',
	contract	=> 'vzBrCP',
	service_graph	=> 'vnsGraphInst',
	concrete_devices=> 'vnsCDev',
	bd		=> 'fvBD',
	epg		=> 'fvAEPg',
	vrf		=> 'fvCtx',
	ep		=> 'fvCEp'
);

{
	for my $obj ( keys %OBJ_MAPPING ) {
		no strict 'refs';

		*{ __PACKAGE__ . "::$obj\_constraint" } = sub {
			my $self = shift;
			return $self->__get_constraint( $OBJ_MAPPING{ $obj } )

lib/Cisco/ACI.pm  view on Meta::CPAN

		$self->__request( 
			$self->__get_uri( "/api/class/fabricNode.json?query-target-filter=eq(fabricNode.role,\"$role\")" ) 
		)->content
	)->{ imdata } }
}

# While the controllers() method retrieves the APIC controllers as Cisco::ACI::FabricNode objects,
# this method retrieves 
sub __apic_appliances {

}

sub health {
	my $self = shift;

	return Cisco::ACI::Health::Inst->new( 
		$self->{ __jp }->decode( 
			$self->__request( 
				$self->__get_uri( '/api/node/mo/topology/health.json' ) 
			)->content
		)->{ imdata }->[0]->{ fabricHealthTotal }->{ attributes }
	)
}

sub overallHealth5min {
	my $self = shift;

	my $r = $self->{ __jp }->decode( 
		$self->__request( 
			$self->__get_uri( '/api/node/mo/topology/HDfabricOverallHealth5min-0.json' ) 
		)->content
	)->{ imdata }->[0];

	#print "r = $r->{ fabricOverallHealthHist5min }->{ attributes }->{ healthAvg }\n";

	my $h = Cisco::ACI::Stats::Curr::OverallHealth->new(
			healthAvg => $r->{ fabricOverallHealthHist5min }->{ attributes }->{ healthAvg }
	);	

	return $h
}

sub faults {
	my $self = shift;

	return map {
		Cisco::ACI::Fault::Inst->new( $_->{ faultInst }->{ attributes } )
	} @{ $self->{ __jp }->decode( 
		$self->__request( 
			$self->__get_uri( '/api/class/faultInst.json' )
		)->content
	)->{ imdata } }

}

sub __request {
	my ( $self, $uri, $data ) = @_;
	my $r;

	if ( $data ) {
		$r = HTTP::Request->new( POST => $uri );
		$r->content( $data );
	}
	else {
		$r = HTTP::Request->new( GET => $uri );
	}

	my $s = $self->{ __ua }->request( $r );
	$self->{ error } = "Login failure: $!" unless $s->is_success;

	return $s
}

sub __get_uri {
	my ( $self, $uri ) = @_;

	return ( $self->{ proto }
		. '://'
		. $self->{ server }
		. ':'
		. $self->{ port }
		. $uri
		)
}

sub __get_login_uri {
	my $self = shift;

	return $self->__get_uri( '/api/mo/aaaLogin.json' )
}

sub __get_fltCnts_uri {
	my $self = shift;

	return $self->__get_uri( '/api/node/mo/fltCnts.json' )
}

1;

__END__
=head2 NAME
Cisco::ACI - Perl interface to the Cisco APIC API.
=head2 SYNOPSIS
This module provides a Perl interface to Cisco APIC API.
    use Cisco::ACI;
    my $aci = Cisco::ACI->new(
			username=> $username,
			password=> $password,
			server	=> $server
    );
    # Required!
    $aci->login;
    # Get the leaf nodes of the pod as an array of
    # Cisco::ACI::FabricNode objects.
    my @leafs = $aci->get_leafs;
    # Same but for spines (both leaf and spine nodes
    # are Cisco::ACI::FabricNode objects).
    my @spines = $aci->get_spines;
    # Or, get a leaf by ID - still returns a 
    # Cisco::ACI::FabricNode object.
    my $leaf = $aci->(101);
    # Do some interesting stuff with it.
    # Like, get the 5min measurements of policy CAM (TCAM) usage
    # as a Cisco::ACI::Eqptcapacity::PolUsage object.
    my $pol_usage = $leaf->PolUsage( '5min' );



( run in 1.359 second using v1.01-cache-2.11-cpan-5a3173703d6 )