AWS SDK DisableBaselineで を使用する - AWS SDK コードの例

Doc AWS SDK Examples GitHub リポジトリには、他にも SDK の例があります。 AWS

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

AWS SDK DisableBaselineで を使用する

次のサンプルコードは、DisableBaseline を使用する方法を説明しています。

アクション例は、より大きなプログラムからのコードの抜粋であり、コンテキスト内で実行する必要があります。次のコード例で、このアクションのコンテキストを確認できます。

.NET
SDK for .NET (v4)
注記

GitHub には、その他のリソースもあります。用例一覧を検索し、AWS コード例リポジトリでの設定と実行の方法を確認してください。

/// <summary> /// Disable a baseline for a specific target and wait for the operation to complete. /// </summary> /// <param name="enabledBaselineIdentifier">The identifier of the baseline to disable.</param> /// <returns>The operation ID or null if there was a conflict.</returns> public async Task<string?> DisableBaselineAsync(string enabledBaselineIdentifier) { try { var request = new DisableBaselineRequest { EnabledBaselineIdentifier = enabledBaselineIdentifier }; var response = await _controlTowerService.DisableBaselineAsync(request); var operationId = response.OperationIdentifier; // Wait for operation to complete while (true) { var status = await GetBaselineOperationAsync(operationId); Console.WriteLine($"Baseline operation status: {status}"); if (status == BaselineOperationStatus.SUCCEEDED || status == BaselineOperationStatus.FAILED) { break; } await Task.Delay(30000); // Wait 30 seconds } return operationId; } catch (ConflictException ex) { Console.WriteLine($"Conflict disabling baseline: {ex.Message}. Skipping disable step."); return null; } catch (AmazonControlTowerException ex) { Console.WriteLine($"Couldn't disable baseline. Here's why: {ex.ErrorCode}: {ex.Message}"); throw; } }
  • API の詳細については、「AWS SDK for .NET API リファレンス」の「DisableBaseline」を参照してください。

Java
SDK for Java 2.x
注記

GitHub には、その他のリソースもあります。用例一覧を検索し、AWS コード例リポジトリでの設定と実行の方法を確認してください。

/** * Disables a baseline for a specified target. * * @param enabledBaselineIdentifier the identifier of the enabled baseline to disable * @return the operation identifier * @throws ControlTowerException if a service-specific error occurs * @throws SdkException if an SDK error occurs */ public CompletableFuture<String> disableBaselineAsync(String enabledBaselineIdentifier) { System.out.println("Starting disable of enabled baseline…"); System.out.println("This operation will check the status every 15 seconds until it completes (SUCCEEDED or FAILED)."); DisableBaselineRequest request = DisableBaselineRequest.builder() .enabledBaselineIdentifier(enabledBaselineIdentifier) .build(); return getAsyncClient().disableBaseline(request) .thenCompose(response -> { String operationId = response.operationIdentifier(); System.out.println("Disable baseline operation ID: " + operationId); // CompletableFuture that will be completed when operation finishes CompletableFuture<String> resultFuture = new CompletableFuture<>(); // Polling loop Runnable poller = new Runnable() { @Override public void run() { getBaselineOperationAsync(operationId) .thenAccept(statusObj -> { String status = statusObj.toString(); // Convert enum/status to string for printing System.out.println("Current disable operation status: " + status + " → waiting for SUCCEEDED or FAILED..."); if ("SUCCEEDED".equalsIgnoreCase(status) || "FAILED".equalsIgnoreCase(status)) { System.out.println("Disable operation finished with status: " + status); resultFuture.complete(operationId); } else { // Schedule next poll in 15 seconds CompletableFuture.delayedExecutor(15, TimeUnit.SECONDS) .execute(this); } }) .exceptionally(ex -> { System.out.println("Error checking baseline operation status: " + ex.getMessage()); resultFuture.completeExceptionally(ex); return null; }); } }; // Start first poll immediately poller.run(); return resultFuture; }) .exceptionally(ex -> { Throwable cause = ex.getCause() != null ? ex.getCause() : ex; if (cause instanceof ControlTowerException e) { String errorCode = e.awsErrorDetails() != null ? e.awsErrorDetails().errorCode() : "UNKNOWN"; String errorMessage = e.awsErrorDetails() != null ? e.awsErrorDetails().errorMessage() : e.getMessage(); System.out.println("ControlTowerException caught while disabling baseline: Code=" + errorCode + ", Message=" + errorMessage); return null; } if (cause instanceof SdkException sdkEx) { System.out.println("SDK exception caught while disabling baseline: " + sdkEx.getMessage()); return null; } System.out.println("Unexpected exception while disabling baseline: " + cause.getMessage()); return null; }); }
  • API の詳細については、「AWS SDK for Java 2.x API リファレンス」の「DisableBaseline」を参照してください。

Python
SDK for Python (Boto3)
注記

GitHub には、その他のリソースもあります。用例一覧を検索し、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_baseline(self, enabled_baseline_identifier: str): """ Disables a baseline for a specific target and waits for the operation to complete. :param enabled_baseline_identifier: The identifier of the baseline to disable. :return: The operation ID. :raises ClientError: If disabling the baseline fails. """ try: response = self.controltower_client.disable_baseline( enabledBaselineIdentifier=enabled_baseline_identifier ) operation_id = response["operationIdentifier"] while True: status = self.get_baseline_operation(operation_id) print(f"Baseline operation status: {status}") if status in ["SUCCEEDED", "FAILED"]: break time.sleep(30) return response["operationIdentifier"] except ClientError as err: if err.response["Error"]["Code"] == "ConflictException": print( f"Conflict disabling baseline: {err.response['Error']['Message']}. Skipping disable step." ) return None else: logger.error( "Couldn't disable baseline. Here's why: %s: %s", err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise
  • API の詳細については、AWS SDK for Python (Boto3) API リファレンスの「DisableBaseline」を参照してください。