Utilizzare con un SDK DescribeDBInstancesAWS - AWS Esempi di codice SDK

Sono disponibili altri esempi AWS SDK nel repository AWS Doc SDK Examples. GitHub

Le traduzioni sono generate tramite traduzione automatica. In caso di conflitto tra il contenuto di una traduzione e la versione originale in Inglese, quest'ultima prevarrà.

Utilizzare con un SDK DescribeDBInstancesAWS

Gli esempi di codice seguenti mostrano come utilizzare DescribeDBInstances.

Java
SDK per Java 2.x
Nota

C'è altro da fare. GitHub Trova l'esempio completo e scopri di più sulla configurazione e l'esecuzione nel Repository di esempi di codice AWS.

/** * Checks the status of a Neptune instance recursively until the desired status is reached or a timeout occurs. * * @param instanceId the ID of the Neptune instance to check * @param desiredStatus the desired status of the Neptune instance * @param startTime the start time of the operation, used to calculate the elapsed time * @param future a {@link CompletableFuture} that will be completed when the desired status is reached */ private void checkStatusRecursive(String instanceId, String desiredStatus, long startTime, CompletableFuture<Void> future) { DescribeDbInstancesRequest request = DescribeDbInstancesRequest.builder() .dbInstanceIdentifier(instanceId) .build(); getAsyncClient().describeDBInstances(request) .whenComplete((response, exception) -> { if (exception != null) { Throwable cause = exception.getCause(); future.completeExceptionally( new CompletionException("Error checking Neptune instance status", cause) ); return; } List<DBInstance> instances = response.dbInstances(); if (instances.isEmpty()) { future.completeExceptionally(new RuntimeException("Instance not found: " + instanceId)); return; } String currentStatus = instances.get(0).dbInstanceStatus(); long elapsedSeconds = (System.currentTimeMillis() - startTime) / 1000; System.out.printf("\r Elapsed: %-20s Status: %-20s", formatElapsedTime((int) elapsedSeconds), currentStatus); System.out.flush(); if (desiredStatus.equalsIgnoreCase(currentStatus)) { System.out.printf("\r Neptune instance reached desired status '%s' after %s.\n", desiredStatus, formatElapsedTime((int) elapsedSeconds)); future.complete(null); } else { CompletableFuture.delayedExecutor(20, TimeUnit.SECONDS) .execute(() -> checkStatusRecursive(instanceId, desiredStatus, startTime, future)); } }); }
Python
SDK per Python (Boto3)
Nota

C'è altro su GitHub. Trova l'esempio completo e scopri di più sulla configurazione e l'esecuzione nel Repository di esempi di codice AWS.

def check_instance_status(neptune_client, instance_id: str, desired_status: str): """ Polls the status of a Neptune DB instance until it reaches desired_status. Uses pagination via describe_db_instances — even for a single instance. Raises: ClientError: If describe_db_instances fails (e.g., instance not found). RuntimeError: If timeout expires before reaching desired status. """ paginator = neptune_client.get_paginator('describe_db_instances') start_time = time.time() while True: try: pages = paginator.paginate(DBInstanceIdentifier=instance_id) instances = [] for page in pages: instances.extend(page.get('DBInstances', [])) except ClientError as err: code = err.response["Error"]["Code"] message = err.response["Error"]["Message"] if code == "DBInstanceNotFound": print(f"Instance '{instance_id}' not found. Please verify the instance ID.") else: print(f"Failed to describe DB instance. {code}: {message}") raise current_status = instances[0].get('DBInstanceStatus') if instances else None elapsed = int(time.time() - start_time) print(f"\rElapsed: {format_elapsed_time(elapsed)} Status: {current_status}", end="", flush=True) if current_status and current_status.lower() == desired_status.lower(): print(f"\nInstance '{instance_id}' reached '{desired_status}' in {format_elapsed_time(elapsed)}.") return if elapsed > TIMEOUT_SECONDS: raise RuntimeError(f"Timeout waiting for '{instance_id}' to reach '{desired_status}'") time.sleep(POLL_INTERVAL_SECONDS)
  • Per i dettagli sull'API, consulta Descrivi DBInstances in AWS SDK for Python (Boto3) API Reference.