def generate_service_requests_report(): try: string_output = io.StringIO() csv_writer = csv.writer(string_output) csv_writer.writerow([ 'TicketNum', 'HostRecord', 'HostID', 'IPAddress', 'IPState', 'IPID', 'RequestCreate', 'RequestedBy', 'LastUpdate', 'CloseDate', 'CloseNotes' ]) ticket_url = service_requests_config.servicenow_url + '?assigned_to=admin' response = requests.get( ticket_url, auth=(service_requests_config.servicenow_username, get_password_from_file( service_requests_config.servicenow_secret_file)), headers=headers, verify=False) data = [] # Check for HTTP codes other than 200 if response.status_code == 200: tickets = response.json() for ticket in tickets['result']: if 'BlueCat' in ticket['short_description']: description = ticket['description'].split(',') ip_address = g.user.get_api().get_configuration( config.default_configuration).get_ip4_address( description[3].partition('ip_address=')[2]) hosts = ip_address.get_linked_entities('HostRecord') host_id = '' for host in hosts: host_id = host.get_id() info = [ ticket['number'], description[2].partition('host_record=')[2], host_id, ip_address.get_address(), ip_address.get_state(), ip_address.get_id(), ticket['sys_created_on'], ticket['sys_created_by'], ticket['sys_updated_on'], ticket['closed_at'], ticket['close_notes'] ] csv_writer.writerow(info) output_response = make_response(string_output.getvalue()) output_response.headers[ 'Content-Disposition'] = 'attachment; filename=service_requests.csv' output_response.headers['Content-type'] = 'text/csv' else: raise Exception( 'Received a non 200 response from the service provider: %s, content: %s' % (response.status_code, response.content)) except Exception as e: return_message = 'Encountered an error while generating the CSV file: %s' % util.safe_str( e) output_response = make_response(return_message, 500) return output_response
def get_router_location(link): headers = {"Accept": "application/json"} response = requests.get(link, auth=(cmdb_config.servicenow_username, get_password_from_file( cmdb_config.servicenow_secret_file)), headers=headers, verify=False) location = response.json() return location['result']['name']
def get_switch_manufacturer(link): headers = {"Accept": "application/json"} response = requests.get(link, auth=(cmdb_config.servicenow_username, get_password_from_file( cmdb_config.servicenow_secret_file)), headers=headers, verify=False) manufacturer = response.json() return manufacturer['result']['name']
def raw_table_data(*args, **kwargs): """Returns table formatted data for display in the TableField component""" # Declare table column header and data data = { 'columns': [ { 'title': 'Ticket' }, { 'title': 'Host Record' }, { 'title': 'IP Address' }, { 'title': 'Creator' }, { 'title': 'Date Created' }, { 'title': 'Actions' }, ], 'data': [] } # HTTP request ticket_url = service_requests_config.servicenow_url + '?assigned_to=admin&state!=closed&active=true' response = requests.get( ticket_url, auth=(service_requests_config.servicenow_username, get_password_from_file( service_requests_config.servicenow_secret_file)), headers=headers, verify=False) # Check for HTTP codes other than 200 if response.status_code == 200: tickets = response.json() for ticket in tickets['result']: description = ticket['description'].split(',') if '|' not in ticket['short_description']: data['data'].append([ ticket['number'], description[2].partition('host_record=')[2], description[3].partition('ip_address=')[2], 'admin', ticket['sys_created_on'], '<button type="button" class="btn btn-primary btn-xs">Approve</button>' ]) return data
def raw_table_data(*args, **kwargs): # pylint: disable=redefined-outer-name data = { 'columns': [ { 'title': 'Name' }, { 'title': 'IP Address' }, { 'title': 'Serial Number' }, { 'title': 'Manufacturer' }, ], 'data': [] } # HTTP request headers = {"Accept": "application/json"} cmdb_url = cmdb_config.servicenow_url + '/api/now/table/cmdb_ci_ip_switch' response = requests.get(cmdb_url, auth=(cmdb_config.servicenow_username, get_password_from_file( cmdb_config.servicenow_secret_file)), headers=headers, verify=False) # Check for HTTP codes other than 200 if response.status_code == 200: switches = response.json() for switch in switches['result']: switch_name = switch['name'] switch_ip = switch['ip_address'] switch_serial = switch['serial_number'] if switch['manufacturer']: switch_manufacturer = get_switch_manufacturer( switch['manufacturer']['link']) data['data'].append( [switch_name, switch_ip, switch_serial, switch_manufacturer]) return data
def get_all_core_company_manufactures(): headers = {"Accept": "application/json"} cmdb_url = cmdb_config.servicenow_url + '/api/now/table/core_company' response = requests.get(cmdb_url, auth=(cmdb_config.servicenow_username, get_password_from_file( cmdb_config.servicenow_secret_file)), headers=headers, verify=False) manu_dict = {} if response.status_code == 200: manufactures = response.json() for manufacture in manufactures['result']: manu_dict.update({manufacture['sys_id']: manufacture['name']}) return manu_dict
def get_gitlab_groups(default_val=False): """ Get a list of groups for display in a drop-down menu box. :return: List of groups name tuples. """ groups = {} try: groups = requests.get( gitlab_import_config.gitlab_url + 'groups?search=' + gitlab_import_config.default_group + '&private_token=' + get_password_from_file(gitlab_import_config.secret_file), None) except Exception as e: flash('An error occured! Please check the user logs for the current session for more information.') if groups.json() and groups.status_code == 200: groups = groups.json() for group in groups: group_id = group['id'] projects = requests.get( gitlab_import_config.gitlab_url + 'groups/' + str(group_id) + '/projects?include_subgroups=True&private_token=' + get_password_from_file(gitlab_import_config.secret_file) + '&per_page=100', None) projects = projects.json() projects.sort(key=lambda x: x['path_with_namespace'], reverse=False) result = [] if default_val: result.append(('1', 'Please Select')) for project in projects: result.append((project['id'], project['path_with_namespace'].rpartition('/')[-0] + '/' + project['name'])) return result else: result =[('1', 'Not Connected! Check logs for more detail')] g.user.logger.warning("Failed to connect to GitLab with the following configurations: url: {}, default_group {}, personal token {}".format( gitlab_import_config.gitlab_url, gitlab_import_config.default_group, get_password_from_file(gitlab_import_config.secret_file)), msg_type=g.user.logger.EXCEPTION) return result
def service_request_host_page_form(): """ Processes the final form after the user has input all the required data. :return: """ form = GenericFormTemplate() ip4_address_list = [] # Retrieve the configuration object try: configuration = g.user.get_api().get_configuration( config.default_configuration) view = configuration.get_view(config.default_view) configuration.assign_ip4_address( form.ip4_address.data, '', '', IPAssignmentActionValues.MAKE_RESERVED) except PortalException as e: g.user.logger.error('%s' % e, msg_type=g.user.logger.EXCEPTION) flash( 'Unable to retrieve configuration or view specified in configuration' ) return redirect( url_for('service_request_hostservice_request_host_page')) # Retrieve form attributes absolute_name = form.hostname.data + '.' + request.form['zone'] ip4_address_list.append(form.ip4_address.data) # This is the field that we will pass the requested information to in ServiceNow ticket_information = { 'short_description': 'BlueCat: Ticket requested to create %s with host record %s' % (ip4_address_list[0], absolute_name), 'description': 'configuration=%s, view=%s, host_record=%s, ip_address=%s' % (configuration.get_name(), view.get_name(), absolute_name, ip4_address_list[0]), 'assigned_to': 'admin', 'category': 'network', 'comments': 'Created from BlueCat Gateway / API' } # Do the HTTP request response = requests.post( service_requests_config.servicenow_url, auth=(service_requests_config.servicenow_username, get_password_from_file( service_requests_config.servicenow_secret_file)), headers=headers, data=json.dumps(ticket_information), verify=False) time.sleep(5) if response.status_code != 201: print('Status:', response.status_code, 'Headers:', response.headers, 'Error Response:', response.json()) exit() # Put form processing code here flash( 'Successfully created ServiceNow Ticket: ' + response.json()['result']['number'], 'succeed') flash('') return redirect(url_for('service_request_hostservice_request_host_page'))
def gitlab_import_gitlab_import_page_form(): form = GenericFormTemplate() form.gitlab_groups.choices = gitlab_import_util.get_gitlab_groups(default_val=True) if form.validate_on_submit(): try: # This is to get and download the archive zip response = {} response = requests.get( gitlab_import_config.gitlab_url + 'projects/' + str( form.gitlab_groups.data) + '/repository/archive.zip?private_token=' + get_password_from_file(gitlab_import_config.secret_file), None) response.raise_for_status() print( "To download, use this link {}projects/{}/repository/archive.zip?private_token={} ".format( gitlab_import_config.gitlab_url, str( form.gitlab_groups.data), get_password_from_file(gitlab_import_config.secret_file))) # Get the file name d = response.headers['content-disposition'] file_name = re.findall("filename=(.+)", d) file_name = "".join(file_name) # Clean the file name up so we can use it for the foldername after unzipping folder_name = file_name.strip('\"').replace('.zip', '') with zipfile.ZipFile(io.BytesIO(response.content)) as thezip: thezip.extractall(gitlab_import_config.workflow_dir) gitlab_directories = list() ziped_dirs = os.path.join(gitlab_import_config.workflow_dir, folder_name, gitlab_import_config.gitlab_import_directory) dirs = os.listdir(ziped_dirs) time_format = datetime.now().strftime("%Y_%m_%d_%H_%M_%S") # Utils dir loading try: if gitlab_import_config.gitlab_import_utils_directory: print("Found utils to load") # Is the folder really there? if os.path.exists(os.path.join("bluecat_portal", gitlab_import_config.gw_utils_directory)): # Move folder from zip to deployed util dir # backup folder # Does the backup folder exist if os.path.exists(os.path.join("bluecat_portal", (gitlab_import_config.backups_folder))): # Create the backup file shutil.make_archive( os.path.join("bluecat_portal", gitlab_import_config.backups_folder) + '/' + gitlab_import_config.gitlab_import_utils_directory + '-' + time_format, 'zip', os.path.join("bluecat_portal", gitlab_import_config.gw_utils_directory)) else: # Create backup folder os.mkdir(os.path.join("bluecat_portal", gitlab_import_config.backups_folder)) shutil.make_archive( os.path.join("bluecat_portal", gitlab_import_config.backups_folder) + '/' + gitlab_import_config.gitlab_import_utils_directory + '-' + time_format, 'zip', os.path.join("bluecat_portal", gitlab_import_config.gw_utils_directory)) # Delete the util folder on the GW server shutil.rmtree(os.path.join("bluecat_portal", gitlab_import_config.gw_utils_directory)) # Move GitHub Util folder to GW server shutil.move( os.path.join(gitlab_import_config.workflow_dir, folder_name, gitlab_import_config.gitlab_import_utils_directory), os.path.join("bluecat_portal", gitlab_import_config.gw_utils_directory.rsplit('/', 1)[0])) else: # Move folder from zip to deployed util dir shutil.move( os.path.join(gitlab_import_config.workflow_dir, folder_name, gitlab_import_config.gitlab_import_utils_directory), os.path.join("bluecat_portal", gitlab_import_config.gw_utils_directory.rsplit('/', 1)[0])) else: print("No utils specified in custom.gitlab_import_utils_directory") app.logger.info("No util folder used") except Exception as e: app.logger.exception("Failed to load GitLab Util folder: {}".format(str(e))) g.user.logger.warning('%s' % util.safe_str(e), msg_type=g.user.logger.EXCEPTION) flash( 'Failed to load GitLab Util folder properly. Check to see if path ' + gitlab_import_config.gw_utils_directory + ' exists on the server.') return redirect(url_for('gitlab_importgitlab_import_gitlab_import_page')) # Create backup shutil.make_archive( os.path.join("bluecat_portal", gitlab_import_config.backups_folder) + '/' + gitlab_import_config.gitlab_import_directory + '-' + time_format, 'zip', os.path.join(gitlab_import_config.workflow_dir, gitlab_import_config.gitlab_import_directory)) for dir in dirs: # Workflow dir loading if os.path.isdir(os.path.join(gitlab_import_config.workflow_dir, folder_name, gitlab_import_config.gitlab_import_directory, dir)): if os.path.exists(os.path.join(gitlab_import_config.workflow_dir, gitlab_import_config.gitlab_import_directory, dir)): # Unregister existing mapped functions for workflow_name, workflow_fields in list(config.workflows.items()): if dir in workflow_fields['categories']: app.logger.info("Unloading workflow %s", workflow_name) workflow_path = get_workflow_path(workflow_name) if not remove_registered_workflow_functions(workflow_path.replace(os.path.sep, '.')): raise Exception("Removal failed {}".format(workflow_path.replace(os.path.sep, '.'))) unload_modules_in_dir(workflow_path.replace(os.path.sep, '.'), os.listdir(workflow_path)) config.workflows.pop(workflow_name) shutil.rmtree(os.path.join(gitlab_import_config.workflow_dir, gitlab_import_config.gitlab_import_directory, dir)) shutil.move(os.path.join(gitlab_import_config.workflow_dir, folder_name, gitlab_import_config.gitlab_import_directory, dir), os.path.join(gitlab_import_config.workflow_dir, gitlab_import_config.gitlab_import_directory)) gitlab_directories.append(dir) permissions = load_permissions_json() status = True # Refresh config.workflows for dir in gitlab_directories: if not gitlab_import_util.custom_workflow_navigator(os.path.join(gitlab_import_config.workflow_dir, gitlab_import_config.gitlab_import_directory, dir), permissions): status = False if not status: raise Exception("Failed to load workflows in memory") # pylint: disable=broad-except except Exception as e: app.logger.exception("Failed to load GitLab workflows: {}".format(str(e))) g.user.logger.warning('%s' % util.safe_str(e), msg_type=g.user.logger.EXCEPTION) flash('Unable to import GitLab workflows properly: ' + util.safe_str(e)) return redirect(url_for('gitlab_importgitlab_import_gitlab_import_page')) finally: if os.path.exists(os.path.join(gitlab_import_config.workflow_dir, folder_name)): shutil.rmtree(os.path.join(gitlab_import_config.workflow_dir, folder_name)) g.user.logger.info('SUCCESS') flash('Imported GitLab workflows successfully', 'succeed') return redirect(url_for('gitlab_importgitlab_import_gitlab_import_page')) else: g.user.logger.info('Form data was not valid.') return render_template( 'gitlab_import_page.html', form=form, text=util.get_text(module_path(), config.language), options=g.user.get_options(), )
def raw_table_data(*args, **kwargs): # pylint: disable=redefined-outer-name data = { 'columns': [ { 'title': 'Name' }, { 'title': 'IP Address' }, { 'title': 'Firmware Version' }, { 'title': 'Ports' }, { 'title': 'Serial Number' }, { 'title': 'Manufacturer' }, { 'title': 'Location' }, ], 'data': [] } # HTTP request headers = {"Accept": "application/json"} cmdb_url = cmdb_config.servicenow_url + '/api/now/table/cmdb_ci_ip_router' response = requests.get(cmdb_url, auth=(cmdb_config.servicenow_username, get_password_from_file( cmdb_config.servicenow_secret_file)), headers=headers, verify=False) # Check for HTTP codes other than 200 if response.status_code == 200: routers = response.json() for router in routers['result']: router_name = router['name'] router_ip = router['ip_address'] router_serial = router['serial_number'] firmware_version = router['firmware_version'] ports = router['ports'] if router['manufacturer']: router_manufacturer = get_router_manufacturer( router['manufacturer']['link']) if router['location']: router_location = get_router_location( router['location']['link']) data['data'].append([ router_name, router_ip, firmware_version, ports, router_serial, router_manufacturer, router_location ]) return data
def approve_ticket(ticket, absolute_name, address): ip4_address_list = [address] try: view = g.user.get_api().get_configuration( config.default_configuration).get_view(config.default_view) except PortalException as e: g.user.logger.error('%s' % e, msg_type=g.user.logger.EXCEPTION) flash('Unable to retrieve configuration or view defined in settings') return redirect( url_for( 'approve_snow_ticket_create_hostapprove_snow_ticket_create_host_approve_snow_ticket_create_host_page' )) # Adding the ticket number to the comments props = {'comments': 'ServiceNow Ticket number: %s' % ticket} # Add host Record try: ip4_address = g.user.get_api().get_configuration( config.default_configuration).get_ip4_address(address) ip4_address.change_state_ip4_address( IPAssignmentActionValues.MAKE_STATIC, '') host_record = view.add_host_record(absolute_name, ip4_address_list, -1, props) except BAMException as e: g.user.logger.error('%s' % e, msg_type=g.user.logger.EXCEPTION) flash('Unable to add host record with error %s' % util.safe_str(e)) return redirect( url_for( 'manage_sservice_requestsmanage_service_requests_manage_service_requests_page' )) # Setup the date format resolved_date = datetime.now() + timedelta(minutes=6) ticket_information = { 'close_code': 'Successful', 'close_notes': 'Approved', 'state': '3', 'assignment_group': 'Team Development Code Reviewers', 'resolved_date': resolved_date.strftime("%Y-%m-%d %H:%M") } # get the sys_id of the ticket response = requests.get( service_requests_config.servicenow_url + '?sysparm_query=number=' + ticket, auth=(service_requests_config.servicenow_username, get_password_from_file( service_requests_config.servicenow_secret_file)), headers=headers, verify=False) if response.status_code == 200: returned_values = response.json() for r in returned_values['result']: ticket_sys_id = r['sys_id'] requests.put(service_requests_config.servicenow_url + '/' + ticket_sys_id, auth=(service_requests_config.servicenow_username, get_password_from_file( service_requests_config.servicenow_secret_file)), headers=headers, data=json.dumps(ticket_information), verify=False) flash( 'Success - Change Request: ' + ticket + ' Closed and Host: ' + host_record.get_name() + ' created', 'succeed') return redirect( url_for( 'manage_service_requestsmanage_service_requests_manage_service_requests_page' ))
def raw_table_data(*args, **kwargs): # pylint: disable=redefined-outer-name data = { 'columns': [{ 'title': 'Name' }, { 'title': 'IP Address' }, { 'title': 'Serial Number' }, { 'title': 'Manufacturer' }, { 'title': 'Device Class' }, { 'title': 'Partition vlans?' }, { 'title': 'Warranty Expiration' }, { 'title': 'Asset Tag' }, { 'title': 'Install Status' }], 'data': [] } # HTTP request headers = {"Accept": "application/json"} cmdb_url = cmdb_config.servicenow_url + '/api/now/table/cmdb_ci_netgear?sysparm_limit=' + cmdb_config.servicenow_max_query_results response = requests.get(cmdb_url, auth=(cmdb_config.servicenow_username, get_password_from_file( cmdb_config.servicenow_secret_file)), headers=headers, verify=False) # Get a dict of all manufactures manu_dict = {} manu_dict = get_all_core_company_manufactures() # Check for HTTP codes other than 200 if response.status_code == 200: net_gears = response.json() for net_gear in net_gears['result']: gear_name = net_gear['name'] gear_ip = net_gear['ip_address'] gear_serial = net_gear['serial_number'] device_type = net_gear['device_type'] can_partitionvlans = net_gear['can_partitionvlans'] warranty_expiration = net_gear['warranty_expiration'] asset_tag = net_gear['asset_tag'] if net_gear['install_status'] == '114': install_status = 'Active' elif net_gear['install_status'] == '6': install_status = 'In Stock' elif net_gear['install_status'] == '7': install_status = 'Retired' elif net_gear['install_status'] == '106': install_status = 'Lab' elif net_gear['install_status'] == '101': install_status = 'Active' else: install_status = net_gear['install_status'] if net_gear['manufacturer']: gear_manufacturer = manu_dict[net_gear['manufacturer'] ['value']] if gear_manufacturer == 'Palo Alto': device_class = 'Firewall' elif gear_manufacturer == 'Silver Peak': device_class = 'SDWAN' elif "rtr" in gear_name == 'True': device_class = 'Router' elif "RTR" in gear_name == 'True': device_class = 'Router' elif "gw" in gear_name == 'True': device_class = 'Router' elif "GW" in gear_name == 'True': device_class = 'Router' else: device_class = net_gear['device_type'] data['data'].append([ gear_name, gear_ip, gear_serial, gear_manufacturer, device_class, can_partitionvlans, warranty_expiration, asset_tag, install_status ]) return data
def raw_table_data(*args, **kwargs): # pylint: disable=redefined-outer-name data = { 'columns': [ { 'title': 'Name' }, { 'title': 'Manufacturer' }, { 'title': 'IP Address' }, { 'title': 'Serial Number' }, { 'title': 'MAC Address' }, { 'title': 'Warranty Expiration' }, { 'title': 'Assets Tag' }, { 'title': 'OS' }, { 'title': 'OS Version' }, { 'title': 'Serial Number' }, { 'title': 'Purchase Date' }, ], 'data': [] } # HTTP request headers = {"Accept": "application/json"} cmdb_url = cmdb_config.servicenow_url + '/api/now/table/cmdb_ci_computer' response = requests.get(cmdb_url, auth=(cmdb_config.servicenow_username, get_password_from_file( cmdb_config.servicenow_secret_file)), headers=headers, verify=False) manu_dict = {} manu_dict = get_all_core_company_manufactures() # Check for HTTP codes other than 200 if response.status_code == 200: computers = response.json() for computer in computers['result']: computer_name = computer['name'] computer_ip = computer['ip_address'] computer_serial = computer['serial_number'] computer_purchase_date = computer['purchase_date'] computer_mac = computer['mac_address'] warranty_expiration = computer['warranty_expiration'] asset_tag = computer['asset_tag'] os = computer['os'] os_version = computer['os_version'] serial_number = computer['serial_number'] if computer['manufacturer']: # Decode the value of the computer_manufacturer computer_manufacturer = manu_dict[computer['manufacturer'] ['value']] data['data'].append([ computer_name, computer_manufacturer, computer_ip, computer_serial, computer_mac, warranty_expiration, asset_tag, os, os_version, serial_number, computer_purchase_date ]) return data