def restoresources(self, CollectorListWidget, SourceListWidget, url, id, key): destinationcollectors = CollectorListWidget.selectedItems() if len(destinationcollectors) == 1: destinationcollectorqstring = destinationcollectors[0].text() destinationcollector = str(destinationcollectorqstring) destinationcollectorid = self.getcollectorid( destinationcollector, url, id, key) filter = "JSON (*.json)" restorefile, status = QtWidgets.QFileDialog.getOpenFileName( self, "Open file(s)...", os.getcwd(), filter) sources = None try: with open(restorefile) as data_file: sources = json.load(data_file) except Exception as e: self.mainwindow.errorbox('Failed to load JSON file.') logger.exception(e) # a sources save file from the UI looks a little different than a save file from this tool, fix it here if sources: if 'sources' in sources: sources = sources['sources'] dialog = restoreSourcesDialog(sources) dialog.exec() dialog.show() if str(dialog.result()) == '1': selectedsources = dialog.getresults() else: return if len(selectedsources) > 0: sumo = SumoLogic(id, key, endpoint=url, log_level=self.mainwindow.log_level) for selectedsource in selectedsources: for sumosource in sources: if ('name' in sumosource and sumosource['name'] == str(selectedsource)) or ( 'config' in sumosource and sumosource['config']['name'] == str(selectedsource)): if 'id' in sumosource: del sumosource['id'] if 'alive' in sumosource: del sumosource['alive'] template = {} template['source'] = sumosource sumo.create_source(destinationcollectorid, template) self.update_source_list(CollectorListWidget, SourceListWidget, url, id, key) else: self.mainwindow.errorbox('No sources selected for import.') else: self.mainwindow.errorbox( 'Please select 1 and only 1 collector to restore sources to.') return
def copysources(self, CollectorListWidgetFrom, CollectorListWidgetTo, SourceListWidgetFrom, SourceListWidgetTo, fromurl, fromid, fromkey, tourl, toid, tokey): logger.info("Copying Sources") try: fromsumo = SumoLogic(fromid, fromkey, endpoint=fromurl) sourcecollectorlist = CollectorListWidgetFrom.selectedItems( ) # get the selected source collector if len( sourcecollectorlist ) == 1: # make sure there is a collector selected, otherwise bail sourcecollector = sourcecollectorlist[0].text( ) # qstring to string conversion sourcecollectorid = self.getcollectorid( sourcecollector, fromurl, fromid, fromkey) destinationcollectorlist = CollectorListWidgetTo.selectedItems( ) # get the selected dest collector if len( destinationcollectorlist ) > 0: # make sure there is a collector selected, otherwise bail fromsources = SourceListWidgetFrom.selectedItems( ) # get the selected sources if len(fromsources ) > 0: # make sure at least one source is selected fromsourcelist = [] for fromsource in fromsources: # iterate through source names to build a warning message fromsourcelist.append(fromsource.text()) else: self.mainwindow.errorbox('No Sources Selected.') return destinationcollectorstring = '' for destinationcollector in destinationcollectorlist: destinationcollectorstring = destinationcollectorstring + destinationcollector.text( ) + ", " message = "You are about to copy the following sources from collector \"" + sourcecollector + "\" to \"" + destinationcollectorstring + "\". Is this correct? \n\n" for source in fromsourcelist: message = message + source + "\n" result = QtWidgets.QMessageBox.question( self, 'Really Copy?', message, QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No, QtWidgets.QMessageBox.No) # bring up the copy dialog if result: # If they clicked "OK" rather than cancel tosumo = SumoLogic(toid, tokey, endpoint=tourl) for destinationcollector in destinationcollectorlist: destinationcollectorname = destinationcollector.text( ) destinationcollectorid = self.getcollectorid( destinationcollectorname, tourl, toid, tokey) # qstring to string conversion sumosources = fromsumo.sources(sourcecollectorid) for source in fromsourcelist: # iterate through the selected sources and copy them for sumosource in sumosources: if sumosource['name'] == source: if 'id' in sumosource: # the API creates an ID so this must be deleted before sending del sumosource['id'] if 'alive' in sumosource: del sumosource[ 'alive'] # the API sets this itself so this must be deleted before sending template = {} template[ 'source'] = sumosource # the API expects a dict with a key called 'source' notduplicate = True sumotosourcelist = tosumo.sources( destinationcollectorid) for sumotosource in sumotosourcelist: if sumotosource[ 'name'] == source: # make sure the source doesn't already exist in the destination notduplicate = False if notduplicate: # finally lets copy this thing tosumo.create_source( destinationcollectorid, template) else: self.mainwindow.errorbox( source + ' already exists, skipping.') # call the update method for the dest collector since they have changed after the copy if len(destinationcollectorlist) > 1: self.mainwindow.infobox( "Copy Complete. Please select an individual destination collector to see an updated source list." ) else: self.updatesourcelist(CollectorListWidgetTo, SourceListWidgetTo, tourl, toid, tokey) else: self.mainwindow.errorbox( 'You Must Select at Least 1 target.') else: self.mainwindow.errorbox('No Source Collector Selected.') except Exception as e: self.mainwindow.errorbox( 'Encountered a bug. Check the console output.') logger.exception(e) return