def snapshot_handler(event, context): # Get the url,username,and password from session attributes print(event) url = event['sessionAttributes']['url'] username = event['sessionAttributes']['username'] password = event['sessionAttributes']['password'] cookies = aci_login(url, username, password) slots = event['currentIntent']['slots'] description = event['currentIntent']['slots']['description'] response = create_snapshot(url, cookies, description) if response.status_code == 200: snapshot_created = snapshot_created_template.format(description) intent_fulfilled = generate_response_code(event, "SKIP", dialog_type="CLOSEFULLFILLED", message=snapshot_created ) return intent_fulfilled else: intent_failed = generate_response_code(event, "SKIP", dialog_type="CLOSEFAILED", message=snapshot_failed ) return intent_failed
def bd_handler(event, context): #Get the url,username,and password from session attributes print(event) if not validate_if_logged_in(event): return generate_response_code(event,"SKIP",dialog_type="ELICIT", message=not_logged_in) url = event['sessionAttributes']['url'] username = event['sessionAttributes']['username'] password = event['sessionAttributes']['password'] cookies = aci_login(url, username, password) slots = event['currentIntent']['slots'] tenant_name = event['currentIntent']['slots']['tenant_name'] bd = event['currentIntent']['slots']['bd'] subnet = event['currentIntent']['slots']['subnet'] tenant_exists=check_tenant_exists(url,cookies,tenant_name) if tenant_exists == True: bd_exists=check_bd_exists(url,cookies,tenant_name,bd) if bd_exists == True: tenant_bd_exist_msg=tenant_bd_exist_message.format(tenant_name,bd) both_tenant_bd_exist = generate_response_code(event, "SKIP", dialog_type="CLOSEFULLFILLED", message=tenant_bd_exist_msg) print(tenant_bd_exist_msg) return both_tenant_bd_exist else: response=create_bd_w_subnet(url,cookies,tenant_name,bd,subnet) print(response) if response.status_code == 200: tenant_exists_bd_created_msg=tenant_exists_bd_created_message.format(tenant_name, bd) tenant_exists_bd_created = generate_response_code(event, "SKIP", dialog_type="CLOSEFULLFILLED", message=tenant_exists_bd_created_msg ) print(tenant_exists_bd_created_msg) return tenant_exists_bd_created else: tenant_not_exist_msg=tenant_not_exist_message.format(tenant_name) tenant_not_exist = generate_response_code(event, "SKIP", dialog_type="CLOSEFULLFILLED", message=tenant_not_exist_msg) print(tenant_not_exist_msg) return tenant_not_exist
def create_epg_port(event, context): #TODO context variable passing??? if not validate_if_logged_in(event): return generate_response_code(event,"SKIP",dialog_type="ELICIT", message=not_logged_in) url = event['sessionAttributes']['url'] username = event['sessionAttributes']['username'] password = event['sessionAttributes']['password'] cookies = aci_login(url, username, password) slots = event['currentIntent']['slots'] tenant_name = event['currentIntent']['slots']['tenant_name'] or 'Heroes' ap = event['currentIntent']['slots']['ap_name'] or 'Save_The_Planet' epg = event['currentIntent']['slots']['epg_name'] leaf = event['currentIntent']['slots']['leaf_id'] port = event['currentIntent']['slots']['port'] physdom = event['currentIntent']['slots']['physdom_name'] or 'Heroes_phys' vlan_id = event['currentIntent']['slots']['vlan_id'] epg_url = '{}node/mo/uni/tn-{}/ap-{}/epg-{}.json'.format(url, tenant_name,ap,epg) print(epg_url) if not check_epg_exists(url, cookies, tenant_name, ap,epg): epg_not_exist_msg=epg_not_exist_message.format(epg) return generate_response_code(event,"SKIP",dialog_type="ELICIT", message= epg_not_exist_msg) else: epg_physdom_response = bind_epg_to_physdom(epg_url, cookies, physdom) from_vlan, to_vlan = vlan_pool_details(url, cookies, physdom) vlan_outside_pool_range_msg = vlan_outside_pool_range_message.format(physdom, from_vlan, to_vlan) from_vlan_id = get_vlan_id(from_vlan) to_vlan_id = get_vlan_id(to_vlan) print(to_vlan_id) print(from_vlan_id) if (int(vlan_id) > to_vlan_id or int(vlan_id) < from_vlan_id): vlan_outside_pool_range_msg=vlan_outside_pool_range_message.format(physdom,from_vlan_id,to_vlan_id) return generate_response_code(event,slots, dialog_type="ELICITSLOT", slot_to_elicit="vlan_id", intent=event['currentIntent']['name'], message=vlan_outside_pool_range_msg) epg_interface_bind = bind_epg_to_interface(epg_url, cookies, vlan_id, leaf, port) epg_bind_to_interface_msg=epg_bind_to_interface_message.format(epg,ap,tenant_name,physdom,leaf,port) intent_fulfilled = generate_response_code(event,"SKIP", dialog_type="CLOSEFULLFILLED",message=epg_bind_to_interface_msg) return intent_fulfilled
def login_handler(event, context): # Login to APIC & return message to user, pass username,password & url attributes in the ElicitIntent for further processing print(event) url_tmp = event['currentIntent']['slots']['fqdn'] username = event['currentIntent']['slots']['username'] password = event['currentIntent']['slots']['password'] url = 'https://' + url_tmp + '/api/' url = fix_url(url) try: cookies = aci_login(url, username, password) print(cookies) if type(cookies) is dict: mymessage = logged_in_message sessionAttributes = { "url": url, "username": username, "password": password } cookies_resp = generate_response_code( event, "SKIP", dialog_type="ELICIT", message=mymessage, sessionattributes=sessionAttributes) print(cookies_resp) return cookies_resp else: mymessage = cookies print(mymessage) cookies_resp = generate_response_code(event, "SKIP", dialog_type="ELICIT", message=mymessage) return cookies_resp except: mymessage = login_challenge cookies_resp = generate_response_code(event, "SKIP", dialog_type="ELICIT", message=mymessage) return cookies_resp
def validate(event, context): print(event) domain = event['currentIntent']['slots']['fqdn'] slots = event['currentIntent']['slots'] print(domain) if domain is not None: domain = fix_url(domain) if not is_ip_or_domain(domain): print('Domain is not valid') return generate_response_code( event, slots, dialog_type="ELICITSLOT", intent=event['currentIntent']['name'], slot_to_elicit="fqdn", message=apic_fqdn_retry) else: print('Domain is valid') return generate_response_code(event, slots, dialog_type="DELEGATE") else: return generate_response_code(event, slots, dialog_type="DELEGATE")
def create_epg_port(event, context): #TODO context variable passing??? url = event['sessionAttributes']['url'] username = event['sessionAttributes']['username'] password = event['sessionAttributes']['password'] print('123') cookies = aci_login(url, username, password) slots = event['currentIntent']['slots'] tenant_name = event['currentIntent']['slots']['tenant_name'] or 'Heroes' ap = event['currentIntent']['slots']['ap_name'] or 'Save_The_Planet' epg = event['currentIntent']['slots']['epg_name'] leaf = event['currentIntent']['slots']['leaf_id'] port = event['currentIntent']['slots']['port'] bd = event['currentIntent']['slots']['bd_name'] physdom = event['currentIntent']['slots']['physdom_name'] or 'Heroes_phys' vlan_id = event['currentIntent']['slots']['vlan_id'] subnet = event['currentIntent']['slots']['subnet'] print('456') tenant_exists = check_tenant_exists(url, cookies, tenant_name) ap_exists = check_ap_exists(url, cookies, tenant_name, ap) bd_exists = check_bd_exists(url, cookies, tenant_name, bd) print(tenant_exists) print(ap_exists) print(bd_exists) if tenant_exists == False: create_tenant(url, cookies, tenant_name) if ap_exists == False: create_ap(url, cookies, tenant_name, ap) if bd_exists == False: print('BD doesnt exist') create_bd_w_subnet(url, cookies, tenant_name, bd, subnet) print('789') epg_url = create_epg(url, cookies, tenant_name, ap, epg, bd) epg_physdom_response = bind_epg_to_physdom(epg_url, cookies, physdom) from_vlan, to_vlan = vlan_pool_details(url, cookies, physdom) vlan_outside_pool_range = 'vlan_id for physdom {} shall be between {} and {}'.format(physdom, from_vlan, to_vlan) from_vlan_id = get_vlan_id(from_vlan) to_vlan_id = get_vlan_id(to_vlan) print('10,11,12') if (int(vlan_id) > to_vlan_id or int(vlan_id) < from_vlan_id): return generate_response_code(event,slots, dialog_type="ELICITSLOT", slot_to_elicit="vlan_id", intent=event['currentIntent']['name'], message=vlan_outside_pool_range) # return { # "dialogAction": { # "type": "ElicitSlot", # "intentName": event['currentIntent']['name'], # "slots": slots, # "slotToElicit": "vlan_id", # "message": {"contentType": "PlainText", "content": vlan_outside_pool_range} # } # } epg_interface_bind = bind_epg_to_interface(epg_url, cookies, vlan_id, leaf, port) epg_created = 'epg {} within App {} belonging to Tenant {} has been created & is associated with physdom {} leaf {} and port {}'.format(epg, ap, tenant_name, physdom, leaf, port) intent_fulfilled = generate_response_code(event,"SKIP", dialog_type="CLOSEFULLFILLED", message=epg_created) return intent_fulfilled
def epg_handler(event, context): # Get the url,username,and password from session attributes print(event) if not validate_if_logged_in(event): return generate_response_code(event, "SKIP", dialog_type="ELICIT", message=not_logged_in) url = event['sessionAttributes']['url'] username = event['sessionAttributes']['username'] password = event['sessionAttributes']['password'] cookies = aci_login(url, username, password) slots = event['currentIntent']['slots'] tenant_name = event['currentIntent']['slots']['tenant_name'] ap = event['currentIntent']['slots']['app_profile'] epg = event['currentIntent']['slots']['epg'] bd = event['currentIntent']['slots']['bd'] subnet = event['currentIntent']['slots']['subnet'] ap_exists = check_ap_exists(url, cookies, tenant_name, ap) if ap_exists == True: epg_exists = check_epg_exists(url, cookies, tenant_name, ap, epg) if epg_exists == True: ap_epg_exist_msg = ap_epg_exist_message.format(ap, epg) both_ap_epg_exist = generate_response_code( event, "SKIP", dialog_type="CLOSEFULLFILLED", message=ap_epg_exist_msg) print(ap_epg_exist_msg) return both_ap_epg_exist else: if bd is None: return generate_response_code( event, slots, dialog_type="ELICITSLOT", intent=event['currentIntent']['name'], slot_to_elicit="bd", message=bd_specify_message) if not check_bd_exists(url, cookies, tenant_name, bd): if subnet is None: bd_specify_subnet_msg = bd_specify_subnet_message.format( bd) return generate_response_code( event, slots, dialog_type="ELICITSLOT", intent=event['currentIntent']['name'], slot_to_elicit="subnet", message=bd_specify_subnet_msg) create_bd_w_subnet(url, cookies, tenant_name, bd, subnet) response = create_epg(url, cookies, tenant_name, ap, epg, bd) print(response) if response.status_code == 200: ap_exists_epg_created_msg = ap_exists_epg_created_message.format( ap, epg) ap_exists_epg_created = generate_response_code( event, "SKIP", dialog_type="CLOSEFULLFILLED", message=ap_exists_epg_created_msg) print(ap_exists_epg_created_msg) return ap_exists_epg_created else: ap_not_exist_msg = ap_not_exist_message.format(tenant_name, ap) ap_not_exist = generate_response_code(event, "SKIP", dialog_type="CLOSEFULLFILLED", message=ap_not_exist_msg) print(ap_not_exist_msg) return ap_not_exist
def app_profile_handler(event, context): #Get the url,username,and password from session attributes print(event) if not validate_if_logged_in(event): return generate_response_code(event, "SKIP", dialog_type="ELICIT", message=not_logged_in) url = event['sessionAttributes']['url'] username = event['sessionAttributes']['username'] password = event['sessionAttributes']['password'] cookies = aci_login(url, username, password) slots = event['currentIntent']['slots'] tenant_name = event['currentIntent']['slots']['tenant_name'] ap = event['currentIntent']['slots']['app_profile'] tenant_exists = check_tenant_exists(url, cookies, tenant_name) if tenant_exists == True: ap_exists = check_ap_exists(url, cookies, tenant_name, ap) if ap_exists == True: tenant_ap_exist_msg = tenant_ap_exist_message.format( tenant_name, ap) both_tenant_ap_exist = generate_response_code( event, "SKIP", dialog_type="CLOSEFULLFILLED", message=tenant_ap_exist_msg) print(tenant_ap_exist_msg) return both_tenant_ap_exist # both_tenant_ap_exist= { # "dialogAction": { # "type": "Close", # "fulfillmentState": "Fulfilled", # "message": {"contentType": "PlainText","content": tenant_ap_exist_msg } # } # } # print(tenant_ap_exist_msg) # return both_tenant_ap_exist else: response = create_ap(url, cookies, tenant_name, ap) print(response) try: if response.status_code == 200: tenant_exists_ap_created_msg = tenant_exists_ap_created_message.format( tenant_name, ap) tenant_exists_ap_created = generate_response_code( event, "SKIP", dialog_type="CLOSEFULLFILLED", message=tenant_exists_ap_created_msg) print(tenant_exists_ap_created_msg) return tenant_exists_ap_created except: error_create_app_profile_msg = error_create_app_profile_message.format( ap, tenant_name) error_create_app_profile = generate_response_code( event, "SKIP", dialog_type="CLOSEFULLFILLED", message=error_create_app_profile_msg) else: tenant_not_exist_msg = tenant_not_exist_message.format(tenant_name) tenant_not_exist = generate_response_code(event, "SKIP", dialog_type="CLOSEFAILED", message=tenant_not_exist_msg) print(tenant_not_exist_msg) return tenant_not_exist
def get_endpoints_handler(event, context): print(event) #Get the url,username,and password from session attributes & tenant_name from slots input, login to APIC, check tenant ap_exists #create new tenant if required if not validate_if_logged_in(event): return generate_response_code( event, "SKIP", dialog_type="ELICIT", message="You have not logged in, please type login to proceed") print(event) url = event['sessionAttributes']['url'] username = event['sessionAttributes']['username'] password = event['sessionAttributes']['password'] print(url) cookies = aci_login(url, username, password) source = event['currentIntent']['slots']['source_ip'] destination = event['currentIntent']['slots']['destination_ip'] dest_email = event['currentIntent']['slots']['dest_email'] subject = '*Troubleshooting Session*\n' file_name = '/tmp/troubleshoot-communications.csv' message = '*Troubleshooting Session* \n' print(source) with open(file_name, 'w') as f: row1 = "ip,mac,tenant_name,ap_profile,epg_name,consumed_contract,provided_contract \n" try: ip, mac, tenant_name, ap_profile, epg_name, consumed_contract, provided_contract = find_endpoint( source, url, cookies) data1 = (("{},{},{},{},{},{},{},\n").format( ip, mac, tenant_name, ap_profile, epg_name, consumed_contract, provided_contract)) message += '*Source Endpoint Details* \n' message += '*IP Address:* {}\n'.format(ip) message += '*MAC Address:* {}\n'.format(mac) message += '*Tenant:* {}\n'.format(tenant_name) message += '*Application Profile:* {}\n'.format(ap_profile) message += '*Endpoint Group:* {}\n'.format(epg_name) message += '*Consumed Contract:* {}\n'.format(consumed_contract) message += '*Provided Contract:* {}\n\n\n'.format( provided_contract) print(message) except: data1 = '{} does not exist\n'.format(source) message += '{} does not exist\n'.format(source) try: ip, mac, tenant_name, ap_profile, epg_name, consumed_contract, provided_contract = find_endpoint( destination, url, cookies) data2 = (("{},{},{},{},{},{},{},\n").format( ip, mac, tenant_name, ap_profile, epg_name, consumed_contract, provided_contract)) message += '*Destination Endpoint Details* \n' message += '*IP Address:* {}\n'.format(ip) message += '*MAC Address:* {}\n'.format(mac) message += '*Tenant:* {}\n'.format(tenant_name) message += '*Application Profile* : {}\n'.format(ap_profile) message += '*Endpoint Group:* {}\n'.format(epg_name) message += '*Consumed Contract:* {}\n'.format(consumed_contract) message += '*Provided Contract:* {}\n\n\n'.format( provided_contract) print(message) except: data2 = '{} does not exist\n'.format(destination) message += '{} does not exist\n'.format(source) f.write(row1), f.write(data1), f.write(data2) dest_email = temporary_email(dest_email) send_email_w_attachment(from_email, dest_email, subject, file_name) message += '\n\nWe have gathered all the information about *{}* and *{}* and have sent an email to you about this'.format( source, destination) endpoints_resp = generate_response_code(event, "SKIP", dialog_type="CLOSEFULLFILLED", message=message) return endpoints_resp