logger.debug(" Provider id: " + connection.getProviderId());
logger.debug(" Inventory items found: " + externalInventoryItems.size());
for (JSONObject json : externalInventoryItems) {
InventoryItem inventoryItem = null;
JSONObject externalInventoryItemAsJSON;
String externalId;
if (client instanceof AWSClient) {
String type = json.getString("type");
if (type.equals(ComputeType.NODE.toString())) {
externalId = json.getString("providerId");
} else {
externalId = json.getString("name");
}
externalInventoryItemAsJSON = json;
} else if (client instanceof GitHubClient) {
externalId = json.getJSONObject("owner").getString("login") + "/" + json.getString("name");
externalInventoryItemAsJSON = json;
} else if (client instanceof PingdomClient) {
externalId = json.getString("id");
externalInventoryItemAsJSON = json;
} else if (client instanceof GoogleAnalyticsClient) {
externalId = json.getString("id");
externalInventoryItemAsJSON = json;
} else {
externalId = json.getString("key");
try {
externalInventoryItemAsJSON = ((JiraClient)client).getProjectDetails(externalId);
} catch (InvalidCredentialsException | IOException e) {
// This should never happen as we've already validated the connection by this point
logger.warn("Unable to get the Jira project details for " + externalId + ": " + e.getMessage());
return;
}
}
try {
inventoryItem = getInventoryItemForExternalId(connection, externalId);
} catch (InventoryItemNotFoundException e) {
// This is more than possible and is handled below
}
if (inventoryItem == null || inventoryItem.isDeleted()) {
if (inventoryItem != null && inventoryItem.isDeleted()) {
// Permanently remove the current inventory item marked as deleted. This can happen if you mark
// an inventory item as deleted (due to external deletion) and then recreate the same inventory
// item externally before the clean process (ran after refresh) could occur.
deleteInventoryItem(inventoryItem);
}
createInventoryItem(connection, externalInventoryItemAsJSON);
} else if (!processedKeys.contains(externalId)) {
updateInventoryItem(inventoryItem, externalInventoryItemAsJSON);
}
processedKeys.add(externalId);
}
// Handle inventory items deleted externally
List<InventoryItem> inventoryItems = getInventoryItems(connection);
for (InventoryItem inventoryItem : inventoryItems) {
if (processedKeys.contains(inventoryItem.getExternalId())) {
continue;
}
// Just mark the item as deleted. Future polling jobs will remove the inventory item marked as deleted
// when appropriate.
if (!inventoryItem.isDeleted()) {
markInventoryItemDeleted(inventoryItem);
}
}
}