Skip to content

Commit

Permalink
Version 2.1.2
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean-François Hivert committed Jun 24, 2019
1 parent e423b02 commit ade14ff
Show file tree
Hide file tree
Showing 18 changed files with 2,044 additions and 748 deletions.
44 changes: 35 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# PHP-CLI SHELL for PATCHMANAGER
PHP-CLI SHELL for PATCHMANAGER
-------------------

This repository is the addon for PHP-CLI SHELL about PATCHMANAGER service.
You have to use base PHP-CLI SHELL project that is here: https://github.com/cloudwatt/php-cli-shell_base


# REQUIREMENTS
REQUIREMENTS
-------------------

#### PATCHMANAGER
* Import profiles which are in addons/dcim/ressources
Expand All @@ -16,7 +18,8 @@ __*/!\ Do not rename custom profiles*__
__*/!\ Version 2.0 add new profiles!*__


# INSTALLATION
INSTALLATION
-------------------

#### APT PHP
Ubuntu only, you can get last PHP version from this PPA:
Expand All @@ -35,11 +38,33 @@ pcre.jit=0
```
*To locate your php.ini, use this command: php -i | grep "Configuration File"*


## USE PHAR

Download last PHAR release and its key from [releases](https://github.com/cloudwatt/php-cli-shell_patchmanager/releases)

![wizard](documentation/readme/wizard.gif)

Wizard help:
`$ php php-cli-shell.phar --help`

Create firewall configuration with command:
`$ php php-cli-shell.phar configuration:application:factory dcim`
*For more informations about configuration file, see 'CONFIGURATION FILE' section*

Create firewall launcher with command:
`$ php php-cli-shell.phar launcher:application:factory dcim`

__*The PHAR contains all PHP-CLI SHELL components (Base, DCIM, IPAM and Firewall)*__


## USE SOURCE

#### REPOSITORIES
* git clone https://github.com/cloudwatt/php-cli-shell_base
* git checkout tags/v2.1.1
* git checkout tags/v2.1.2
* git clone https://github.com/cloudwatt/php-cli-shell_patchmanager
* git checkout tags/v2.1.1
* git checkout tags/v2.1.2
* Merge these two repositories

#### CONFIGURATION FILE
Expand All @@ -64,17 +89,18 @@ pcre.jit=0
* vim patchmanager.php
* Change [DCIM_SERVER_KEY] with the key of your PatchManager server in configuration file


EXECUTION
-------------------

#### CREDENTIALS FILE
/!\ For security reason, you can use a read only account!
__*Change informations which are between []*__
* vim credentialsFile
* read -sr USER_PASSWORD_INPUT
* export DCIM_[DCIM_SERVER_KEY]_LOGIN=[YourLoginHere]
* export DCIM_[DCIM_SERVER_KEY]_PASSWORD=$USER_PASSWORD_INPUT
__Change [DCIM_SERVER_KEY] with the key of your PatchManager server in configuration file__


# EXECUTION
__Change [DCIM_SERVER_KEY] with the key of your PatchManager server in configuration file__

#### SHELL
Launch PHP-CLI Shell for PatchManager service
Expand Down
13 changes: 8 additions & 5 deletions addons/dcim/api/equipment.php
Original file line number Diff line number Diff line change
Expand Up @@ -385,13 +385,16 @@ public function getSlotIds($slotLabel = null)
*/
public function getModuleId($slotId)
{
if($this->equipmentExists() && C\Tools::is('int&&>0', $slotId)) {
if($this->equipmentExists() && C\Tools::is('int&&>0', $slotId))
{
$result = $this->_adapter->getEquipmentIdBySlotId($slotId);
return ($this->_adapter->isValidReturn($result)) ? ((int) $result) : (false);
}
else {
return false;

if($this->_adapter->isValidReturn($result)) {
return (int) $result;
}
}

return false;
}

/**
Expand Down
29 changes: 29 additions & 0 deletions addons/dcim/api/equipment/abstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,33 @@ protected function _getObject()
{
return false;
}

/**
* Return the parent equipment ID this is on
*
* @return false|int
*/
abstract public function getParentEquipmentId();

/**
* Return the first top module equipment ID this is on
*
* @return false|int
*/
public function getModuleEquipmentId()
{
$parentEquipmentId = $this->getParentEquipmentId();
return ($parentEquipmentId !== false) ? ($this->_getModuleEquipmentId($parentEquipmentId)) : (false);
}

/**
* Return the first top module equipment ID this is on
*
* @return int
*/
protected function _getModuleEquipmentId($equipmentId, $moduleId = false)
{
$parentEquipId = $this->_adapter->getParentEquipmentIdByEquipmentId($equipmentId);
return ($this->_adapter->isValidReturn($parentEquipId)) ? ($this->_getModuleEquipmentId($parentEquipId, $equipmentId)) : ($moduleId);
}
}
22 changes: 1 addition & 21 deletions addons/dcim/api/equipment/port.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,7 @@ public function getEndConnectedPortId()
*/
public function getParentEquipmentId()
{
if(!$this->hasPortId() || !$this->portExists()) {
return false;
}
else
if($this->portExists())
{
$result = $this->_adapter->getParentEquipmentIdByPortId($this->getPortId());

Expand All @@ -154,23 +151,6 @@ public function getParentEquipmentId()
return false;
}

/**
* Return the first top module equipment ID this port is on
*
* @return false|int
*/
public function getModuleEquipmentId()
{
$parentEquipmentId = $this->getParentEquipmentId();
return ($parentEquipmentId !== false) ? ($this->_getModuleEquipmentId($parentEquipmentId)) : (false);
}

protected function _getModuleEquipmentId($equipmentId, $moduleId = false)
{
$parentEquipId = $this->_adapter->getParentEquipmentIdByEquipmentId($equipmentId);
return ($this->_adapter->isValidReturn($parentEquipId)) ? ($this->_getModuleEquipmentId($parentEquipId, $equipmentId)) : ($moduleId);
}

/**
* Return the top equipment ID this port is on
*
Expand Down
42 changes: 33 additions & 9 deletions addons/dcim/api/equipment/slot.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,27 @@ public function isEmpty()
return ($this->getChildEquipmentId() === false);
}

public function getChildEquipmentId()
/**
* Return the parent equipment ID this slot is on
*
* @return false|int
*/
public function getParentEquipmentId()
{
if(!$this->hasSlotId() || !$this->slotExists()) {
return false;
}
elseif($this->_childEquipmentId === null) {
$result = $this->_adapter->getEquipmentIdBySlotId($this->getSlotId());
$this->_childEquipmentId = ($this->_adapter->isValidReturn($result)) ? ((int) $result) : (false);
if($this->slotExists())
{
$result = $this->_adapter->getParentEquipmentIdBySlotId($this->getSlotId());

if($this->_adapter->isValidReturn($result)) {
return (int) $result;
}
}

return $this->_childEquipmentId;
return false;
}

/**
* Return the top equipment ID this port is on
* Return the top equipment ID this slot is on
*
* @return false|int
*/
Expand Down Expand Up @@ -125,6 +131,24 @@ public function getEquipmentApi()
return $this->_equipmentApi;
}

/**
* Return the child equipment ID this slot is on
*
* @return false|int
*/
public function getChildEquipmentId()
{
if(!$this->hasSlotId() || !$this->slotExists()) {
return false;
}
elseif($this->_childEquipmentId === null) {
$result = $this->_adapter->getEquipmentIdBySlotId($this->getSlotId());
$this->_childEquipmentId = ($this->_adapter->isValidReturn($result)) ? ((int) $result) : (false);
}

return $this->_childEquipmentId;
}

/**
* @param bool $resetObjectId
* @return void
Expand Down
50 changes: 46 additions & 4 deletions addons/dcim/connector/soap.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,31 @@ public function __construct(Service $service, C\Config $config, $server, $login,
protected function _initSoapAPI($key, $server, $urn, $httpProxy, $httpsProxy)
{
$this->_soapAPI->{$key} = new C\Soap($server.'/'.$urn, 'DCIM_'.$key, $this->_debug);

switch(substr($server, 0, 6))
{
case 'http:/':
if(C\Tools::is('string&&!empty', $httpProxy)) {
$useHttpProxy = $httpProxy;
}
break;
case 'https:':
if(C\Tools::is('string&&!empty', $httpsProxy)) {
$useHttpProxy = $httpsProxy;
}
break;
default:
throw new Exception("L'adresse du serveur DCIM doit commencer par http ou https", E_USER_ERROR);
}

if(isset($useHttpProxy) && preg_match('#^(http(?:s)?:\/\/[^:]*)(?::([0-9]*))?$#i', $useHttpProxy, $matches))
{
$this->_soapAPI->{$key}->setOpt('proxy_host', $matches[1]);

if(isset($matches[2])) {
$this->_soapAPI->{$key}->setOpt('proxy_port', $matches[2]);
}
}
}

public function getServerId()
Expand Down Expand Up @@ -242,7 +267,7 @@ public function getCabinetIdsByLocationId($locationId)
$cabinetIds = array();
$cabinetLabels = $this->getCabinetLabelsByLocationId($locationId);

foreach($cabinetLabels as $index => $cabinetLabel)
foreach($cabinetLabels as $cabinetLabel)
{
if($this->isValidReturn($cabinetLabel))
{
Expand All @@ -269,6 +294,11 @@ public function getCabinetLabelsByLocationId($locationId)
return $this->_castToString($cabinetLabels);
}

/**
* @param int $locationId
* @param string $cabinetLabel
* @return int|string Cabinet ID or string if an error occurs
*/
public function getCabinetIdByLocationIdCabinetLabel($locationId, $cabinetLabel)
{
$args = $this->getArgs(array($cabinetLabel, $locationId));
Expand Down Expand Up @@ -765,23 +795,35 @@ public function getReportResults($reportName, array $wildCards = null)
$results = $this->_soapAPI->search->reportToClient($args);

// [ERROR] [ERROR] Incorrect format of the Wild Cards.
if($this->isValidReturn($results)) {
if($this->isValidReturn($results))
{
$bytes = explode(';', $results->return);
//$csv = implode(array_map("chr", $bytes));
$csv = pack('C*', ...$bytes);
$csv = str_replace("\n\000", '', $csv);
$csv = explode("\n", $csv);

array_walk($csv, function(&$line, $key) {
$line = str_getcsv($line, $this->_reportCsvDelimiter);
});

$keys = array_shift($csv);

array_walk($keys, function(&$key) {
$key = mb_strtolower($key);
$key = str_replace(' ', '_', $key);
});
array_walk($csv, function(&$value, $key, $keys) {
$value = array_combine($keys, $value);

array_walk($csv, function(&$value, $key, $keys)
{
if(count($keys) === count($value)) {
$value = array_combine($keys, $value);
}
else {
throw new Exception("DCIM report result is not valid, head field number (".count($keys).") do not match value field number (".count($value).")", E_USER_ERROR);
}
}, $keys);

return $csv;

}
Expand Down
Loading

0 comments on commit ade14ff

Please sign in to comment.