-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathImportGroupsFromCSV.js
94 lines (84 loc) · 3.71 KB
/
ImportGroupsFromCSV.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
javascript:
/*
bookmarklet name: /ImportGroupsCSV#
https://github.com/gabrielsroka/gabrielsroka.github.io/blob/master/ImportGroupsFromCSV.js
Import Groups from CSV
Setup:
Drag and drop all of this to your bookmark toolbar. Or:
Create a Chrome Snippet for JavaScript:
1. Press F12 (Windows) to open DevTools.
2. Go to Sources > Snippets, click New Snippet.
3. Give it a name, eg, "Import Groups from CSV".
4. Copy/paste the code.
5. Save (Ctrl+S, Windows).
Usage:
1. Sign in to Okta Admin Console.
2. If using the bookmark, click it. Or, if using a snippet, click the Run button on the bottom right or press Ctrl+Enter (Windows).
3. Look for the popup window in the upper-left corner of your browser.
Sample CSV (header row is required, description is optional):
name,description
Group 1,The first group
*/
(function () {
const popup = createPopup('Import Groups from CSV');
const groups = [];
$('<input type="file">')
.appendTo(popup)
.change(function () {
const reader = new FileReader();
reader.onload = () => parseFile(reader.result);
reader.readAsText(this.files[0]);
});
function parseFile(file) {
const lineSeparator = /\r\n|\r|\n/;
const fieldSeparator = ',';
const lines = file.split(lineSeparator);
const fields = lines.shift().split(fieldSeparator);
const headers = Object.fromEntries(fields.map((val, i) => [val, i])); /* Map header name to number. */
lines.forEach(line => {
if (line == '') return;
const fields = line.split(fieldSeparator);
const group = {
profile: {
name: fields[headers.name],
description: fields[headers.description]
}
};
groups.push(group);
});
newGroup(0);
}
function newGroup(i) {
popup.html('Importing ' + groups[i].profile.name + ' (' + (i + 1) + ' of ' + groups.length + ')');
$.post({
url: '/api/v1/groups',
data: JSON.stringify(groups[i]),
contentType: 'application/json'
}).then(function (group, status, jqXHR) {
if (++i < groups.length) {
const remaining = jqXHR.getResponseHeader('X-Rate-Limit-Remaining');
if (remaining && remaining <= 10) {
popup.html('Sleeping...');
const intervalID = setInterval(() => {
if ((new Date()).getTime() / 1000 > jqXHR.getResponseHeader('X-Rate-Limit-Reset')) {
clearInterval(intervalID);
newGroup(i);
}
}, 1000);
} else {
newGroup(i);
}
} else {
popup.html('Imported ' + groups.length + ' groups. Done.');
}
}).fail(jqXHR => popup.html('Error on line ' + (i + 1) + ': ' + jqXHR.responseJSON.errorCauses[0].errorSummary));
}
function createPopup(title) {
const popup = $(`<div style='position: absolute; z-index: 1000; top: 0px; max-height: calc(100% - 28px); max-width: calc(100% - 28px); padding: 8px; margin: 4px; overflow: auto; ` +
`background-color: white; border: 1px solid #ddd;'>` +
`${title}<div style='display: block; float: right;'><a href='https://github.com/gabrielsroka/gabrielsroka.github.io/blob/master/ImportGroupsFromCSV.js' target='_blank' rel='noopener' style='padding: 4px'>?</a> ` +
`<a class=close style='cursor: pointer; padding: 4px'>X</a></div><br><br></div>`).appendTo(document.body);
popup.find('a.close').click(() => popup.remove());
return $('<div></div>').appendTo(popup);
}
})();