Skip to content

Commit e75dbb0

Browse files
author
Jean-François Hivert
committed
Version 1.1
1 parent 51fdecb commit e75dbb0

16 files changed

Lines changed: 772 additions & 395 deletions

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ You have to use base PHP-CLI SHELL project that is here: https://github.com/clou
1010
* Import profiles which are in ressources/dcim
1111
* formats: ressources/dcim/formats
1212
* Reports: ressources/dcim/reports
13-
* Searches: ressources/dcim/searches
13+
* Searches: ressources/dcim/searches
14+
__*/!\ Version 1.1 add new profiles!*__
1415

1516

1617
# INSTALLATION
@@ -24,9 +25,9 @@ __Do not forget to install php7.1-soap__
2425

2526
#### REPOSITORIES
2627
* git clone https://github.com/cloudwatt/php-cli-shell_base
27-
* git checkout tags/v1.0
28+
* git checkout tags/v1.1
2829
* git clone https://github.com/cloudwatt/php-cli-shell_patchmanager
29-
* git checkout tags/v1.0
30+
* git checkout tags/v1.1
3031
* Merge these two repositories
3132

3233
#### CONFIGURATION FILE

configurations/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.user.json
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{
22
"DEFAULT": {
33
"sys": {
4-
"browserCmd": "xdg-open"
4+
"browserCmd": "xdg-open",
5+
"secureShellCmd": "ssh"
56
}
67
},
78

dcim.php.example

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,29 @@
11
<?php
2-
include_once('services/dcim.php');
2+
define("ROOT_DIR", __DIR__);
3+
require_once('services/dcim.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 [DCIM_SERVER_KEY] with the key of your PatchManager server in configuration file
624
* Example: $MAIN = new Service_Dcim(__DIR__ . '/config.json', 'myPmKey');
725
*/
8-
$MAIN = new Service_Dcim(__DIR__ . '/config.json', '[DCIM_SERVER_KEY]');
26+
$MAIN = new Service_Dcim($configurations, '[DCIM_SERVER_KEY]');
927

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

dcim/abstract.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
2-
include_once(__DIR__ . '/main.php');
3-
include_once(__DIR__ . '/connector/abstract.php');
4-
include_once(__DIR__ . '/connector/soap.php');
2+
require_once(__DIR__ . '/main.php');
3+
require_once(__DIR__ . '/connector/abstract.php');
4+
require_once(__DIR__ . '/connector/soap.php');
55

66
abstract class DCIM_Abstract extends DCIM_Main
77
{

dcim/api/abstract.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public function __construct($objectId = null)
1919
$this->objectExists();
2020
}
2121
elseif($objectId !== null) {
22-
throw new Exception("This object ID '".$objectId."' is not valid", E_USER_ERROR);
22+
throw new Exception("This object ID must be an integer, '".gettype($objectId)."' is not valid", E_USER_ERROR);
2323
}
2424
}
2525

@@ -101,6 +101,32 @@ public function getUserAttr($category, $attrLabel)
101101
}
102102
}
103103

104+
protected function _getSubObjects($objects, $fieldName, $name)
105+
{
106+
if($objects !== false)
107+
{
108+
if($name !== null)
109+
{
110+
$subObjects = array();
111+
112+
foreach($objects as $object)
113+
{
114+
if($object[$fieldName] === $name) {
115+
$subObjects[] = $object;
116+
}
117+
}
118+
119+
return $subObjects;
120+
}
121+
else {
122+
return $objects;
123+
}
124+
}
125+
else {
126+
return false;
127+
}
128+
}
129+
104130
public function __get($name)
105131
{
106132
switch(mb_strtolower($name))

dcim/api/location.php

Lines changed: 56 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ class Dcim_Api_Location extends Dcim_Api_Abstract
33
{
44
const OBJECT_TYPE = 'location';
55
const REPORT_NAMES = array(
6+
'root' => 'CW - TOOLS-CLI - Location - Root',
67
'label' => 'CW - TOOLS-CLI - Location1',
78
'location' => 'CW - TOOLS-CLI - Location2',
89
'subLocation' => 'CW - TOOLS-CLI - Location3',
910
);
1011

11-
const ROOT_SITE = 'Cloudwatt';
12-
1312
static protected $_rootLocationId = null;
13+
static protected $_rootLocationIds = null;
1414

1515
protected $_parentLocationId;
1616
protected $_parentLocationApi;
@@ -47,21 +47,30 @@ public function getParentLocationId()
4747
{
4848
if($this->_parentLocationId === null)
4949
{
50+
$this->_parentLocationId = false;
51+
5052
$path = $this->getPath();
5153
$path = explode(',', $path);
5254

53-
$locationId = $this->getRootLocationId();
55+
$selfClassName = static::class;
56+
$Dcim_Api_Location = new $selfClassName();
57+
$locationId = $Dcim_Api_Location->getSubLocationId($path[0]);
5458

55-
for($i=1; $i<count($path); $i++)
59+
if($locationId !== false)
5660
{
57-
$locationId = $this->_DCIM->getLocationIdByParentLocationIdLocationLabel($locationId, $path[$i], false);
61+
for($i=1; $i<count($path); $i++)
62+
{
63+
$locationId = $this->_DCIM->getLocationIdByParentLocationIdLocationLabel($locationId, $path[$i], false);
5864

59-
if($locationId === false) {
60-
break;
65+
if($locationId === false) {
66+
break;
67+
}
6168
}
62-
}
6369

64-
$this->_parentLocationId = ($i === count($path)) ? ($locationId) : (false);
70+
if($i === count($path)) {
71+
$this->_parentLocationId = $locationId;
72+
}
73+
}
6574
}
6675

6776
return $this->_parentLocationId;
@@ -105,7 +114,7 @@ public function getSubLocationIds()
105114
return $this->_DCIM->getSubLocationIds($this->getLocationId(), false);
106115
}
107116
else {
108-
return false;
117+
return $this->getRootLocationIds();
109118
}
110119
}
111120

@@ -115,7 +124,9 @@ public function getSubLocationId($locationLabel)
115124
return $this->_DCIM->getLocationIdByParentLocationIdLocationLabel($this->getLocationId(), $locationLabel, false);
116125
}
117126
else {
118-
return false;
127+
$results = self::$_DCIM->getReportResults(self::REPORT_NAMES['root']);
128+
$result = $this->_getSubObjects($results, 'name', $locationLabel);
129+
return (Tools::is('array', $result) && count($result) === 1) ? ($result[0]['entity_id']) : (false);
119130
}
120131
}
121132

@@ -155,10 +166,9 @@ public function __call($method, $parameters = null)
155166
{
156167
switch($method)
157168
{
158-
/*case 'getRootLocationId':
159-
return self::getRootLocationId();*/
160-
default:
169+
default: {
161170
throw new Exception('Method '.$method.' does not exist', E_USER_ERROR);
171+
}
162172
}
163173
}
164174

@@ -177,14 +187,43 @@ public function __get($name)
177187

178188
public static function getRootLocationId()
179189
{
180-
if(self::$_rootLocationId === null) {
181-
$result = self::$_DCIM->getSiteId(self::ROOT_SITE);
182-
self::$_rootLocationId = (self::$_DCIM->isValidReturn($result)) ? ($result) : (false);
190+
if(self::$_rootLocationId === null)
191+
{
192+
$results = self::$_DCIM->getReportResults(self::REPORT_NAMES['root']);
193+
194+
if(Tools::is('array', $results) && count($results) === 1) {
195+
self::$_rootLocationId = $results[0]['entity_id'];
196+
}
197+
else {
198+
throw new Exception("Unable to get root location ID", E_USER_ERROR);
199+
}
183200
}
184201

185202
return self::$_rootLocationId;
186203
}
187204

205+
public static function getRootLocationIds()
206+
{
207+
if(self::$_rootLocationIds === null)
208+
{
209+
$results = self::$_DCIM->getReportResults(self::REPORT_NAMES['root']);
210+
211+
if(Tools::is('array&&count>0', $results))
212+
{
213+
array_walk($results, function(&$item) {
214+
$item = $item['entity_id'];
215+
});
216+
217+
self::$_rootLocationIds = $results;
218+
}
219+
else {
220+
throw new Exception("Unable to retreive root location IDs", E_USER_ERROR);
221+
}
222+
}
223+
224+
return self::$_rootLocationIds;
225+
}
226+
188227
public static function searchLocations($locationLabel, $locationId = null, $recursion = false)
189228
{
190229
$args = array('label' => $locationLabel);

dcim/main.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php
2-
include_once(__DIR__ . '/tools.php');
2+
require_once(__DIR__ . '/tools.php');
33

44
abstract class DCIM_Main extends DCIM_Tools
55
{
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2+
<report>
3+
<name>CW - TOOLS-CLI - Location - Root</name>
4+
<description>INFTSK-5007</description>
5+
<components>
6+
<component type="table">
7+
<search configurations="false" domain="com.patchmanager.shared.beans.Location">
8+
<name>CW - TOOLS-CLI - Location - Root</name>
9+
<expression>0&amp;1</expression>
10+
<constraints>
11+
<constraint alias="1" match-case="false" negation="true">
12+
<field>location.parent.name</field>
13+
<type>STRING</type>
14+
<comparison>HAS_VALUE</comparison>
15+
<comparison-configuration/>
16+
<value/>
17+
</constraint>
18+
<constraint alias="2" match-case="false" negation="true">
19+
<field>location.parent.layer</field>
20+
<type>STRING</type>
21+
<comparison>HAS_VALUE</comparison>
22+
<comparison-configuration/>
23+
<value/>
24+
</constraint>
25+
</constraints>
26+
</search>
27+
<data-format aggregates-only="false" domain="com.patchmanager.shared.beans.Location" filtering="NO_FILTERING" ignore-duplicates="false" narrow="false">
28+
<name>CW - TOOLS-CLI - Location</name>
29+
<description>INFTSK-5007</description>
30+
<fields>
31+
<field aggregate="false" format="{1}" header="Entity ID">
32+
<expression>location.entityId</expression>
33+
</field>
34+
<field aggregate="false" format="{1}" header="Name">
35+
<expression>location.name</expression>
36+
</field>
37+
<field aggregate="false" format="{1}" header="Path">
38+
<expression>location.parent</expression>
39+
<renderer>com.patchmanager.shared.beans.dataformat.renderer.ExpressionRenderer</renderer>
40+
<renderer-configuration>
41+
<value key="[0]expression">{qname}</value>
42+
</renderer-configuration>
43+
</field>
44+
</fields>
45+
<ordering-fields>
46+
<ordering-field order-ascending="true">
47+
<expression>location.template</expression>
48+
<renderer>com.patchmanager.shared.beans.dataformat.renderer.ExpressionRenderer</renderer>
49+
<renderer-configuration>
50+
<value key="expression">{name}</value>
51+
</renderer-configuration>
52+
</ordering-field>
53+
<ordering-field order-ascending="true">
54+
<expression>location.parent</expression>
55+
<renderer>com.patchmanager.shared.beans.dataformat.renderer.ExpressionRenderer</renderer>
56+
<renderer-configuration>
57+
<value key="expression">{qname}</value>
58+
</renderer-configuration>
59+
</ordering-field>
60+
<ordering-field order-ascending="true">
61+
<expression>location.name</expression>
62+
</ordering-field>
63+
</ordering-fields>
64+
</data-format>
65+
</component>
66+
</components>
67+
</report>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2+
<search configurations="false" domain="com.patchmanager.shared.beans.Location">
3+
<name>CW - TOOLS-CLI - Location - Root</name>
4+
<expression>0&amp;1</expression>
5+
<constraints>
6+
<constraint alias="1" match-case="false" negation="true">
7+
<field>location.parent.name</field>
8+
<type>STRING</type>
9+
<comparison>HAS_VALUE</comparison>
10+
<comparison-configuration/>
11+
<value/>
12+
</constraint>
13+
<constraint alias="2" match-case="false" negation="true">
14+
<field>location.parent.layer</field>
15+
<type>STRING</type>
16+
<comparison>HAS_VALUE</comparison>
17+
<comparison-configuration/>
18+
<value/>
19+
</constraint>
20+
</constraints>
21+
</search>

0 commit comments

Comments
 (0)