Skip to content

Commit d176e5c

Browse files
author
Jean-François Hivert
committed
Version 1.1
1 parent 12086bd commit d176e5c

22 files changed

+996
-1165
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ __Do not forget to install php7.1-curl__
2323

2424
#### REPOSITORY
2525
* git clone https://github.com/cloudwatt/php-cli-shell_base
26-
* git checkout tags/v1.0
26+
* git checkout tags/v1.1
2727
* git clone https://github.com/cloudwatt/php-cli-shell_phpipam
28-
* git checkout tags/v1.0
28+
* git checkout tags/v1.1
2929
* Merge these two repositories
3030

3131
#### CONFIGURATION FILE

configurations/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.user.json

config.json.example renamed to configurations/config.json.example

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
{
22
"DEFAULT": {
33
"sys": {
4-
"browserCmd": "xdg-open"
4+
"browserCmd": "xdg-open",
5+
"secureShellCmd": "ssh"
56
}
67
},
78

89
"IPAM": {
910
"servers": {
10-
"[IPAM_SERVER_KEY]": "https://myPatchManagerServerAddress.ext",
11+
"[IPAM_SERVER_KEY]": "https://myPhpIpamServerAddress.ext",
1112
"myIpamKey": "https://demo.phpipam.net"
1213
},
1314

ipam.php.example

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,29 @@
11
<?php
2-
include_once('services/ipam.php');
2+
define("ROOT_DIR", __DIR__);
3+
require_once('services/ipam.php');
4+
5+
if(!isset($configurations))
6+
{
7+
$configurations = array(
8+
__DIR__ . '/configurations/config.json',
9+
__DIR__ . '/configurations/config.user.json',
10+
);
11+
}
12+
13+
/**
14+
* Déplace le curseur d'une ligne vers le haut
15+
* Fix le saut de ligne lors de la touche entrée pour lancer le script CLI
16+
*
17+
* Permet d'harmoniser le traitement des sauts de lignes:
18+
* --> Saut de ligne avant un texte et non après!
19+
*/
20+
echo "\033[1A";
321

422
/**
523
* Change [IPAM_SERVER_KEY] with the key of your PHPIAPM server in configuration file
6-
* Example: $MAIN = new Service_Ipam(__DIR__ . '/config.json', 'myIpamKey');
24+
* Example: $MAIN = new Service_Ipam($configurations, 'myIpamKey');
725
*/
8-
$MAIN = new Service_Ipam(__DIR__ . '/config.json', '[IPAM_SERVER_KEY]');
26+
$MAIN = new Service_Ipam($configurations, '[IPAM_SERVER_KEY]');
927

10-
echo "\r\n";
28+
echo PHP_EOL;
1129
exit();

ipam/abstract.php

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?php
2-
include_once(__DIR__ . '/main.php');
3-
include_once(__DIR__ . '/interface.php');
4-
include_once(__DIR__ . '/connector/abstract.php');
5-
include_once(__DIR__ . '/connector/sql.php');
6-
include_once(__DIR__ . '/connector/rest.php');
2+
require_once(__DIR__ . '/main.php');
3+
require_once(__DIR__ . '/interface.php');
4+
require_once(__DIR__ . '/connector/abstract.php');
5+
require_once(__DIR__ . '/connector/sql.php');
6+
require_once(__DIR__ . '/connector/rest.php');
77

88
abstract class IPAM_Abstract extends IPAM_Main implements IPAM_Interface
99
{
@@ -20,21 +20,21 @@ public function __construct(array $servers, $printInfoMessages = true)
2020
$server = mb_strtoupper($server);
2121

2222
if(!$config->servers->key_exists($server)) {
23-
throw new Exception("Unable to retreive IPAM server @ ".$server, E_USER_ERROR);
23+
throw new Exception("Unable to retreive IPAM server for '".$server."' from config", E_USER_ERROR);
2424
}
2525
elseif(!$config->contexts->key_exists($server)) {
26-
throw new Exception("Unable to retreive IPAM context @ ".$server, E_USER_ERROR);
26+
throw new Exception("Unable to retreive IPAM context for '".$server."' from config", E_USER_ERROR);
2727
}
2828
else
2929
{
3030
$login = getenv('IPAM_'.$server.'_LOGIN');
3131
$password = getenv('IPAM_'.$server.'_PASSWORD');
3232

3333
if($login === false || $password === false) {
34-
throw new Exception("Unable to retreive IPAM credentials for [".$server."] from env", E_USER_ERROR);
34+
throw new Exception("Unable to retreive IPAM credentials for '".$server."' from env", E_USER_ERROR);
3535
}
3636

37-
$this->_aIPAM[$server] = new IPAM_Connector_Rest($config->servers[$server], $config->contexts[$server], $login, $password, $printInfoMessages);
37+
$this->_aIPAM[$server] = new IPAM_Connector_Rest($server, $config->servers[$server], $config->contexts[$server], $login, $password, $printInfoMessages);
3838
}
3939
}
4040

@@ -79,6 +79,7 @@ public function getVlanNamesByVlanIds(array $vlanIds, array $environments)
7979
foreach($this->_aIPAM as $IPAM) {
8080
$result = $IPAM->getVlanNamesByVlanIds($vlanIds, $environments);
8181
$vlanNames = array_merge($vlanNames, $result);
82+
// @todo array_unique ??
8283
}
8384

8485
return $vlanNames;
@@ -121,6 +122,11 @@ public function getIpam($equipLabel = null)
121122
throw new Exception('Impossible de retourner le service IPAM adapté', E_USER_ERROR);
122123
}
123124

125+
public function getAllIpam()
126+
{
127+
return $this->_aIPAM;
128+
}
129+
124130
/*public function __call($name, array $arguments)
125131
{
126132
if($this->_oIPAM !== null) {
@@ -158,6 +164,8 @@ public function __get($name)
158164
return $this->_aIPAM['CORP'];
159165
case 'dev':
160166
return $this->_aIPAM['DEV'];
167+
case 'all':
168+
return $this->_aIPAM;
161169
}
162170
}
163171

ipam/api/abstract.php

Lines changed: 67 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
<?php
22
abstract class Ipam_Api_Abstract
33
{
4-
protected static $_IPAM = null;
4+
protected static $_IPAM = null; // Global IPAM (enabled)
5+
protected static $_aIPAM = array(); // a = all/array/available IPAM
6+
7+
protected $_IPAM_ = null; // Local IPAM (for this instance)
58

69
protected $_errorMessage = null;
710

@@ -14,6 +17,12 @@ abstract class Ipam_Api_Abstract
1417

1518
public function __construct($objectId = null)
1619
{
20+
/**
21+
* Permet de garder la référence de l'IPAM actuellement activé
22+
* pour cette instance d'Ipam_Api_Abstract
23+
*/
24+
$this->_IPAM_ = self::$_IPAM;
25+
1726
if($this->objectIdIsValid($objectId)) {
1827
$this->_objectId = (int) $objectId;
1928
$this->objectExists();
@@ -123,15 +132,16 @@ protected function _getSubObjects($objects, $fieldName, $name)
123132
}
124133
}
125134

126-
static protected function _searchObjects(array $objects, $fieldName, $name)
135+
protected static function _searchObjects(array $objects, $fieldName, $name, $strict = false)
127136
{
128137
$results = array();
129138
$name = preg_quote($name, '#');
130139
$name = str_replace('\*', '.*', $name);
140+
$name = ($strict) ? ('^('.$name.')$') : ('('.$name.')');
131141

132142
foreach($objects as $object)
133143
{
134-
if(preg_match('#('.$name.')#i', $object[$fieldName])) {
144+
if(preg_match('#'.$name.'#i', $object[$fieldName])) {
135145
$results[] = $object;
136146
}
137147
}
@@ -143,9 +153,14 @@ public function __get($name)
143153
{
144154
switch(mb_strtolower($name))
145155
{
156+
case 'name':
157+
case 'label': {
158+
return $this->getObjectLabel();
159+
}
146160
case 'ipam':
147-
case '_ipam': {
148-
return self::$_IPAM;
161+
case '_ipam':
162+
case '_ipam_': {
163+
return $this->_IPAM_;
149164
}
150165
default: {
151166
throw new Exception("This attribute '".$name."' does not exist", E_USER_ERROR);
@@ -155,16 +170,60 @@ public function __get($name)
155170

156171
public function __call($method, $parameters = null)
157172
{
158-
throw new Exception('Method '.$method.' does not exist', E_USER_ERROR);
173+
throw new Exception("Method '".$method."' does not exist", E_USER_ERROR);
159174
}
160175

161176
public function getErrorMessage()
162177
{
163178
return $this->_errorMessage;
164179
}
165180

166-
public static function setIpam(IPAM_Main $IPAM)
181+
public static function setIpam($IPAM)
182+
{
183+
if($IPAM instanceof IPAM_Main) {
184+
self::$_IPAM = $IPAM;
185+
return true;
186+
}
187+
elseif(Tools::is('array&&count>0', $IPAM))
188+
{
189+
$check = true;
190+
191+
foreach($IPAM as $_IPAM)
192+
{
193+
if(!($_IPAM instanceof IPAM_Main)) {
194+
$check = false;
195+
break;
196+
}
197+
}
198+
199+
if($check) {
200+
self::$_IPAM = current($IPAM);
201+
self::$_aIPAM = $IPAM;
202+
return true;
203+
}
204+
}
205+
206+
throw new Exception("Unable to set IPAM object(s), it is not IPAM_Main instance or an array of it", E_USER_ERROR);
207+
}
208+
209+
public static function getIpam()
210+
{
211+
return (count(self::$_aIPAM) > 0) ? (self::$_aIPAM) : (self::$_IPAM);
212+
}
213+
214+
public static function enableIpam($key)
215+
{
216+
if(array_key_exists($key, self::$_aIPAM)) {
217+
self::$_IPAM = self::$_aIPAM[$key];
218+
return true;
219+
}
220+
else {
221+
return false;
222+
}
223+
}
224+
225+
public static function getIpamEnabled()
167226
{
168-
self::$_IPAM = $IPAM;
227+
return self::$_IPAM->getServerId();
169228
}
170229
}

ipam/api/address.php

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ class Ipam_Api_Address extends Ipam_Api_Abstract
33
{
44
const OBJECT_TYPE = 'address';
55
const FIELD_NAME = 'hostname';
6+
const FIELD_DESC = 'description';
67

78
const TAGS = array(
89
1 => 'offline',
@@ -93,6 +94,9 @@ public function __get($name)
9394
switch($name)
9495
{
9596
case 'ip':
97+
case 'address': {
98+
return $this->_getField('ip', 'string&&!empty');
99+
}
96100
case 'hostname':
97101
case 'description':
98102
case 'tag':
@@ -133,18 +137,61 @@ public function __call($method, $parameters = null)
133137
return parent::__call($method, $parameters);
134138
}
135139

136-
public static function searchIpAddresses($ip)
140+
public function findIpAddresses($ip, $strict = false)
141+
{
142+
return self::_searchIpAddresses($ip, $this->_IPAM, $strict);
143+
}
144+
145+
public function findAddressNames($name, $strict = false)
146+
{
147+
return self::_searchAddressNames($name, $this->_IPAM, $strict);
148+
}
149+
150+
public function findAddressDescs($desc, $strict = false)
151+
{
152+
return self::_searchAddressDescs($desc, $this->_IPAM, $strict);
153+
}
154+
155+
public static function searchIpAddresses($ip, $strict = false)
156+
{
157+
return self::_searchIpAddresses($ip, null, $strict);
158+
}
159+
160+
public static function searchAddressNames($name, $strict = false)
137161
{
138-
return self::$_IPAM->searchAddresses($ip);
162+
return self::_searchAddressNames($name, null, $strict);
139163
}
140164

141-
public static function searchAddressNames($addressName)
165+
public static function searchAddressDescs($desc, $strict = false)
142166
{
143-
return self::$_IPAM->searchAddHostname($addressName);
167+
return self::_searchAddressDescs($desc, null, $strict);
144168
}
145169

146-
public static function searchAddressDescs($addressDesc)
170+
// $strict for future use
171+
protected static function _searchIpAddresses($ip, IPAM_Main $IPAM = null, $strict = false)
147172
{
148-
return self::$_IPAM->searchAddDescription($addressDesc);
173+
if($IPAM === null) {
174+
$IPAM = self::$_IPAM;
175+
}
176+
177+
return $IPAM->searchAddresses($ip);
178+
}
179+
180+
protected static function _searchAddressNames($name, IPAM_Main $IPAM = null, $strict = false)
181+
{
182+
if($IPAM === null) {
183+
$IPAM = self::$_IPAM;
184+
}
185+
186+
return $IPAM->searchAddHostname($name, $strict);
187+
}
188+
189+
protected static function _searchAddressDescs($desc, IPAM_Main $IPAM = null, $strict = false)
190+
{
191+
if($IPAM === null) {
192+
$IPAM = self::$_IPAM;
193+
}
194+
195+
return $IPAM->searchAddDescription($desc, $strict);
149196
}
150197
}

0 commit comments

Comments
 (0)