Skip to content

Commit ade14ff

Browse files
author
Jean-François Hivert
committed
Version 2.1.2
1 parent e423b02 commit ade14ff

File tree

18 files changed

+2044
-748
lines changed

18 files changed

+2044
-748
lines changed

README.md

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
# PHP-CLI SHELL for PATCHMANAGER
1+
PHP-CLI SHELL for PATCHMANAGER
2+
-------------------
23

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

67

7-
# REQUIREMENTS
8+
REQUIREMENTS
9+
-------------------
810

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

1820

19-
# INSTALLATION
21+
INSTALLATION
22+
-------------------
2023

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

41+
42+
## USE PHAR
43+
44+
Download last PHAR release and its key from [releases](https://github.com/cloudwatt/php-cli-shell_patchmanager/releases)
45+
46+
![wizard](documentation/readme/wizard.gif)
47+
48+
Wizard help:
49+
`$ php php-cli-shell.phar --help`
50+
51+
Create firewall configuration with command:
52+
`$ php php-cli-shell.phar configuration:application:factory dcim`
53+
*For more informations about configuration file, see 'CONFIGURATION FILE' section*
54+
55+
Create firewall launcher with command:
56+
`$ php php-cli-shell.phar launcher:application:factory dcim`
57+
58+
__*The PHAR contains all PHP-CLI SHELL components (Base, DCIM, IPAM and Firewall)*__
59+
60+
61+
## USE SOURCE
62+
3863
#### REPOSITORIES
3964
* git clone https://github.com/cloudwatt/php-cli-shell_base
40-
* git checkout tags/v2.1.1
65+
* git checkout tags/v2.1.2
4166
* git clone https://github.com/cloudwatt/php-cli-shell_patchmanager
42-
* git checkout tags/v2.1.1
67+
* git checkout tags/v2.1.2
4368
* Merge these two repositories
4469

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

92+
93+
EXECUTION
94+
-------------------
95+
6796
#### CREDENTIALS FILE
6897
/!\ For security reason, you can use a read only account!
6998
__*Change informations which are between []*__
7099
* vim credentialsFile
71100
* read -sr USER_PASSWORD_INPUT
72101
* export DCIM_[DCIM_SERVER_KEY]_LOGIN=[YourLoginHere]
73102
* export DCIM_[DCIM_SERVER_KEY]_PASSWORD=$USER_PASSWORD_INPUT
74-
__Change [DCIM_SERVER_KEY] with the key of your PatchManager server in configuration file__
75-
76-
77-
# EXECUTION
103+
__Change [DCIM_SERVER_KEY] with the key of your PatchManager server in configuration file__
78104

79105
#### SHELL
80106
Launch PHP-CLI Shell for PatchManager service

addons/dcim/api/equipment.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -385,13 +385,16 @@ public function getSlotIds($slotLabel = null)
385385
*/
386386
public function getModuleId($slotId)
387387
{
388-
if($this->equipmentExists() && C\Tools::is('int&&>0', $slotId)) {
388+
if($this->equipmentExists() && C\Tools::is('int&&>0', $slotId))
389+
{
389390
$result = $this->_adapter->getEquipmentIdBySlotId($slotId);
390-
return ($this->_adapter->isValidReturn($result)) ? ((int) $result) : (false);
391-
}
392-
else {
393-
return false;
391+
392+
if($this->_adapter->isValidReturn($result)) {
393+
return (int) $result;
394+
}
394395
}
396+
397+
return false;
395398
}
396399

397400
/**

addons/dcim/api/equipment/abstract.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,33 @@ protected function _getObject()
3636
{
3737
return false;
3838
}
39+
40+
/**
41+
* Return the parent equipment ID this is on
42+
*
43+
* @return false|int
44+
*/
45+
abstract public function getParentEquipmentId();
46+
47+
/**
48+
* Return the first top module equipment ID this is on
49+
*
50+
* @return false|int
51+
*/
52+
public function getModuleEquipmentId()
53+
{
54+
$parentEquipmentId = $this->getParentEquipmentId();
55+
return ($parentEquipmentId !== false) ? ($this->_getModuleEquipmentId($parentEquipmentId)) : (false);
56+
}
57+
58+
/**
59+
* Return the first top module equipment ID this is on
60+
*
61+
* @return int
62+
*/
63+
protected function _getModuleEquipmentId($equipmentId, $moduleId = false)
64+
{
65+
$parentEquipId = $this->_adapter->getParentEquipmentIdByEquipmentId($equipmentId);
66+
return ($this->_adapter->isValidReturn($parentEquipId)) ? ($this->_getModuleEquipmentId($parentEquipId, $equipmentId)) : ($moduleId);
67+
}
3968
}

addons/dcim/api/equipment/port.php

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,7 @@ public function getEndConnectedPortId()
139139
*/
140140
public function getParentEquipmentId()
141141
{
142-
if(!$this->hasPortId() || !$this->portExists()) {
143-
return false;
144-
}
145-
else
142+
if($this->portExists())
146143
{
147144
$result = $this->_adapter->getParentEquipmentIdByPortId($this->getPortId());
148145

@@ -154,23 +151,6 @@ public function getParentEquipmentId()
154151
return false;
155152
}
156153

157-
/**
158-
* Return the first top module equipment ID this port is on
159-
*
160-
* @return false|int
161-
*/
162-
public function getModuleEquipmentId()
163-
{
164-
$parentEquipmentId = $this->getParentEquipmentId();
165-
return ($parentEquipmentId !== false) ? ($this->_getModuleEquipmentId($parentEquipmentId)) : (false);
166-
}
167-
168-
protected function _getModuleEquipmentId($equipmentId, $moduleId = false)
169-
{
170-
$parentEquipId = $this->_adapter->getParentEquipmentIdByEquipmentId($equipmentId);
171-
return ($this->_adapter->isValidReturn($parentEquipId)) ? ($this->_getModuleEquipmentId($parentEquipId, $equipmentId)) : ($moduleId);
172-
}
173-
174154
/**
175155
* Return the top equipment ID this port is on
176156
*

addons/dcim/api/equipment/slot.php

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,21 +48,27 @@ public function isEmpty()
4848
return ($this->getChildEquipmentId() === false);
4949
}
5050

51-
public function getChildEquipmentId()
51+
/**
52+
* Return the parent equipment ID this slot is on
53+
*
54+
* @return false|int
55+
*/
56+
public function getParentEquipmentId()
5257
{
53-
if(!$this->hasSlotId() || !$this->slotExists()) {
54-
return false;
55-
}
56-
elseif($this->_childEquipmentId === null) {
57-
$result = $this->_adapter->getEquipmentIdBySlotId($this->getSlotId());
58-
$this->_childEquipmentId = ($this->_adapter->isValidReturn($result)) ? ((int) $result) : (false);
58+
if($this->slotExists())
59+
{
60+
$result = $this->_adapter->getParentEquipmentIdBySlotId($this->getSlotId());
61+
62+
if($this->_adapter->isValidReturn($result)) {
63+
return (int) $result;
64+
}
5965
}
6066

61-
return $this->_childEquipmentId;
67+
return false;
6268
}
6369

6470
/**
65-
* Return the top equipment ID this port is on
71+
* Return the top equipment ID this slot is on
6672
*
6773
* @return false|int
6874
*/
@@ -125,6 +131,24 @@ public function getEquipmentApi()
125131
return $this->_equipmentApi;
126132
}
127133

134+
/**
135+
* Return the child equipment ID this slot is on
136+
*
137+
* @return false|int
138+
*/
139+
public function getChildEquipmentId()
140+
{
141+
if(!$this->hasSlotId() || !$this->slotExists()) {
142+
return false;
143+
}
144+
elseif($this->_childEquipmentId === null) {
145+
$result = $this->_adapter->getEquipmentIdBySlotId($this->getSlotId());
146+
$this->_childEquipmentId = ($this->_adapter->isValidReturn($result)) ? ((int) $result) : (false);
147+
}
148+
149+
return $this->_childEquipmentId;
150+
}
151+
128152
/**
129153
* @param bool $resetObjectId
130154
* @return void

addons/dcim/connector/soap.php

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,31 @@ public function __construct(Service $service, C\Config $config, $server, $login,
9898
protected function _initSoapAPI($key, $server, $urn, $httpProxy, $httpsProxy)
9999
{
100100
$this->_soapAPI->{$key} = new C\Soap($server.'/'.$urn, 'DCIM_'.$key, $this->_debug);
101+
102+
switch(substr($server, 0, 6))
103+
{
104+
case 'http:/':
105+
if(C\Tools::is('string&&!empty', $httpProxy)) {
106+
$useHttpProxy = $httpProxy;
107+
}
108+
break;
109+
case 'https:':
110+
if(C\Tools::is('string&&!empty', $httpsProxy)) {
111+
$useHttpProxy = $httpsProxy;
112+
}
113+
break;
114+
default:
115+
throw new Exception("L'adresse du serveur DCIM doit commencer par http ou https", E_USER_ERROR);
116+
}
117+
118+
if(isset($useHttpProxy) && preg_match('#^(http(?:s)?:\/\/[^:]*)(?::([0-9]*))?$#i', $useHttpProxy, $matches))
119+
{
120+
$this->_soapAPI->{$key}->setOpt('proxy_host', $matches[1]);
121+
122+
if(isset($matches[2])) {
123+
$this->_soapAPI->{$key}->setOpt('proxy_port', $matches[2]);
124+
}
125+
}
101126
}
102127

103128
public function getServerId()
@@ -242,7 +267,7 @@ public function getCabinetIdsByLocationId($locationId)
242267
$cabinetIds = array();
243268
$cabinetLabels = $this->getCabinetLabelsByLocationId($locationId);
244269

245-
foreach($cabinetLabels as $index => $cabinetLabel)
270+
foreach($cabinetLabels as $cabinetLabel)
246271
{
247272
if($this->isValidReturn($cabinetLabel))
248273
{
@@ -269,6 +294,11 @@ public function getCabinetLabelsByLocationId($locationId)
269294
return $this->_castToString($cabinetLabels);
270295
}
271296

297+
/**
298+
* @param int $locationId
299+
* @param string $cabinetLabel
300+
* @return int|string Cabinet ID or string if an error occurs
301+
*/
272302
public function getCabinetIdByLocationIdCabinetLabel($locationId, $cabinetLabel)
273303
{
274304
$args = $this->getArgs(array($cabinetLabel, $locationId));
@@ -765,23 +795,35 @@ public function getReportResults($reportName, array $wildCards = null)
765795
$results = $this->_soapAPI->search->reportToClient($args);
766796

767797
// [ERROR] [ERROR] Incorrect format of the Wild Cards.
768-
if($this->isValidReturn($results)) {
798+
if($this->isValidReturn($results))
799+
{
769800
$bytes = explode(';', $results->return);
770801
//$csv = implode(array_map("chr", $bytes));
771802
$csv = pack('C*', ...$bytes);
772803
$csv = str_replace("\n\000", '', $csv);
773804
$csv = explode("\n", $csv);
805+
774806
array_walk($csv, function(&$line, $key) {
775807
$line = str_getcsv($line, $this->_reportCsvDelimiter);
776808
});
809+
777810
$keys = array_shift($csv);
811+
778812
array_walk($keys, function(&$key) {
779813
$key = mb_strtolower($key);
780814
$key = str_replace(' ', '_', $key);
781815
});
782-
array_walk($csv, function(&$value, $key, $keys) {
783-
$value = array_combine($keys, $value);
816+
817+
array_walk($csv, function(&$value, $key, $keys)
818+
{
819+
if(count($keys) === count($value)) {
820+
$value = array_combine($keys, $value);
821+
}
822+
else {
823+
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);
824+
}
784825
}, $keys);
826+
785827
return $csv;
786828

787829
}

0 commit comments

Comments
 (0)