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

Fix CMDB Converter #53

Merged
merged 3 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
52 changes: 52 additions & 0 deletions src/steps/cmdb/__snapshots__/converters.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`createCMDBEntity that properties are converted correctly 1`] = `
{
"_class": [
"Configuration",
],
"_key": "test-id",
"_rawData": [
{
"name": "default",
"rawData": {
"uploadStatus": "SKIPPED",
"uploadStatusReason": "Raw data currently disabled for this entity type",
},
},
],
"_type": "service_now_cmdb_object",
"assignedTo": undefined,
"attributes": undefined,
"businessUnit": undefined,
"category": undefined,
"correlationId": undefined,
"createdOn": 1554520152000,
"displayName": "test-name",
"environment": undefined,
"id": "test-id",
"installStatus": undefined,
"ipAddress": undefined,
"macAddress": undefined,
"managedBy": undefined,
"managedByGroup": undefined,
"model": undefined,
"name": "test-name",
"operationalStatus": undefined,
"ownedBy": undefined,
"serial": undefined,
"subcategory": undefined,
"sysClassNames": [
"fred",
"george",
],
"test-u-custom-3": "custom 3",
"u-custom-1": "custom 1",
"u-custom-2": "",
"u-custom-boolean": true,
"u-custom-number": 234,
"u-custom-object": "{"a":"a","b":"b","c":123}",
"updatedOn": 1586142552000,
"warrantyExpiration": undefined,
}
`;
27 changes: 27 additions & 0 deletions src/steps/cmdb/converters.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { CMDBItem } from '../../types';
import { createCMDBEntity } from './converters';

describe('createCMDBEntity', () => {
test('that properties are converted correctly', () => {
const data: CMDBItem = ({
sys_id: 'test-id',
sys_created_on: '2019-04-05 21:09:12',
sys_updated_on: '2020-04-05 21:09:12',
name: 'test-name',
u_custom_1: 'custom 1',
u_custom_2: '',
test_u_custom_3: 'custom 3',
u_custom_4: undefined,
u_custom_object: {
a: 'a',
b: 'b',
c: 123,
},
u_custom_number: 234,
u_custom_boolean: true,
} as unknown) as CMDBItem;

const entity = createCMDBEntity(data, ['fred', 'george']);
expect(entity).toMatchSnapshot();
});
});
19 changes: 17 additions & 2 deletions src/steps/cmdb/converters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,22 @@ export function createCMDBEntity(
for (const [key, value] of Object.entries(data)) {
//custom fields
if (key.startsWith('u_') || key.includes('_u_')) {
customFields[key.split('_').join('-')] = JSON.stringify(value);
const customFieldsKey = key.split('_').join('-');

// Don't include undefined/null values
if (value === undefined || value === null) {
continue;
}

let finalValue = value;
if (typeof value == 'object') {
// Only stringify if value is an object to avoid double quotes around primitive values.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I believe that this condition is correct but maybe be overly simplistic.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Arrays will be stringified

finalValue = JSON.stringify(value);
} else {
finalValue = value;
}

customFields[customFieldsKey] = finalValue;
}
}

Expand All @@ -36,7 +51,7 @@ export function createCMDBEntity(
attributes: data.attributes,
businessUnit: data.business_unit,
category: data.category,
enviroment: data.environment,
environment: data.environment,
ipAddress: data.ip_address,
macAddress: data.mac_address,
operationalStatus: data.operational_status,
Expand Down
Loading