From db16122b127137fbec39cf806a09a48ec8375409 Mon Sep 17 00:00:00 2001 From: Samuel Poulton Date: Wed, 26 Jun 2024 13:59:41 -0600 Subject: [PATCH 1/3] Fix CMDB entity converter to only use JSON.stringify on object values. Eliminates double quotes from being included as part of the value. --- .../__snapshots__/converters.test.ts.snap | 52 +++++++++++++++++++ src/steps/cmdb/converters.test.ts | 27 ++++++++++ src/steps/cmdb/converters.ts | 19 ++++++- 3 files changed, 96 insertions(+), 2 deletions(-) create mode 100644 src/steps/cmdb/__snapshots__/converters.test.ts.snap create mode 100644 src/steps/cmdb/converters.test.ts 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..e20794c --- /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": 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, +} +`; 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..ff83d3c 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 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, From 77d1ceef72b0033652688baf6f186a9d6fdd6d97 Mon Sep 17 00:00:00 2001 From: Samuel Poulton Date: Wed, 26 Jun 2024 14:05:38 -0600 Subject: [PATCH 2/3] update comment for clarity. --- src/steps/cmdb/converters.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/steps/cmdb/converters.ts b/src/steps/cmdb/converters.ts index ff83d3c..abc16c9 100644 --- a/src/steps/cmdb/converters.ts +++ b/src/steps/cmdb/converters.ts @@ -25,7 +25,7 @@ export function createCMDBEntity( let finalValue = value; if (typeof value == 'object') { - // Only stringify if value is an object to avoid double quotes around values. + // Only stringify if value is an object to avoid double quotes around primitive values. finalValue = JSON.stringify(value); } else { finalValue = value; From 45241837c56a9ad3b67a556da3583c0f81de21c5 Mon Sep 17 00:00:00 2001 From: Samuel Poulton Date: Wed, 26 Jun 2024 14:28:33 -0600 Subject: [PATCH 3/3] Append UTC to dates. --- src/steps/cmdb/__snapshots__/converters.test.ts.snap | 4 ++-- src/steps/cmdb/converters.ts | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/steps/cmdb/__snapshots__/converters.test.ts.snap b/src/steps/cmdb/__snapshots__/converters.test.ts.snap index e20794c..7259b0b 100644 --- a/src/steps/cmdb/__snapshots__/converters.test.ts.snap +++ b/src/steps/cmdb/__snapshots__/converters.test.ts.snap @@ -21,7 +21,7 @@ exports[`createCMDBEntity that properties are converted correctly 1`] = ` "businessUnit": undefined, "category": undefined, "correlationId": undefined, - "createdOn": 1554520152000, + "createdOn": 1554498552000, "displayName": "test-name", "environment": undefined, "id": "test-id", @@ -46,7 +46,7 @@ exports[`createCMDBEntity that properties are converted correctly 1`] = ` "u-custom-boolean": true, "u-custom-number": 234, "u-custom-object": "{"a":"a","b":"b","c":123}", - "updatedOn": 1586142552000, + "updatedOn": 1586120952000, "warrantyExpiration": undefined, } `; diff --git a/src/steps/cmdb/converters.ts b/src/steps/cmdb/converters.ts index abc16c9..15766e4 100644 --- a/src/steps/cmdb/converters.ts +++ b/src/steps/cmdb/converters.ts @@ -63,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, }, },