From 1e95a6a9f1f75d24f8f5f26a542f44317c4288c1 Mon Sep 17 00:00:00 2001 From: Maxime Van Hees Date: Thu, 24 Jul 2025 13:01:05 +0200 Subject: [PATCH] cleaned up example scripts --- examples/boot_management.rhai | 88 ++++++++++--- examples/server_management.rhai | 114 +++++++++++++--- examples/server_ordering.rhai | 214 ++++++++++++++++++++++++------- examples/ssh_key_management.rhai | 96 +++++++++++--- src/scripting/printing/mod.rs | 1 - 5 files changed, 412 insertions(+), 101 deletions(-) diff --git a/examples/boot_management.rhai b/examples/boot_management.rhai index 2d06531..25db4a8 100644 --- a/examples/boot_management.rhai +++ b/examples/boot_management.rhai @@ -1,21 +1,73 @@ -// Get the boot configuration for a server -// Replace 1825193 with the server number you want to fetch -let boot_config = hetzner.get_boot_configuration(1825193); -print(boot_config); +// --- Input Variables --- +// These variables should be updated by the user before running the script. -// Get the rescue boot configuration for a server -// Replace 1825193 with the server number you want to fetch -let rescue_config = hetzner.get_rescue_boot_configuration(1825193); -print(rescue_config); +// Server number for boot management operations. +// Example: 1825193 +let SERVER_NUMBER = 0; // Replace with an actual server number -// Enable rescue mode -// Replace 1825193 with the server number you want to enable rescue mode on -// Replace "linux" with the desired OS -// Replace the fingerprint with your SSH key fingerprint -//let enabled_rescue = hetzner.enable_rescue_mode(1825193, "linux", ["13:dc:a2:1e:a9:d2:1d:a9:39:f4:44:c5:f1:00:ec:c7"]); -//print(enabled_rescue); +// Operating System for enabling rescue mode. +// Example: "linux", "linux64", "freebsd64" +let RESCUE_OS = "linux"; -// Disable rescue mode -// Replace 1825193 with the server number you want to disable rescue mode on -//let disabled_rescue = hetzner.disable_rescue_mode(1825193); -//print(disabled_rescue); \ No newline at end of file +// SSH Key Fingerprint to be used for rescue mode. +// Example: "13:dc:a2:1e:a9:d2:1d:a9:39:f4:44:c5:f1:00:ec:c7" +let SSH_KEY_FINGERPRINT = "YOUR_SSH_KEY_FINGERPRINT_HERE"; + + +// --- Boot Management Operations --- + +// 1. Get the boot configuration for a server +// This section retrieves the current boot configuration for a specified server. +// Uncomment the following lines and set SERVER_NUMBER to an actual server number to use. +/* +print("Fetching boot configuration for server number: " + SERVER_NUMBER); +try { + let boot_config = hetzner.get_boot_configuration(SERVER_NUMBER); + print("Boot configuration details:"); + print(boot_config); +} catch (err) { + print("Error fetching boot configuration: " + err); +} +*/ + +// 2. Get the rescue boot configuration for a server +// This section retrieves the rescue boot configuration for a specified server. +// Uncomment the following lines and set SERVER_NUMBER to an actual server number to use. +/* +print("\nFetching rescue boot configuration for server number: " + SERVER_NUMBER); +try { + let rescue_config = hetzner.get_rescue_boot_configuration(SERVER_NUMBER); + print("Rescue boot configuration details:"); + print(rescue_config); +} catch (err) { + print("Error fetching rescue boot configuration: " + err); +} +*/ + +// 3. Enable rescue mode +// This section enables rescue mode for a specified server with a chosen OS and SSH key. +// Uncomment the following lines and set SERVER_NUMBER, RESCUE_OS, and SSH_KEY_FINGERPRINT to actual values to use. +/* +print("\nAttempting to enable rescue mode for server number: " + SERVER_NUMBER + " with OS: " + RESCUE_OS); +try { + let enabled_rescue = hetzner.enable_rescue_mode(SERVER_NUMBER, RESCUE_OS, [SSH_KEY_FINGERPRINT]); + print("Rescue mode enabled successfully:"); + print(enabled_rescue); +} catch (err) { + print("Error enabling rescue mode: " + err); +} +*/ + +// 4. Disable rescue mode +// This section disables rescue mode for a specified server. +// Uncomment the following lines and set SERVER_NUMBER to an actual server number to use. +/* +print("\nAttempting to disable rescue mode for server number: " + SERVER_NUMBER); +try { + let disabled_rescue = hetzner.disable_rescue_mode(SERVER_NUMBER); + print("Rescue mode disabled successfully:"); + print(disabled_rescue); +} catch (err) { + print("Error disabling rescue mode: " + err); +} +*/ \ No newline at end of file diff --git a/examples/server_management.rhai b/examples/server_management.rhai index 6c93e82..fa5b352 100644 --- a/examples/server_management.rhai +++ b/examples/server_management.rhai @@ -1,21 +1,101 @@ -// Get all servers and print them in a table -// let servers = hetzner.get_servers(); -// servers.pretty_print(); +// --- Input Variables --- +// These variables should be updated by the user before running the script. -// // Get a specific server and print its details -// // Replace 2550253 with the server number you want to fetch -// let server = hetzner.get_server(2550253); -// print(server); +// Server number for operations like getting, updating, canceling, or withdrawing cancellation. +// Example: 1234567 +let SERVER_NUMBER = 0; // Replace with an actual server number -// Update the name of a specific server and print it -// print(hetzner.update_server_name(2550253, "kristof-123456")); +// New name for updating a server's name. +// Example: "my-new-server-name" +let NEW_SERVER_NAME = "your-new-server-name"; -// Query cancellation data for a server -let c_d = hetzner.get_cancellation_data(2550253); -print(c_d); +// Desired cancellation date for a server. +// Format: "YYYY-MM-DD" +// Example: "2025-12-31" +let CANCELLATION_DATE = "YYYY-MM-DD"; -// Cancel a server -// Replace 2550253 with the server number you want to cancel -// Replace "2014-04-15" with the desired cancellation date -let cancelled_server = hetzner.cancel_server(2550253, "2014-04-15"); -print(cancelled_server); \ No newline at end of file + +// --- Server Management Operations --- + +// 1. Get all servers +// This section retrieves all servers associated with your Hetzner account +// and prints them in a human-readable table format. +print("Fetching all servers..."); +try { + let all_servers = hetzner.get_servers(); + all_servers.pretty_print(); + print("All servers fetched and displayed."); +} catch (err) { + print("Error fetching all servers: " + err); +} + +// 2. Get a specific server by number +// This section demonstrates how to retrieve details for a single server +// using its server number. +// Uncomment the following lines and set SERVER_NUMBER to an actual server number to use. +/* +print("\nAttempting to fetch specific server with number: " + SERVER_NUMBER); +try { + let specific_server = hetzner.get_server(SERVER_NUMBER); + print("Specific server details:"); + print(specific_server); +} catch (err) { + print("Error fetching specific server: " + err); +} +*/ + +// 3. Update a server's name +// This section updates the name of an existing server. +// Uncomment the following lines and set SERVER_NUMBER and NEW_SERVER_NAME to actual values to use. +/* +print("\nAttempting to update server name for server number: " + SERVER_NUMBER + " to new name: " + NEW_SERVER_NAME); +try { + let updated_server = hetzner.update_server_name(SERVER_NUMBER, NEW_SERVER_NAME); + print("Server name updated successfully:"); + print(updated_server); +} catch (err) { + print("Error updating server name: " + err); +} +*/ + +// 4. Query cancellation data for a server +// This section retrieves the cancellation data for a specific server. +// Uncomment the following lines and set SERVER_NUMBER to an actual server number to use. +/* +print("\nAttempting to query cancellation data for server number: " + SERVER_NUMBER); +try { + let cancellation_data = hetzner.get_cancellation_data(SERVER_NUMBER); + print("Cancellation data:"); + print(cancellation_data); +} catch (err) { + print("Error querying cancellation data: " + err); +} +*/ + +// 5. Cancel a server +// This section cancels a server with a specified cancellation date. +// Use with caution! This action will initiate the cancellation process for the server. +// Uncomment the following lines and set SERVER_NUMBER and CANCELLATION_DATE to actual values to use. +/* +print("\nAttempting to cancel server number: " + SERVER_NUMBER + " with cancellation date: " + CANCELLATION_DATE); +try { + let cancelled_server = hetzner.cancel_server(SERVER_NUMBER, CANCELLATION_DATE); + print("Server cancellation initiated successfully:"); + print(cancelled_server); +} catch (err) { + print("Error canceling server: " + err); +} +*/ + +// 6. Withdraw a server cancellation +// This section withdraws a previously initiated server cancellation. +// Uncomment the following lines and set SERVER_NUMBER to an actual server number to use. +/* +print("\nAttempting to withdraw cancellation for server number: " + SERVER_NUMBER); +try { + hetzner.withdraw_cancellation(SERVER_NUMBER); + print("Server cancellation withdrawn successfully."); +} catch (err) { + print("Error withdrawing server cancellation: " + err); +} +*/ \ No newline at end of file diff --git a/examples/server_ordering.rhai b/examples/server_ordering.rhai index 73326db..d0a6d9a 100644 --- a/examples/server_ordering.rhai +++ b/examples/server_ordering.rhai @@ -1,61 +1,179 @@ -/// --- Get all available products (servers) that we can order and print them in a table -// let available_server_products = hetzner.get_server_products(); -// available_server_products.pretty_print(); +// --- Input Variables --- +// These variables should be updated by the user before running the script. -/// --- List the details from a specific sever product based on the ID -// let example_server_product = hetzner.get_server_product_by_id("AX41-NVMe"); -// print(example_server_product); +// Product ID for ordering a new server or fetching product details. +// Example: "AX41-NVMe" +let SERVER_PRODUCT_ID = "YOUR_SERVER_PRODUCT_ID_HERE"; -/// --- Order a server -// 1. Grab the SSH key to pass to the deployment -let ssh_key = hetzner.get_ssh_key("e0:73:80:26:80:46:f0:c8:bb:74:f4:d0:2d:10:2d:6f"); -// 2. Use the builder to bundle the details on what to order -let order_builder = new_server_builder("AX41-NVMe") - .with_authorized_keys([ssh_key.fingerprint]) - .with_test(true); +// SSH Key Fingerprint to be used for server deployment. +// Example: "e0:73:80:26:80:46:f0:c8:bb:74:f4:d0:2d:10:2d:6f" +let SSH_KEY_FINGERPRINT = "YOUR_SSH_KEY_FINGERPRINT_HERE"; -let ordered_server_transaction = hetzner.order_server(order_builder); -print(ordered_server_transaction); +// Transaction ID for fetching specific transaction details. +// Example: "120000706572" or "B20250723-3204053-2775263" +let TRANSACTION_ID = "YOUR_TRANSACTION_ID_HERE"; +// Auction Product ID for fetching specific auction server product details or ordering. +// Example: 2739642 +let AUCTION_PRODUCT_ID = 0; // Replace with an actual auction product ID -/// --- List all the transactions from the past 30 days -// let transactions_last_30 = hetzner.get_transactions(); -// print(transactions_last_30); +// --- Server Ordering Operations --- -/// --- Fetch a transcation by ID -// let example_transaction = hetzner.get_transaction_by_id("120000706572"); -// print(example_transaction); +// 1. Get all available server products +// This section retrieves all available server products that can be ordered +// and prints them in a human-readable table format. +print("Fetching all available server products..."); +try { + let available_server_products = hetzner.get_server_products(); + available_server_products.pretty_print(); + print("All available server products fetched and displayed."); +} catch (err) { + print("Error fetching all available server products: " + err); +} -/// --- List all the auction transaction from the past 30 days -// let auction_transactions_last_30 = hetzner.get_auction_transactions(); -// auction_transactions_last_30.pretty_print(); +// 2. Get details of a specific server product by ID +// This section demonstrates how to retrieve details for a single server product +// using its product ID. +// Uncomment the following lines and set SERVER_PRODUCT_ID to an actual product ID to use. +/* +print("\nAttempting to fetch specific server product with ID: " + SERVER_PRODUCT_ID); +try { + let example_server_product = hetzner.get_server_product_by_id(SERVER_PRODUCT_ID); + print("Specific server product details:"); + print(example_server_product); +} catch (err) { + print("Error fetching specific server product: " + err); +} +*/ -/// --- Fetch a auction transaction by ID -// let example_auction_transaction = hetzner.get_auction_transaction_by_id(""); -// print(example_auction_transaction); +// 3. Order a new server +// This section demonstrates how to order a new server. +// Ensure SERVER_PRODUCT_ID and SSH_KEY_FINGERPRINT are set correctly. +// Uncomment the following lines to order a server. +/* +print("\nAttempting to order a new server with product ID: " + SERVER_PRODUCT_ID); +try { + // First, grab the SSH key to pass to the deployment + let ssh_key = hetzner.get_ssh_key(SSH_KEY_FINGERPRINT); -/// --- List all the auctioned server products -// let auctioned_servers = hetzner.get_auction_server_products(); -// auctioned_servers.pretty_print(); + // Use the builder to bundle the details on what to order + let order_builder = new_server_builder(SERVER_PRODUCT_ID) + .with_authorized_keys([ssh_key.fingerprint]) + .with_test(true); // Set to 'false' for a real order -/// --- Get information about one specific auctioned server by ID -// let auctioned_server = hetzner.get_auction_server_product_by_id("2739642"); -// print(auctioned_server); + let ordered_server_transaction = hetzner.order_server(order_builder); + print("Server ordered successfully. Transaction details:"); + print(ordered_server_transaction); +} catch (err) { + print("Error ordering server: " + err); +} +*/ -/// --- Order an auction server -// 1. Grab the SSH key to pass to the deployment -// let ssh_key = hetzner.get_ssh_key("e0:73:80:26:80:46:f0:c8:bb:74:f4:d0:2d:10:2d:6f"); -// 2. Use the builder to bundle the details on what to order -// let order_builder = new_auction_server_builder(2741558) -// .with_authorized_keys([ssh_key.fingerprint]) -// .with_lang("en") -// .with_comment("test") -// .with_test(false); +// 4. List all transactions from the past 30 days +// This section retrieves all server order transactions from the past 30 days. +// Uncomment the following lines to list transactions. +/* +print("\nFetching all server transactions from the past 30 days..."); +try { + let transactions_last_30 = hetzner.get_transactions(); + transactions_last_30.pretty_print(); + print("All transactions fetched and displayed."); +} catch (err) { + print("Error fetching transactions: " + err); +} +*/ -// let ordered_auction_server = hetzner.order_auction_server(order_builder); -// print(ordered_auction_server); -// --> we get a transaction ID from this --> which we can use to fetch information about the transaction -// e.g. B20250723-3204053-2775263 +// 5. Fetch a specific transaction by ID +// This section demonstrates how to fetch details for a specific transaction. +// Uncomment the following lines and set TRANSACTION_ID to an actual transaction ID to use. +/* +print("\nAttempting to fetch transaction with ID: " + TRANSACTION_ID); +try { + let example_transaction = hetzner.get_transaction_by_id(TRANSACTION_ID); + print("Specific transaction details:"); + print(example_transaction); +} catch (err) { + print("Error fetching transaction by ID: " + err); +} +*/ -let transaction = hetzner.get_auction_transaction_by_id("B20250723-3204053-2775263"); -print(transaction) \ No newline at end of file +// 6. List all auction transactions from the past 30 days +// This section retrieves all auction server transactions from the past 30 days. +// Uncomment the following lines to list auction transactions. +/* +print("\nFetching all auction transactions from the past 30 days..."); +try { + let auction_transactions_last_30 = hetzner.get_auction_transactions(); + auction_transactions_last_30.pretty_print(); + print("All auction transactions fetched and displayed."); +} catch (err) { + print("Error fetching auction transactions: " + err); +} +*/ + +// 7. Fetch a specific auction transaction by ID +// This section demonstrates how to fetch details for a specific auction transaction. +// Uncomment the following lines and set TRANSACTION_ID to an actual auction transaction ID to use. +/* +print("\nAttempting to fetch auction transaction with ID: " + TRANSACTION_ID); +try { + let example_auction_transaction = hetzner.get_auction_transaction_by_id(TRANSACTION_ID); + print("Specific auction transaction details:"); + print(example_auction_transaction); +} catch (err) { + print("Error fetching auction transaction by ID: " + err); +} +*/ + +// 8. List all auctioned server products +// This section retrieves all available auctioned server products. +// Uncomment the following lines to list auctioned servers. +/* +print("\nFetching all auctioned server products..."); +try { + let auctioned_servers = hetzner.get_auction_server_products(); + auctioned_servers.pretty_print(); + print("All auctioned server products fetched and displayed."); +} catch (err) { + print("Error fetching auctioned server products: " + err); +} +*/ + +// 9. Get information about one specific auctioned server by ID +// This section demonstrates how to retrieve details for a specific auctioned server product. +// Uncomment the following lines and set AUCTION_PRODUCT_ID to an actual auction product ID to use. +/* +print("\nAttempting to fetch specific auctioned server product with ID: " + AUCTION_PRODUCT_ID); +try { + let auctioned_server = hetzner.get_auction_server_product_by_id(AUCTION_PRODUCT_ID); + print("Specific auctioned server product details:"); + print(auctioned_server); +} catch (err) { + print("Error fetching specific auctioned server product: " + err); +} +*/ + +// 10. Order an auction server +// This section demonstrates how to order an auction server. +// Ensure AUCTION_PRODUCT_ID and SSH_KEY_FINGERPRINT are set correctly. +// Uncomment the following lines to order an auction server. +/* +print("\nAttempting to order an auction server with product ID: " + AUCTION_PRODUCT_ID); +try { + // First, grab the SSH key to pass to the deployment + let ssh_key = hetzner.get_ssh_key(SSH_KEY_FINGERPRINT); + + // Use the builder to bundle the details on what to order + let order_builder = new_auction_server_builder(AUCTION_PRODUCT_ID) + .with_authorized_keys([ssh_key.fingerprint]) + .with_lang("en") // Optional: language for the order confirmation + .with_comment("test order") // Optional: comment for the order + .with_test(true); // Set to 'false' for a real order + + let ordered_auction_server = hetzner.order_auction_server(order_builder); + print("Auction server ordered successfully. Transaction details:"); + print(ordered_auction_server); +} catch (err) { + print("Error ordering auction server: " + err); +} +*/ \ No newline at end of file diff --git a/examples/ssh_key_management.rhai b/examples/ssh_key_management.rhai index a1bad1c..29caec4 100644 --- a/examples/ssh_key_management.rhai +++ b/examples/ssh_key_management.rhai @@ -1,21 +1,83 @@ -// Get all SSH keys and print them in a table -let keys = hetzner.get_ssh_keys(); -keys.pretty_print(); +// Fingerprint of an existing SSH key for fetching, updating, or deleting. +// Example: "13:dc:a2:1e:a9:d2:1d:a9:39:f4:44:c5:f1:00:ec:c7" +let SSH_KEY_FINGERPRINT = "YOUR_SSH_KEY_FINGERPRINT_HERE"; -// Get a specific SSH key -// Replace "13:dc:a2:1e:a9:d2:1d:a9:39:f4:44:c5:f1:00:ec:c7" with the fingerprint of the key you want to fetch -// let key = hetzner.get_ssh_key("13:dc:a2:1e:a9:d2:1d:a9:39:f4:44:c5:f1:00:ec:c7"); -// print(key); +// Name for a new SSH key or for updating an existing key. +// Example: "my-new-ssh-key" or "my-updated-key-name" +let SSH_KEY_NAME = "YOUR_SSH_KEY_NAME_HERE"; -// Add a new SSH key -let new_key = hetzner.add_ssh_key("vanheesm@incubaid.com", "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIFyZJCEsvRc0eitsOoq+ywC5Lmqejvk3hXMVbO0AxPrd"); -print(new_key); +// Public key content for adding a new SSH key. +// Example: "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIFyZJCEsvRc0eitsOoq+ywC5Lmqejvk3hXMVbO0AxPrd your_email@example.com" +let SSH_PUBLIC_KEY = "YOUR_SSH_PUBLIC_KEY_HERE"; -// Update an SSH key's name -// Replace "cb:8b:ef:a7:fe:04:87:3f:e5:55:cd:12:e3:e8:9f:99" with the fingerprint of the key you want to update -// let updated_key = hetzner.update_ssh_key_name("e0:73:80:26:80:46:f0:c8:bb:74:f4:d0:2d:10:2d:6f", "my-updated-key-name"); -// print(updated_key); +// --- SSH Key Management Operations --- -// Delete an SSH key -// Replace "cb:8b:ef:a7:fe:04:87:3f:e5:55:cd:12:e3:e8:9f:99" with the fingerprint of the key you want to delete -// hetzner.delete_ssh_key("e1:a7:27:ed:12:77:6a:4c:3a:cd:30:18:c4:f3:d0:88"); \ No newline at end of file +// 1. Get all SSH keys +// This section retrieves all SSH keys associated with your Hetzner account +// and prints them in a human-readable table format. +print("Fetching all SSH keys..."); +let all_keys = hetzner.get_ssh_keys(); +all_keys.pretty_print(); +print("All SSH keys fetched and displayed."); + + +// 2. Get a specific SSH key by fingerprint +// This section demonstrates how to retrieve details for a single SSH key +// using its fingerprint. +// Uncomment the following lines and set SSH_KEY_FINGERPRINT to an actual key fingerprint to use. +/* +print("\nAttempting to fetch specific SSH key with fingerprint: " + SSH_KEY_FINGERPRINT); +try { + let specific_key = hetzner.get_ssh_key(SSH_KEY_FINGERPRINT); + print("Specific SSH key details:"); + print(specific_key); +} catch (err) { + print("Error fetching specific SSH key: " + err); +} +*/ + + +// 3. Add a new SSH key +// This section adds a new SSH key to your Hetzner account. +// Ensure SSH_KEY_NAME and SSH_PUBLIC_KEY are set correctly. +// Uncomment the following lines to add a new key. +/* +print("\nAttempting to add new SSH key with name: " + SSH_KEY_NAME); +try { + let added_key = hetzner.add_ssh_key(SSH_KEY_NAME, SSH_PUBLIC_KEY); + print("New SSH key added successfully:"); + print(added_key); +} catch (err) { + print("Error adding new SSH key: " + err); +} +*/ + + +// 4. Update an SSH key's name +// This section updates the name of an existing SSH key. +// Uncomment the following lines and set SSH_KEY_FINGERPRINT and SSH_KEY_NAME to actual values to use. +/* +print("\nAttempting to update SSH key name for fingerprint: " + SSH_KEY_FINGERPRINT + " to new name: " + SSH_KEY_NAME); +try { + let updated_key = hetzner.update_ssh_key_name(SSH_KEY_FINGERPRINT, SSH_KEY_NAME); + print("SSH key name updated successfully:"); + print(updated_key); +} catch (err) { + print("Error updating SSH key name: " + err); +} +*/ + + +// 5. Delete an SSH key +// This section deletes an SSH key from your Hetzner account. +// Use with caution! Once deleted, an SSH key cannot be recovered. +// Uncomment the following lines and set SSH_KEY_FINGERPRINT to the key you wish to delete. +/* +print("\nAttempting to delete SSH key with fingerprint: " + SSH_KEY_FINGERPRINT); +try { + hetzner.delete_ssh_key(SSH_KEY_FINGERPRINT); + print("SSH key deleted successfully."); +} catch (err) { + print("Error deleting SSH key: " + err); +} +*/ \ No newline at end of file diff --git a/src/scripting/printing/mod.rs b/src/scripting/printing/mod.rs index e4ccf5b..ea7a948 100644 --- a/src/scripting/printing/mod.rs +++ b/src/scripting/printing/mod.rs @@ -7,7 +7,6 @@ mod server_ordering_table; // This will be called when we print(...) or pretty_print() an Array (with Dynamic values) pub fn pretty_print_dispatch(array: Array) { - println!("pretty print dispatch"); if array.is_empty() { println!(""); return;