Skip to content

Commit 944ed9c

Browse files
author
Jean-François Hivert
committed
Version 2.0
1 parent d176e5c commit 944ed9c

48 files changed

Lines changed: 6614 additions & 4728 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

README.md

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,47 +8,64 @@ You have to use base PHP-CLI SHELL project that is here: https://github.com/clou
88

99
#### PHPIPAM
1010
/!\ Tested on PHPIPAM version 1.3.1: https://github.com/phpipam/phpipam
11-
* Copy all custom API controllers located in ressources/ipam on your PHPIPAM instance
12-
* Cw_addresses.php: /var/www/phpipam/api/controllers/custom/Cw_addresses.php
11+
* Copy all custom API controllers located in addons/ipam/ressources on your PHPIPAM instance
12+
* Cw_sections.php: /var/www/phpipam/api/controllers/custom/Cw_sections.php
13+
* Cw_subnets.php: /var/www/phpipam/api/controllers/custom/Cw_subnets.php
14+
* Cw_vlans.php: /var/www/phpipam/api/controllers/custom/Cw_vlans.php
15+
* Cw_addresses.php: /var/www/phpipam/api/controllers/custom/Cw_addresses.php
16+
17+
__*/!\ Do not rename custom controllers*__
18+
__*/!\ Version 2.0 add new profiles!*__
1319

1420

1521
# INSTALLATION
1622

1723
#### APT PHP
24+
Ubuntu only, you can get last PHP version from this PPA:
1825
__*https://launchpad.net/~ondrej/+archive/ubuntu/php*__
1926
* add-apt-repository ppa:ondrej/php
20-
* apt-get update
21-
* apt install php7.1-cli php7.1-mbstring php7.1-readline php7.1-curl
22-
__Do not forget to install php7.1-curl__
27+
* apt update
28+
29+
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__
2333

2434
#### REPOSITORY
2535
* git clone https://github.com/cloudwatt/php-cli-shell_base
26-
* git checkout tags/v1.1
36+
* git checkout tags/v2.0
2737
* git clone https://github.com/cloudwatt/php-cli-shell_phpipam
28-
* git checkout tags/v1.1
38+
* git checkout tags/v2.0
2939
* Merge these two repositories
3040

3141
#### CONFIGURATION FILE
32-
* mv config.json.example config.json
33-
* vim config.json
34-
* servers field contains all PHPIPAM server addresses which must be identified by custom key [IPAM_SERVER_KEY]
42+
* mv configurations/ipam.json.example configurations/ipam.json
43+
* vim configurations/ipam.json
44+
* servers field contains all PHPIPAM server which must be identified by custom key [IPAM_SERVER_KEY]
3545
__server key must be unique and you will use it on next steps. You have an example in config file__
36-
* contexts field contains all API application name configured on your PHPIPAM instance
46+
* contexts section contains all API application name configured on your PHPIPAM instance
3747
__On your PHPIPAM instance, go to Administration > API and create application without code or security__
48+
* Optionnal
49+
* You can create user configuration files to overwrite some configurations
50+
These files will be ignored for commits, so your user config files can not be overwrited by a futur release
51+
* mv configurations/ipam.user.json.example configurations/ipam.user.json
52+
* vim configurations/ipam.user.json
53+
Change configuration like browserCmd
54+
* All *.user.json files are ignored by .gitignore
3855

3956
#### PHP LAUNCHER FILE
4057
* mv ipam.php.example phpipam.php
4158
* vim phpipam.php
4259
* Change [IPAM_SERVER_KEY] with the key of your PHPIPAM server in configuration file
4360

4461
#### CREDENTIALS FILE
45-
/!\ For security reason, use a read only account or API app configured in read only mode!
62+
/!\ For security reason, you can use a read only account or API app configured in read only mode!
4663
__*Change informations which are between []*__
4764
* vim credentialsFile
4865
* read -sr USER_PASSWORD_INPUT
4966
* export IPAM_[IPAM_SERVER_KEY]_LOGIN=[YourLoginHere]
5067
* export IPAM_[IPAM_SERVER_KEY]_PASSWORD=$USER_PASSWORD_INPUT
51-
__Change [IPAM_SERVER_KEY] with the key of your PHPIPAM server in configuration file__
68+
__Change [IPAM_SERVER_KEY] with the key of your PHPIPAM server in configuration file__
5269

5370

5471
# EXECUTION

addons/ipam/api/abstract.php

Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
<?php
2+
namespace Addon\Ipam;
3+
4+
use Core as C;
5+
6+
abstract class Api_Abstract extends C\Addon\Api_Abstract
7+
{
8+
const FIELD_ID = 'id';
9+
const FIELD_NAME = 'name';
10+
11+
const WILDCARD = '*';
12+
const SEPARATOR_PATH = ',';
13+
const SEPARATOR_SECTION = '#';
14+
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+
}
42+
43+
public static function objectIdIsValid($objectId)
44+
{
45+
return C\Tools::is('int&&>0', $objectId);
46+
}
47+
48+
public function hasObjectId()
49+
{
50+
return ($this->_objectId !== null);
51+
}
52+
53+
public function getObjectId()
54+
{
55+
return $this->_objectId;
56+
}
57+
58+
public function objectExists()
59+
{
60+
if(!$this->hasObjectId()) {
61+
return false;
62+
}
63+
elseif($this->_objectExists === null) {
64+
$this->_objectExists = ($this->_getObject() !== false);
65+
}
66+
return $this->_objectExists;
67+
}
68+
69+
protected function _setObjectLabel($objectLabel)
70+
{
71+
if(!$this->objectExists() && C\Tools::is('string&&!empty', $objectLabel)) {
72+
$this->_objectLabel = $objectLabel;
73+
return true;
74+
}
75+
else {
76+
return false;
77+
}
78+
}
79+
80+
public function hasObjectLabel()
81+
{
82+
return ($this->getObjectLabel() !== false);
83+
}
84+
85+
public function getObjectLabel()
86+
{
87+
if($this->_objectLabel !== null) { // /!\ Ne pas appeler hasObjectLabel sinon boucle infinie
88+
return $this->_objectLabel;
89+
}
90+
elseif($this->hasObjectId()) { // /!\ Ne pas appeler objectExists sinon boucle infinie
91+
$objectDatas = $this->_getObject();
92+
$this->_objectLabel = ($objectDatas !== false) ? ($objectDatas[static::FIELD_NAME]) : (false);
93+
return $this->_objectLabel;
94+
}
95+
else {
96+
return false;
97+
}
98+
}
99+
100+
/**
101+
* Méthode courte comme getPath
102+
*/
103+
public function getLabel()
104+
{
105+
return $this->getObjectLabel();
106+
}
107+
108+
public function __get($name)
109+
{
110+
switch($name)
111+
{
112+
case 'ipam':
113+
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();
122+
}
123+
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+
}
141+
}
142+
}
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);
188+
}
189+
190+
/**
191+
* @return null|Addon\Ipam\Main
192+
*/
193+
public static function getIpamEnabled()
194+
{
195+
return self::getAdapterEnabled();
196+
}
197+
}

0 commit comments

Comments
 (0)