58 lines
2.0 KiB
Plaintext
58 lines
2.0 KiB
Plaintext
// This script demonstrates how to list available images using various filters.
|
|
// It showcases the `ListImagesParamsBuilder` for customizing the query.
|
|
|
|
// Initialize the Hetzner client with your API token.
|
|
let client = new_hetzner_client(get_env("HETZNER_API_TOKEN"));
|
|
|
|
// Create a new builder for listing images.
|
|
// You can use this builder to filter and sort the results.
|
|
let params_builder = new_list_images_params_builder();
|
|
|
|
// The `ListImagesParamsBuilder` provides several optional methods:
|
|
//
|
|
// .with_status(status: string):
|
|
// Filters images by status. Can be "available" or "creating".
|
|
//
|
|
// .with_type(type: string):
|
|
// Filters images by type. Can be "system", "snapshot", or "backup".
|
|
//
|
|
// .with_sort(sort: string):
|
|
// Sorts the results. Can be "id", "id:asc", "id:desc", "name", "name:asc",
|
|
// "name:desc", "created", "created:asc", "created:desc".
|
|
//
|
|
// .with_bound_to(server_id: string):
|
|
// Filters images bound to a specific server ID.
|
|
//
|
|
// .with_include_deprecated(include: bool):
|
|
// Determines whether to include deprecated images. Defaults to `false`.
|
|
//
|
|
// .with_name(name: string):
|
|
// Filters images by name.
|
|
//
|
|
// .with_label_selector(selector: string):
|
|
// Filters by label selector.
|
|
//
|
|
// .with_architecture(architecture: string):
|
|
// Filters by architecture. Can be "x86" or "arm".
|
|
|
|
// Example: List all available snapshots, sorted by creation date.
|
|
let params = params_builder
|
|
.with_type("snapshot")
|
|
.with_status("available")
|
|
.with_sort("created:desc");
|
|
|
|
// List the images using the configured parameters.
|
|
print("Listing all available snapshots...");
|
|
let images = client.list_images(params);
|
|
|
|
// Display the results in a table.
|
|
print(images.show_table());
|
|
|
|
// Example 2: Find a specific system image by name.
|
|
let params2 = new_list_images_params_builder()
|
|
.with_type("system")
|
|
.with_name("ubuntu-24.04");
|
|
|
|
print("\nFinding Ubuntu 24.04 image...");
|
|
let ubuntu_image = client.list_images(params2);
|
|
print(ubuntu_image.show_table()); |