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
+ * Description to Hostname Assignment Demo
8
+ *
9
+ * This example demonstrates:
10
+ * 1. Fetching all servers from the API
11
+ * 2. Assigning description field values to hostname for all servers with description
12
+ * 3. Generating a report of successful assignments
13
+ */
14
+ async function descriptionToHostnameDemo ( ) {
15
+ try {
16
+ logger . info ( '=== Description to Hostname Assignment 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 description to hostname assignment...' ) ;
35
+
36
+ const servers = await serverOps . getServers ( ) ;
37
+ logger . info ( `Retrieved ${ servers . length } servers from the API` ) ;
38
+
39
+ // 3. Find servers for hostname assignment
40
+ logger . info ( '\n--- Identifying Servers for Hostname Assignment ---' ) ;
41
+
42
+ const serversForHostnameAssignment = servers . filter ( server =>
43
+ server . description &&
44
+ server . description . trim ( ) !== ''
45
+ ) ;
46
+
47
+ logger . info ( `Found ${ serversForHostnameAssignment . length } servers with description values` ) ;
48
+ logger . info ( `These will have their hostname field assigned with description value` ) ;
49
+
50
+ if ( serversForHostnameAssignment . length > 0 ) {
51
+ logger . info ( `Assigning hostname values for ${ serversForHostnameAssignment . length } servers` ) ;
52
+
53
+ const assignmentResults = {
54
+ successful : 0 ,
55
+ failed : 0 ,
56
+ errors : [ ] as string [ ] ,
57
+ successfulAssignments : [ ] as Array < { id : string | number , description : string , environment : string | number } >
58
+ } ;
59
+
60
+ for ( const server of serversForHostnameAssignment ) {
61
+ try {
62
+ logger . info ( `Assigning hostname for ${ server . id } : "${ server . description } "` ) ;
63
+
64
+ // Perform the actual API call to update the server
65
+ await serverOps . updateServer ( server . id , { host_name : server . description } ) ;
66
+
67
+ logger . info ( `✅ ${ server . id } : hostname assigned with "${ server . description } "` ) ;
68
+ assignmentResults . successful ++ ;
69
+ assignmentResults . successfulAssignments . push ( {
70
+ id : server . id ,
71
+ description : server . description ! ,
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--- Assignment Results ---' ) ;
84
+ logger . info ( `Total servers processed: ${ serversForHostnameAssignment . length } ` ) ;
85
+ logger . info ( `Successful assignments: ${ assignmentResults . successful } ` ) ;
86
+ logger . info ( `Failed assignments: ${ assignmentResults . failed } ` ) ;
87
+
88
+ // Report of successful assignments
89
+ if ( assignmentResults . successfulAssignments . length > 0 ) {
90
+ logger . info ( '\n--- Successfully Assigned Hostname Values ---' ) ;
91
+ logger . info ( '┌─────────────┬─────────────────────────────────┬─────────────────┐' ) ;
92
+ logger . info ( '│ Server ID │ Description → Hostname │ Environment │' ) ;
93
+ logger . info ( '├─────────────┼─────────────────────────────────┼─────────────────┤' ) ;
94
+
95
+ assignmentResults . successfulAssignments . forEach ( assignment => {
96
+ const serverId = assignment . id . toString ( ) . padEnd ( 11 ) ;
97
+ const descriptionAssignment = assignment . description . padEnd ( 31 ) ;
98
+ const environment = assignment . environment . toString ( ) . padEnd ( 15 ) ;
99
+ logger . info ( `│ ${ serverId } │ ${ descriptionAssignment } │ ${ 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 description values to assign' ) ;
113
+ }
114
+
115
+ // 4. Summary
116
+ logger . info ( '\n--- Summary ---' ) ;
117
+ logger . info ( `Total servers: ${ servers . length } ` ) ;
118
+ logger . info ( `Servers processed for assignment: ${ serversForHostnameAssignment . length } ` ) ;
119
+ logger . info ( `Servers without description: ${ servers . length - serversForHostnameAssignment . length } ` ) ;
120
+
121
+ logger . info ( '\n=== Description to Hostname Assignment 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
+ descriptionToHostnameDemo ( )
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 { descriptionToHostnameDemo } ;
0 commit comments