Skip to content

Commit f32af6e

Browse files
committed
Add hostname to FQDN overwrite demo script and update package.json
1 parent ba48d0f commit f32af6e

File tree

2 files changed

+144
-1
lines changed

2 files changed

+144
-1
lines changed

examples/hostname-to-fqdn-demo.ts

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
import { TidalApiClient } from '../src/api/client';
2+
import { ServerBulkOperations } from '../src/operations/servers';
3+
import { logger } from '../src/utils/logger';
4+
import { loadConfig, getAuthCredentials } from '../src/config/environment';
5+
6+
/**
7+
* Hostname to FQDN Overwrite Demo
8+
*
9+
* This example demonstrates:
10+
* 1. Fetching all servers from the API
11+
* 2. Overwriting fqdn field with host_name values for all servers with host_name
12+
* 3. Generating a report of successful overwrites
13+
*/
14+
async function hostnameToFqdnDemo() {
15+
try {
16+
logger.info('=== Hostname to FQDN Overwrite Demo ===');
17+
18+
// Load configuration and credentials
19+
const config = loadConfig();
20+
const credentials = getAuthCredentials();
21+
22+
// Initialize the client
23+
const client = new TidalApiClient({ workspace: config.workspace });
24+
25+
logger.info('Authenticating with Tidal API...');
26+
await client.authenticate(credentials.username, credentials.password);
27+
logger.info('Authentication successful');
28+
29+
// Initialize server operations
30+
const serverOps = new ServerBulkOperations(client);
31+
32+
// 2. Fetch all servers from the API
33+
logger.info('\n--- Fetching Servers from API ---');
34+
logger.info('Retrieving all servers for hostname to fqdn overwrite...');
35+
36+
const servers = await serverOps.getServers();
37+
logger.info(`Retrieved ${servers.length} servers from the API`);
38+
39+
// 3. Find servers for fqdn overwrite
40+
logger.info('\n--- Identifying Servers for FQDN Overwrite ---');
41+
42+
const serversForFqdnOverwrite = servers.filter(server =>
43+
server.host_name &&
44+
server.host_name.trim() !== ''
45+
);
46+
47+
logger.info(`Found ${serversForFqdnOverwrite.length} servers with host_name values`);
48+
logger.info(`These will have their fqdn field overwritten with host_name value`);
49+
50+
if (serversForFqdnOverwrite.length > 0) {
51+
logger.info(`Overwriting fqdn values for ${serversForFqdnOverwrite.length} servers`);
52+
53+
const assignmentResults = {
54+
successful: 0,
55+
failed: 0,
56+
errors: [] as string[],
57+
successfulAssignments: [] as Array<{ id: string | number, hostname: string, environment: string | number }>
58+
};
59+
60+
for (const server of serversForFqdnOverwrite) {
61+
try {
62+
logger.info(`Overwriting fqdn for ${server.id}: "${server.host_name}"`);
63+
64+
// Perform the actual API call to update the server
65+
await serverOps.updateServer(server.id, { fqdn: server.host_name });
66+
67+
logger.info(`✅ ${server.id}: fqdn overwritten with "${server.host_name}"`);
68+
assignmentResults.successful++;
69+
assignmentResults.successfulAssignments.push({
70+
id: server.id,
71+
hostname: server.host_name,
72+
environment: server.environment_id || 'Unknown'
73+
});
74+
75+
} catch (error) {
76+
const errorMessage = `Failed to update ${server.id}: ${error instanceof Error ? error.message : 'Unknown error'}`;
77+
logger.error(errorMessage);
78+
assignmentResults.failed++;
79+
assignmentResults.errors.push(errorMessage);
80+
}
81+
}
82+
83+
logger.info('\n--- Overwrite Results ---');
84+
logger.info(`Total servers processed: ${serversForFqdnOverwrite.length}`);
85+
logger.info(`Successful overwrites: ${assignmentResults.successful}`);
86+
logger.info(`Failed overwrites: ${assignmentResults.failed}`);
87+
88+
// Report of successful overwrites
89+
if (assignmentResults.successfulAssignments.length > 0) {
90+
logger.info('\n--- Successfully Overwritten FQDN Values ---');
91+
logger.info('┌─────────────┬─────────────────────────────────┬─────────────────┐');
92+
logger.info('│ Server ID │ Hostname → FQDN │ Environment │');
93+
logger.info('├─────────────┼─────────────────────────────────┼─────────────────┤');
94+
95+
assignmentResults.successfulAssignments.forEach(assignment => {
96+
const serverId = assignment.id.toString().padEnd(11);
97+
const hostnameAssignment = assignment.hostname.padEnd(31);
98+
const environment = assignment.environment.toString().padEnd(15);
99+
logger.info(`│ ${serverId}${hostnameAssignment}${environment} │`);
100+
});
101+
102+
logger.info('└─────────────┴─────────────────────────────────┴─────────────────┘');
103+
}
104+
105+
if (assignmentResults.errors.length > 0) {
106+
logger.info('\nErrors encountered:');
107+
assignmentResults.errors.forEach((error, index) => {
108+
logger.error(`${index + 1}. ${error}`);
109+
});
110+
}
111+
} else {
112+
logger.info('No servers found with host_name values to overwrite');
113+
}
114+
115+
// 4. Summary
116+
logger.info('\n--- Summary ---');
117+
logger.info(`Total servers: ${servers.length}`);
118+
logger.info(`Servers processed for overwrite: ${serversForFqdnOverwrite.length}`);
119+
logger.info(`Servers without host_name: ${servers.length - serversForFqdnOverwrite.length}`);
120+
121+
logger.info('\n=== Hostname to FQDN Overwrite Completed Successfully ===');
122+
123+
} catch (error) {
124+
logger.error('Demo failed:', error);
125+
throw error;
126+
}
127+
}
128+
129+
// Run the demo if this file is executed directly
130+
if (require.main === module) {
131+
hostnameToFqdnDemo()
132+
.then(() => {
133+
logger.info('Demo completed successfully');
134+
process.exit(0);
135+
})
136+
.catch((error) => {
137+
logger.error('Demo failed:', error);
138+
process.exit(1);
139+
});
140+
}
141+
142+
export { hostnameToFqdnDemo };

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
"lint": "eslint src/**/*.ts",
1313
"start": "ts-node src/index.ts",
1414
"dev": "ts-node --watch src/index.ts",
15-
"demo:server-backup": "ts-node examples/server-backup-demo.ts"
15+
"demo:server-backup": "ts-node examples/server-backup-demo.ts",
16+
"demo:hostname-fqdn": "ts-node examples/hostname-to-fqdn-demo.ts"
1617
},
1718
"dependencies": {
1819
"@types/uuid": "^10.0.0",

0 commit comments

Comments
 (0)