Skip to content

Commit

Permalink
Version 1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean-François Hivert committed Sep 27, 2018
1 parent 51fdecb commit e75dbb0
Show file tree
Hide file tree
Showing 16 changed files with 772 additions and 395 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ You have to use base PHP-CLI SHELL project that is here: https://github.com/clou
* Import profiles which are in ressources/dcim
* formats: ressources/dcim/formats
* Reports: ressources/dcim/reports
* Searches: ressources/dcim/searches
* Searches: ressources/dcim/searches
__*/!\ Version 1.1 add new profiles!*__


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

#### REPOSITORIES
* git clone https://github.com/cloudwatt/php-cli-shell_base
* git checkout tags/v1.0
* git checkout tags/v1.1
* git clone https://github.com/cloudwatt/php-cli-shell_patchmanager
* git checkout tags/v1.0
* git checkout tags/v1.1
* Merge these two repositories

#### CONFIGURATION FILE
Expand Down
1 change: 1 addition & 0 deletions configurations/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.user.json
3 changes: 2 additions & 1 deletion config.json.example → configurations/config.json.example
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"DEFAULT": {
"sys": {
"browserCmd": "xdg-open"
"browserCmd": "xdg-open",
"secureShellCmd": "ssh"
}
},

Expand Down
24 changes: 21 additions & 3 deletions dcim.php.example
Original file line number Diff line number Diff line change
@@ -1,11 +1,29 @@
<?php
include_once('services/dcim.php');
define("ROOT_DIR", __DIR__);
require_once('services/dcim.php');

if(!isset($configurations))
{
$configurations = array(
__DIR__ . '/configurations/config.json',
__DIR__ . '/configurations/config.user.json',
);
}

/**
* Déplace le curseur d'une ligne vers le haut
* Fix le saut de ligne lors de la touche entrée pour lancer le script CLI
*
* Permet d'harmoniser le traitement des sauts de lignes:
* --> Saut de ligne avant un texte et non après!
*/
echo "\033[1A";

/**
* Change [DCIM_SERVER_KEY] with the key of your PatchManager server in configuration file
* Example: $MAIN = new Service_Dcim(__DIR__ . '/config.json', 'myPmKey');
*/
$MAIN = new Service_Dcim(__DIR__ . '/config.json', '[DCIM_SERVER_KEY]');
$MAIN = new Service_Dcim($configurations, '[DCIM_SERVER_KEY]');

echo "\r\n";
echo PHP_EOL;
exit();
6 changes: 3 additions & 3 deletions dcim/abstract.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
include_once(__DIR__ . '/main.php');
include_once(__DIR__ . '/connector/abstract.php');
include_once(__DIR__ . '/connector/soap.php');
require_once(__DIR__ . '/main.php');
require_once(__DIR__ . '/connector/abstract.php');
require_once(__DIR__ . '/connector/soap.php');

abstract class DCIM_Abstract extends DCIM_Main
{
Expand Down
28 changes: 27 additions & 1 deletion dcim/api/abstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function __construct($objectId = null)
$this->objectExists();
}
elseif($objectId !== null) {
throw new Exception("This object ID '".$objectId."' is not valid", E_USER_ERROR);
throw new Exception("This object ID must be an integer, '".gettype($objectId)."' is not valid", E_USER_ERROR);
}
}

Expand Down Expand Up @@ -101,6 +101,32 @@ public function getUserAttr($category, $attrLabel)
}
}

protected function _getSubObjects($objects, $fieldName, $name)
{
if($objects !== false)
{
if($name !== null)
{
$subObjects = array();

foreach($objects as $object)
{
if($object[$fieldName] === $name) {
$subObjects[] = $object;
}
}

return $subObjects;
}
else {
return $objects;
}
}
else {
return false;
}
}

public function __get($name)
{
switch(mb_strtolower($name))
Expand Down
73 changes: 56 additions & 17 deletions dcim/api/location.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ class Dcim_Api_Location extends Dcim_Api_Abstract
{
const OBJECT_TYPE = 'location';
const REPORT_NAMES = array(
'root' => 'CW - TOOLS-CLI - Location - Root',
'label' => 'CW - TOOLS-CLI - Location1',
'location' => 'CW - TOOLS-CLI - Location2',
'subLocation' => 'CW - TOOLS-CLI - Location3',
);

const ROOT_SITE = 'Cloudwatt';

static protected $_rootLocationId = null;
static protected $_rootLocationIds = null;

protected $_parentLocationId;
protected $_parentLocationApi;
Expand Down Expand Up @@ -47,21 +47,30 @@ public function getParentLocationId()
{
if($this->_parentLocationId === null)
{
$this->_parentLocationId = false;

$path = $this->getPath();
$path = explode(',', $path);

$locationId = $this->getRootLocationId();
$selfClassName = static::class;
$Dcim_Api_Location = new $selfClassName();
$locationId = $Dcim_Api_Location->getSubLocationId($path[0]);

for($i=1; $i<count($path); $i++)
if($locationId !== false)
{
$locationId = $this->_DCIM->getLocationIdByParentLocationIdLocationLabel($locationId, $path[$i], false);
for($i=1; $i<count($path); $i++)
{
$locationId = $this->_DCIM->getLocationIdByParentLocationIdLocationLabel($locationId, $path[$i], false);

if($locationId === false) {
break;
if($locationId === false) {
break;
}
}
}

$this->_parentLocationId = ($i === count($path)) ? ($locationId) : (false);
if($i === count($path)) {
$this->_parentLocationId = $locationId;
}
}
}

return $this->_parentLocationId;
Expand Down Expand Up @@ -105,7 +114,7 @@ public function getSubLocationIds()
return $this->_DCIM->getSubLocationIds($this->getLocationId(), false);
}
else {
return false;
return $this->getRootLocationIds();
}
}

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

Expand Down Expand Up @@ -155,10 +166,9 @@ public function __call($method, $parameters = null)
{
switch($method)
{
/*case 'getRootLocationId':
return self::getRootLocationId();*/
default:
default: {
throw new Exception('Method '.$method.' does not exist', E_USER_ERROR);
}
}
}

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

public static function getRootLocationId()
{
if(self::$_rootLocationId === null) {
$result = self::$_DCIM->getSiteId(self::ROOT_SITE);
self::$_rootLocationId = (self::$_DCIM->isValidReturn($result)) ? ($result) : (false);
if(self::$_rootLocationId === null)
{
$results = self::$_DCIM->getReportResults(self::REPORT_NAMES['root']);

if(Tools::is('array', $results) && count($results) === 1) {
self::$_rootLocationId = $results[0]['entity_id'];
}
else {
throw new Exception("Unable to get root location ID", E_USER_ERROR);
}
}

return self::$_rootLocationId;
}

public static function getRootLocationIds()
{
if(self::$_rootLocationIds === null)
{
$results = self::$_DCIM->getReportResults(self::REPORT_NAMES['root']);

if(Tools::is('array&&count>0', $results))
{
array_walk($results, function(&$item) {
$item = $item['entity_id'];
});

self::$_rootLocationIds = $results;
}
else {
throw new Exception("Unable to retreive root location IDs", E_USER_ERROR);
}
}

return self::$_rootLocationIds;
}

public static function searchLocations($locationLabel, $locationId = null, $recursion = false)
{
$args = array('label' => $locationLabel);
Expand Down
2 changes: 1 addition & 1 deletion dcim/main.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
include_once(__DIR__ . '/tools.php');
require_once(__DIR__ . '/tools.php');

abstract class DCIM_Main extends DCIM_Tools
{
Expand Down
67 changes: 67 additions & 0 deletions ressources/dcim/reports/CW - TOOLS-CLI - Location - Root.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<report>
<name>CW - TOOLS-CLI - Location - Root</name>
<description>INFTSK-5007</description>
<components>
<component type="table">
<search configurations="false" domain="com.patchmanager.shared.beans.Location">
<name>CW - TOOLS-CLI - Location - Root</name>
<expression>0&amp;1</expression>
<constraints>
<constraint alias="1" match-case="false" negation="true">
<field>location.parent.name</field>
<type>STRING</type>
<comparison>HAS_VALUE</comparison>
<comparison-configuration/>
<value/>
</constraint>
<constraint alias="2" match-case="false" negation="true">
<field>location.parent.layer</field>
<type>STRING</type>
<comparison>HAS_VALUE</comparison>
<comparison-configuration/>
<value/>
</constraint>
</constraints>
</search>
<data-format aggregates-only="false" domain="com.patchmanager.shared.beans.Location" filtering="NO_FILTERING" ignore-duplicates="false" narrow="false">
<name>CW - TOOLS-CLI - Location</name>
<description>INFTSK-5007</description>
<fields>
<field aggregate="false" format="{1}" header="Entity ID">
<expression>location.entityId</expression>
</field>
<field aggregate="false" format="{1}" header="Name">
<expression>location.name</expression>
</field>
<field aggregate="false" format="{1}" header="Path">
<expression>location.parent</expression>
<renderer>com.patchmanager.shared.beans.dataformat.renderer.ExpressionRenderer</renderer>
<renderer-configuration>
<value key="[0]expression">{qname}</value>
</renderer-configuration>
</field>
</fields>
<ordering-fields>
<ordering-field order-ascending="true">
<expression>location.template</expression>
<renderer>com.patchmanager.shared.beans.dataformat.renderer.ExpressionRenderer</renderer>
<renderer-configuration>
<value key="expression">{name}</value>
</renderer-configuration>
</ordering-field>
<ordering-field order-ascending="true">
<expression>location.parent</expression>
<renderer>com.patchmanager.shared.beans.dataformat.renderer.ExpressionRenderer</renderer>
<renderer-configuration>
<value key="expression">{qname}</value>
</renderer-configuration>
</ordering-field>
<ordering-field order-ascending="true">
<expression>location.name</expression>
</ordering-field>
</ordering-fields>
</data-format>
</component>
</components>
</report>
21 changes: 21 additions & 0 deletions ressources/dcim/searches/CW - TOOLS-CLI - Location - Root.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<search configurations="false" domain="com.patchmanager.shared.beans.Location">
<name>CW - TOOLS-CLI - Location - Root</name>
<expression>0&amp;1</expression>
<constraints>
<constraint alias="1" match-case="false" negation="true">
<field>location.parent.name</field>
<type>STRING</type>
<comparison>HAS_VALUE</comparison>
<comparison-configuration/>
<value/>
</constraint>
<constraint alias="2" match-case="false" negation="true">
<field>location.parent.layer</field>
<type>STRING</type>
<comparison>HAS_VALUE</comparison>
<comparison-configuration/>
<value/>
</constraint>
</constraints>
</search>
Loading

0 comments on commit e75dbb0

Please sign in to comment.