SIGN IN SIGN UP

Welcome to the AWS Code Examples Repository. This repo contains code examples used in the AWS documentation, AWS SDK Developer Guides, and more. For more information, see the Readme.md file below.

0 0 0 Java
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
"""
Purpose
Shows how to use the AWS SDK for Python (Boto3) with AWS Support to
create and manage support cases.
"""
import logging
import boto3
from botocore.exceptions import ClientError
logger = logging.getLogger(__name__)
# snippet-start:[python.example_code.support.SupportWrapper_full]
# snippet-start:[python.example_code.support.SupportWrapper_decl]
class SupportWrapper:
"""Encapsulates Support actions."""
def __init__(self, support_client):
"""
:param support_client: A Boto3 Support client.
"""
self.support_client = support_client
@classmethod
def from_client(cls):
"""
Instantiates this class from a Boto3 client.
"""
support_client = boto3.client("support")
return cls(support_client)
# snippet-end:[python.example_code.support.SupportWrapper_decl]
# snippet-start:[python.example_code.support.DescribeServices]
def describe_services(self, language):
"""
Get the descriptions of AWS services available for support for a language.
:param language: The language for support services.
Currently, only "en" (English) and "ja" (Japanese) are supported.
:return: The list of AWS service descriptions.
"""
try:
response = self.support_client.describe_services(language=language)
services = response["services"]
except ClientError as err:
if err.response["Error"]["Code"] == "SubscriptionRequiredException":
logger.info(
"You must have a Business, Enterprise On-Ramp, or Enterprise Support "
"plan to use the AWS Support API. \n\tPlease upgrade your subscription to run these "
"examples."
)
else:
logger.error(
"Couldn't get Support services for language %s. Here's why: %s: %s",
language,
err.response["Error"]["Code"],
err.response["Error"]["Message"],
)
raise
else:
return services
# snippet-end:[python.example_code.support.DescribeServices]
# snippet-start:[python.example_code.support.DescribeSeverityLevels]
def describe_severity_levels(self, language):
"""
Get the descriptions of available severity levels for support cases for a language.
:param language: The language for support severity levels.
Currently, only "en" (English) and "ja" (Japanese) are supported.
:return: The list of severity levels.
"""
try:
response = self.support_client.describe_severity_levels(language=language)
severity_levels = response["severityLevels"]
except ClientError as err:
if err.response["Error"]["Code"] == "SubscriptionRequiredException":
logger.info(
"You must have a Business, Enterprise On-Ramp, or Enterprise Support "
"plan to use the AWS Support API. \n\tPlease upgrade your subscription to run these "
"examples."
)
else:
logger.error(
"Couldn't get severity levels for language %s. Here's why: %s: %s",
language,
err.response["Error"]["Code"],
err.response["Error"]["Message"],
)
raise
else:
return severity_levels
# snippet-end:[python.example_code.support.DescribeSeverityLevels]
# snippet-start:[python.example_code.support.CreateCase]
def create_case(self, service, category, severity):
"""
Create a new support case.
:param service: The service to use for the new case.
:param category: The category to use for the new case.
:param severity: The severity to use for the new case.
:return: The caseId of the new case.
"""
try:
response = self.support_client.create_case(
subject="Example case for testing, ignore.",
serviceCode=service["code"],
severityCode=severity["code"],
categoryCode=category["code"],
communicationBody="Example support case body.",
language="en",
issueType="customer-service",
)
case_id = response["caseId"]
except ClientError as err:
if err.response["Error"]["Code"] == "SubscriptionRequiredException":
logger.info(
"You must have a Business, Enterprise On-Ramp, or Enterprise Support "
"plan to use the AWS Support API. \n\tPlease upgrade your subscription to run these "
"examples."
)
else:
logger.error(
"Couldn't create case. Here's why: %s: %s",
err.response["Error"]["Code"],
err.response["Error"]["Message"],
)
raise
else:
return case_id
# snippet-end:[python.example_code.support.CreateCase]
# snippet-start:[python.example_code.support.AddAttachmentToSet]
def add_attachment_to_set(self):
"""
Add an attachment to a set, or create a new attachment set if one does not exist.
:return: The attachment set ID.
"""
try:
response = self.support_client.add_attachments_to_set(
attachments=[
{
"fileName": "attachment_file.txt",
"data": b"This is a sample file for attachment to a support case.",
}
]
)
new_set_id = response["attachmentSetId"]
except ClientError as err:
if err.response["Error"]["Code"] == "SubscriptionRequiredException":
logger.info(
"You must have a Business, Enterprise On-Ramp, or Enterprise Support "
"plan to use the AWS Support API. \n\tPlease upgrade your subscription to run these "
"examples."
)
else:
logger.error(
"Couldn't add attachment. Here's why: %s: %s",
err.response["Error"]["Code"],
err.response["Error"]["Message"],
)
raise
else:
return new_set_id
# snippet-end:[python.example_code.support.AddAttachmentToSet]
# snippet-start:[python.example_code.support.AddCommunicationToCase]
def add_communication_to_case(self, attachment_set_id, case_id):
"""
Add a communication and an attachment set to a case.
:param attachment_set_id: The ID of an existing attachment set.
:param case_id: The ID of the case.
"""
try:
self.support_client.add_communication_to_case(
caseId=case_id,
communicationBody="This is an example communication added to a support case.",
attachmentSetId=attachment_set_id,
)
except ClientError as err:
if err.response["Error"]["Code"] == "SubscriptionRequiredException":
logger.info(
"You must have a Business, Enterprise On-Ramp, or Enterprise Support "
"plan to use the AWS Support API. \n\tPlease upgrade your subscription to run these "
"examples."
)
else:
logger.error(
"Couldn't add communication. Here's why: %s: %s",
err.response["Error"]["Code"],
err.response["Error"]["Message"],
)
raise
# snippet-end:[python.example_code.support.AddCommunicationToCase]
# snippet-start:[python.example_code.support.DescribeCommunications]
def describe_all_case_communications(self, case_id):
"""
Describe all the communications for a case using a paginator.
:param case_id: The ID of the case.
:return: The communications for the case.
"""
try:
communications = []
paginator = self.support_client.get_paginator("describe_communications")
for page in paginator.paginate(caseId=case_id):
communications += page["communications"]
except ClientError as err:
if err.response["Error"]["Code"] == "SubscriptionRequiredException":
logger.info(
"You must have a Business, Enterprise On-Ramp, or Enterprise Support "
"plan to use the AWS Support API. \n\tPlease upgrade your subscription to run these "
"examples."
)
else:
logger.error(
"Couldn't describe communications. Here's why: %s: %s",
err.response["Error"]["Code"],
err.response["Error"]["Message"],
)
raise
else:
return communications
# snippet-end:[python.example_code.support.DescribeCommunications]
# snippet-start:[python.example_code.support.DescribeAttachment]
def describe_attachment(self, attachment_id):
"""
Get information about an attachment by its attachmentID.
:param attachment_id: The ID of the attachment.
:return: The name of the attached file.
"""
try:
response = self.support_client.describe_attachment(
attachmentId=attachment_id
)
attached_file = response["attachment"]["fileName"]
except ClientError as err:
if err.response["Error"]["Code"] == "SubscriptionRequiredException":
logger.info(
"You must have a Business, Enterprise On-Ramp, or Enterprise Support "
"plan to use the AWS Support API. \n\tPlease upgrade your subscription to run these "
"examples."
)
else:
logger.error(
"Couldn't get attachment description. Here's why: %s: %s",
err.response["Error"]["Code"],
err.response["Error"]["Message"],
)
raise
else:
return attached_file
# snippet-end:[python.example_code.support.DescribeAttachment]
# snippet-start:[python.example_code.support.ResolveCase]
def resolve_case(self, case_id):
"""
Resolve a support case by its caseId.
:param case_id: The ID of the case to resolve.
:return: The final status of the case.
"""
try:
response = self.support_client.resolve_case(caseId=case_id)
final_status = response["finalCaseStatus"]
except ClientError as err:
if err.response["Error"]["Code"] == "SubscriptionRequiredException":
logger.info(
"You must have a Business, Enterprise On-Ramp, or Enterprise Support "
"plan to use the AWS Support API. \n\tPlease upgrade your subscription to run these "
"examples."
)
else:
logger.error(
"Couldn't resolve case. Here's why: %s: %s",
err.response["Error"]["Code"],
err.response["Error"]["Message"],
)
raise
else:
return final_status
# snippet-end:[python.example_code.support.ResolveCase]
# snippet-start:[python.example_code.support.DescribeCases]
def describe_cases(self, after_time, before_time, resolved):
"""
Describe support cases over a period of time, optionally filtering
by status.
:param after_time: The start time to include for cases.
:param before_time: The end time to include for cases.
:param resolved: True to include resolved cases in the results,
otherwise results are open cases.
:return: The final status of the case.
"""
try:
cases = []
paginator = self.support_client.get_paginator("describe_cases")
for page in paginator.paginate(
afterTime=after_time,
beforeTime=before_time,
includeResolvedCases=resolved,
language="en",
):
cases += page["cases"]
except ClientError as err:
if err.response["Error"]["Code"] == "SubscriptionRequiredException":
logger.info(
"You must have a Business, Enterprise On-Ramp, or Enterprise Support "
"plan to use the AWS Support API. \n\tPlease upgrade your subscription to run these "
"examples."
)
else:
logger.error(
"Couldn't describe cases. Here's why: %s: %s",
err.response["Error"]["Code"],
err.response["Error"]["Message"],
)
raise
else:
if resolved:
cases = filter(lambda case: case["status"] == "resolved", cases)
return cases
# snippet-end:[python.example_code.support.DescribeCases]
# snippet-end:[python.example_code.support.SupportWrapper_full]