-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-api.mjs
More file actions
73 lines (64 loc) · 2.13 KB
/
test-api.mjs
File metadata and controls
73 lines (64 loc) · 2.13 KB
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
#!/usr/bin/env node
/**
* Quick test script to validate the SolarWinds API connection
* Run with: node test-api.mjs
* Make sure PAPERTRAIL_TOKEN or SWO_API_TOKEN is set in .env or environment
*/
import "dotenv/config";
import { loadConfig } from "./dist/config.js";
import { SwoClient } from "./dist/client/swo-client.js";
import { getTimeRange } from "./dist/utils/time.js";
async function main() {
console.log("Loading config...");
let config;
try {
config = loadConfig();
} catch (e) {
console.error(`✗ Config error: ${e.message}`);
console.log("\nMake sure to set your token:");
console.log(" export PAPERTRAIL_TOKEN=your-token");
console.log(" # or");
console.log(" export SWO_API_TOKEN=your-token");
process.exit(1);
}
console.log(`✓ Region: ${config.region}`);
console.log(`✓ Base URL: ${config.baseUrl}`);
console.log(`✓ Token: ${config.apiToken.slice(0, 8)}...${config.apiToken.slice(-4)}`);
const client = new SwoClient(config);
const { startTime, endTime } = getTimeRange({ daysBack: 1 });
console.log(`\nTime range: ${startTime} to ${endTime}`);
console.log("\n--- Testing search_logs ---");
try {
const logs = await client.searchLogs({
filter: "*",
startTime,
endTime,
maxPages: 1,
pageSize: 10,
});
console.log(`✓ Found ${logs.length} logs`);
if (logs.length > 0) {
console.log(` Sample: ${logs[0].message?.slice(0, 80)}...`);
}
} catch (error) {
console.error(`✗ search_logs error: ${error.message}`);
if (error.statusCode === 401) {
console.log(" → Check your API token is valid");
}
}
console.log("\n--- Testing list_entities ---");
try {
const entities = await client.listEntities({ limit: 5 });
console.log(`✓ Found ${entities.length} entities`);
entities.slice(0, 3).forEach((e) => {
console.log(` - ${e.type || 'unknown'}: ${e.name || e.id}`);
});
} catch (error) {
console.error(`✗ list_entities error: ${error.message}`);
}
console.log("\n✓ API validation complete!");
}
main().catch((e) => {
console.error("Fatal error:", e);
process.exit(1);
});