AWS Support SDK for Kotlin を使用する例 - AWS SDK コード例

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

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

AWS Support SDK for Kotlin を使用する例

次のコード例は、 で for Kotlin AWS SDKを使用してアクションを実行し、一般的なシナリオを実装する方法を示しています AWS Support。

「基本」は、重要なオペレーションをサービス内で実行する方法を示すコード例です。

アクションはより大きなプログラムからのコードの抜粋であり、コンテキスト内で実行する必要があります。アクションは個々のサービス機能を呼び出す方法を示していますが、コンテキスト内のアクションは、関連するシナリオで確認できます。

各例には、完全なソースコードへのリンクが含まれています。ここでは、コンテキストでコードを設定および実行する方法の手順を確認できます。

開始方法

次のコード例は、 AWS Supportの使用を開始する方法を示しています。

SDK Kotlin の場合
注記

の詳細については、「」を参照してください GitHub。用例一覧を検索し、AWS コード例リポジトリでの設定と実行の方法を確認してください。

/** Before running this Kotlin code example, set up your development environment, including your credentials. For more information, see the following documentation topic: https://docs.aws.amazon.com/sdk-for-kotlin/latest/developer-guide/setup.html In addition, you must have the AWS Business Support Plan to use the AWS Support Java API. For more information, see: https://aws.amazon.com/premiumsupport/plans/ This Kotlin example performs the following task: 1. Gets and displays available services. */ suspend fun main() { displaySomeServices() } // Return a List that contains a Service name and Category name. suspend fun displaySomeServices() { val servicesRequest = DescribeServicesRequest { language = "en" } SupportClient { region = "us-west-2" }.use { supportClient -> val response = supportClient.describeServices(servicesRequest) println("Get the first 10 services") var index = 1 response.services?.forEach { service -> if (index == 11) { return@forEach } println("The Service name is: " + service.name) // Get the categories for this service. service.categories?.forEach { cat -> println("The category name is ${cat.name}") index++ } } } }
  • API 詳細については、AWS SDKKotlin APIリファレンス のDescribeServices「」の「」を参照してください。

基本

次のコードサンプルは、以下の操作方法を示しています。

  • ケースの利用可能なサービスと重要度レベルを取得して表示する方法

  • 選択したサービス、カテゴリ、重要度レベルを使用してサポートケースを作成する方法

  • 当日のオープンケースのリストを取得して表示する方法

  • 新しいケースに添付セットとコミュニケーションを追加する方法

  • ケースの新しい添付ファイルとコミュニケーションについて説明する方法

  • ケースを解決する方法

  • 当日の解決済みケースのリストを取得して表示する方法

SDK Kotlin の場合
注記

の詳細については、「」を参照してください GitHub。用例一覧を検索し、AWS コード例リポジトリでの設定と実行の方法を確認してください。

/** Before running this Kotlin code example, set up your development environment, including your credentials. For more information, see the following documentation topic: https://docs.aws.amazon.com/sdk-for-kotlin/latest/developer-guide/setup.html In addition, you must have the AWS Business Support Plan to use the AWS Support Java API. For more information, see: https://aws.amazon.com/premiumsupport/plans/ This Kotlin example performs the following tasks: 1. Gets and displays available services. 2. Gets and displays severity levels. 3. Creates a support case by using the selected service, category, and severity level. 4. Gets a list of open cases for the current day. 5. Creates an attachment set with a generated file. 6. Adds a communication with the attachment to the support case. 7. Lists the communications of the support case. 8. Describes the attachment set included with the communication. 9. Resolves the support case. 10. Gets a list of resolved cases for the current day. */ suspend fun main(args: Array<String>) { val usage = """ Usage: <fileAttachment> Where: fileAttachment - The file can be a simple saved .txt file to use as an email attachment. """ if (args.size != 1) { println(usage) exitProcess(0) } val fileAttachment = args[0] println("***** Welcome to the AWS Support case example scenario.") println("***** Step 1. Get and display available services.") val sevCatList = displayServices() println("***** Step 2. Get and display Support severity levels.") val sevLevel = displaySevLevels() println("***** Step 3. Create a support case using the selected service, category, and severity level.") val caseIdVal = createSupportCase(sevCatList, sevLevel) if (caseIdVal != null) { println("Support case $caseIdVal was successfully created!") } else { println("A support case was not successfully created!") exitProcess(1) } println("***** Step 4. Get open support cases.") getOpenCase() println("***** Step 5. Create an attachment set with a generated file to add to the case.") val attachmentSetId = addAttachment(fileAttachment) println("The Attachment Set id value is $attachmentSetId") println("***** Step 6. Add communication with the attachment to the support case.") addAttachSupportCase(caseIdVal, attachmentSetId) println("***** Step 7. List the communications of the support case.") val attachId = listCommunications(caseIdVal) println("The Attachment id value is $attachId") println("***** Step 8. Describe the attachment set included with the communication.") describeAttachment(attachId) println("***** Step 9. Resolve the support case.") resolveSupportCase(caseIdVal) println("***** Step 10. Get a list of resolved cases for the current day.") getResolvedCase() println("***** This Scenario has successfully completed") } suspend fun getResolvedCase() { // Specify the start and end time. val now = Instant.now() LocalDate.now() val yesterday = now.minus(1, ChronoUnit.DAYS) val describeCasesRequest = DescribeCasesRequest { maxResults = 30 afterTime = yesterday.toString() beforeTime = now.toString() includeResolvedCases = true } SupportClient { region = "us-west-2" }.use { supportClient -> val response = supportClient.describeCases(describeCasesRequest) response.cases?.forEach { sinCase -> println("The case status is ${sinCase.status}") println("The case Id is ${sinCase.caseId}") println("The case subject is ${sinCase.subject}") } } } suspend fun resolveSupportCase(caseIdVal: String) { val caseRequest = ResolveCaseRequest { caseId = caseIdVal } SupportClient { region = "us-west-2" }.use { supportClient -> val response = supportClient.resolveCase(caseRequest) println("The status of case $caseIdVal is ${response.finalCaseStatus}") } } suspend fun describeAttachment(attachId: String?) { val attachmentRequest = DescribeAttachmentRequest { attachmentId = attachId } SupportClient { region = "us-west-2" }.use { supportClient -> val response = supportClient.describeAttachment(attachmentRequest) println("The name of the file is ${response.attachment?.fileName}") } } suspend fun listCommunications(caseIdVal: String?): String? { val communicationsRequest = DescribeCommunicationsRequest { caseId = caseIdVal maxResults = 10 } SupportClient { region = "us-west-2" }.use { supportClient -> val response = supportClient.describeCommunications(communicationsRequest) response.communications?.forEach { comm -> println("the body is: " + comm.body) comm.attachmentSet?.forEach { detail -> return detail.attachmentId } } } return "" } suspend fun addAttachSupportCase( caseIdVal: String?, attachmentSetIdVal: String?, ) { val caseRequest = AddCommunicationToCaseRequest { caseId = caseIdVal attachmentSetId = attachmentSetIdVal communicationBody = "Please refer to attachment for details." } SupportClient { region = "us-west-2" }.use { supportClient -> val response = supportClient.addCommunicationToCase(caseRequest) if (response.result) { println("You have successfully added a communication to an AWS Support case") } else { println("There was an error adding the communication to an AWS Support case") } } } suspend fun addAttachment(fileAttachment: String): String? { val myFile = File(fileAttachment) val sourceBytes = (File(fileAttachment).readBytes()) val attachmentVal = Attachment { fileName = myFile.name data = sourceBytes } val setRequest = AddAttachmentsToSetRequest { attachments = listOf(attachmentVal) } SupportClient { region = "us-west-2" }.use { supportClient -> val response = supportClient.addAttachmentsToSet(setRequest) return response.attachmentSetId } } suspend fun getOpenCase() { // Specify the start and end time. val now = Instant.now() LocalDate.now() val yesterday = now.minus(1, ChronoUnit.DAYS) val describeCasesRequest = DescribeCasesRequest { maxResults = 20 afterTime = yesterday.toString() beforeTime = now.toString() } SupportClient { region = "us-west-2" }.use { supportClient -> val response = supportClient.describeCases(describeCasesRequest) response.cases?.forEach { sinCase -> println("The case status is ${sinCase.status}") println("The case Id is ${sinCase.caseId}") println("The case subject is ${sinCase.subject}") } } } suspend fun createSupportCase( sevCatListVal: List<String>, sevLevelVal: String, ): String? { val serCode = sevCatListVal[0] val caseCategory = sevCatListVal[1] val caseRequest = CreateCaseRequest { categoryCode = caseCategory.lowercase(Locale.getDefault()) serviceCode = serCode.lowercase(Locale.getDefault()) severityCode = sevLevelVal.lowercase(Locale.getDefault()) communicationBody = "Test issue with ${serCode.lowercase(Locale.getDefault())}" subject = "Test case, please ignore" language = "en" issueType = "technical" } SupportClient { region = "us-west-2" }.use { supportClient -> val response = supportClient.createCase(caseRequest) return response.caseId } } suspend fun displaySevLevels(): String { var levelName = "" val severityLevelsRequest = DescribeSeverityLevelsRequest { language = "en" } SupportClient { region = "us-west-2" }.use { supportClient -> val response = supportClient.describeSeverityLevels(severityLevelsRequest) response.severityLevels?.forEach { sevLevel -> println("The severity level name is: ${sevLevel.name}") if (sevLevel.name == "High") { levelName = sevLevel.name!! } } return levelName } } // Return a List that contains a Service name and Category name. suspend fun displayServices(): List<String> { var serviceCode = "" var catName = "" val sevCatList = mutableListOf<String>() val servicesRequest = DescribeServicesRequest { language = "en" } SupportClient { region = "us-west-2" }.use { supportClient -> val response = supportClient.describeServices(servicesRequest) println("Get the first 10 services") var index = 1 response.services?.forEach { service -> if (index == 11) { return@forEach } println("The Service name is ${service.name}") if (service.name == "Account") { serviceCode = service.code.toString() } // Get the categories for this service. service.categories?.forEach { cat -> println("The category name is ${cat.name}") if (cat.name == "Security") { catName = cat.name!! } } index++ } } // Push the two values to the list. serviceCode.let { sevCatList.add(it) } catName.let { sevCatList.add(it) } return sevCatList }

アクション

次のコード例は、AddAttachmentsToSet を使用する方法を示しています。

SDK Kotlin の場合
注記

の詳細については、「」を参照してください GitHub。用例一覧を検索し、AWS コード例リポジトリでの設定と実行の方法を確認してください。

suspend fun addAttachment(fileAttachment: String): String? { val myFile = File(fileAttachment) val sourceBytes = (File(fileAttachment).readBytes()) val attachmentVal = Attachment { fileName = myFile.name data = sourceBytes } val setRequest = AddAttachmentsToSetRequest { attachments = listOf(attachmentVal) } SupportClient { region = "us-west-2" }.use { supportClient -> val response = supportClient.addAttachmentsToSet(setRequest) return response.attachmentSetId } }
  • API 詳細については、Kotlin リファレンス のAddAttachmentsToSet「」の「」を参照してください。 AWS SDK API

次のコード例は、AddCommunicationToCase を使用する方法を示しています。

SDK Kotlin の場合
注記

の詳細については、「」を参照してください GitHub。用例一覧を検索し、AWS コード例リポジトリでの設定と実行の方法を確認してください。

suspend fun addAttachSupportCase( caseIdVal: String?, attachmentSetIdVal: String?, ) { val caseRequest = AddCommunicationToCaseRequest { caseId = caseIdVal attachmentSetId = attachmentSetIdVal communicationBody = "Please refer to attachment for details." } SupportClient { region = "us-west-2" }.use { supportClient -> val response = supportClient.addCommunicationToCase(caseRequest) if (response.result) { println("You have successfully added a communication to an AWS Support case") } else { println("There was an error adding the communication to an AWS Support case") } } }
  • API 詳細については、Kotlin リファレンス のAddCommunicationToCase「」の「」を参照してください。 AWS SDK API

次のコード例は、CreateCase を使用する方法を示しています。

SDK Kotlin の場合
注記

の詳細については、「」を参照してください GitHub。用例一覧を検索し、AWS コード例リポジトリでの設定と実行の方法を確認してください。

suspend fun createSupportCase( sevCatListVal: List<String>, sevLevelVal: String, ): String? { val serCode = sevCatListVal[0] val caseCategory = sevCatListVal[1] val caseRequest = CreateCaseRequest { categoryCode = caseCategory.lowercase(Locale.getDefault()) serviceCode = serCode.lowercase(Locale.getDefault()) severityCode = sevLevelVal.lowercase(Locale.getDefault()) communicationBody = "Test issue with ${serCode.lowercase(Locale.getDefault())}" subject = "Test case, please ignore" language = "en" issueType = "technical" } SupportClient { region = "us-west-2" }.use { supportClient -> val response = supportClient.createCase(caseRequest) return response.caseId } }
  • API 詳細については、Kotlin リファレンス のCreateCase「」の「」を参照してください。 AWS SDK API

次の例は、DescribeAttachment を使用する方法を説明しています。

SDK Kotlin の場合
注記

の詳細については、「」を参照してください GitHub。用例一覧を検索し、AWS コード例リポジトリでの設定と実行の方法を確認してください。

suspend fun describeAttachment(attachId: String?) { val attachmentRequest = DescribeAttachmentRequest { attachmentId = attachId } SupportClient { region = "us-west-2" }.use { supportClient -> val response = supportClient.describeAttachment(attachmentRequest) println("The name of the file is ${response.attachment?.fileName}") } }
  • API 詳細については、Kotlin リファレンス のDescribeAttachment「」の「」を参照してください。 AWS SDK API

次の例は、DescribeCases を使用する方法を説明しています。

SDK Kotlin の場合
注記

の詳細については、「」を参照してください GitHub。用例一覧を検索し、AWS コード例リポジトリでの設定と実行の方法を確認してください。

suspend fun getOpenCase() { // Specify the start and end time. val now = Instant.now() LocalDate.now() val yesterday = now.minus(1, ChronoUnit.DAYS) val describeCasesRequest = DescribeCasesRequest { maxResults = 20 afterTime = yesterday.toString() beforeTime = now.toString() } SupportClient { region = "us-west-2" }.use { supportClient -> val response = supportClient.describeCases(describeCasesRequest) response.cases?.forEach { sinCase -> println("The case status is ${sinCase.status}") println("The case Id is ${sinCase.caseId}") println("The case subject is ${sinCase.subject}") } } }
  • API 詳細については、Kotlin リファレンス のDescribeCases「」の「」を参照してください。 AWS SDK API

次のコード例は、DescribeCommunications を使用する方法を示しています。

SDK Kotlin の場合
注記

の詳細については、「」を参照してください GitHub。用例一覧を検索し、AWS コード例リポジトリでの設定と実行の方法を確認してください。

suspend fun listCommunications(caseIdVal: String?): String? { val communicationsRequest = DescribeCommunicationsRequest { caseId = caseIdVal maxResults = 10 } SupportClient { region = "us-west-2" }.use { supportClient -> val response = supportClient.describeCommunications(communicationsRequest) response.communications?.forEach { comm -> println("the body is: " + comm.body) comm.attachmentSet?.forEach { detail -> return detail.attachmentId } } } return "" }
  • API 詳細については、Kotlin リファレンス のDescribeCommunications「」の「」を参照してください。 AWS SDK API

次の例は、DescribeServices を使用する方法を説明しています。

SDK Kotlin の場合
注記

の詳細については、「」を参照してください GitHub。用例一覧を検索し、AWS コード例リポジトリでの設定と実行の方法を確認してください。

// Return a List that contains a Service name and Category name. suspend fun displayServices(): List<String> { var serviceCode = "" var catName = "" val sevCatList = mutableListOf<String>() val servicesRequest = DescribeServicesRequest { language = "en" } SupportClient { region = "us-west-2" }.use { supportClient -> val response = supportClient.describeServices(servicesRequest) println("Get the first 10 services") var index = 1 response.services?.forEach { service -> if (index == 11) { return@forEach } println("The Service name is ${service.name}") if (service.name == "Account") { serviceCode = service.code.toString() } // Get the categories for this service. service.categories?.forEach { cat -> println("The category name is ${cat.name}") if (cat.name == "Security") { catName = cat.name!! } } index++ } } // Push the two values to the list. serviceCode.let { sevCatList.add(it) } catName.let { sevCatList.add(it) } return sevCatList }
  • API 詳細については、Kotlin リファレンス のDescribeServices「」の「」を参照してください。 AWS SDK API

次のコード例は、DescribeSeverityLevels を使用する方法を示しています。

SDK Kotlin の場合
注記

の詳細については、「」を参照してください GitHub。用例一覧を検索し、AWS コード例リポジトリでの設定と実行の方法を確認してください。

suspend fun displaySevLevels(): String { var levelName = "" val severityLevelsRequest = DescribeSeverityLevelsRequest { language = "en" } SupportClient { region = "us-west-2" }.use { supportClient -> val response = supportClient.describeSeverityLevels(severityLevelsRequest) response.severityLevels?.forEach { sevLevel -> println("The severity level name is: ${sevLevel.name}") if (sevLevel.name == "High") { levelName = sevLevel.name!! } } return levelName } }
  • API 詳細については、Kotlin リファレンス のDescribeSeverityLevels「」の「」を参照してください。 AWS SDK API

次のコード例は、ResolveCase を使用する方法を示しています。

SDK Kotlin の場合
注記

の詳細については、「」を参照してください GitHub。用例一覧を検索し、AWS コード例リポジトリでの設定と実行の方法を確認してください。

suspend fun resolveSupportCase(caseIdVal: String) { val caseRequest = ResolveCaseRequest { caseId = caseIdVal } SupportClient { region = "us-west-2" }.use { supportClient -> val response = supportClient.resolveCase(caseRequest) println("The status of case $caseIdVal is ${response.finalCaseStatus}") } }
  • API 詳細については、Kotlin リファレンス のResolveCase「」の「」を参照してください。 AWS SDK API