loris_log.boto3Client
The boto3 client to establish connection to the AWS cloud.
1""" 2The boto3 client to establish connection to the AWS cloud. 3""" 4 5import time 6import boto3 7from .myException import InvalidRoleAccessKeyException,\ 8 InvalidRoleSecretException,\ 9 InvalidAttributeException,\ 10 InvalidRegionNameException,\ 11 EmptyParameterException 12 13class Boto3Client: 14 """Boto 3 client to communicate to the AWS Cloudwatch. 15 """ 16 17 def __init__(self, region_name, aws_key, aws_secret): 18 """ 19 Boto 3 client default constructor. 20 21 Args:\n 22 region_name (string): The AWS service region name. 23 aws_key (string): The AWS service role access key. 24 aws_secret (string): The AWS service role secret key. 25 26 Raises InvalidAttributeException: \n 27 If the region_name, aws_key, aws_secret is not of type 28 string. 29 30 Raises InvalidRegionNameException: \n 31 If the region_name is a none or empty. 32 33 Raises InvalidRoleAccessKeyException:\n 34 If the aws_key is a none or empty. 35 36 Raises InvalidRoleSecretException:\n 37 If the aws_secret is a none or empty. 38 """ 39 if isinstance(region_name, str) is False or\ 40 isinstance(aws_key, str) is False or\ 41 isinstance(aws_secret, str) is False: 42 raise InvalidAttributeException() 43 44 if region_name == "" or region_name is None: 45 raise InvalidRegionNameException() 46 47 if aws_key == "" or aws_key is None: 48 raise InvalidRoleAccessKeyException() 49 50 if aws_secret == "" or aws_secret is None: 51 raise InvalidRoleSecretException() 52 53 self.client = boto3.client("logs", 54 region_name=region_name, 55 aws_access_key_id=aws_key, 56 aws_secret_access_key=aws_secret) 57 58 def get_log_groups(self): 59 """ 60 Get all the log groups available in the AWS cloudwatch. 61 62 Returns:\n 63 list: A list of log group names. 64 """ 65 response = self.client.describe_log_groups() 66 return [logGroup['logGroupName'] for logGroup in response['logGroups']] 67 68 def get_log_stream(self, log_group_name): 69 """ 70 Get all the log stream inside a log group. 71 72 Args:\n 73 log_group_name (string): The name of the log group. 74 75 Returns:\n 76 list: All the log streams' name available in a log group. 77 """ 78 response = self.client.describe_log_streams( 79 logGroupName=log_group_name 80 ) 81 82 return [logGroup['logStreamName'] for logGroup in response['logStreams']] 83 84 def __create_log_groups(self, log_group_name): 85 """ 86 Create the log group if the not group is not present. 87 88 Args:\n 89 log_group_name (string): the log group name that wanted to be created. 90 91 Returns:\n 92 bool: True if the operation to create the log group was a success or False 93 otherwise. 94 """ 95 if log_group_name not in self.get_log_groups(): 96 response = self.client.create_log_group( 97 logGroupName=log_group_name, 98 ) 99 return response 100 return None 101 102 def create_log_group_stream(self, log_group_name, log_stream_name): 103 """ 104 Create the log stream if the current log group is present. Otherwise, 105 create a new log group then create the desire the log stream 106 107 Args:\n 108 log_group_name (string): the log group name. 109 log_stream_name (string): the log stream name. 110 111 Returns:\n 112 bool: whether the operation is a success (True) or otherwise (False). 113 114 Raises InvalidAttributeException:\n 115 If the log_group_name or log_stream_name is not of type string. 116 117 Raises EmptyParameterException:\n 118 If the log_group_name or log_stream_name is empty. 119 """ 120 if isinstance(log_group_name, str) is False or\ 121 isinstance(log_stream_name, str) is False: 122 raise InvalidAttributeException() 123 124 if log_stream_name == "" or log_group_name == "": 125 raise EmptyParameterException() 126 127 # check if the relevant log stream or log group 128 # is present 129 if log_group_name in self.get_log_groups() and \ 130 log_stream_name not in self.get_log_stream(log_group_name): 131 # if the log stream is not present 132 # create the log stream. 133 self.client.create_log_stream( 134 logGroupName=log_group_name, 135 logStreamName=log_stream_name 136 ) 137 return True 138 139 # If the log group is not present, create the log group 140 log_group_response = self.__create_log_groups(log_group_name) 141 # If the log group is successfully created 142 if log_group_response is not None: 143 # create the log stream and return the response 144 self.create_log_group_stream(log_group_name, log_stream_name) 145 return True 146 147 # Otherwise, return None to show the log stream and log group was present 148 return False 149 150 def set_log_message(self, log_group_name, log_stream_name, log_message): 151 """ 152 Allow the application to push the relevant log message to the cloudwatch. 153 154 Args:\n 155 log_group_name (string): The name of the log group. 156 log_stream_name (string): The name of the log stream. 157 log_message (list): A list of application/ service/ api message. 158 159 Returns:\n 160 response: A list of aws responses. 161 162 Raises InvalidAttributeException:\n 163 If the log_group_name, log_stream_name, or log_message 164 is not of type string. 165 166 Raises EmptyParameterException:\n 167 If the log_group_name, log_stream_name, or log_message 168 is empty. 169 """ 170 if isinstance(log_group_name, str) is False or\ 171 isinstance(log_stream_name, str) is False or\ 172 isinstance(log_message, str) is False: 173 raise InvalidAttributeException() 174 175 if log_group_name == "" or log_stream_name == "" or\ 176 log_message == "": 177 raise EmptyParameterException() 178 179 # check if the log_group name and log stream is present 180 if log_group_name in self.get_log_groups() and \ 181 log_stream_name in self.get_log_stream(log_group_name): 182 # if present only start to push the log message 183 # onto the cloudwatch. 184 self.client.put_log_events( 185 logGroupName=log_group_name, 186 logStreamName=log_stream_name, 187 logEvents=[ 188 { 189 'timestamp': int(round(time.time() * 1000)), 190 'message': time.strftime('%Y-%m-%d %H:%M:%S') + \ 191 "\t" + log_message 192 } 193 ] 194 ) 195 return True 196 return False
14class Boto3Client: 15 """Boto 3 client to communicate to the AWS Cloudwatch. 16 """ 17 18 def __init__(self, region_name, aws_key, aws_secret): 19 """ 20 Boto 3 client default constructor. 21 22 Args:\n 23 region_name (string): The AWS service region name. 24 aws_key (string): The AWS service role access key. 25 aws_secret (string): The AWS service role secret key. 26 27 Raises InvalidAttributeException: \n 28 If the region_name, aws_key, aws_secret is not of type 29 string. 30 31 Raises InvalidRegionNameException: \n 32 If the region_name is a none or empty. 33 34 Raises InvalidRoleAccessKeyException:\n 35 If the aws_key is a none or empty. 36 37 Raises InvalidRoleSecretException:\n 38 If the aws_secret is a none or empty. 39 """ 40 if isinstance(region_name, str) is False or\ 41 isinstance(aws_key, str) is False or\ 42 isinstance(aws_secret, str) is False: 43 raise InvalidAttributeException() 44 45 if region_name == "" or region_name is None: 46 raise InvalidRegionNameException() 47 48 if aws_key == "" or aws_key is None: 49 raise InvalidRoleAccessKeyException() 50 51 if aws_secret == "" or aws_secret is None: 52 raise InvalidRoleSecretException() 53 54 self.client = boto3.client("logs", 55 region_name=region_name, 56 aws_access_key_id=aws_key, 57 aws_secret_access_key=aws_secret) 58 59 def get_log_groups(self): 60 """ 61 Get all the log groups available in the AWS cloudwatch. 62 63 Returns:\n 64 list: A list of log group names. 65 """ 66 response = self.client.describe_log_groups() 67 return [logGroup['logGroupName'] for logGroup in response['logGroups']] 68 69 def get_log_stream(self, log_group_name): 70 """ 71 Get all the log stream inside a log group. 72 73 Args:\n 74 log_group_name (string): The name of the log group. 75 76 Returns:\n 77 list: All the log streams' name available in a log group. 78 """ 79 response = self.client.describe_log_streams( 80 logGroupName=log_group_name 81 ) 82 83 return [logGroup['logStreamName'] for logGroup in response['logStreams']] 84 85 def __create_log_groups(self, log_group_name): 86 """ 87 Create the log group if the not group is not present. 88 89 Args:\n 90 log_group_name (string): the log group name that wanted to be created. 91 92 Returns:\n 93 bool: True if the operation to create the log group was a success or False 94 otherwise. 95 """ 96 if log_group_name not in self.get_log_groups(): 97 response = self.client.create_log_group( 98 logGroupName=log_group_name, 99 ) 100 return response 101 return None 102 103 def create_log_group_stream(self, log_group_name, log_stream_name): 104 """ 105 Create the log stream if the current log group is present. Otherwise, 106 create a new log group then create the desire the log stream 107 108 Args:\n 109 log_group_name (string): the log group name. 110 log_stream_name (string): the log stream name. 111 112 Returns:\n 113 bool: whether the operation is a success (True) or otherwise (False). 114 115 Raises InvalidAttributeException:\n 116 If the log_group_name or log_stream_name is not of type string. 117 118 Raises EmptyParameterException:\n 119 If the log_group_name or log_stream_name is empty. 120 """ 121 if isinstance(log_group_name, str) is False or\ 122 isinstance(log_stream_name, str) is False: 123 raise InvalidAttributeException() 124 125 if log_stream_name == "" or log_group_name == "": 126 raise EmptyParameterException() 127 128 # check if the relevant log stream or log group 129 # is present 130 if log_group_name in self.get_log_groups() and \ 131 log_stream_name not in self.get_log_stream(log_group_name): 132 # if the log stream is not present 133 # create the log stream. 134 self.client.create_log_stream( 135 logGroupName=log_group_name, 136 logStreamName=log_stream_name 137 ) 138 return True 139 140 # If the log group is not present, create the log group 141 log_group_response = self.__create_log_groups(log_group_name) 142 # If the log group is successfully created 143 if log_group_response is not None: 144 # create the log stream and return the response 145 self.create_log_group_stream(log_group_name, log_stream_name) 146 return True 147 148 # Otherwise, return None to show the log stream and log group was present 149 return False 150 151 def set_log_message(self, log_group_name, log_stream_name, log_message): 152 """ 153 Allow the application to push the relevant log message to the cloudwatch. 154 155 Args:\n 156 log_group_name (string): The name of the log group. 157 log_stream_name (string): The name of the log stream. 158 log_message (list): A list of application/ service/ api message. 159 160 Returns:\n 161 response: A list of aws responses. 162 163 Raises InvalidAttributeException:\n 164 If the log_group_name, log_stream_name, or log_message 165 is not of type string. 166 167 Raises EmptyParameterException:\n 168 If the log_group_name, log_stream_name, or log_message 169 is empty. 170 """ 171 if isinstance(log_group_name, str) is False or\ 172 isinstance(log_stream_name, str) is False or\ 173 isinstance(log_message, str) is False: 174 raise InvalidAttributeException() 175 176 if log_group_name == "" or log_stream_name == "" or\ 177 log_message == "": 178 raise EmptyParameterException() 179 180 # check if the log_group name and log stream is present 181 if log_group_name in self.get_log_groups() and \ 182 log_stream_name in self.get_log_stream(log_group_name): 183 # if present only start to push the log message 184 # onto the cloudwatch. 185 self.client.put_log_events( 186 logGroupName=log_group_name, 187 logStreamName=log_stream_name, 188 logEvents=[ 189 { 190 'timestamp': int(round(time.time() * 1000)), 191 'message': time.strftime('%Y-%m-%d %H:%M:%S') + \ 192 "\t" + log_message 193 } 194 ] 195 ) 196 return True 197 return False
Boto 3 client to communicate to the AWS Cloudwatch.
18 def __init__(self, region_name, aws_key, aws_secret): 19 """ 20 Boto 3 client default constructor. 21 22 Args:\n 23 region_name (string): The AWS service region name. 24 aws_key (string): The AWS service role access key. 25 aws_secret (string): The AWS service role secret key. 26 27 Raises InvalidAttributeException: \n 28 If the region_name, aws_key, aws_secret is not of type 29 string. 30 31 Raises InvalidRegionNameException: \n 32 If the region_name is a none or empty. 33 34 Raises InvalidRoleAccessKeyException:\n 35 If the aws_key is a none or empty. 36 37 Raises InvalidRoleSecretException:\n 38 If the aws_secret is a none or empty. 39 """ 40 if isinstance(region_name, str) is False or\ 41 isinstance(aws_key, str) is False or\ 42 isinstance(aws_secret, str) is False: 43 raise InvalidAttributeException() 44 45 if region_name == "" or region_name is None: 46 raise InvalidRegionNameException() 47 48 if aws_key == "" or aws_key is None: 49 raise InvalidRoleAccessKeyException() 50 51 if aws_secret == "" or aws_secret is None: 52 raise InvalidRoleSecretException() 53 54 self.client = boto3.client("logs", 55 region_name=region_name, 56 aws_access_key_id=aws_key, 57 aws_secret_access_key=aws_secret)
Boto 3 client default constructor.
Args:
region_name (string): The AWS service region name.
aws_key (string): The AWS service role access key.
aws_secret (string): The AWS service role secret key.
Raises InvalidAttributeException:
If the region_name, aws_key, aws_secret is not of type
string.
Raises InvalidRegionNameException:
If the region_name is a none or empty.
Raises InvalidRoleAccessKeyException:
If the aws_key is a none or empty.
Raises InvalidRoleSecretException:
If the aws_secret is a none or empty.
59 def get_log_groups(self): 60 """ 61 Get all the log groups available in the AWS cloudwatch. 62 63 Returns:\n 64 list: A list of log group names. 65 """ 66 response = self.client.describe_log_groups() 67 return [logGroup['logGroupName'] for logGroup in response['logGroups']]
Get all the log groups available in the AWS cloudwatch.
Returns:
list: A list of log group names.
69 def get_log_stream(self, log_group_name): 70 """ 71 Get all the log stream inside a log group. 72 73 Args:\n 74 log_group_name (string): The name of the log group. 75 76 Returns:\n 77 list: All the log streams' name available in a log group. 78 """ 79 response = self.client.describe_log_streams( 80 logGroupName=log_group_name 81 ) 82 83 return [logGroup['logStreamName'] for logGroup in response['logStreams']]
Get all the log stream inside a log group.
Args:
log_group_name (string): The name of the log group.
Returns:
list: All the log streams' name available in a log group.
103 def create_log_group_stream(self, log_group_name, log_stream_name): 104 """ 105 Create the log stream if the current log group is present. Otherwise, 106 create a new log group then create the desire the log stream 107 108 Args:\n 109 log_group_name (string): the log group name. 110 log_stream_name (string): the log stream name. 111 112 Returns:\n 113 bool: whether the operation is a success (True) or otherwise (False). 114 115 Raises InvalidAttributeException:\n 116 If the log_group_name or log_stream_name is not of type string. 117 118 Raises EmptyParameterException:\n 119 If the log_group_name or log_stream_name is empty. 120 """ 121 if isinstance(log_group_name, str) is False or\ 122 isinstance(log_stream_name, str) is False: 123 raise InvalidAttributeException() 124 125 if log_stream_name == "" or log_group_name == "": 126 raise EmptyParameterException() 127 128 # check if the relevant log stream or log group 129 # is present 130 if log_group_name in self.get_log_groups() and \ 131 log_stream_name not in self.get_log_stream(log_group_name): 132 # if the log stream is not present 133 # create the log stream. 134 self.client.create_log_stream( 135 logGroupName=log_group_name, 136 logStreamName=log_stream_name 137 ) 138 return True 139 140 # If the log group is not present, create the log group 141 log_group_response = self.__create_log_groups(log_group_name) 142 # If the log group is successfully created 143 if log_group_response is not None: 144 # create the log stream and return the response 145 self.create_log_group_stream(log_group_name, log_stream_name) 146 return True 147 148 # Otherwise, return None to show the log stream and log group was present 149 return False
Create the log stream if the current log group is present. Otherwise, create a new log group then create the desire the log stream
Args:
log_group_name (string): the log group name.
log_stream_name (string): the log stream name.
Returns:
bool: whether the operation is a success (True) or otherwise (False).
Raises InvalidAttributeException:
If the log_group_name or log_stream_name is not of type string.
Raises EmptyParameterException:
If the log_group_name or log_stream_name is empty.
151 def set_log_message(self, log_group_name, log_stream_name, log_message): 152 """ 153 Allow the application to push the relevant log message to the cloudwatch. 154 155 Args:\n 156 log_group_name (string): The name of the log group. 157 log_stream_name (string): The name of the log stream. 158 log_message (list): A list of application/ service/ api message. 159 160 Returns:\n 161 response: A list of aws responses. 162 163 Raises InvalidAttributeException:\n 164 If the log_group_name, log_stream_name, or log_message 165 is not of type string. 166 167 Raises EmptyParameterException:\n 168 If the log_group_name, log_stream_name, or log_message 169 is empty. 170 """ 171 if isinstance(log_group_name, str) is False or\ 172 isinstance(log_stream_name, str) is False or\ 173 isinstance(log_message, str) is False: 174 raise InvalidAttributeException() 175 176 if log_group_name == "" or log_stream_name == "" or\ 177 log_message == "": 178 raise EmptyParameterException() 179 180 # check if the log_group name and log stream is present 181 if log_group_name in self.get_log_groups() and \ 182 log_stream_name in self.get_log_stream(log_group_name): 183 # if present only start to push the log message 184 # onto the cloudwatch. 185 self.client.put_log_events( 186 logGroupName=log_group_name, 187 logStreamName=log_stream_name, 188 logEvents=[ 189 { 190 'timestamp': int(round(time.time() * 1000)), 191 'message': time.strftime('%Y-%m-%d %H:%M:%S') + \ 192 "\t" + log_message 193 } 194 ] 195 ) 196 return True 197 return False
Allow the application to push the relevant log message to the cloudwatch.
Args:
log_group_name (string): The name of the log group.
log_stream_name (string): The name of the log stream.
log_message (list): A list of application/ service/ api message.
Returns:
response: A list of aws responses.
Raises InvalidAttributeException:
If the log_group_name, log_stream_name, or log_message
is not of type string.
Raises EmptyParameterException:
If the log_group_name, log_stream_name, or log_message
is empty.