Skip to content

Commit 98ad937

Browse files
author
Jean-François Hivert
committed
Version 2.1
1 parent 944ed9c commit 98ad937

24 files changed

Lines changed: 1571 additions & 1356 deletions

README.md

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,21 @@ __*https://launchpad.net/~ondrej/+archive/ubuntu/php*__
2727
* apt update
2828

2929
You have to install a PHP version >= 7.1:
30-
* apt update
31-
* apt install php7.2-cli php7.2-soap php7.2-mbstring php7.2-readline php7.2-curl
32-
__Do not forget to install php7.2-curl__
30+
* apt install php7.3-cli php7.3-soap php7.3-mbstring php7.3-readline php7.3-curl
31+
__Do not forget to install php7.3-curl__
32+
33+
For MacOS users which use PHP 7.3, there is an issue with PCRE.
34+
You have to add this configuration in your php.ini:
35+
```ini
36+
pcre.jit=0
37+
```
38+
*To locate your php.ini, use this command: php -i | grep "Configuration File"*
3339

3440
#### REPOSITORY
3541
* git clone https://github.com/cloudwatt/php-cli-shell_base
36-
* git checkout tags/v2.0
42+
* git checkout tags/v2.1
3743
* git clone https://github.com/cloudwatt/php-cli-shell_phpipam
38-
* git checkout tags/v2.0
44+
* git checkout tags/v2.1
3945
* Merge these two repositories
4046

4147
#### CONFIGURATION FILE

addons/ipam/adapter.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
namespace Addon\Ipam;
3+
4+
use Core as C;
5+
6+
abstract class Adapter extends C\Addon\Adapter
7+
{
8+
}

addons/ipam/api/abstract.php

Lines changed: 5 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -12,49 +12,12 @@ abstract class Api_Abstract extends C\Addon\Api_Abstract
1212
const SEPARATOR_PATH = ',';
1313
const SEPARATOR_SECTION = '#';
1414

15-
/**
16-
* @var string
17-
*/
18-
protected static $_parentAdapter = __NAMESPACE__ .'\Main';
19-
20-
/**
21-
* @var Addon\Ipam\Main
22-
*/
23-
protected static $_IPAM = null; // Global IPAM (enabled)
24-
25-
/**
26-
* @var Addon\Ipam\Main[]
27-
*/
28-
protected static $_aIPAM = array(); // a = all/array/available IPAM
29-
30-
/**
31-
* @var Addon\Ipam\Main
32-
*/
33-
protected $_IPAM_ = null; // Local IPAM (for this instance)
34-
35-
36-
public function __construct($objectId = null)
37-
{
38-
parent::__construct($objectId);
39-
$this->_IPAM_ = &$this->_ownerAdapter; // /!\ A executer avant _setObjectId
40-
$this->_setObjectId($objectId); // @todo temp
41-
}
4215

4316
public static function objectIdIsValid($objectId)
4417
{
4518
return C\Tools::is('int&&>0', $objectId);
4619
}
4720

48-
public function hasObjectId()
49-
{
50-
return ($this->_objectId !== null);
51-
}
52-
53-
public function getObjectId()
54-
{
55-
return $this->_objectId;
56-
}
57-
5821
public function objectExists()
5922
{
6023
if(!$this->hasObjectId()) {
@@ -111,87 +74,19 @@ public function __get($name)
11174
{
11275
case 'ipam':
11376
case '_IPAM': {
114-
return $this->_IPAM_;
115-
}
116-
case 'id': {
117-
return $this->getObjectId();
118-
}
119-
case 'name':
120-
case 'label': {
121-
return $this->getObjectLabel();
77+
return $this->_adapter;
12278
}
12379
default: {
124-
throw new Exception("This attribute '".$name."' does not exist", E_USER_ERROR);
125-
}
126-
}
127-
}
128-
129-
public function __call($method, $parameters = null)
130-
{
131-
if(substr($method, 0, 3) === 'get')
132-
{
133-
$name = substr($method, 3);
134-
$name = mb_strtolower($name);
135-
136-
switch($name)
137-
{
138-
case 'name': {
139-
return $this->_getField(static::FIELD_NAME, 'string&&!empty');
140-
}
80+
return parent::__get($name);
14181
}
14282
}
143-
144-
throw new Exception("Method '".$method."' does not exist", E_USER_ERROR);
145-
}
146-
147-
/**
148-
* @param Addon\Ipam\Main|Addon\Ipam\Main[] $IPAM
149-
* @return bool
150-
*/
151-
public static function setIpam($IPAM)
152-
{
153-
return self::setAdapter($IPAM);
154-
}
155-
156-
/**
157-
* @param Addon\Ipam\Main|Addon\Ipam\Main[] $adapter
158-
* @throw Core\Exception
159-
* @return bool
160-
*/
161-
public static function setAdapter($adapter)
162-
{
163-
$status = parent::setAdapter($adapter);
164-
165-
if($status) {
166-
self::$_IPAM = &self::$_adapter;
167-
self::$_aIPAM = &self::$_allAdapters;
168-
}
169-
170-
return $status;
171-
}
172-
173-
/**
174-
* @return null|Addon\Ipam\Main|Addon\Ipam\Main[]
175-
*/
176-
public static function getIpam()
177-
{
178-
return self::getAdapter();
179-
}
180-
181-
/**
182-
* @param string $key
183-
* @return bool
184-
*/
185-
public static function enableIpam($key)
186-
{
187-
return self::enableAdapter($key);
18883
}
18984

19085
/**
191-
* @return null|Addon\Ipam\Main
86+
* @return Addon\Ipam\Orchestrator
19287
*/
193-
public static function getIpamEnabled()
88+
protected static function _getOrchestrator()
19489
{
195-
return self::getAdapterEnabled();
90+
return Orchestrator::getInstance();
19691
}
19792
}

addons/ipam/api/address.php

Lines changed: 27 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,6 @@ class Api_Address extends Api_Abstract
2626
const RESERVED = 'reserved';
2727
const DHCP = 'DHCP';
2828

29-
/**
30-
* Enable or disable cache feature
31-
* /!\ Cache must be per type
32-
*
33-
* @var array
34-
*/
35-
protected static $_cache = array(); // IPAM server ID keys, boolean value
36-
37-
/**
38-
* All sections (cache)
39-
* /!\ Cache must be per type
40-
*
41-
* @var array
42-
*/
43-
protected static $_objects = array(); // IPAM server ID keys, array value
44-
4529
/**
4630
* @var int
4731
*/
@@ -98,7 +82,7 @@ protected function _getObject()
9882
if($this->_objectExists === null || $this->objectExists())
9983
{
10084
if($this->_objectDatas === null) {
101-
$this->_objectDatas = $this->_IPAM->getAddress($this->getAddressId());
85+
$this->_objectDatas = $this->_adapter->getAddress($this->getAddressId());
10286
}
10387

10488
return $this->_objectDatas;
@@ -174,7 +158,7 @@ public function getSubnet()
174158
$subnetId = $this->getSubnetId();
175159

176160
if($subnetId !== false) {
177-
return $this->_IPAM->getSubnet($subnetId);
161+
return $this->_adapter->getSubnet($subnetId);
178162
}
179163
else {
180164
return false;
@@ -254,7 +238,7 @@ public function getAddress()
254238
}
255239
}
256240

257-
public function create($description = '', $note = '', $port = '', $tag = self::ONLINE)
241+
public function create($description = '', $note = '', $port = '', $tag = self::ONLINE, $autoRegisterToStore = true)
258242
{
259243
$this->_errorMessage = null;
260244

@@ -271,7 +255,7 @@ public function create($description = '', $note = '', $port = '', $tag = self::O
271255
}
272256

273257
try {
274-
$status = $this->_IPAM->createAddress($this->getSubnetId(), $this->getAddress(), $this->getHostname(), $description, $note, $port, $tag);
258+
$status = $this->_adapter->createAddress($this->getSubnetId(), $this->getAddress(), $this->getHostname(), $description, $note, $port, $tag);
275259
}
276260
catch(E\Message $e) {
277261
$this->_errorMessage = $e->getMessage();
@@ -282,10 +266,15 @@ public function create($description = '', $note = '', $port = '', $tag = self::O
282266
{
283267
$addresses = $this->findIpAddresses($this->getAddress(), $this->getSubnetId(), true);
284268

285-
if($addresses !== false && count($addresses) === 1) {
269+
if($addresses !== false && count($addresses) === 1)
270+
{
286271
$addressId = $addresses[0][self::FIELD_ID];
287272
$this->_hardReset(false);
288273
$this->_setObjectId($addressId);
274+
275+
if($autoRegisterToStore) {
276+
$this->_registerToStore();
277+
}
289278
}
290279
else {
291280
$status = false;
@@ -362,7 +351,7 @@ protected function _updateInfos($label, $description)
362351
if($this->addressExists())
363352
{
364353
try {
365-
$status = $this->_IPAM->modifyAddress($this->getAddressId(), $label, $description);
354+
$status = $this->_adapter->modifyAddress($this->getAddressId(), $label, $description);
366355
}
367356
catch(E\Message $e) {
368357
$this->_errorMessage = $e->getMessage();
@@ -385,13 +374,14 @@ public function remove()
385374
if($this->addressExists())
386375
{
387376
try {
388-
$status = $this->_IPAM->removeAddress($this->getAddressId());
377+
$status = $this->_adapter->removeAddress($this->getAddressId());
389378
}
390379
catch(E\Message $e) {
391380
$this->_errorMessage = $e->getMessage();
392381
$status = false;
393382
}
394383

384+
$this->_unregisterFromStore();
395385
$this->_hardReset();
396386
return $status;
397387
}
@@ -514,7 +504,7 @@ public function getAddresses($address = '*', $IPv = null, $strict = false)
514504
public function findAddresses($address, $IPv = null, $strict = false)
515505
{
516506
if($this->hasAddressId()) {
517-
return self::_searchAddresses($this->_IPAM, $address, $IPv, $this->getSubnetId(), $strict);
507+
return self::_searchAddresses($this->_adapter, $address, $IPv, $this->getSubnetId(), $strict);
518508
}
519509
else {
520510
return false;
@@ -538,14 +528,14 @@ public static function searchAddresses($address, $IPv = null, $subnetId = null,
538528
/**
539529
* Return all addresses matches request
540530
*
541-
* @param Addon\Ipam\Main $IPAM IPAM connector
531+
* @param Addon\Ipam\Adapter $IPAM IPAM adapter
542532
* @param string $address Address IP or name, wildcard * is allowed
543533
* @param int $IPv IP version, 4 or 6
544534
* @param int $subnetId Subnet ID
545535
* @param bool $strict
546536
* @return false|array
547537
*/
548-
protected static function _searchAddresses(Main $IPAM = null, $address = '*', $IPv = null, $subnetId = null, $strict = false)
538+
protected static function _searchAddresses(Adapter $IPAM = null, $address = '*', $IPv = null, $subnetId = null, $strict = false)
549539
{
550540
if(Tools::isIP($address)) {
551541
return self::_searchIpAddresses($IPAM, $address, $subnetId, $strict);
@@ -564,7 +554,7 @@ protected static function _searchAddresses(Main $IPAM = null, $address = '*', $I
564554

565555
public function findIpAddresses($ip, $subnetId = null, $strict = false)
566556
{
567-
return self::_searchIpAddresses($this->_IPAM, $ip, $subnetId, $strict);
557+
return self::_searchIpAddresses($this->_adapter, $ip, $subnetId, $strict);
568558
}
569559

570560
public static function searchIpAddresses($ip, $subnetId = null, $strict = false)
@@ -573,29 +563,29 @@ public static function searchIpAddresses($ip, $subnetId = null, $strict = false)
573563
}
574564

575565
// $strict for future use
576-
protected static function _searchIpAddresses(Main $IPAM = null, $ip = '*', $subnetId = null, $strict = false)
566+
protected static function _searchIpAddresses(Adapter $IPAM = null, $ip = '*', $subnetId = null, $strict = false)
577567
{
578568
if($IPAM === null) {
579-
$IPAM = self::$_IPAM;
569+
$IPAM = self::_getAdapter();
580570
}
581571

582572
return $IPAM->searchAddressIP($ip, $subnetId, $strict);
583573
}
584574

585575
public function findAddressNames($name, $IPv = null, $subnetId = null, $strict = false)
586576
{
587-
return self::_searchAddressNames($this->_IPAM, $name, $IPv, $subnetId, $strict);
577+
return self::_searchAddressNames($this->_adapter, $name, $IPv, $subnetId, $strict);
588578
}
589579

590580
public static function searchAddressNames($name, $IPv = null, $subnetId = null, $strict = false)
591581
{
592582
return self::_searchAddressNames(null, $name, $IPv, $subnetId, $strict);
593583
}
594584

595-
protected static function _searchAddressNames(Main $IPAM = null, $name = '*', $IPv = null, $subnetId = null, $strict = false)
585+
protected static function _searchAddressNames(Adapter $IPAM = null, $name = '*', $IPv = null, $subnetId = null, $strict = false)
596586
{
597587
if($IPAM === null) {
598-
$IPAM = self::$_IPAM;
588+
$IPAM = self::_getAdapter();
599589
}
600590

601591
if($name === null) {
@@ -610,7 +600,7 @@ protected static function _searchAddressNames(Main $IPAM = null, $name = '*', $I
610600
$separator = preg_quote(self::SEPARATOR_SECTION, '#');
611601
$status = preg_match('#(?:'.$separator.'(?<section>.+?)'.$separator.')?(?<name>.+)#i', $name, $nameParts);
612602

613-
if($status && C\Tools::is('string&&!empty', $nameParts['section']) && C\Tools::is('string&&!empty', $descParts['name'])) {
603+
if($status && C\Tools::is('string&&!empty', $nameParts['section']) && C\Tools::is('string&&!empty', $nameParts['name'])) {
614604
$sectionNameFilter = $nameParts['section'];
615605
$name = $nameParts['name'];
616606
}
@@ -624,18 +614,18 @@ protected static function _searchAddressNames(Main $IPAM = null, $name = '*', $I
624614

625615
public function findAddressDescs($desc, $IPv = null, $subnetId = null, $strict = false)
626616
{
627-
return self::_searchAddressDescs($this->_IPAM, $desc, $IPv, $subnetId, $strict);
617+
return self::_searchAddressDescs($this->_adapter, $desc, $IPv, $subnetId, $strict);
628618
}
629619

630620
public static function searchAddressDescs($desc, $IPv = null, $subnetId = null, $strict = false)
631621
{
632622
return self::_searchAddressDescs(null, $desc, $IPv, $subnetId, $strict);
633623
}
634624

635-
protected static function _searchAddressDescs(Main $IPAM = null, $desc = '*', $IPv = null, $subnetId = null, $strict = false)
625+
protected static function _searchAddressDescs(Adapter $IPAM = null, $desc = '*', $IPv = null, $subnetId = null, $strict = false)
636626
{
637627
if($IPAM === null) {
638-
$IPAM = self::$_IPAM;
628+
$IPAM = self::_getAdapter();
639629
}
640630

641631
if($desc === null) {
@@ -666,7 +656,7 @@ protected static function _filterAddressOnSectionName(array $addresses, $field,
666656
{
667657
if(C\Tools::is('string&&!empty', $sectionNameFilter))
668658
{
669-
$sections = Api_Section::searchSections($nameParts['section'], null, true);
659+
$sections = Api_Section::searchSections($sectionNameFilter, null, true);
670660

671661
if($sections !== false && count($sections) === 1)
672662
{

0 commit comments

Comments
 (0)