Skip to content
This repository has been archived by the owner on Sep 3, 2024. It is now read-only.

INT-2714 - Fix for hardware mapping #35

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ and this project adheres to

## [Unreleased]

### Fixed

- Assigned `unknown` value if hardware `manufacturer`, `make` or `model` field
is undefined.
- Fixed part corresponding to iterating hardware assigned user.
- Cleaned up code for creating hardware related mapped relationships (we can
only access the `serial` value).

## 2.0.0 - 2022-11-10

- Reorganized files
Expand Down
85 changes: 84 additions & 1 deletion src/collector/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,90 @@ export interface PaginatedResponse {
rows: any[];
}

export type HardwareAsset = Opaque<any, 'HardwareAsset'>;
export type HardwareAsset = Opaque<
{
id: number;
name: string;
asset_tag: string;
serial: string;
model: {
id: number;
name: string;
};
model_number: string;
eol: {
date: string;
formatted: string;
};
status_label: {
id: number;
name: string;
status_type: string;
status_meta: string;
};
category: {
id: number;
name: string;
};
manufacturer?: {
id: number;
name: string;
};
supplier: {
id: number;
name: string;
};
notes: string;
order_number: string;
company?: string;
location: {
id: number;
name: string;
};
rtd_location: {
id: number;
name: string;
};
image: string;
qr: string;
created_at: {
datetime: string;
formatted: string;
};
updated_at: {
datetime: string;
formatted: string;
};
purchase_date: {
date: string;
formatted: string;
};
purchase_cost: string;
checkin_counter: number;
checkout_counter: number;
requests_counter: number;
user_can_checkout: boolean;
available_actions: {
checkout: boolean;
checkin: boolean;
clone: boolean;
restore: boolean;
update: boolean;
delete: boolean;
};
assigned_to?: {
id: number;
username?: string;
name: string;
first_name?: string;
last_name?: string;
employee_number?: string;
type: string;
};
},
'HardwareAsset'
>;

export type SnipeItUser = Opaque<any, 'SnipeItUser'>;
export type SnipeItConsumable = Opaque<any, 'SnipeItConsumable'>;
export type SnipeItLicense = Opaque<any, 'SnipeItLicense'>;
Expand Down

Large diffs are not rendered by default.

37 changes: 26 additions & 11 deletions src/steps/fetch-hardware/converter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import {
convertProperties,
createIntegrationEntity,
createMappedRelationship,
Entity,
Expand All @@ -25,23 +24,21 @@ export function convertHardware(
entityData: {
source: data,
assign: {
...convertProperties(data),
_key: getHardwareKey(data.id),
_key: getHardwareKey(data.id.toString()),
_type: Entities.HARDWARE._type,
_class: Entities.HARDWARE._class,
id: getHardwareKey(data.id),
id: getHardwareKey(data.id.toString()),
assetId: data.id,
deviceId: `${data.id}`,
displayName: data.name,
activatedOn: parseTimePropertyValue(data.activated_on),
username: user?.username || data.assigned_to?.username,
userId: user?.id || data.assigned_to?.id,
email: user?.email,
assetTag: data.asset_tag,
category: data.category?.name,
manufacturer: data.manufacturer?.name,
make: data.manufacturer?.name,
model: data.model?.name,
manufacturer: data.manufacturer?.name || 'unknown',
make: data.manufacturer?.name || 'unknown',
model: data.model?.name || 'unknown',
serial: data.serial,
supplier: data.supplier?.name,
EOL: !!data.eol,
Expand All @@ -56,6 +53,24 @@ export function convertHardware(
locationId: data.location?.id,
createdOn: parseTimePropertyValue(data.created_at?.datetime),
updatedOn: parseTimePropertyValue(data.updated_at?.datetime),
modelNumber: data.model_number,
orderNumber: data.order_number,
company: data.company,
rtdLocation: data.rtd_location?.name,
image: data.image,
qr: data.qr,
purchaseOn: parseTimePropertyValue(data.purchase_date?.date),
purchaseCost: data.purchase_cost,
checkinCounter: data.checkin_counter,
checkoutCounter: data.checkout_counter,
requestsCounter: data.requests_counter,
userCanCheckout: data.user_can_checkout,
isCheckoutAvailable: data.available_actions?.checkout,
isCheckinAvailable: data.available_actions?.checkin,
isCloneAvailable: data.available_actions?.clone,
isRestoreAvailable: data.available_actions?.restore,
isUpdateAvailable: data.available_actions?.update,
isDeleteAvailable: data.available_actions?.delete,
},
},
});
Expand All @@ -75,10 +90,12 @@ export function mapHardwareRelationship(
sourceEntityKey: account._key,
targetFilterKeys: [['_class', filterKey]],
targetEntity: {
...convertProperties(hardware),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to verify that the properties that were being produce here are not being used by customers.

_key: hardware._key,
_type: hardware._type,
_class: hardware._class,
id: hardware.id,
displayName: hardware.displayName,
createdOn: hardware.createdOn as number,
},
},
});
Expand All @@ -98,8 +115,6 @@ export function mapHardwareLocationRelationship(
sourceEntityKey: getLocationKey(hardware.locationId as string),
targetFilterKeys: [['_class', 'id', 'locationId']],
targetEntity: {
// not sure if it was inteded, but previously,
// _class was a string, not an array.
_class: Entities.HARDWARE._class,
id: hardware.id,
locationId: hardware.locationId as number,
Expand Down
2 changes: 2 additions & 0 deletions src/steps/fetch-hardware/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ test('fetchHardwareAssets', async () => {
url: {
protocol: false,
query: false,
hostname: false,
port: false,
},
},
},
Expand Down
28 changes: 10 additions & 18 deletions src/steps/fetch-hardware/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ export async function fetchHardwareAssets({
const hardwareIds: number[] = [];

await client.iterateHardware(async (device) => {
const assignedUser = device.assigned_to;
const username = assignedUser?.username || assignedUser;
const user = username ? await client.fetchUser(username) : undefined;
const assignedTo = device.assigned_to;
const user =
assignedTo?.type === 'user'
? await client.fetchUser(assignedTo.username as string)
: undefined;

hardwareIds.push(device.id);

Expand All @@ -36,20 +38,10 @@ export async function fetchHardwareAssets({
const targetEntity = convertHardware(device, user);

const relationships: MappedRelationship[] = [];
if (accountEntity) {
if (targetEntity.macAddress) {
relationships.push(
mapHardwareRelationship(accountEntity, targetEntity, 'macAddress'),
);
} else if (targetEntity.serial) {
relationships.push(
mapHardwareRelationship(accountEntity, targetEntity, 'serial'),
);
} else if (targetEntity.hardwareId) {
relationships.push(
mapHardwareRelationship(accountEntity, targetEntity, 'hardwareId'),
);
}
if (accountEntity && targetEntity.serial) {
relationships.push(
mapHardwareRelationship(accountEntity, targetEntity, 'serial'),
);
}

if (targetEntity.locationId) {
Expand All @@ -60,7 +52,7 @@ export async function fetchHardwareAssets({
await jobState.addRelationships(relationships);
} else {
logger.warn(
{ assetId: device.assetId, username: assignedUser?.username },
{ assetId: device.id, username: user?.username },
'Hardware asset has no supported identifier, mapped relationship not created',
);
}
Expand Down