Unverified Commit f2ada61b authored by Adrien Clairembault's avatar Adrien Clairembault Committed by GitHub
Browse files

API: Add 'with_networkports' option to getItems endpoint (#8688)

* API: Add 'with_networkports' option to getItems

* Fix doc

* Use var
parent 5f903e89
......@@ -179,7 +179,7 @@ $ curl -X GET \
## Lost password
This endpoint allows to request password recovery and password reset. This endpoint works under the following
This endpoint allows to request password recovery and password reset. This endpoint works under the following
conditions:
* GLPI has notifications enabled
* the email address of the user belongs to a user account.
......@@ -504,7 +504,7 @@ $ curl -X GET \
* *with_disks*: Only for Computer, retrieve the associated file-systems. Optional.
* *with_softwares*: Only for Computer, retrieve the associated software's installations. Optional.
* *with_connections*: Only for Computer, retrieve the associated direct connections (like peripherals and printers) .Optional.
* *with_networkports*: Retrieve all network's connections and advanced network's informations. Optional.
* *with_networkports*: Retrieve all network connections and advanced informations. Optionnal.
* *with_infocoms*: Retrieve financial and administrative informations. Optional.
* *with_contracts*: Retrieve associated contracts. Optional.
* *with_documents*: Retrieve associated external documents. Optional.
......@@ -605,6 +605,7 @@ Note: To download a document see [Download a document file](#download-a-document
* *searchText* (default NULL): array of filters to pass on the query (with key = field and value the text to search)
* *is_deleted* (default: false): Return deleted element. Optional.
* *add_keys_names*: Retrieve friendly names. Array containing fkey(s) and/or "id". Optional.
* *with_networkports*: Retrieve all network connections and advanced informations. Optionnal.
* **Returns**:
* 200 (OK) with items data.
* 206 (PARTIAL CONTENT) with items data defined by range.
......@@ -780,7 +781,7 @@ $ curl -X GET \
* *with_disks*: Only for Computer, retrieve the associated file-systems. Optional.
* *with_softwares*: Only for Computer, retrieve the associated software's installations. Optional.
* *with_connections*: Only for Computer, retrieve the associated direct connections (like peripherals and printers) .Optional.
* *with_networkports*: Retrieve all network's connections and advanced network's informations. Optional.
* *with_networkports*: Retrieve all network connections and advanced informations. Optionnal.
* *with_infocoms*: Retrieve financial and administrative informations. Optional.
* *with_contracts*: Retrieve associated contracts. Optional.
* *with_documents*: Retrieve associated external documents. Optional.
......@@ -942,7 +943,7 @@ $ curl -X GET \
```
* *metacriteria* (optional): array of meta-criterion objects to filter search. Optional.
A meta search is a link with another itemtype (ex: Computer with softwares).
A meta search is a link with another itemtype (ex: Computer with softwares).
**Deprecated: Now criteria support meta flag, you should use it instead direct metacriteria option.**
Each meta-criterion object must provide:
......@@ -1459,7 +1460,7 @@ You need to uncomment (removing #) theses lines:
By enabling URL rewriting, you could use API with this URL : <http://path/to/glpi/api/>.
You need also to enable rewrite module in apache httpd and permit GLPI's .htaccess to override server configuration (see AllowOverride directive).
**Note for apache+fpm users:**
**Note for apache+fpm users:**
You may have difficulties to pass Authorization header in this configuration.
You have two options :
......
......@@ -531,7 +531,7 @@ abstract class API {
* - 'with_disks': Only for Computer, retrieve the associated filesystems. Optionnal.
* - 'with_softwares': Only for Computer, retrieve the associated softwares installations. Optionnal.
* - 'with_connections': Only for Computer, retrieve the associated direct connections (like peripherals and printers) .Optionnal.
* - 'with_networkports':Retrieve all network connections and advanced network informations. Optionnal.
* - 'with_networkports':Retrieve all network connections and advanced informations. Optionnal.
* - 'with_infocoms': Retrieve financial and administrative informations. Optionnal.
* - 'with_contracts': Retrieve associated contracts. Optionnal.
* - 'with_documents': Retrieve associated external documents. Optionnal.
......@@ -734,167 +734,8 @@ abstract class API {
}
// retrieve item networkports
if (isset($params['with_networkports'])
&& $params['with_networkports']) {
$fields['_networkports'] = [];
if (!NetworkEquipment::canView()) {
$fields['_networkports'] = $this->arrayRightError();
} else {
foreach (NetworkPort::getNetworkPortInstantiations() as $networkport_type) {
$netport_table = $networkport_type::getTable();
$netp_iterator = $DB->request([
'SELECT' => [
'netp.id AS netport_id',
'netp.entities_id',
'netp.is_recursive',
'netp.logical_number',
'netp.name',
'netp.mac',
'netp.comment',
'netp.is_dynamic',
'netp_subtable.*'
],
'FROM' => 'glpi_networkports AS netp',
'LEFT JOIN' => [
"$netport_table AS netp_subtable" => [
'ON' => [
'netp_subtable' => 'networkports_id',
'netp' => 'id'
]
]
],
'WHERE' => [
'netp.instantiation_type' => $networkport_type,
'netp.items_id' => $id,
'netp.itemtype' => $itemtype,
'netp.is_deleted' => 0
]
]);
while ($data = $netp_iterator->next()) {
if (isset($data['netport_id'])) {
// append network name
$concat_expr = new QueryExpression(
"GROUP_CONCAT(CONCAT(".$DB->quoteName('ipadr.id').", ".$DB->quoteValue(Search::SHORTSEP)." , ".$DB->quoteName('ipadr.name').")
SEPARATOR ".$DB->quoteValue(Search::LONGSEP).") AS ".$DB->quoteName('ipadresses')
);
$netn_iterator = $DB->request([
'SELECT' => [
$concat_expr,
'netn.id AS networknames_id',
'netn.name AS networkname',
'netn.fqdns_id',
'fqdn.name AS fqdn_name',
'fqdn.fqdn'
],
'FROM' => [
'glpi_networknames AS netn'
],
'LEFT JOIN' => [
'glpi_ipaddresses AS ipadr' => [
'ON' => [
'ipadr' => 'items_id',
'netn' => 'id',
[
'AND' => ['ipadr.itemtype' => 'NetworkName']
]
]
],
'glpi_fqdns AS fqdn' => [
'ON' => [
'fqdn' => 'id',
'netn' => 'fqdns_id'
]
],
'glpi_ipaddresses_ipnetworks AS ipadnet' => [
'ON' => [
'ipadnet' => 'ipaddresses_id',
'ipadr' => 'id'
]
],
'glpi_ipnetworks AS ipnet' => [
'ON' => [
'ipnet' => 'id',
'ipadnet' => 'ipnetworks_id'
]
]
],
'WHERE' => [
'netn.itemtype' => 'NetworkPort',
'netn.items_id' => $data['netport_id']
],
'GROUPBY' => [
'netn.id',
'netn.name',
'netn.fqdns_id',
'fqdn.name',
'fqdn.fqdn'
]
]);
if (count($netn_iterator)) {
$data_netn = $netn_iterator->next();
$raw_ipadresses = explode(Search::LONGSEP, $data_netn['ipadresses']);
$ipadresses = [];
foreach ($raw_ipadresses as $ipadress) {
$ipadress = explode(Search::SHORTSEP, $ipadress);
//find ip network attached to these ip
$ipnetworks = [];
$ipnet_iterator = $DB->request([
'SELECT' => [
'ipnet.id',
'ipnet.completename',
'ipnet.name',
'ipnet.address',
'ipnet.netmask',
'ipnet.gateway',
'ipnet.ipnetworks_id',
'ipnet.comment'
],
'FROM' => 'glpi_ipnetworks AS ipnet',
'INNER JOIN' => [
'glpi_ipaddresses_ipnetworks AS ipadnet' => [
'ON' => [
'ipadnet' => 'ipnetworks_id',
'ipnet' => 'id'
]
]
],
'WHERE' => [
'ipadnet.ipaddresses_id' => $ipadress[0]
]
]);
while ($data_ipnet = $ipnet_iterator->next()) {
$ipnetworks[] = $data_ipnet;
}
$ipadresses[] = [
'id' => $ipadress[0],
'name' => $ipadress[1],
'IPNetwork' => $ipnetworks
];
}
$data['NetworkName'] = [
'id' => $data_netn['networknames_id'],
'name' => $data_netn['networkname'],
'fqdns_id' => $data_netn['fqdns_id'],
'FQDN' => [
'id' => $data_netn['fqdns_id'],
'name' => $data_netn['fqdn_name'],
'fqdn' => $data_netn['fqdn']
],
'IPAddress' => $ipadresses
];
}
}
$fields['_networkports'][$networkport_type][] = $data;
}
}
}
if (isset($params['with_networkports']) && $params['with_networkports']) {
$fields['_networkports'] = $this->getNetworkPorts($id, $itemtype);
}
// retrieve item infocoms
......@@ -1158,6 +999,7 @@ abstract class API {
* - 'searchText' (default: NULL): array of filters to pass on the query (with key = field and value the search)
* - 'is_deleted' (default: false): show trashbin. Optionnal
* - 'add_keys_names' (default: []): insert raw name(s) for given itemtype(s) and fkey(s)
* - 'with_networkports'(default: false): Retrieve all network connections and advanced informations. Optional.
* @param integer $totalcount output parameter who receive the total count of the query resulat.
* As this function paginate results (with a mysql LIMIT),
* we can have the full range. (default 0)
......@@ -1172,14 +1014,15 @@ abstract class API {
// default params
$default = ['expand_dropdowns' => false,
'get_hateoas' => true,
'only_id' => false,
'range' => "0-".$_SESSION['glpilist_limit'],
'sort' => "id",
'order' => "ASC",
'searchText' => null,
'is_deleted' => false,
'add_keys_names' => [],
'get_hateoas' => true,
'only_id' => false,
'range' => "0-".$_SESSION['glpilist_limit'],
'sort' => "id",
'order' => "ASC",
'searchText' => null,
'is_deleted' => false,
'add_keys_names' => [],
'with_networkports' => false,
];
$params = array_merge($default, $params);
......@@ -1335,6 +1178,10 @@ abstract class API {
);
}
if (isset($params['with_networkports']) && $params['with_networkports']) {
$data['_networkports'] = $this->getNetworkPorts($data['id'], $itemtype);
}
$found[] = $data;
}
}
......@@ -1402,7 +1249,7 @@ abstract class API {
* - 'with_disks': Only for Computer, retrieve the associated filesystems. Optionnal.
* - 'with_softwares': Only for Computer, retrieve the associated softwares installations. Optionnal.
* - 'with_connections': Only for Computer, retrieve the associated direct connections (like peripherals and printers) .Optionnal.
* - 'with_networkports': Retrieve all network connections and advanced network informations. Optionnal.
* - 'with_networkports': Retrieve all network connections and advanced informations. Optionnal.
* - 'with_infocoms': Retrieve financial and administrative informations. Optionnal.
* - 'with_contracts': Retrieve associated contracts. Optionnal.
* - 'with_documents': Retrieve associated external documents. Optionnal.
......@@ -2798,6 +2645,187 @@ abstract class API {
return $_names;
}
/**
* Get network ports
*
* @since x.x.x
*
* @param int $id Id of the source item
* @param string $itemtype Type of the source item
*
* @return array
*/
protected function getNetworkPorts(
int $id,
string $itemtype
): array {
global $DB;
$_networkports = [];
if (!NetworkEquipment::canView()) {
$_networkports = $this->arrayRightError();
} else {
$networkport_types = NetworkPort::getNetworkPortInstantiations();
foreach ($networkport_types as $networkport_type) {
$netport_table = $networkport_type::getTable();
$netp_iterator = $DB->request([
'SELECT' => [
'netp.id AS netport_id',
'netp.entities_id',
'netp.is_recursive',
'netp.logical_number',
'netp.name',
'netp.mac',
'netp.comment',
'netp.is_dynamic',
'netp_subtable.*'
],
'FROM' => 'glpi_networkports AS netp',
'LEFT JOIN' => [
"$netport_table AS netp_subtable" => [
'ON' => [
'netp_subtable' => 'networkports_id',
'netp' => 'id'
]
]
],
'WHERE' => [
'netp.instantiation_type' => $networkport_type,
'netp.items_id' => $id,
'netp.itemtype' => $itemtype,
'netp.is_deleted' => 0
]
]);
while ($data = $netp_iterator->next()) {
if (isset($data['netport_id'])) {
// append network name
$concat_expr = new QueryExpression(
"GROUP_CONCAT(CONCAT(".$DB->quoteName('ipadr.id').", ".$DB->quoteValue(Search::SHORTSEP)." , ".$DB->quoteName('ipadr.name').")
SEPARATOR ".$DB->quoteValue(Search::LONGSEP).") AS ".$DB->quoteName('ipadresses')
);
$netn_iterator = $DB->request([
'SELECT' => [
$concat_expr,
'netn.id AS networknames_id',
'netn.name AS networkname',
'netn.fqdns_id',
'fqdn.name AS fqdn_name',
'fqdn.fqdn'
],
'FROM' => [
'glpi_networknames AS netn'
],
'LEFT JOIN' => [
'glpi_ipaddresses AS ipadr' => [
'ON' => [
'ipadr' => 'items_id',
'netn' => 'id',
[
'AND' => ['ipadr.itemtype' => 'NetworkName']
]
]
],
'glpi_fqdns AS fqdn' => [
'ON' => [
'fqdn' => 'id',
'netn' => 'fqdns_id'
]
],
'glpi_ipaddresses_ipnetworks AS ipadnet' => [
'ON' => [
'ipadnet' => 'ipaddresses_id',
'ipadr' => 'id'
]
],
'glpi_ipnetworks AS ipnet' => [
'ON' => [
'ipnet' => 'id',
'ipadnet' => 'ipnetworks_id'
]
]
],
'WHERE' => [
'netn.itemtype' => 'NetworkPort',
'netn.items_id' => $data['netport_id']
],
'GROUPBY' => [
'netn.id',
'netn.name',
'netn.fqdns_id',
'fqdn.name',
'fqdn.fqdn'
]
]);
if (count($netn_iterator)) {
$data_netn = $netn_iterator->next();
$raw_ipadresses = explode(Search::LONGSEP, $data_netn['ipadresses']);
$ipadresses = [];
foreach ($raw_ipadresses as $ipadress) {
$ipadress = explode(Search::SHORTSEP, $ipadress);
//find ip network attached to these ip
$ipnetworks = [];
$ipnet_iterator = $DB->request([
'SELECT' => [
'ipnet.id',
'ipnet.completename',
'ipnet.name',
'ipnet.address',
'ipnet.netmask',
'ipnet.gateway',
'ipnet.ipnetworks_id',
'ipnet.comment'
],
'FROM' => 'glpi_ipnetworks AS ipnet',
'INNER JOIN' => [
'glpi_ipaddresses_ipnetworks AS ipadnet' => [
'ON' => [
'ipadnet' => 'ipnetworks_id',
'ipnet' => 'id'
]
]
],
'WHERE' => [
'ipadnet.ipaddresses_id' => $ipadress[0]
]
]);
while ($data_ipnet = $ipnet_iterator->next()) {
$ipnetworks[] = $data_ipnet;
}
$ipadresses[] = [
'id' => $ipadress[0],
'name' => $ipadress[1],
'IPNetwork' => $ipnetworks
];
}
$data['NetworkName'] = [
'id' => $data_netn['networknames_id'],
'name' => $data_netn['networkname'],
'fqdns_id' => $data_netn['fqdns_id'],
'FQDN' => [
'id' => $data_netn['fqdns_id'],
'name' => $data_netn['fqdn_name'],
'fqdn' => $data_netn['fqdn']
],
'IPAddress' => $ipadresses
];
}
}
$_networkports[$networkport_type][] = $data;
}
}
}
return $_networkports;
}
/**
* Get the profile picture of the given user
*
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment