-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
99 lines (86 loc) · 2.8 KB
/
index.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
import fs from 'fs';
import csv from 'csv-parser';
const isOffboard = (process.argv.pop().toLowerCase() === 'offboard');
const title = `Orkes Bulk User O${isOffboard ? 'ff' : 'n'}boarding`;
const bar = '='.repeat(title.length);
console.log([bar, title, bar].join('\n'));
const ORKES_URI = process.env.ORKES_URI;
const ORKES_TOKEN = process.env.ORKES_TOKEN;
if (!ORKES_URI || !ORKES_TOKEN) {
console.error('Please set these environment variables:')
if (!ORKES_URI) {
console.error('ORKES_URI = cluster uri e.g. https://my-company.orkesconductor.io/api');
}
if (!ORKES_TOKEN) {
console.error('ORKES_TOKEN = Press "Copy Token" near bottom of left navigation in Orkes UI');
}
process.exit(1);
}
const file = 'users.csv';
console.log(`Reading CSV input from: ${file}`);
const saveUser = (action, user) => {
const config = {
method: action,
headers: {
'X-Authorization': ORKES_TOKEN,
'Content-Type': 'application/json',
},
};
let verb = 'deleted';
if (action === 'PUT') {
config.body = JSON.stringify(user);
verb = 'saved';
}
fetch(`${ORKES_URI}/users/${user.id}`, config)
.then((r) => r.ok ? r.json() : Promise.reject(r))
.then(() => console.log(`User ${verb}: ${user.name} (${user.id})`))
.catch((error) => {
console.error(`Error for ${user.name} (${user.id}):`)
if (typeof error.json === 'function') {
error.json()
.then(jsonError => console.error(jsonError))
.catch(error => console.error(error.statusText));
} else {
console.error(error);
}
});
};
const processSplit = (arr) => arr.split(',')
.map((item) => item.trim())
.filter((item) => item.length > 0);
const validRoles = [ 'ADMIN', 'USER', 'METADATA_MANAGER', 'WORKFLOW_MANAGER', 'USER_READ_ONLY' ];
const data = [];
const validateId = (id) => {
const trimId = id.trim();
if (id.indexOf('@') === -1) {
console.error(`ID ${trimId} is not a valid email`);
process.exit(1);
}
return trimId;
};
const validateRole = (role) => {
const upperRole = role.toUpperCase();
if (validRoles.indexOf(upperRole) === -1) {
console.error(`Role ${upperRole} is not a valid role`);
process.exit(1);
}
return upperRole;
};
const main = () => {
const processedData = data.map((user) => ({
...user,
id: validateId(user.id),
roles: processSplit(user.roles).map((role) => validateRole(role)),
groups: processSplit(user.groups),
}));
console.log(`CSV file successfully processed: ${processedData.length} rows read\n`);
const action = isOffboard ? 'DELETE' : 'PUT';
processedData.forEach((entry) => saveUser(action, entry));
};
fs.createReadStream(file)
.pipe(csv())
.on('data', (row) => data.push(row))
.on('end', main)
.on('error', (err) => {
console.error('Error occurred while reading the CSV file:', err);
});