2023-04-24 06:51:46 -05:00
# 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__ )
2023-10-18 10:35:05 -07:00
2023-04-24 06:51:46 -05:00
# snippet-start:[python.example_code.support.SupportWrapper_full]
# snippet-start:[python.example_code.support.SupportWrapper_decl]
class SupportWrapper :
""" Encapsulates Support actions. """
2023-10-18 10:35:05 -07:00
2023-04-24 06:51:46 -05:00
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.
"""
2023-10-18 10:35:05 -07:00
support_client = boto3 . client ( " support " )
2023-04-24 06:51:46 -05:00
return cls ( support_client )
2023-10-18 10:35:05 -07:00
# snippet-end:[python.example_code.support.SupportWrapper_decl]
2023-04-24 06:51:46 -05:00
# 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 :
2023-10-18 10:35:05 -07:00
response = self . support_client . describe_services ( language = language )
services = response [ " services " ]
2023-04-24 06:51:46 -05:00
except ClientError as err :
2023-10-18 10:35:05 -07:00
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 \t Please upgrade your subscription to run these "
" examples. "
)
2023-04-24 06:51:46 -05:00
else :
logger . error (
2023-10-18 10:35:05 -07:00
" Couldn ' t get Support services for language %s . Here ' s why: %s : %s " ,
language ,
err . response [ " Error " ] [ " Code " ] ,
err . response [ " Error " ] [ " Message " ] ,
)
2023-04-24 06:51:46 -05:00
raise
else :
return services
2023-10-18 10:35:05 -07:00
2023-04-24 06:51:46 -05:00
# 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 :
2023-10-18 10:35:05 -07:00
response = self . support_client . describe_severity_levels ( language = language )
severity_levels = response [ " severityLevels " ]
2023-04-24 06:51:46 -05:00
except ClientError as err :
2023-10-18 10:35:05 -07:00
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 \t Please upgrade your subscription to run these "
" examples. "
)
2023-04-24 06:51:46 -05:00
else :
logger . error (
2023-10-18 10:35:05 -07:00
" Couldn ' t get severity levels for language %s . Here ' s why: %s : %s " ,
language ,
err . response [ " Error " ] [ " Code " ] ,
err . response [ " Error " ] [ " Message " ] ,
)
2023-04-24 06:51:46 -05:00
raise
else :
return severity_levels
2023-10-18 10:35:05 -07:00
2023-04-24 06:51:46 -05:00
# 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 (
2023-10-18 10:35:05 -07:00
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 " ,
2023-04-24 06:51:46 -05:00
)
2023-10-18 10:35:05 -07:00
case_id = response [ " caseId " ]
2023-04-24 06:51:46 -05:00
except ClientError as err :
2023-10-18 10:35:05 -07:00
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 \t Please upgrade your subscription to run these "
" examples. "
)
2023-04-24 06:51:46 -05:00
else :
logger . error (
" Couldn ' t create case. Here ' s why: %s : %s " ,
2023-10-18 10:35:05 -07:00
err . response [ " Error " ] [ " Code " ] ,
err . response [ " Error " ] [ " Message " ] ,
)
2023-04-24 06:51:46 -05:00
raise
else :
return case_id
2023-10-18 10:35:05 -07:00
2023-04-24 06:51:46 -05:00
# 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 = [
{
2023-10-18 10:35:05 -07:00
" fileName " : " attachment_file.txt " ,
" data " : b " This is a sample file for attachment to a support case. " ,
2023-04-24 06:51:46 -05:00
}
2023-10-18 10:35:05 -07:00
]
)
new_set_id = response [ " attachmentSetId " ]
2023-04-24 06:51:46 -05:00
except ClientError as err :
2023-10-18 10:35:05 -07:00
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 \t Please upgrade your subscription to run these "
" examples. "
)
2023-04-24 06:51:46 -05:00
else :
logger . error (
" Couldn ' t add attachment. Here ' s why: %s : %s " ,
2023-10-18 10:35:05 -07:00
err . response [ " Error " ] [ " Code " ] ,
err . response [ " Error " ] [ " Message " ] ,
)
2023-04-24 06:51:46 -05:00
raise
else :
return new_set_id
2023-10-18 10:35:05 -07:00
2023-04-24 06:51:46 -05:00
# 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. " ,
2023-10-18 10:35:05 -07:00
attachmentSetId = attachment_set_id ,
2023-04-24 06:51:46 -05:00
)
except ClientError as err :
2023-10-18 10:35:05 -07:00
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 \t Please upgrade your subscription to run these "
" examples. "
)
2023-04-24 06:51:46 -05:00
else :
logger . error (
" Couldn ' t add communication. Here ' s why: %s : %s " ,
2023-10-18 10:35:05 -07:00
err . response [ " Error " ] [ " Code " ] ,
err . response [ " Error " ] [ " Message " ] ,
)
2023-04-24 06:51:46 -05:00
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 = [ ]
2023-10-18 10:35:05 -07:00
paginator = self . support_client . get_paginator ( " describe_communications " )
2023-04-24 06:51:46 -05:00
for page in paginator . paginate ( caseId = case_id ) :
2023-10-18 10:35:05 -07:00
communications + = page [ " communications " ]
2023-04-24 06:51:46 -05:00
except ClientError as err :
2023-10-18 10:35:05 -07:00
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 \t Please upgrade your subscription to run these "
" examples. "
)
2023-04-24 06:51:46 -05:00
else :
logger . error (
" Couldn ' t describe communications. Here ' s why: %s : %s " ,
2023-10-18 10:35:05 -07:00
err . response [ " Error " ] [ " Code " ] ,
err . response [ " Error " ] [ " Message " ] ,
)
2023-04-24 06:51:46 -05:00
raise
else :
return communications
2023-10-18 10:35:05 -07:00
2023-04-24 06:51:46 -05:00
# 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
)
2023-10-18 10:35:05 -07:00
attached_file = response [ " attachment " ] [ " fileName " ]
2023-04-24 06:51:46 -05:00
except ClientError as err :
2023-10-18 10:35:05 -07:00
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 \t Please upgrade your subscription to run these "
" examples. "
)
2023-04-24 06:51:46 -05:00
else :
logger . error (
" Couldn ' t get attachment description. Here ' s why: %s : %s " ,
2023-10-18 10:35:05 -07:00
err . response [ " Error " ] [ " Code " ] ,
err . response [ " Error " ] [ " Message " ] ,
)
2023-04-24 06:51:46 -05:00
raise
else :
return attached_file
2023-10-18 10:35:05 -07:00
2023-04-24 06:51:46 -05:00
# 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 :
2023-10-18 10:35:05 -07:00
response = self . support_client . resolve_case ( caseId = case_id )
final_status = response [ " finalCaseStatus " ]
2023-04-24 06:51:46 -05:00
except ClientError as err :
2023-10-18 10:35:05 -07:00
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 \t Please upgrade your subscription to run these "
" examples. "
)
2023-04-24 06:51:46 -05:00
else :
logger . error (
" Couldn ' t resolve case. Here ' s why: %s : %s " ,
2023-10-18 10:35:05 -07:00
err . response [ " Error " ] [ " Code " ] ,
err . response [ " Error " ] [ " Message " ] ,
)
2023-04-24 06:51:46 -05:00
raise
else :
return final_status
2023-10-18 10:35:05 -07:00
2023-04-24 06:51:46 -05:00
# 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 = [ ]
2023-10-18 10:35:05 -07:00
paginator = self . support_client . get_paginator ( " describe_cases " )
2023-04-24 06:51:46 -05:00
for page in paginator . paginate (
2023-10-18 10:35:05 -07:00
afterTime = after_time ,
beforeTime = before_time ,
includeResolvedCases = resolved ,
language = " en " ,
) :
cases + = page [ " cases " ]
2023-04-24 06:51:46 -05:00
except ClientError as err :
2023-10-18 10:35:05 -07:00
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 \t Please upgrade your subscription to run these "
" examples. "
)
2023-04-24 06:51:46 -05:00
else :
logger . error (
" Couldn ' t describe cases. Here ' s why: %s : %s " ,
2023-10-18 10:35:05 -07:00
err . response [ " Error " ] [ " Code " ] ,
err . response [ " Error " ] [ " Message " ] ,
)
2023-04-24 06:51:46 -05:00
raise
else :
if resolved :
2023-10-18 10:35:05 -07:00
cases = filter ( lambda case : case [ " status " ] == " resolved " , cases )
2023-04-24 06:51:46 -05:00
return cases
2023-10-18 10:35:05 -07:00
2023-04-24 06:51:46 -05:00
# snippet-end:[python.example_code.support.DescribeCases]
2023-10-18 10:35:05 -07:00
2023-04-24 06:51:46 -05:00
# snippet-end:[python.example_code.support.SupportWrapper_full]