Skip to content

Commit

Permalink
Fix(API): fix API call during merge request (#13)
Browse files Browse the repository at this point in the history
* Fix(API): fix API call during merge request

* Adapt CHANGELOG
  • Loading branch information
stonebuzz authored Dec 27, 2024
1 parent ea4b1e7 commit 1ccb868
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

- SQL error when merging the Jamf device linked to a GLPI asset
- SQL error when adding `Event`
- Fix `Computer` merge process

## [3.1.1]

Expand Down
13 changes: 9 additions & 4 deletions ajax/merge.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,12 @@
$plugin_sync_itemtype = 'PluginJamfMobileSync';
}

$jamf_item = PluginJamfAPI::getMobileDeviceByID($jamf_id, true);
if ($data['jamf_type'] === 'MobileDevice') {
$jamf_item = PluginJamfAPI::getMobileDeviceByID($jamf_id, true);
} else {
$jamf_item = PluginJamfAPI::getComputerByID($jamf_id, true);
}

if ($jamf_item === null) {
// API error or device no longer exists in Jamf
throw new RuntimeException('Jamf API error or item no longer exists!');
Expand All @@ -87,11 +92,11 @@
$rules = new PluginJamfRuleImportCollection();

//WTF is this, Jamf?
$os_details = $jamf_item['ios'] ?? $jamf_item['tvos'];
$os_details = $jamf_item['ios'] ?? $jamf_item['tvos'] ?? '';
$ruleinput = [
'name' => $jamf_item['name'],
'name' => $jamf_item['name'] ?? $jamf_item['general']['name'],
'itemtype' => $itemtype,
'last_inventory' => $jamf_item['lastInventoryUpdateTimestamp'],
'last_inventory' => $jamf_item['lastInventoryUpdateTimestamp'] ?? $jamf_item['general']['lastContactTime'],
'managed' => $jamf_item['managed'] ?? $os_details['managed'],
'supervised' => $jamf_item['supervised'] ?? $os_details['supervised'],
];
Expand Down
17 changes: 5 additions & 12 deletions inc/api.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -496,21 +496,14 @@ public static function getAllComputers()
return $all_results;
}

public static function getComputerByID(int $id, ?string $section = null): ?array
public static function getComputerByID(int $id, bool $detailed = false)
{
if (!static::$connection) {
static::$connection = new static::$connection_class();
}
$endpoint = "/v1/computer-inventory";
$query_params = [
'section' => $section,
'filter' => 'id==' . $id
];
$response = static::$connection->getClient()->get(static::$connection->getAPIUrl($endpoint, true) . '?' . http_build_query($query_params));
$result = json_decode($response->getBody()->getContents(), true);
if (isset($result['results']) && count($result['results']) > 0) {
return $result['results'][0];
}
return null;
$endpoint = "/v1/computer-inventory" . ($detailed ? '-detail' : '') . "/{$id}";
$response = static::$connection->getClient()->get(static::$connection->getAPIUrl($endpoint, true));
return json_decode($response->getBody()->getContents(), true);
}

}
2 changes: 1 addition & 1 deletion inc/migration.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ public function apply_3_0_0_migration(): void
}
} else if ($device['jamf_type'] === 'Computer') {
// We need to query the JSS for the computer's model identifier
$computer = $this->api::getComputerByID($device['jamf_items_id'], 'hardware');
$computer = $this->api::getComputerByID($device['jamf_items_id'], true);
if ($computer !== null) {
$this->db->update(
'glpi_plugin_jamf_devices',
Expand Down

0 comments on commit 1ccb868

Please sign in to comment.