diff --git a/src/steps/cmdb/__snapshots__/converters.test.ts.snap b/src/steps/cmdb/__snapshots__/converters.test.ts.snap new file mode 100644 index 0000000..7259b0b --- /dev/null +++ b/src/steps/cmdb/__snapshots__/converters.test.ts.snap @@ -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": 1554498552000, + "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": 1586120952000, + "warrantyExpiration": undefined, +} +`; diff --git a/src/steps/cmdb/converters.test.ts b/src/steps/cmdb/converters.test.ts new file mode 100644 index 0000000..4b3b93d --- /dev/null +++ b/src/steps/cmdb/converters.test.ts @@ -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(); + }); +}); diff --git a/src/steps/cmdb/converters.ts b/src/steps/cmdb/converters.ts index 5b85041..15766e4 100644 --- a/src/steps/cmdb/converters.ts +++ b/src/steps/cmdb/converters.ts @@ -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. + finalValue = JSON.stringify(value); + } else { + finalValue = value; + } + + customFields[customFieldsKey] = finalValue; } } @@ -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, @@ -48,8 +63,8 @@ export function createCMDBEntity( ownedBy: data.owned_by?.value, managedByGroup: data.managed_by_group?.value, assignedTo: data.assigned_to?.value, - createdOn: parseTimePropertyValue(data.sys_created_on), - updatedOn: parseTimePropertyValue(data.sys_updated_on), + createdOn: parseTimePropertyValue(data.sys_created_on + ' UTC'), + updatedOn: parseTimePropertyValue(data.sys_updated_on + ' UTC'), ...customFields, }, },