Skip to content

Commit

Permalink
Version 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean-François Hivert committed Mar 18, 2019
1 parent dfe8e82 commit 84a7883
Show file tree
Hide file tree
Showing 144 changed files with 11,408 additions and 3,561 deletions.
39 changes: 24 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,41 +7,50 @@ You have to use base PHP-CLI SHELL project that is here: https://github.com/clou
# REQUIREMENTS

#### PATCHMANAGER
* Import profiles which are in ressources/dcim
* Import profiles which are in addons/dcim/ressources
* formats: ressources/dcim/formats
* Reports: ressources/dcim/reports
* Searches: ressources/dcim/searches
__*/!\ Version 1.1 add new profiles!*__

__*/!\ Do not rename custom profiles*__
__*/!\ Version 2.0 add new profiles!*__


# INSTALLATION

#### APT PHP
Ubuntu only, you can get last PHP version from this PPA:
__*https://launchpad.net/~ondrej/+archive/ubuntu/php*__
* add-apt-repository ppa:ondrej/php
* apt-get update
* apt install php7.1-cli php7.1-mbstring php7.1-readline php7.1-soap
__Do not forget to install php7.1-soap__
* apt update

You have to install a PHP version >= 7.1:
* apt update
* apt install php7.2-cli php7.2-soap php7.2-mbstring php7.2-readline php7.2-curl
__Do not forget to install php7.2-soap__

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

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

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

#### CREDENTIALS FILE
/!\ For security reason, use a read only account!
/!\ For security reason, you can use a read only account!
__*Change informations which are between []*__
* vim credentialsFile
* read -sr USER_PASSWORD_INPUT
Expand Down
307 changes: 307 additions & 0 deletions addons/dcim/api/abstract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,307 @@
<?php
namespace Addon\Dcim;

use Core as C;

abstract class Api_Abstract extends C\Addon\Api_Abstract
{
const FIELD_ID = 'entity_id';
const FIELD_NAME = 'name';

const WILDCARD = '*';
const SEPARATOR_PATH = ',';

/**
* @var string
*/
protected static $_parentAdapter = __NAMESPACE__ .'\Main';

/**
* @var Addon\Dcim\Main
*/
protected static $_DCIM = null; // Global DCIM (enabled)

/**
* @var Addon\Dcim\Main[]
*/
protected static $_aDCIM = array(); // a = all/array/available DCIM

/**
* @var Addon\Dcim\Main
*/
protected $_DCIM_ = null; // Local DCIM (for this instance)


public function __construct($objectId = null)
{
parent::__construct($objectId);
$this->_DCIM_ = &$this->_ownerAdapter; // /!\ A executer avant _setObjectId
$this->_setObjectId($objectId); // @todo temp
}

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

public function hasObjectId()
{
return ($this->_objectId !== null);
}

public function getObjectId()
{
return $this->_objectId;
}

public function objectExists()
{
if($this->_objectExists !== null) {
return $this->_objectExists;
}
elseif($this->hasObjectId()) {
$this->_objectExists = ($this->_getObject() !== false);
return $this->_objectExists;
}
else {
return false;
}
}

protected function _setObjectLabel($objectLabel)
{
if(!$this->objectExists() && C\Tools::is('string&&!empty', $objectLabel)) {
$this->_objectLabel = $objectLabel;
return true;
}
else {
return false;
}
}

public function hasObjectLabel()
{
return ($this->getObjectLabel() !== false);
}

public function getObjectLabel()
{
if($this->_objectLabel !== null) { // /!\ Ne pas appeler hasObjectLabel sinon boucle infinie
return $this->_objectLabel;
}
elseif($this->hasObjectId()) { // /!\ Ne pas appeler objectExists sinon boucle infinie
$objectDatas = $this->_getObject();
$this->_objectLabel = ($objectDatas !== false) ? ($objectDatas[static::FIELD_NAME]) : (false);
return $this->_objectLabel;
}
else {
return false;
}
}

/**
* Méthode courte comme getPath
*/
public function getLabel()
{
return $this->getObjectLabel();
}

public function getTemplateName()
{
if($this->objectExists()) {
$result = $this->_DCIM->resolvToTemplate(static::OBJECT_TYPE, $this->getObjectId());
return ($this->_DCIM->isValidReturn($result)) ? ($result) : (false);
}
else {
return false;
}
}

/**
* @param string $category
* @param null|string $attribute
* @return false|string
*/
public function getUserAttrField($category, $attribute = null)
{
if(!C\Tools::is('string&&!empty', $attribute)) {
$attribute = $category;
$category = 'default';
$noCateg = true;
}

$attrField = $this->_DCIM->getUserAttrName($category, $attribute);
return ($attrField === false && isset($noCateg)) ? ($attribute) : ($attrField);
}

/**
* @param string $category
* @param null|string $attribute
* @return false|string
*/
public function getUserAttribute($category, $attribute = null)
{
if($this->objectExists())
{
$attrField = $this->getUserAttrField($category, $attribute);

if($attrField !== false) {
$result = $this->_DCIM->getUserAttrById(static::OBJECT_TYPE, $this->getObjectId(), $attrField);
return ($this->_DCIM->isValidReturn($result)) ? ($result) : (false);
}
}

return false;
}

public function __get($name)
{
switch($name)
{
case 'dcim':
case '_DCIM': {
return self::$_DCIM;
}
case 'id': {
return $this->getObjectId();
}
case 'label': {
return $this->getObjectLabel();
}
case 'templateName': {
return $this->getTemplateName();
}
default: {
throw new Exception("This attribute '".$name."' does not exist", E_USER_ERROR);
}
}
}

public function __call($method, $parameters = null)
{
if(substr($method, 0, 3) === 'get')
{
$name = substr($method, 3);
$name = mb_strtolower($name);

switch($name)
{
case 'label': {
return $this->getObjectLabel();
}
}
}

throw new Exception("Method '".$method."' does not exist", E_USER_ERROR);
}

/**
* @param string $section
* @param array $attributes
* @return false|string
*/
protected static function _getReportName($section, array $attributes)
{
if(array_key_exists($section, static::REPORT_NAMES))
{
$reportNames = static::REPORT_NAMES[$section];
$attributes = implode('&', $attributes);

if(is_string($reportNames)) {
return $reportNames;
}
elseif(is_array($reportNames) && array_key_exists($attributes, $reportNames)) {
return $reportNames[$attributes];
}
}

return false;
}

/**
* @param string $reportName
* @param null|array $args
* @param false|string $fieldNameFilter
* @throw Addon\Dcim\Exception
* @return array
*/
protected static function _getObjectsFromReport($reportName, array $args = null, $fieldNameFilter = false)
{
if($args !== null)
{
array_walk($args, function(&$item)
{
if(!C\Tools::is('human', $item)) {
$item = static::WILDCARD;
}
});
}

// @todo use _getReportName
$results = self::$_DCIM->getReportResults(static::REPORT_NAMES[$reportName], $args);

if(C\Tools::is('array&&count>0', $results))
{
if($fieldNameFilter !== false) {
$results = array_column($results, $fieldNameFilter);
}

return $results;
}
else {
throw new Exception("Unable to retrieve objects from report '".$reportName."'", E_USER_ERROR);
}
}

/**
* @param Addon\Dcim\Main|Addon\Dcim\Main[] $DCIM
* @return bool
*/
public static function setDcim($DCIM)
{
return self::setAdapter($DCIM);
}

/**
* @param Addon\Dcim\Main|Addon\Dcim\Main[] $adapter
* @throw Core\Exception
* @return bool
*/
public static function setAdapter($adapter)
{
$status = parent::setAdapter($adapter);

if($status) {
self::$_DCIM = &self::$_adapter;
self::$_aDCIM = &self::$_allAdapters;
}

return $status;
}

/**
* @return null|Addon\Dcim\Main|Addon\Dcim\Main[]
*/
public static function getDcim()
{
return self::getAdapter();
}

/**
* @param string $key
* @return bool
*/
public static function enableDcim($key)
{
return self::enableAdapter($key);
}

/**
* @return null|Addon\Dcim\Main
*/
public static function getDcimEnabled()
{
return self::getAdapterEnabled();
}
}
Loading

0 comments on commit 84a7883

Please sign in to comment.