Skip to content

Commit 84a7883

Browse files
author
Jean-François Hivert
committed
Version 2.0
1 parent dfe8e82 commit 84a7883

File tree

144 files changed

+11408
-3561
lines changed

Some content is hidden

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

144 files changed

+11408
-3561
lines changed

README.md

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,41 +7,50 @@ You have to use base PHP-CLI SHELL project that is here: https://github.com/clou
77
# REQUIREMENTS
88

99
#### PATCHMANAGER
10-
* Import profiles which are in ressources/dcim
10+
* Import profiles which are in addons/dcim/ressources
1111
* formats: ressources/dcim/formats
1212
* Reports: ressources/dcim/reports
1313
* Searches: ressources/dcim/searches
14-
__*/!\ Version 1.1 add new profiles!*__
14+
15+
__*/!\ Do not rename custom profiles*__
16+
__*/!\ Version 2.0 add new profiles!*__
1517

1618

1719
# INSTALLATION
1820

1921
#### APT PHP
22+
Ubuntu only, you can get last PHP version from this PPA:
2023
__*https://launchpad.net/~ondrej/+archive/ubuntu/php*__
2124
* add-apt-repository ppa:ondrej/php
22-
* apt-get update
23-
* apt install php7.1-cli php7.1-mbstring php7.1-readline php7.1-soap
24-
__Do not forget to install php7.1-soap__
25+
* apt update
26+
27+
You have to install a PHP version >= 7.1:
28+
* apt update
29+
* apt install php7.2-cli php7.2-soap php7.2-mbstring php7.2-readline php7.2-curl
30+
__Do not forget to install php7.2-soap__
2531

2632
#### REPOSITORIES
2733
* git clone https://github.com/cloudwatt/php-cli-shell_base
28-
* git checkout tags/v1.1
34+
* git checkout tags/v2.0
2935
* git clone https://github.com/cloudwatt/php-cli-shell_patchmanager
30-
* git checkout tags/v1.1
36+
* git checkout tags/v2.0
3137
* Merge these two repositories
3238

3339
#### CONFIGURATION FILE
34-
* mv configurations/config.json.example configurations/config.json
35-
* vim configurations/config.json
36-
* servers field contains all PatchManager server addresses which must be identified by custom key [DCIM_SERVER_KEY]
40+
* mv configurations/dcim.json.example configurations/dcim.json
41+
* vim configurations/dcim.json
42+
* servers field contains all PatchManager server which must be identified by custom key [DCIM_SERVER_KEY]
3743
__server key must be unique and you will use it on next steps. You have an example in config file__
38-
* userAttrs field contains all custom attributes which must be created on your PatchManager
39-
__If you have a serial number custom attribute, change [PM_ATTR_SN] with the name of this attribute__
44+
* userAttrs section contains all custom attributes which must be created in your PatchManager
45+
__If you have a serial number custom attribute, change [PM_ATTR_SN] with the name of this attribute otherwise leave false__
46+
* preferences section contains all options about PatchManager
47+
__CSV delimiter option must be identical between PHP-CLI configuration and PatchManager user preferences__
4048
* Optionnal
4149
* You can create user configuration files to overwrite some configurations
4250
These files will be ignored for commits, so your user config files can not be overwrited by a futur release
43-
* vim configurations/config.user.json
44-
Change configuration like browserCmd
51+
* mv configurations/dcim.user.json.example configurations/dcim.user.json
52+
* vim configurations/dcim.user.json
53+
Change configuration like browserCmd or DCIM preferences
4554
* All *.user.json files are ignored by .gitignore
4655

4756
#### PHP LAUNCHER FILE
@@ -50,7 +59,7 @@ __Do not forget to install php7.1-soap__
5059
* Change [DCIM_SERVER_KEY] with the key of your PatchManager server in configuration file
5160

5261
#### CREDENTIALS FILE
53-
/!\ For security reason, use a read only account!
62+
/!\ For security reason, you can use a read only account!
5463
__*Change informations which are between []*__
5564
* vim credentialsFile
5665
* read -sr USER_PASSWORD_INPUT

addons/dcim/api/abstract.php

Lines changed: 307 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,307 @@
1+
<?php
2+
namespace Addon\Dcim;
3+
4+
use Core as C;
5+
6+
abstract class Api_Abstract extends C\Addon\Api_Abstract
7+
{
8+
const FIELD_ID = 'entity_id';
9+
const FIELD_NAME = 'name';
10+
11+
const WILDCARD = '*';
12+
const SEPARATOR_PATH = ',';
13+
14+
/**
15+
* @var string
16+
*/
17+
protected static $_parentAdapter = __NAMESPACE__ .'\Main';
18+
19+
/**
20+
* @var Addon\Dcim\Main
21+
*/
22+
protected static $_DCIM = null; // Global DCIM (enabled)
23+
24+
/**
25+
* @var Addon\Dcim\Main[]
26+
*/
27+
protected static $_aDCIM = array(); // a = all/array/available DCIM
28+
29+
/**
30+
* @var Addon\Dcim\Main
31+
*/
32+
protected $_DCIM_ = null; // Local DCIM (for this instance)
33+
34+
35+
public function __construct($objectId = null)
36+
{
37+
parent::__construct($objectId);
38+
$this->_DCIM_ = &$this->_ownerAdapter; // /!\ A executer avant _setObjectId
39+
$this->_setObjectId($objectId); // @todo temp
40+
}
41+
42+
public static function objectIdIsValid($objectId)
43+
{
44+
return C\Tools::is('int&&>0', $objectId);
45+
}
46+
47+
public function hasObjectId()
48+
{
49+
return ($this->_objectId !== null);
50+
}
51+
52+
public function getObjectId()
53+
{
54+
return $this->_objectId;
55+
}
56+
57+
public function objectExists()
58+
{
59+
if($this->_objectExists !== null) {
60+
return $this->_objectExists;
61+
}
62+
elseif($this->hasObjectId()) {
63+
$this->_objectExists = ($this->_getObject() !== false);
64+
return $this->_objectExists;
65+
}
66+
else {
67+
return false;
68+
}
69+
}
70+
71+
protected function _setObjectLabel($objectLabel)
72+
{
73+
if(!$this->objectExists() && C\Tools::is('string&&!empty', $objectLabel)) {
74+
$this->_objectLabel = $objectLabel;
75+
return true;
76+
}
77+
else {
78+
return false;
79+
}
80+
}
81+
82+
public function hasObjectLabel()
83+
{
84+
return ($this->getObjectLabel() !== false);
85+
}
86+
87+
public function getObjectLabel()
88+
{
89+
if($this->_objectLabel !== null) { // /!\ Ne pas appeler hasObjectLabel sinon boucle infinie
90+
return $this->_objectLabel;
91+
}
92+
elseif($this->hasObjectId()) { // /!\ Ne pas appeler objectExists sinon boucle infinie
93+
$objectDatas = $this->_getObject();
94+
$this->_objectLabel = ($objectDatas !== false) ? ($objectDatas[static::FIELD_NAME]) : (false);
95+
return $this->_objectLabel;
96+
}
97+
else {
98+
return false;
99+
}
100+
}
101+
102+
/**
103+
* Méthode courte comme getPath
104+
*/
105+
public function getLabel()
106+
{
107+
return $this->getObjectLabel();
108+
}
109+
110+
public function getTemplateName()
111+
{
112+
if($this->objectExists()) {
113+
$result = $this->_DCIM->resolvToTemplate(static::OBJECT_TYPE, $this->getObjectId());
114+
return ($this->_DCIM->isValidReturn($result)) ? ($result) : (false);
115+
}
116+
else {
117+
return false;
118+
}
119+
}
120+
121+
/**
122+
* @param string $category
123+
* @param null|string $attribute
124+
* @return false|string
125+
*/
126+
public function getUserAttrField($category, $attribute = null)
127+
{
128+
if(!C\Tools::is('string&&!empty', $attribute)) {
129+
$attribute = $category;
130+
$category = 'default';
131+
$noCateg = true;
132+
}
133+
134+
$attrField = $this->_DCIM->getUserAttrName($category, $attribute);
135+
return ($attrField === false && isset($noCateg)) ? ($attribute) : ($attrField);
136+
}
137+
138+
/**
139+
* @param string $category
140+
* @param null|string $attribute
141+
* @return false|string
142+
*/
143+
public function getUserAttribute($category, $attribute = null)
144+
{
145+
if($this->objectExists())
146+
{
147+
$attrField = $this->getUserAttrField($category, $attribute);
148+
149+
if($attrField !== false) {
150+
$result = $this->_DCIM->getUserAttrById(static::OBJECT_TYPE, $this->getObjectId(), $attrField);
151+
return ($this->_DCIM->isValidReturn($result)) ? ($result) : (false);
152+
}
153+
}
154+
155+
return false;
156+
}
157+
158+
public function __get($name)
159+
{
160+
switch($name)
161+
{
162+
case 'dcim':
163+
case '_DCIM': {
164+
return self::$_DCIM;
165+
}
166+
case 'id': {
167+
return $this->getObjectId();
168+
}
169+
case 'label': {
170+
return $this->getObjectLabel();
171+
}
172+
case 'templateName': {
173+
return $this->getTemplateName();
174+
}
175+
default: {
176+
throw new Exception("This attribute '".$name."' does not exist", E_USER_ERROR);
177+
}
178+
}
179+
}
180+
181+
public function __call($method, $parameters = null)
182+
{
183+
if(substr($method, 0, 3) === 'get')
184+
{
185+
$name = substr($method, 3);
186+
$name = mb_strtolower($name);
187+
188+
switch($name)
189+
{
190+
case 'label': {
191+
return $this->getObjectLabel();
192+
}
193+
}
194+
}
195+
196+
throw new Exception("Method '".$method."' does not exist", E_USER_ERROR);
197+
}
198+
199+
/**
200+
* @param string $section
201+
* @param array $attributes
202+
* @return false|string
203+
*/
204+
protected static function _getReportName($section, array $attributes)
205+
{
206+
if(array_key_exists($section, static::REPORT_NAMES))
207+
{
208+
$reportNames = static::REPORT_NAMES[$section];
209+
$attributes = implode('&', $attributes);
210+
211+
if(is_string($reportNames)) {
212+
return $reportNames;
213+
}
214+
elseif(is_array($reportNames) && array_key_exists($attributes, $reportNames)) {
215+
return $reportNames[$attributes];
216+
}
217+
}
218+
219+
return false;
220+
}
221+
222+
/**
223+
* @param string $reportName
224+
* @param null|array $args
225+
* @param false|string $fieldNameFilter
226+
* @throw Addon\Dcim\Exception
227+
* @return array
228+
*/
229+
protected static function _getObjectsFromReport($reportName, array $args = null, $fieldNameFilter = false)
230+
{
231+
if($args !== null)
232+
{
233+
array_walk($args, function(&$item)
234+
{
235+
if(!C\Tools::is('human', $item)) {
236+
$item = static::WILDCARD;
237+
}
238+
});
239+
}
240+
241+
// @todo use _getReportName
242+
$results = self::$_DCIM->getReportResults(static::REPORT_NAMES[$reportName], $args);
243+
244+
if(C\Tools::is('array&&count>0', $results))
245+
{
246+
if($fieldNameFilter !== false) {
247+
$results = array_column($results, $fieldNameFilter);
248+
}
249+
250+
return $results;
251+
}
252+
else {
253+
throw new Exception("Unable to retrieve objects from report '".$reportName."'", E_USER_ERROR);
254+
}
255+
}
256+
257+
/**
258+
* @param Addon\Dcim\Main|Addon\Dcim\Main[] $DCIM
259+
* @return bool
260+
*/
261+
public static function setDcim($DCIM)
262+
{
263+
return self::setAdapter($DCIM);
264+
}
265+
266+
/**
267+
* @param Addon\Dcim\Main|Addon\Dcim\Main[] $adapter
268+
* @throw Core\Exception
269+
* @return bool
270+
*/
271+
public static function setAdapter($adapter)
272+
{
273+
$status = parent::setAdapter($adapter);
274+
275+
if($status) {
276+
self::$_DCIM = &self::$_adapter;
277+
self::$_aDCIM = &self::$_allAdapters;
278+
}
279+
280+
return $status;
281+
}
282+
283+
/**
284+
* @return null|Addon\Dcim\Main|Addon\Dcim\Main[]
285+
*/
286+
public static function getDcim()
287+
{
288+
return self::getAdapter();
289+
}
290+
291+
/**
292+
* @param string $key
293+
* @return bool
294+
*/
295+
public static function enableDcim($key)
296+
{
297+
return self::enableAdapter($key);
298+
}
299+
300+
/**
301+
* @return null|Addon\Dcim\Main
302+
*/
303+
public static function getDcimEnabled()
304+
{
305+
return self::getAdapterEnabled();
306+
}
307+
}

0 commit comments

Comments
 (0)