Utilizzare con un SDK DisableControlAWS - 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 DisableControlAWS

Gli esempi di codice seguenti mostrano come utilizzare DisableControl.

Gli esempi di operazioni sono estratti di codice da programmi più grandi e devono essere eseguiti nel contesto. È possibile visualizzare questa operazione nel contesto nel seguente esempio di codice:

.NET
SDK per .NET (v4)
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.

/// <summary> /// Disable a control for a specified target. /// </summary> /// <param name="controlArn">The ARN of the control to disable.</param> /// <param name="targetIdentifier">The identifier of the target (e.g., OU ARN).</param> /// <returns>The operation ID.</returns> public async Task<string> DisableControlAsync(string controlArn, string targetIdentifier) { try { var request = new DisableControlRequest { ControlIdentifier = controlArn, TargetIdentifier = targetIdentifier }; var response = await _controlTowerService.DisableControlAsync(request); var operationId = response.OperationIdentifier; // Wait for operation to complete while (true) { var status = await GetControlOperationAsync(operationId); Console.WriteLine($"Control operation status: {status}"); if (status == ControlOperationStatus.SUCCEEDED || status == ControlOperationStatus.FAILED) { break; } await Task.Delay(30000); // Wait 30 seconds } return operationId; } catch (Amazon.ControlTower.Model.ResourceNotFoundException) { Console.WriteLine("Control not found."); throw; } catch (AmazonControlTowerException ex) { Console.WriteLine($"Couldn't disable control. Here's why: {ex.ErrorCode}: {ex.Message}"); throw; } }
  • Per i dettagli sull'API, consulta la DisableControlsezione AWS SDK per .NET API Reference.

Java
SDK per Java 2.x
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.

/** * Disables a control for a specified target. * * @param controlIdentifier the identifier of the control to disable * @param targetIdentifier the identifier of the target (e.g., OU ARN) * @return the operation identifier * @throws ControlTowerException if a service-specific error occurs * @throws SdkException if an SDK error occurs */ public CompletableFuture<String> disableControlAsync( String controlIdentifier, String targetIdentifier) { DisableControlRequest request = DisableControlRequest.builder() .controlIdentifier(controlIdentifier) .targetIdentifier(targetIdentifier) .build(); return getAsyncClient().disableControl(request) .thenCompose(response -> { String operationId = response.operationIdentifier(); System.out.println("Disable control operation started. Operation ID: " + operationId); CompletableFuture<String> resultFuture = new CompletableFuture<>(); Runnable poller = new Runnable() { @Override public void run() { getControlOperationAsync(operationId) .thenAccept(status -> { System.out.println("Control operation status: " + status); if (status == ControlOperationStatus.SUCCEEDED || status == ControlOperationStatus.FAILED) { resultFuture.complete(operationId); } else { // poll again after 30 seconds CompletableFuture.delayedExecutor(30, TimeUnit.SECONDS) .execute(this); } }) .exceptionally(ex -> { resultFuture.completeExceptionally(ex); return null; }); } }; // start polling immediately poller.run(); return resultFuture; }) .exceptionally(ex -> { Throwable cause = ex.getCause() != null ? ex.getCause() : ex; if (cause instanceof ControlTowerException e) { String errorCode = e.awsErrorDetails().errorCode(); if ("ResourceNotFoundException".equals(errorCode)) { // SPEC: notify user and continue System.out.println("Control not found for disabling: " + e.getMessage()); return null; } throw new CompletionException( "Error disabling control: " + e.getMessage(), e); } if (cause instanceof SdkException) { throw new CompletionException( "SDK error disabling control: " + cause.getMessage(), cause); } throw new CompletionException( "Failed to disable control", cause); }); }
  • Per i dettagli sull'API, consulta la DisableControlsezione AWS SDK for Java 2.x API Reference.

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.

class ControlTowerWrapper: """Encapsulates AWS Control Tower and Control Catalog functionality.""" def __init__( self, controltower_client: boto3.client, controlcatalog_client: boto3.client ): """ :param controltower_client: A Boto3 Amazon ControlTower client. :param controlcatalog_client: A Boto3 Amazon ControlCatalog client. """ self.controltower_client = controltower_client self.controlcatalog_client = controlcatalog_client @classmethod def from_client(cls): controltower_client = boto3.client("controltower") controlcatalog_client = boto3.client("controlcatalog") return cls(controltower_client, controlcatalog_client) def disable_control(self, control_arn: str, target_identifier: str): """ Disables a control for a specified target. :param control_arn: The ARN of the control to disable. :param target_identifier: The identifier of the target (e.g., OU ARN). :return: The operation ID. :raises ClientError: If disabling the control fails. """ try: response = self.controltower_client.disable_control( controlIdentifier=control_arn, targetIdentifier=target_identifier ) operation_id = response["operationIdentifier"] while True: status = self.get_control_operation(operation_id) print(f"Control operation status: {status}") if status in ["SUCCEEDED", "FAILED"]: break time.sleep(30) return operation_id except ClientError as err: if err.response["Error"]["Code"] == "ResourceNotFoundException": logger.error("Control not found.") else: logger.error( "Couldn't disable control. Here's why: %s: %s", err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise
  • Per i dettagli sull'API, consulta DisableControl AWSSDK for Python (Boto3) API Reference.