UpdateOpsItem 搭配 AWS SDK或 使用 CLI - AWS Systems Manager

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

UpdateOpsItem 搭配 AWS SDK或 使用 CLI

下列程式碼範例示範如何使用 UpdateOpsItem

動作範例是大型程式的程式碼摘錄,必須在內容中執行。您可以在下列程式碼範例的內容中看到此動作:

CLI
AWS CLI

若要更新 OpsItem

下列update-ops-item範例會更新 的描述、優先順序和類別 OpsItem。此外,命令會指定SNS主題, OpsItem 在編輯或變更時傳送通知。

aws ssm update-ops-item \ --ops-item-id "oi-287b5EXAMPLE" \ --description "Primary OpsItem for failover event 2020-01-01-fh398yf" \ --priority 2 \ --category "Security" \ --notifications "Arn=arn:aws:sns:us-east-2:111222333444:my-us-east-2-topic"

輸出:

This command produces no output.

如需詳細資訊,請參閱 Systems Manager 使用者指南中的使用 OpsItemsAWS

  • 如需API詳細資訊,請參閱 命令參考 UpdateOpsItem中的 。 AWS CLI

Java
SDK 適用於 Java 2.x
注意

還有更多 。 GitHub尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

/** * Resolves an AWS SSM OpsItem asynchronously. * * @param opsID The ID of the OpsItem to resolve. * <p> * This method initiates an asynchronous request to resolve an SSM OpsItem. * If an exception occurs, it handles the error appropriately. */ public void resolveOpsItem(String opsID) { UpdateOpsItemRequest opsItemRequest = UpdateOpsItemRequest.builder() .opsItemId(opsID) .status(OpsItemStatus.RESOLVED) .build(); CompletableFuture<Void> future = CompletableFuture.runAsync(() -> { getAsyncClient().updateOpsItem(opsItemRequest) .thenAccept(response -> { System.out.println("OpsItem resolved successfully."); }) .exceptionally(ex -> { throw new CompletionException(ex); }).join(); }).exceptionally(ex -> { Throwable cause = (ex instanceof CompletionException) ? ex.getCause() : ex; if (cause instanceof SsmException) { throw new RuntimeException("SSM error: " + cause.getMessage(), cause); } else { throw new RuntimeException("Unexpected error: " + cause.getMessage(), cause); } }); try { future.join(); } catch (CompletionException ex) { throw ex.getCause() instanceof RuntimeException ? (RuntimeException) ex.getCause() : ex; } }
  • 如需API詳細資訊,請參閱 參考 UpdateOpsItem中的 。 AWS SDK for Java 2.x API

JavaScript
SDK 適用於 JavaScript (v3)
注意

還有更多 。 GitHub尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

import { UpdateOpsItemCommand, SSMClient } from "@aws-sdk/client-ssm"; import { parseArgs } from "util"; /** * Update an SSM OpsItem. * @param {{ opsItemId: string, status?: OpsItemStatus }} */ export const main = async ({ opsItemId, status = undefined, // The OpsItem status. Status can be Open, In Progress, or Resolved }) => { const client = new SSMClient({}); try { await client.send( new UpdateOpsItemCommand({ OpsItemId: opsItemId, Status: status, }), ); console.log("Ops item updated."); return { Success: true }; } catch (caught) { if ( caught instanceof Error && caught.name === "OpsItemLimitExceededException" ) { console.warn( `Couldn't create ops item because you have exceeded your open OpsItem limit. ${caught.message}.`, ); } else { throw caught; } } };
  • 如需API詳細資訊,請參閱 參考 UpdateOpsItem中的 。 AWS SDK for JavaScript API

Python
SDK for Python (Boto3)
注意

還有更多 。 GitHub尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

class OpsItemWrapper: """Encapsulates AWS Systems Manager OpsItem actions.""" def __init__(self, ssm_client): """ :param ssm_client: A Boto3 Systems Manager client. """ self.ssm_client = ssm_client self.id = None @classmethod def from_client(cls): """ :return: A OpsItemWrapper instance. """ ssm_client = boto3.client("ssm") return cls(ssm_client) def update(self, title=None, description=None, status=None): """ Update an OpsItem. :param title: The new OpsItem title. :param description: The new OpsItem description. :param status: The new OpsItem status. :return: """ args = dict(OpsItemId=self.id) if title is not None: args["Title"] = title if description is not None: args["Description"] = description if status is not None: args["Status"] = status try: self.ssm_client.update_ops_item(**args) except ClientError as err: logger.error( "Couldn't update ops item %s. Here's why: %s: %s", self.id, err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise
  • 如需API詳細資訊,請參閱 UpdateOpsItem 中的 AWS SDK for Python (Boto3) API參考

如需開發人員指南和程式碼範例的完整清單 AWS SDK,請參閱 搭配 使用 Systems Manager AWS SDK。本主題也包含入門的相關資訊,以及先前SDK版本的詳細資訊。