-
Notifications
You must be signed in to change notification settings - Fork 1
/
clearpass-csv2api.js
executable file
·770 lines (731 loc) · 27.9 KB
/
clearpass-csv2api.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
#!/usr/bin/env node
const CppmApi = require('aruba-clearpass-api')
const async = require('async')
const IMPORTING =
{
device: {
key: 'mac',
aliases: ['device', 'devices'],
privilege_create: 'mac_create',
privilege_edit: 'full-user-control',
required: ['role_id'],
CppmApiCreate: 'createDevice',
CppmApiReplace: 'replaceDeviceByMac',
CppmApiUpdate: 'updateDeviceByMac'
},
guest: {
key: 'username',
aliases: ['guest', 'guests', 'guestuser', 'guestusers'],
privilege_create: 'create_user',
privilege_edit: 'full-user-control',
required: ['password', 'role_id'],
CppmApiCreate: 'createGuest',
CppmApiReplace: 'replaceGuestByUserName',
CppmApiUpdate: 'updateGuestByUserName'
}
/* "endpoint": {
"key": "mac",
"aliases": ["endpoint", "endpoints"],
"privilege_create": "cppm_endpoints",
"privilege_edit": "cppm_endpoints",
"required": [],
"CppmApiCreate": "createEndpoint",
"CppmApiReplace": "replaceEndpointByMac",
"CppmApiUpdate": "updateEndpointByMac"
} */
}
const { program } = require('commander')
program
.version('1.0.0')
.helpOption('-h, --help', 'Display help for command')
.option('-v, --verbose', 'Output extra debugging', false)
.option('--importing <importing>', 'What is being imported. Current accepted values: ' + Object.keys(IMPORTING).join(', '), 'device') // Match names from API Explorer
.on('option:verbose', function () {
// if (this.opts().verbose) log.level = 'debug'
})
.on('option:importing', function () {
const importing = this.opts().importing = this.opts().importing.toLowerCase()
console.debug('importing', importing)
let importingMatch = false
for (const importKey in IMPORTING) {
if (Object.prototype.hasOwnProperty.call(IMPORTING, importKey)) {
if (importKey === importing) {
importingMatch = true
break
}
const imp = IMPORTING[importKey]
for (let i = 0; i < imp.aliases.length; i++) {
if (imp.aliases[i] === importing) {
importingMatch = true
this.opts().importing = importKey
break
}
}
}
if (importingMatch) {
break
}
}
if (!importingMatch) {
console.error('Invalid value for: --importing (Allowed: ' + Object.keys(IMPORTING).join(', ') + ')')
process.exit(1)
}
})
// Main import function
program
.command('import <csv_file>'/*, { isDefault: true } */)
.description('Import a CSV of items into ClearPass')
.option('--host <host>', 'The IP address/hostname of ClearPass.', '127.0.0.1')
// .option('-p, --port <port>', 'Port override', 443)
.option('--insecure', 'Disable SSL validation', false)
.option('--token <token>', 'Generated Bearer token', '')
.option('--client_id <client_id>', 'API Client ID', 'Client1')
.option('--client_secret <client_secret>', 'API Client Secret', '')
.option('-x --extra <extra>', 'An extra key=value pair. Multiple supported.', collectPairs, {})
.option('--exclude <list of columns to exclude>', 'Excluded list', [])
.option('--sessions <sessions>', 'Max simultaneous sessions', 1)
.option('--strategy <strategy>', `Strategy to use when making API calls
create-only Create new items only. Existing will be ignored.
create-or-replace Create new items or replace existing entries with the CSV.
create-or-update Create new items or update existing entries with data in the CSV.
update-only Update existing items only. Entries without a match will be skipped.
update-or-create Update existing items. If the entry does not exist it will be created.
replace-only Replace existing items only. Entries without a match will be skipped.
replace-or-create Replace existing items. If the entry does not exist it will be created.
`, 'create-only')
.option('--change-of-authorization', 'Send RADIUS CoA requests', false) // changeOfAuthorization
.option('--dry-run', 'Dry run, do not send any API', false) // dryRun
.on('option:token', function () {
// aruba-clearpass-api auto adds 'Bearer '
if (this.opts().token.substring(0, 7).toLowerCase() === 'bearer ') {
this.opts().token = this.opts().token.substring(7)
}
})
.on('option:exclude', function () {
this.opts().exclude = typeof (this.opts().exclude) === 'string' ? this.opts().exclude.split(',') : []
})
.on('option:strategy', function () {
const strategies = [
'create-only', // POST
'create-or-replace', // POST-PUT
'create-or-update', // POST-PATCH
'update-only', // PATCH
'update-or-create', // PATCH-POST
'replace-only', // PUT
'replace-or-create' // PUT-POST
]
if (!strategies.includes(this.opts().strategy)) {
console.error('Invalid value for: --strategy (Allowed: ' + strategies.join(', ') + ')')
process.exit(1)
}
})
.action(commandActionImportCsv)
// Takes the first two lines of the csv and outputs what would have been created.
program
.command('testcsv <csv_file>'/*, { isDefault: true } */)
.description('Confirm the csv seems valid.')
.option('-x --extra <extra>', 'An extra key=value pair. Multiple supported.', collectPairs, {})
.option('--exclude <list of columns to exclude>', 'Excluded list', [])
.on('option:exclude', function () {
this.opts().exclude = typeof (this.opts().exclude) === 'string' ? this.opts().exclude.split(',') : []
})
.action(commandActionTestCSV)
// Confirm connectivity settings and permissions
program
.command('ping')
.description('Test connectivity, authentication and privileges')
.option('--host <host>', 'The IP address/hostname of ClearPass.', '127.0.0.1')
// .option('-p, --port <port>', 'Port override', 443)
.option('--token <token>', 'Generated Bearer token', '')
.option('--client_id <client_id>', 'API Client ID', 'Client1')
.option('--client_secret <client_secret>', 'API Client Secret', '')
.option('--insecure', 'Disable SSL validation', false)
.on('option:token', function () {
// aruba-clearpass-api auto adds 'Bearer '
if (this.opts().token.substring(0, 7).toLowerCase() === 'bearer ') {
this.opts().token = this.opts().token.substring(7)
}
})
.addHelpText('after', `
To override the port, pass --host <host>:<port>.
Pass either --client_id and --client_secret or simply --token. Generate Access Token in the UI can be used to generate a short term token.
API Clients are created in Guest » Administration » API Services » API Clients.
You need the following set:
* Operating Mode: ClearPass REST API
* Operator Profile: A profile with sufficient privileges
* Grant Type: Client Credentials
The Client ID and Client Secret are passed as arguments. Ensure you protect the secret.
Privileges:
* ALL : API Services > Allow API Access
* Devices : Devices > Create New Device and Guest Manager > Full User Control
* Guests : Guest Manager > Create New Guest Account and Guest Manager > Full User Control
`)
.action(commandActionPing)
// Generate a randomized csv
program
.command('generate <count>')
.description('Generate a random CSV')
.option('-x --extra <extra>', 'An extra key=value pair. Multiple supported.', collectPairs, {})
.action(commandActionGenerate)
program.addHelpText('after', `
Example calls:
Confirm credentials and permissions:
$ clearpass-csv2api ping --host=192.0.2.10 --client_id=Client1 --client_secret=asdfadsf
Test the CSV:
$ clearpass-csv2api testcsv devices.csv
$ clearpass-csv2api --importing=guest testcsv guest.csv
Initiate the import:
$ clearpass-csv2api --verbose import --host 192.0.2.10 --client_id=Client1 --client_secret=asdfadsf devices.csv
$ clearpass-csv2api --verbose import --host 192.0.2.10 --client_id=Client1 --client_secret=asdfadsf --strategy=update-or-create devices-sync.csv
$ clearpass-csv2api --verbose --importing=guest import --host 192.0.2.10 --client_id=Client1 --client_secret=asdfadsf guests.csv
$ clearpass-csv2api --verbose import --host 192.0.2.10 --client_id=Client1 --client_secret=asdfadsf --exclude=id,region -x "notes=Import from ACME" -x "visitor_company=IoT Corp" devices.csv
Generate test data:
$ clearpass-csv2api generate 100 > devices-100.csv
$ clearpass-csv2api generate 100 -x role_id=1 -x "notes=Import from ACME" > devices-ACME-Contractors.csv
$ clearpass-csv2api --importing=guest generate 100 > guests-100.csv
`)
/**
* Need to accumulate -x --extra
*/
function collectPairs (value, previous) {
const eq = value.indexOf('=')
if (eq <= 0 || eq === value.length) {
console.error("Expected 'key=value' pair: ", value)
process.exit(1)
}
const key = value.substring(0, eq)
const val = value.substring(eq + 1)
if (/\W/.test(key)) {
console.error("Key can only contain letters, numbers and underscore 'key=value' pair: ", key)
process.exit(1)
}
previous[key] = val
return previous
}
function createClient (options) {
const client = new CppmApi({
host: options.host,
token: options.token,
clientId: options.client_id,
clientSecret: options.client_secret,
sslValidation: !options.insecure
})
return client
}
function normalizeData (data, options) {
let i = 0
let len = options.exclude.length
for (i = 0; i < len; i++) {
delete data[options.exclude[i]]
}
Object.assign(data, options.extra)
const booleans = ['enabled']
len = booleans.length
for (i = 0; i < len; i++) {
if (typeof data[booleans[i]] === 'string') {
const b = data[booleans[i]].toLowerCase()
data[booleans[i]] = b === '1' ||
b === 'true' ||
b === 'yes' ||
b === 'on'
}
}
return data
}
function validateHeaders (headers, options, imp) {
const missing = []
if (!headers.includes(imp.key)) {
missing.push(imp.key)
}
for (let i = 0; i < imp.required.length; i++) {
if (!headers.includes(imp.required[i])) {
missing.push(imp.required[i])
}
}
if (missing.length > 0) {
return 'The following headers are required: ' + missing.join(', ')
}
return true
}
function validateData (data, options, imp) {
// Check required fields. Skips when unset as that must be checked above.
const missing = []
let key = '?'
if (typeof (data[imp.key]) !== 'undefined' && data[imp.key] === '') {
missing.push(imp.key)
} else {
key = data[imp.key]
}
for (let i = 0; i < imp.required.length; i++) {
if (typeof (data[imp.required[i]]) !== 'undefined' && data[imp.required[i]] === '') {
missing.push(imp.required[i])
}
}
if (missing.length > 0) {
return key + ' - SKIPPING: ' + 'The following fields are empty: ' + missing.join(', ')
}
// NULL checks
// Note if a row is missing a column, csv-parser outright skips it. You will not get null.
// That is indicative of worse problems though.
return true
}
function handleAPIClientError (stats, key, error, verbose) {
const response = error.response
if (!response) {
console.error(key + ' - ERROR: ' + 'Unknown error:', JSON.stringify(error, null, 2))
stats.errors++
} else if (response.status === 400 && response.data && response.data.title === 'invalid_client') {
stats.errors++
console.error(key + ' - ERROR: ' + 'Bad keys?:', response.data.detail)
} else if (response.status === 404 && response.data && response.data.title === 'Not Found') {
// Does not exists
stats.not_exists++
console.error(key + ' - ERROR: ' + (response.data.detail || 'Not found'))
} else if (response.status === 422 && response.data && response.data.result && response.data.result.user_exists) {
// Exists
stats.exists++
console.error(key + ' - ERROR: ' + (response.data.result.message || 'Already exists'))
} else if (response.status === 422 && response.data) {
stats.errors++
console.error(key + ' - ERROR: ' + response.data.detail, response.data.validation_messages)
} else {
stats.errors++
console.error(key + ' - ERROR: ' + 'Unknown status: ' + response.status, JSON.stringify(response.data, null, 2))
}
}
async function commandActionPing (options, command) {
// const verbose = command.parent.opts().verbose
const client = createClient(options)
client.getMyInfo(function (error, result, statusCode) {
if (error) {
let msg = error.message
if (result && result.detail) {
msg += ' (' + result.detail + ')'
}
console.error('FAILED: ' + msg)
} else {
if (statusCode === 200) {
console.log('SUCCESS', result)
client.getMyPrivileges(function (error, result, statusCode) {
if (error) {
let msg = error.message
if (result && result.detail) {
msg += ' (' + result.detail + ')'
}
console.error('FAILED: ' + msg)
} else {
if (statusCode === 200) {
const imp = IMPORTING[command.parent.opts().importing]
if (!Array.isArray(result.privileges)) {
console.error('FAILED: ' + "Missing 'privileges'")
} else {
if (result.privileges.includes(imp.privilege_create)) {
console.log('SUCCESS', 'Create privilege confirmed')
} else {
console.error('FAILED: ' + "Missing '" + imp.privilege_create + "' privilege")
}
if (result.privileges.includes(imp.privilege_edit)) {
console.log('SUCCESS', 'Edit privilege confirmed')
} else {
console.error('FAILED: ' + "Missing '" + imp.privilege_edit + "' privilege")
}
}
} else {
console.warn('Unexpected code: ' + statusCode, JSON.stringify(result, null, 2))
}
}
})
} else {
console.warn('Unexpected code: ' + statusCode, JSON.stringify(result, null, 2))
}
}
})
}
/**
* Generate random CSV. Similar to other scripts, all logging is stderr and stdout will be the CSV itself.
*/
function commandActionGenerate (count, options, command) {
// const verbose = command.parent.opts().verbose
// const imp = IMPORTING[command.parent.opts().importing]
count = parseInt(count)
if (isNaN(count) || count <= 0) {
console.error('Count must be an integer')
process.exit(1)
}
let extraKeys = ''
let extraValues = ''
for (const extraKey in options.extra) {
if (Object.prototype.hasOwnProperty.call(options.extra, extraKey)) {
extraKeys += ',' + extraKey
let extraValue = options.extra[extraKey]
if (extraValue.replace(/ /g, '').match(/[\s,"]/)) {
extraValue = '"' + extraValue.replace(/"/g, '""') + '"'
}
extraValues += ',' + extraValue
}
}
if (command.parent.opts().importing === 'device') {
const ceil = 16777215
if (count > ceil) {
console.error('Count must be less than %d', ceil)
process.exit(1)
}
let prefix = Math.floor(Math.random() * ceil)
const prefixStr = prefix.toString(16).toUpperCase()
prefix *= 256 * 256 * 256
let header = 'mac'
const hasRoleId = options.extra.role_id
if (!hasRoleId) {
header += ',role_id'
}
const hasVisitorName = options.extra.visitor_name
if (!hasVisitorName) {
header += ',visitor_name'
}
console.log(header + extraKeys)
for (let i = 0; i < count; i++) {
let mac = prefix + i
mac = mac.toString(16).toUpperCase()
while (mac.length < 12) {
mac = '0' + mac
}
mac = mac.match(/.{2}/g).join('-')
let row = mac
if (!hasRoleId) {
row += ',3'
}
if (!hasVisitorName) {
row += ',Device ' + prefixStr + ' and ' + i
}
console.log(row + extraValues)
}
} else if (command.parent.opts().importing === 'guest') {
const ceil = 16777215 // Arbitrary for Guests
if (count > ceil) {
console.error('Count must be less than %d', ceil)
process.exit(1)
}
const prefix = Math.floor(Math.random() * ceil)
const prefixStr = prefix.toString(16).toUpperCase()
console.log('username,password,role_id,visitor_name' + extraKeys)
for (let i = 0; i < count; i++) {
const username = prefix + '_' + String(i).padStart(6, '0')
const password = username.split('').reverse().join('')
console.log('%s,%s,%d,"%s"%s', username, password, 3, 'Device ' + prefixStr + ' and ' + i, extraValues)
}
} else {
console.error('Importing type not yet supported in the generator: ', command.parent.opts().importing)
}
}
function commandActionTestCSV (csvFile, options, command) {
const csv = require('csv-parser')
const fs = require('fs')
const errors = {}
const imp = IMPORTING[command.parent.opts().importing]
const verbose = command.parent.opts().verbose
let first = true
fs.createReadStream(csvFile)
.pipe(csv({ strict: true }))
.on('headers', (headers) => {
// Catches EFBBBF (UTF-8 BOM) because the buffer-to-string
// conversion translates it to FEFF (UTF-16 BOM)
if (headers[0].charCodeAt(0) === 0xFEFF) {
headers[0] = headers[0].slice(1)
this.headers = headers
}
const validated = validateHeaders(headers, options, imp)
if (validated !== true) {
if (errors[validated]) {
errors[validated]++
} else {
errors[validated] = 1
}
}
})
.on('data', (data) => {
// console.log(data);
data = normalizeData(data, options)
if (first) {
first = false
console.log('Review this structure for correctness:')
console.log(JSON.stringify(data, null, 2))
}
const validated = validateData(data, options, imp)
if (validated !== true) {
if (errors[validated]) {
errors[validated]++
} else {
errors[validated] = 1
}
}
})
.on('error', (error) => {
if (errors[error.message]) {
errors[error.message]++
} else {
errors[error.message] = 1
}
console.log('error', error.message) // TODO: end is only called when no on.error is called!!!
})
.on('end', () => { // TODO: end is only called when no on.error is called!!!
const errorMessages = Object.keys(errors)
if (errorMessages.length > 0) {
console.error('The following errors were reported:')
if (verbose) {
console.error(JSON.stringify(errors, null, 2))
} else {
console.error(JSON.stringify(errorMessages, null, 2))
}
console.error('FAILURE')
} else {
console.log('SUCCESS')
}
})
}
function commandActionImportCsv (csvFile, options, command) {
const client = createClient(options)
const imp = IMPORTING[command.parent.opts().importing]
const verbose = command.parent.opts().verbose
const coa = options.changeOfAuthorization
// TODO stack the tests console.log('Pinging');
// TODO stack the tests let ping = await commandActionPing(options, command);
// TODO stack the tests console.log('Pinged', ping);
const csv = require('csv-parser')
const fs = require('fs')
const stats = {
attempts: 0,
created: -1,
updated: -1,
replaced: -1,
exists: 0,
not_exists: 0,
skipped: 0,
errors: 0,
unknowns: 0,
min: -1,
max: -1,
duration: -1
}
const rows = [] // async.eachOfLimit
const start0 = Date.now()
fs.createReadStream(csvFile)
.pipe(csv({ strict: true }))
.on('headers', (headers) => {
// Catches EFBBBF (UTF-8 BOM) because the buffer-to-string
// conversion translates it to FEFF (UTF-16 BOM)
if (headers[0].charCodeAt(0) === 0xFEFF) {
headers[0] = headers[0].slice(1)
this.headers = headers
}
const validated = validateHeaders(headers, options, imp)
if (validated !== true) {
console.error(validated)
process.exit(1)
}
})
.on('data', (data) => {
// Memory intensive but the only current way to rate limit outbound calls.
rows.push(data)
})
.on('end', () => {
const strategy = options.strategy
let doing = 'Creating'
let doCreate = false
let doUpdate = false
let doReplace = false
let orCreate = false
let orUpdate = false
let orReplace = false
if (strategy.startsWith('create')) {
doCreate = true
doing = 'Creating'
stats.created = 0
if (strategy.endsWith('update')) {
orUpdate = true
stats.updated = 0
} else if (strategy.endsWith('replace')) {
orReplace = true
stats.replaced = 0
}
} else if (strategy.startsWith('update')) {
doUpdate = true
doing = 'Updating'
stats.updated = 0
if (strategy.endsWith('create')) {
orCreate = true
stats.created = 0
} else if (strategy.endsWith('replace')) {
// N/A stats.replaced = 0;
}
} else if (strategy.startsWith('replace')) {
doReplace = true
doing = 'Replacing'
stats.replaced = 0
if (strategy.endsWith('update')) {
// N/A stats.updated = 0;
} else if (strategy.endsWith('create')) {
orCreate = true
stats.created = 0
}
} else {
console.error('Unknown strategy: ', strategy) // Impossible
}
stats.attempts = rows.length
// process 10 chunks at a time
async.forEachOfLimit(rows, parseInt(options.sessions), (row, k, callback) => {
row = normalizeData(row, options)
const validated = validateData(row, options, imp)
if (validated !== true) {
console.error(validated)
stats.skipped++
callback()
return
}
const key = row[imp.key]
if (verbose) {
console.log(key + ' - ' + doing + '...', row)
} else {
console.log(key + ' - ' + doing + '...')
}
if (!options.dryRun) {
if (doCreate) {
let doCallback = true
const start = Date.now()
client[imp.CppmApiCreate + 'Async'](row, coa)
.then((response) => {
const result = response.data
if (response.status === 201) {
const dur = Date.now() - start
console.log(key + ' - ' + 'Created: ' + result.id + ' in ' + dur + 'ms')
if (verbose) {
console.log(key, JSON.stringify(result, null, 2))
}
stats.created++
if (dur > stats.max) stats.max = dur
if (stats.min < 0 || (stats.min > 0 && dur < stats.min)) stats.min = dur
} else {
stats.unknowns++
console.error(key + ' - ERROR: ' + 'Unexpected status: ' + response.status, JSON.stringify(result, null, 2))
}
})
.catch((error) => {
const response = error.response
if ((orUpdate || orReplace) && response && response.status === 422 && response.data && response.data.result && response.data.result.user_exists) {
const doing = orUpdate ? 'Updating' : 'Replacing'
console.log(key + ' - ' + 'Exists - ' + doing + '...')
doCallback = false
const method = orUpdate ? imp.CppmApiUpdate : imp.CppmApiReplace
const did = orUpdate ? 'Updated' : 'Replaced'
client[method + 'Async'](key, row, coa)
.then((response) => {
const result = response.data
if (response.status === 200) {
const dur = Date.now() - start
console.log(key + ' - ' + did + ': ' + result.id + ' in ' + dur + 'ms')
if (verbose) {
console.log(key, JSON.stringify(result, null, 2))
}
if (orUpdate) {
stats.updated++
} else {
stats.replaced++
}
if (dur > stats.max) stats.max = dur
if (stats.min < 0 || (stats.min > 0 && dur < stats.min)) stats.min = dur
} else {
stats.unknowns++
console.error(key + ' - ' + 'Unexpected status: ' + response.status, JSON.stringify(result, null, 2))
}
})
.catch((error) => {
handleAPIClientError(stats, key, error, verbose)
})
.finally(() => {
callback()
})
} else {
handleAPIClientError(stats, key, error, verbose)
}
})
.finally(() => {
if (doCallback) {
callback()
}
})
} else if (doUpdate || doReplace) {
const method = doUpdate ? imp.CppmApiUpdate : imp.CppmApiReplace
const did = doUpdate ? 'Updated' : 'Replaced'
let doCallback = true
const start = Date.now()
client[method + 'Async'](key, row, coa)
.then((response) => {
const result = response.data
if (response.status === 200) {
const dur = Date.now() - start
console.log(key + ' - ' + did + ': ' + result.id + ' in ' + dur + 'ms')
if (verbose) {
console.log(key, JSON.stringify(result, null, 2))
}
if (doUpdate) {
stats.updated++
} else {
stats.replaced++
}
if (dur > stats.max) stats.max = dur
if (stats.min < 0 || (stats.min > 0 && dur < stats.min)) stats.min = dur
} else {
stats.unknowns++
console.error(key + ' - ' + 'Unknown status: ' + response.status, JSON.stringify(result, null, 2))
}
})
.catch((error) => {
const response = error.response
if (orCreate && response && response.status === 404 && response.data && response.data.title && response.data.title === 'Not Found') {
console.log(key + ' - ' + 'Not Found - Creating...')
doCallback = false
client[imp.CppmApiCreate + 'Async'](row, coa)
.then((response) => {
const result = response.data
if (response.status === 201) {
const dur = Date.now() - start
console.log(key + ' - ' + 'Created: ' + result.id + ' in ' + dur + 'ms')
if (verbose) {
console.log(key, JSON.stringify(result, null, 2))
}
stats.created++
if (dur > stats.max) stats.max = dur
if (stats.min < 0 || (stats.min > 0 && dur < stats.min)) stats.min = dur
} else {
stats.unknowns++
console.error(key + ' - ' + 'Unknown status: ' + response.status, JSON.stringify(result, null, 2))
}
})
.catch((error) => {
handleAPIClientError(stats, key, error, verbose)
})
.finally(() => {
callback()
})
} else {
handleAPIClientError(stats, key, error, verbose)
}
})
.finally(() => {
if (doCallback) {
callback()
}
})
} else {
console.error(key + ' - ERROR: ' + 'Impossible to be here')
callback()
}
} else {
callback()
}
}).finally(() => {
stats.duration = Date.now() - start0
console.log('Results:', JSON.stringify(stats, null, 2))
})
})
}
program.parse(process.argv)