-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
105 lines (87 loc) · 3.4 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
100
101
102
103
104
105
import Luzmo from '@luzmo/nodejs-sdk';
import fs from 'fs';
import dotenv from 'dotenv';
dotenv.config();
// Initialize Luzmo client
const client = new Luzmo({
api_key: process.env.LUZMO_KEY,
api_token: process.env.LUZMO_TOKEN,
host: process.env.LUZMO_API_HOST
});
async function migrateIntegrations() {
// Arrays to store migration reports
let migrationsReport = [];
let partialFailures = [];
let completeFailures = [];
try {
// Step 1: Load all Integrations.
const integrations = await client.get('integration', {
include: [{
model: "Securable",
attributes: ["id", "type"]
}]
});
// Check if any Integrations were found
if (integrations.count === 0) {
console.log("No integrations found. Exiting script.");
return;
}
// Step 2: Create a collection for each Integration
for (const integration of integrations.rows) {
const integrationName = integration.name;
const integrationId = integration.id;
let collectionId;
let status = 'success';
let associatedSecurablesCount = 0;
try {
// Create a new Collection
const collectionCreationResponse = await client.create('collection', { name: integrationName });
collectionId = collectionCreationResponse.id;
// Associate dashboards and datasets from the Integration with the new Collection
for (const securable of integration.securables) {
const securableId = securable.id;
try {
await client.associate('collection', collectionId, { role: 'Securables', id: securableId });
associatedSecurablesCount++;
} catch (associationError) {
status = 'partial failure';
partialFailures.push({ integrationId, collectionId, securableId, error: associationError.message });
}
}
} catch (createCollectionError) {
status = 'failure';
completeFailures.push({ integrationId, error: createCollectionError.message });
}
// Record migration report
migrationsReport.push({ integrationId, collectionId, status, associatedSecurablesCount });
}
// Write migration report to CSV
const csvHeader = 'Integration ID,Collection ID,Migration Status,Associated Securables Count,Error Message\n';
let csvData = csvHeader;
migrationsReport.forEach(row => {
const { integrationId, collectionId, status, associatedSecurablesCount, error } = row;
const errorMessage = error ? `"${error}"` : '';
csvData += `"${integrationId}","${collectionId}","${status}","${associatedSecurablesCount}","${errorMessage}"\n`;
});
// Print migration report to console
console.log("Migration from Integrations to Collections completed. Report:");
console.table(migrationsReport);
// Print partial failures to console
if (partialFailures.length > 0) {
console.log("Partial failure details:");
console.table(partialFailures);
}
// Print complete failures to console
if (completeFailures.length > 0) {
console.log("Complete failure details:");
console.table(completeFailures);
}
// Save migration report to CSV
fs.writeFileSync('migration_report.csv', csvData);
console.log("Migration report saved as 'migration_report.csv'");
} catch (error) {
console.error("Error occurred during migration:", error);
}
}
// Start the migration process
migrateIntegrations();