Skip to content

Commit

Permalink
[NF] Add pollable flag to switches
Browse files Browse the repository at this point in the history
  • Loading branch information
barryo committed Aug 20, 2020
1 parent 71af81f commit 00ccf4d
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 2 deletions.
2 changes: 1 addition & 1 deletion app/Console/Commands/Switches/SnmpPoll.php
Expand Up @@ -83,7 +83,7 @@ public function handle() {
}

} else {
$switches = D2EM::getRepository( SwitcherEntity::class )->getActive();
$switches = D2EM::getRepository( SwitcherEntity::class )->getPollable();
}

if( count( $switches ) ){
Expand Down
8 changes: 8 additions & 0 deletions app/Http/Controllers/Switches/SwitchController.php
Expand Up @@ -131,11 +131,17 @@ public function feInit(){
],

'infrastructure' => 'Infrastructure',

'active' => [
'title' => 'Active',
'type' => self::$FE_COL_TYPES[ 'YES_NO' ]
],

'poll' => [
'title' => 'Poll',
'type' => self::$FE_COL_TYPES[ 'YES_NO' ]
],

'model' => 'Model',

'ipv4addr' => 'IPv4 Address',
Expand Down Expand Up @@ -405,6 +411,7 @@ protected function addEditPrepareForm( $id = null ): array
'vendorid' => request()->old( 'vendorid', $this->object->getVendor() ? $this->object->getVendor()->getId() : null ),
'model' => request()->old( 'model', $this->object->getModel() ),
'active' => request()->old( 'active', ( $this->object->getActive() ? 1 : 0 ) ),
'poll' => request()->old( 'poll', ( $this->object->getPoll() ? 1 : 0 ) ),
'asn' => request()->old( 'asn', $this->object->getAsn() ),
'loopback_ip' => request()->old( 'loopback_ip', $this->object->getLoopbackIP() ),
'loopback_name' => request()->old( 'loopback_name', $this->object->getLoopbackName() ),
Expand Down Expand Up @@ -545,6 +552,7 @@ public function customStore( StoreRequest $request )
$this->object->setMgmtMacAddress( preg_replace( "/[^a-f0-9]/i", '', strtolower( $request->input( 'mgmt_mac_address', '' ) ) ) );

$this->object->setActive( $request->input( 'active' ) ?? false );
$this->object->setPoll( $request->input( 'poll' ) ?? false );

$this->object->setCabinet( D2EM::getRepository( CabinetEntity::class )->find( $request->input( 'cabinetid' ) ) );
$this->object->setInfrastructure( D2EM::getRepository( InfrastructureEntity::class )->find( $request->input( 'infrastructure' ) ) );
Expand Down
33 changes: 33 additions & 0 deletions database/Entities/Switcher.php
Expand Up @@ -145,6 +145,12 @@ class Switcher
*/
protected $active;

/**
* @var boolean poll
*/
protected $poll;


/**
* @var \DateTime
*/
Expand Down Expand Up @@ -515,6 +521,33 @@ public function getActive()



/**
* Set poll
*
* If true, the switch can be polled via SNMP/etc to update ports
*
* @param boolean $poll
* @return Switcher
*/
public function setPoll(bool $poll)
{
$this->poll = $poll;

return $this;
}

/**
* Get poll
*
* @return boolean Should the switch be polled via SNMP/etc
*/
public function getPoll(): bool
{
return $this->poll;
}



/**
* Set hostname
*
Expand Down
12 changes: 12 additions & 0 deletions database/Repositories/Switcher.php
Expand Up @@ -248,6 +248,17 @@ public function getActive(){
return $this->getEntityManager()->createQuery( $q )->getResult();
}

/**
* Get all active pollable switches as Doctrine2 objects
*
* @return array
*/
public function getPollable(){
$q = "SELECT s FROM \\Entities\\Switcher s WHERE s.active = 1 AND s.poll = 1";
return $this->getEntityManager()->createQuery( $q )->getResult();
}


/**
* Returns all switch ports for a given switch.
*
Expand Down Expand Up @@ -852,6 +863,7 @@ public function getAllForFeList( \stdClass $feParams, int $id = null, $params =
i.name AS infrastructure,
s.model AS model,
s.active AS active,
s.poll AS poll,
s.notes AS notes,
s.lastPolled AS lastPolled,
s.hostname AS hostname,
Expand Down
5 changes: 5 additions & 0 deletions database/xml/Entities.Switcher.dcm.xml
Expand Up @@ -23,6 +23,11 @@
<option name="default">1</option>
</options>
</field>
<field name="poll" type="boolean" nullable="false">
<options>
<option name="default">1</option>
</options>
</field>
<field name="os" type="string" nullable="true"/>
<field name="osDate" type="datetime" nullable="true" column="osDate"/>
<field name="osVersion" type="string" nullable="true" column="osVersion"/>
Expand Down
11 changes: 11 additions & 0 deletions resources/views/switches/edit-form.foil.php
Expand Up @@ -85,6 +85,17 @@
->check()
->blockHelp( "Inactive switches are removed from polling, monitoring, etc." );
?>

<?= Former::checkbox( 'poll' )
->label( '&nbsp;' )
->text( 'Poll' )
->value( 1 )
->inline()
->check()
->blockHelp( "Should this switch be polled automatically? Disabling this does not prevent you from manually polling the switch "
. "via the UI or via Artisan on the command line by explicity specifying it. It will just not be polled via the all switches "
. "Artisan <code>switch:snmp-poll</code> command." );
?>
</div>
</div>

Expand Down
8 changes: 7 additions & 1 deletion tests/Browser/SwitchControllerTest.php
Expand Up @@ -83,7 +83,8 @@ public function testAdd()
->assertInputValue( 'snmppasswd', 'mXPOSpC52cSFg1qN' )
->assertInputValue( 'model', 'FESX648' )
->assertSelected( 'vendorid', '12')
->assertChecked( 'active' );
->assertChecked( 'active' )
->assertChecked( 'poll' );

// 2. test add step 2
$browser->select( 'cabinetid', 1 )
Expand Down Expand Up @@ -113,6 +114,7 @@ public function testAdd()
$this->assertEquals( 12, $switch->getVendor()->getId() );
$this->assertEquals( 'FESX648', $switch->getModel() );
$this->assertEquals( true, $switch->getActive() );
$this->assertEquals( true, $switch->getPoll() );
$this->assertEquals( '192.0.2.1', $switch->getIpv4addr() );
$this->assertEquals( '2001:db8::999', $switch->getIpv6addr() );
$this->assertEquals( 'aa0011bb2287', $switch->getMgmtMacAddress() );
Expand Down Expand Up @@ -140,6 +142,7 @@ public function testAdd()
$this->assertEquals( 12, $switch->getVendor()->getId() );
$this->assertEquals( 'FESX648', $switch->getModel() );
$this->assertEquals( true, $switch->getActive() );
$this->assertEquals( true, $switch->getPoll() );
$this->assertEquals( '192.0.2.1', $switch->getIpv4addr() );
$this->assertEquals( '2001:db8::999', $switch->getIpv6addr() );
$this->assertEquals( 'aa0011bb2287', $switch->getMgmtMacAddress() );
Expand All @@ -160,6 +163,7 @@ public function testAdd()
->select( 'vendorid', 11 )
->type( 'model', 'TI24X' )
->uncheck( 'active' )
->uncheck( 'poll' )
->type( 'ipv4addr', '192.0.2.2' )
->type( 'ipv6addr', '2001:db8::9999' )
->type( 'mgmt_mac_address', 'AA:00.11:BB.22-88' )
Expand All @@ -183,6 +187,7 @@ public function testAdd()
$this->assertEquals( 11, $switch->getVendor()->getId() );
$this->assertEquals( 'TI24X', $switch->getModel() );
$this->assertEquals( false, $switch->getActive() );
$this->assertEquals( false, $switch->getPoll() );
$this->assertEquals( '192.0.2.2', $switch->getIpv4addr() );
$this->assertEquals( '2001:db8::9999', $switch->getIpv6addr() );
$this->assertEquals( 'aa0011bb2288', $switch->getMgmtMacAddress() );
Expand Down Expand Up @@ -210,6 +215,7 @@ public function testAdd()
$this->assertEquals( 11, $switch->getVendor()->getId() );
$this->assertEquals( 'TI24X', $switch->getModel() );
$this->assertEquals( false, $switch->getActive() );
$this->assertEquals( false, $switch->getPoll() );
$this->assertEquals( '192.0.2.2', $switch->getIpv4addr() );
$this->assertEquals( '2001:db8::9999', $switch->getIpv6addr() );
$this->assertEquals( 'aa0011bb2288', $switch->getMgmtMacAddress() );
Expand Down

0 comments on commit 00ccf4d

Please sign in to comment.