def zip_update_invoke(self, payload={}): self.copy_html_files() _lambda = Lambdas('create_map', 'create_map.run', memory=3008) copy_tree(_lambda.source, self.src_tmp) copy_tree(self.folder_src_lambdas, self.src_tmp) _lambda.source = self.src_tmp return _lambda.update().invoke(payload)
def test_bug_cache_issue_in_lambdas(self): Lambdas('browser.lambda_browser').update_with_src() payload = {"params": ["go_js", "graph_MKF", "default"]} self.png_data = Lambdas('browser.lambda_browser').invoke(payload) Dev.pprint(self.png_data) self.png_data = Lambdas('browser.lambda_browser').invoke(payload) Dev.pprint(self.png_data)
def run(event, context): channel = event.get('channel') try: dot_to_svg = Lambdas('utils.dot_to_svg').invoke svg_to_png = Lambdas('utils.svg_to_png').invoke svg = dot_to_svg(event) result = svg_to_png({"svg": svg , "width": event.get("width")}) if result.get('image'): return result['image'] return { 'error' : result} except Exception as error: slack_message(":red_circle: Error in dot_to_png: {0}".format(error), [], channel) return { 'error': error }
def create_dashboard_for_jira_key(self, jira_key): from utils.aws.Lambdas import Lambdas lambda_graph = Lambdas('gsbot.gsbot_graph') payload = { 'data': {}, "params": ['expand', jira_key, 9, 'delivered by,risks_up'] } result = lambda_graph.invoke(payload) graph_data = json.loads(result) graph_name = graph_data.get('graph_name') sleep(1.0) # to give time for ELK to index return self.create_dashboard_for_graph(graph_name, jira_key)
def puml_to_png_using_lambda_function(self, puml, target_file=None): if target_file is None: target_file = self.tmp_png_file puml_to_png = Lambdas('utils.puml_to_png').invoke result = puml_to_png({"puml": puml}) with open(target_file, "wb") as fh: fh.write(base64.decodebytes(result['png_base64'].encode())) return target_file
class Test_Lambda_pdf_to_slack(unittest.TestCase): def setUp(self): self.png_to_slack = Lambdas('utils.png_to_slack', memory=3008) def test_update_and_invoke(self): png_file = '/tmp/lambda_png_file.png' png_data = base64.b64encode(open(png_file, 'rb').read()).decode() Dev.pprint(len(png_data)) payload = { "png_data": png_data, 'aws_secrets_id': 'slack-gs-bot', 'channel': 'DDKUZTK6X' } result = self.png_to_slack.update_with_src().invoke(payload) Dev.pprint(result) def test__delete_temp_png_files(self): s3_bucket = 'gs-lambda-tests' tmp_files = S3().find_files(s3_bucket, S3().tmp_file_folder) for tmp_file in tmp_files: S3().file_delete(s3_bucket, tmp_file) Dev.pprint(S3().find_files(s3_bucket, S3().tmp_file_folder))
def dot_render(slack_event, params=None): text = slack_event["text"] channel_id = slack_event["channel"] code = text.split("```") if len(code) != 3: text = '*GS Bot command execution error :exclamation:*' attachments = [{ "text": 'you need to provide the code to red inside *```* blocks', 'color': 'danger' }] return text, attachments dot = "digraph G {\n" + code[1] + "\n }" text = ":information_source: Rending dot code with size: {0}".format( len(dot)) attachments = [] Lambdas('utils.dot_to_slack').invoke_async({ 'dot': dot, 'channel': channel_id }) return text, attachments
def send_png_file_to_slack(self, team_id, channel, target, png_file): if team_id and channel: s3_bucket = 'gs-lambda-tests' s3_key = S3().file_upload_as_temp_file(png_file, s3_bucket) png_to_slack = Lambdas('utils.png_to_slack') payload = { 's3_bucket': s3_bucket, 's3_key': s3_key, 'team_id': team_id, 'channel': channel, 'title': target } png_to_slack.invoke_async(payload) return None, None else: return base64.b64encode(open(png_file, 'rb').read()).decode()
class Test_Lambda_gsbot_gs_jira(unittest.TestCase): def setUp(self): self.step_lambda = Lambdas('gsbot.gsbot_gs_jira', memory = 3008) def test_lambda_update(self): self.step_lambda.update_with_src() def _send_command_message(self,command): payload = {'params' : [command] , 'data': {'team_id':'T7F3AUXGV', 'channel':'DDKUZTK6X'}} return self.step_lambda.upload_and_invoke(payload) # see answer in slack, or add a return to line 17 (in lambda_gs_bot) def test_invoke(self): response = self._send_command_message('invoke') Dev.pprint(response)
def gdocs(slack_event, params=None): Lambdas('gs.lambda_gdocs').invoke_async({ 'params': params, 'data': slack_event }) return None, None
class Test_lambda_api_gateway_simple_test(unittest.TestCase): def setUp(self): self.target = Lambdas('api_gateway.trigger_server_reload') def test_update_and_invoke(self): result = self.target.update_with_src().invoke({}) Dev.pprint(result)
class Test_lambda_api_gateway_simple_test(unittest.TestCase): def setUp(self): self.simple_test = Lambdas('api_gateway.simple_test') def test_update_and_invoke(self): result = self.simple_test.update().invoke({'aa': 'bb'}) Dev.pprint(result)
def process_posted_body( self, postdata ): # handle the encoding created by API GW, which uses as transformation try: # { "body" : $input.json('$' ) } return Lambdas('gs.slack_interaction').invoke({"body": postdata}) except Exception as error: return "Error in processing posted data: {0}".format(str(error))
class Test_Lambda_gs_bot(unittest.TestCase): def setUp(self): self.step_lambda = Lambdas('gsbot.gsbot_slack', memory=3008) def test_lambda_update(self): self.step_lambda.update_with_src() def _send_command_message(self, command): payload = {'params': [command], 'data': {}} return self.step_lambda.upload_and_invoke( payload ) # see answer in slack, or add a return to line 17 (in lambda_gs_bot) def test_hello(self): response = self._send_command_message('test') Dev.pprint(response)
def test__create_button_to_test_dialog(self): Lambdas('gs.jira_dialog').update() self.api_attach.set_text ('Click on button below to test dialog' ) \ .set_callback_id("button-dialog-test" ) \ .add_button ("button", "open dialog", "open", "primary") attachments = self.api_attach.render() slack_message("", attachments, 'DDKUZTK6X')
class Test_Lambda_hello_world(unittest.TestCase): def setUp(self): self.hello_world = Lambdas('dev.hello_world') def test_update_and_invoke(self): payload = {"name": 'world'} result = self.hello_world.update().invoke(payload) assert result == 'hello world'
def log_debug(message, data=None, category="API_GS_Bot"): payload = { "index": "gs_bot_logs", "level": "debug", "message": message, "category": category, "data": data } Lambdas('utils_log_to_elk').invoke_async(payload)
def jira(slack_event, params=[]): Lambdas('gs.elastic_jira').invoke_async( { "params": params, "user": slack_event.get('user'), "channel": slack_event.get('channel'), 'team_id': slack_event.get('team_id') }, ) return None, None
class Test_Lambda_dot_to_png(unittest.TestCase): def setUp(self): #upload_dependency('plantuml') #path_libs = '../_lambda_dependencies/plantuml' self.plant_to_png = Lambdas( 'utils.puml_to_png', memory=3008).delete().create() #,path_libs=path_libs) def test_update_invoke(self): puml = "@startuml \n aaa30->bbb12 \n @enduml" result = self.plant_to_png.update().invoke({"puml": puml}) Dev.pprint(result) #Show_Img.from_svg_string(result['png_base64']) def test_just_invoke___more_complex_diagram(self): puml = """ @startuml /'skinparam dpi 500 '/ :Main aaa Admin: as Admin (Use the application) as (Use) User -> (Start) User --> (Use) (Use) --> (b50011) Admin ---> (Use) note right of admin : This is an example. note right of (Use) A note can also be on several lines very cool end note note "This note is connected\\nto several objects." as N2 (Start) .. N2 N2 .. (Use) @enduml """ result = self.plant_to_png.invoke({"puml": puml}) Show_Img.from_svg_string(result['png_base64'])
def get_graph_data(self, graph_name): params = {'params': ['raw_data', graph_name, 'details'], 'data': {}} data = Lambdas('gsbot.gsbot_graph').invoke(params) if type(data) is str: s3_key = data s3_bucket = 'gs-lambda-tests' tmp_file = S3().file_download_and_delete(s3_bucket, s3_key) data = Json.load_json_and_delete(tmp_file) return data return data
def create_dashboard_for_graph(self, graph_name, root_node): from utils.aws.Lambdas import Lambdas from view_helpers.Vis_Js import Vis_Js self.graph_name = graph_name self.jira_key = root_node payload = {"graph_name": graph_name, 'destination_node': root_node} graph_paths = Lambdas('gs.graph_paths').invoke(payload) self.create_dashboard_with_R1_R2() graph_data = Vis_Js().get_graph_data(graph_name) nodes = graph_data.get('nodes') colors_scores = {} for key, node in nodes.items(): if node: if 'R2' in node.get('Labels'): summary = node.get('Summary') cell_key = ( "r" + summary.split('-')[0].replace('.', '_')).strip() #Dev.pprint(graph_paths.get(key)) if graph_paths.get(key): score = len(graph_paths.get(key)) colors_scores[cell_key] = score #Dev.pprint(score) #Dev.pprint(key + ' ' + cell_key + ' ' + str(score) + ' ' + summary) #return colors_scores #Dev.pprint(colors_scores) js_codes = [] for i in range(1, 7): for j in range(1, 7): r2_id = "r{0}_{1}".format(i, j) color = colors_scores.get(r2_id) self.js_apply_css_color(js_codes, r2_id, color) self.js_eval(js_codes) return self
class Test_Lambda_dot_to_png(unittest.TestCase): def setUp(self): self.dot_to_png = Lambdas('utils.dot_to_png') def test_update_invoke(self): dot = """digraph G {\naaa -> bbb;\n}\n""" png = self.dot_to_png.update().invoke({"dot": dot}) Dev.pprint(png) #Show_Img.from_svg_string(png) def test_update_invoke__small_image(self): dot = """digraph G {\naaa -> bbb;\n}\n""" png = self.dot_to_png.update().invoke({"dot": dot, "width": 25}) # see pic SciView which should have less quality Show_Img.from_svg_string(png) def test_update_invoke__trigger_error(self): dot = """digraph G {\naaaa-aaa-aaa-a>--asd@£-ad;\n}\n""" result = self.dot_to_png.update().invoke({"dot": dot, "channel": "DDKUZTK6X"}) Dev.pprint(result)
def run(event, context): channel = event['channel'] puml = event['puml'] puml = puml.replace('<', '<').replace('>', '>') (fd, tmp_file) = tempfile.mkstemp('png)') puml_to_png = Lambdas('utils.puml_to_png').invoke result = puml_to_png({"puml": puml}) with open(tmp_file, "wb") as fh: fh.write(base64.decodebytes(result['png_base64'].encode())) return upload_png_file(channel, tmp_file)
class Test_lambda_api_gateway_simple_test(unittest.TestCase): def setUp(self): self.simple_test = Lambdas('api_gateway.trigger_sync_jira_sheets') def test_update_and_invoke(self): file_id = '1yDxu5YxL9FxY5wQ1EEQlAYGt3flIsm2VTyWwPny5RLA' result = self.simple_test.update_with_src().invoke( {'queryStringParameters': { 'file_id': file_id }}) message = Misc.json_load(result.get('body')) Dev.pprint(message) #assert result == 'API Gateway test' def test_update_and_invoke__action_diff(self): file_id = '1yDxu5YxL9FxY5wQ1EEQlAYGt3flIsm2VTyWwPny5RLA' result = self.simple_test.update_with_src().invoke( {'queryStringParameters': { 'file_id': file_id, 'action': 'diff' }}) message = Misc.json_load(result.get('body')) Dev.pprint(message) def test_update_and_invoke__action_sync(self): file_id = '1yDxu5YxL9FxY5wQ1EEQlAYGt3flIsm2VTyWwPny5RLA' result = self.simple_test.update_with_src().invoke( {'queryStringParameters': { 'file_id': file_id, 'action': 'sync' }}) message = Misc.json_load(result.get('body')) Dev.pprint(message) def test_just_update(self): self.simple_test.update_with_src()
class Test_Lambda_slack_message(unittest.TestCase): def setUp(self): self.slack_message = Lambdas('utils.slack_message', path_libs='../_lambda_dependencies/slack') def test_update_invoke(self): channel = "gs-bot-tests" text = ":point_right: an message from lambda" attachments = [{ "text": "an attachment (good)", "color": "good" }, { "text": "an attachment (danger)", "color": "danger" }] result = self.slack_message.upload_and_invoke({ "channel": channel, "text": text, "attachments": attachments }) del result['message']['ts'] assert result['ok'] is True assert result['message'] == { 'attachments': [{ 'color': '2eb886', 'fallback': 'an attachment (good)', 'id': 1, 'text': 'an attachment (good)' }, { 'color': 'a30200', 'fallback': 'an attachment (danger)', 'id': 2, 'text': 'an attachment (danger)' }], 'bot_id': 'BDKLUMX4K', 'subtype': 'bot_message', 'text': ':point_right: an message from lambda', 'type': 'message', 'username': '******' }
class Test_Lambda_pdf_to_slack(unittest.TestCase): def setUp(self): self.pdf_to_slack = Lambdas('utils.pdf_to_slack', memory=3008).create() #Lambdas('utils.puml_to_png', memory=3008).delete().create() def test_update_and_invoke(self): gdrive = GDrive() file_id = gdrive.find_by_name('GSlides API tests').get('id') pdf_bytes = gdrive.file_export(file_id) pdf_data = base64.b64encode(pdf_bytes).decode() payload = {"pdf_data": pdf_data, 'channel': 'DDKUZTK6X'} result = self.pdf_to_slack.update_with_src().invoke(payload) Dev.pprint(result)
def run(event, context): channel = event['channel'] dot = event['dot'] (fd, tmp_file) = tempfile.mkstemp('png)') dot_to_png = Lambdas('utils.dot_to_png').invoke png_data = dot_to_png({"dot": dot, "width": 500, "channel": channel}) if len(png_data) > 5: with open(tmp_file, "wb") as fh: fh.write(base64.decodebytes(png_data.encode())) result = upload_png_file(channel, tmp_file) return result else: #slack_message(":red_circle: Error: could not create png file (file size: {0})".format(len(png_data)), [], channel) return {"status": "error"}
def handle_command(self, slack_event): try: if slack_event.get('text'): command = slack_event.get('text').replace('<@UDK5W7W3T>', '') \ .replace('<@UG2BHLSSU>', '') \ .strip() # remove the @gsbot names (PBX and GS-CST) from the message (this needs a better solution) if not command: return None, None log_debug( 'command: {0} | team_id: {1} | channel: {2} | user: {3} ' .format(command, slack_event.get('team_id'), slack_event.get('channel'), slack_event.get('user')), category='API_GS_Bot.handle_command') #refactor code below to separate method method_name = command.split(' ')[0].split('\n')[0] if method_name in [ 'graph', 'slack', 'gs_jira' ]: # this is the new way to route commands,where a lambda function is invoked lambda_name = 'gsbot.gsbot_{0}'.format(method_name) method_params = command.split(' ')[1:] Lambdas(lambda_name).invoke_async({ 'params': method_params, 'data': slack_event }) return None, None else: method = self.resolve_command_method( command) # find method to invoke if method: method_params = command.split(' ')[1:] (text, attachments) = method(slack_event, method_params) # invoke method else: text = ":exclamation: GS bot command `{0}` not found. Use `gsbot help` to see a list of available commands".format( method_name) attachments = [] else: return None, None except Exception as error: text = '*GS Bot command execution error in `handle_command` :exclamation:*' attachments = [{'text': ' ' + str(error), 'color': 'danger'}] return text, attachments
class Test_Lambda_log_to_elk(unittest.TestCase): def setUp(self): #path_libs = '../_lambda_dependencies/elastic' self.log_to_elk = Lambdas( 'utils.log_to_elk') #, path_libs = path_libs) def test_update_invoke(self): payload = {} result = self.log_to_elk.update().invoke(payload) Dev.pprint(result) def test_invoke___check_multiple_log_options(self): self.log_to_elk.invoke({ 'level': 'info', 'category': 'category info', 'message': 'message info', 'data': { 'answer': 42 } }) self.log_to_elk.invoke({ 'level': 'debug', 'category': 'category debug', 'message': 'message debug', 'data': { 'answer': 42 } }) self.log_to_elk.invoke({ 'level': 'error', 'category': 'category error', 'message': 'message error', 'data': { 'answer': 42 } }) self.log_to_elk.invoke({'level': 'debug'})
def mindmap(slack_event, params=None): channel = slack_event.get('channel') team_id = slack_event.get('team_id') if len(params) < 1: text = ':red_circle: Hi, for the `mindmap` command, you need to provide an `graph_name`' slack_message(text, [], channel, team_id) return None, None graph_name = params.pop(0) graph_params = ['go_js', graph_name, 'mindmap'] graph_params.extend(params) Lambdas('browser.lambda_browser').invoke_async({ "params": graph_params, 'data': { 'team_id': team_id, 'channel': channel } }) return None, None