def importCsvAsCreds(self, filename): """ This method imports csv file with credentials. Args: filename (str): path to csv file with credentials definitions **csv file must follow format(no headers):** **credname,username,password,path_to_key,key_password,use_password_or_key*,encrypted_yes_or_no,env_var_used_to_encrypt** *valid options: password | key Examples: format: >>> rootVM,root,MyPass,/path/to/key,key_password,password,no,GC_KEY execution: >>> importCsvAsCreds("creds/creds.csv") """ #todo strip whitespace self.gLogging.debug("importCsvAsCreds invoked") MyCred = Query() try: with open(filename, 'r') as infile: for line in infile: if len(line) > 4: newone = {"credname": line.split(",")[0], "username": line.split(",")[1], "password": line.split(",")[2], "key": line.split(",")[3], "key_password": line.split(",")[4], "use": line.split(",")[5], "encrypted": line.split(",")[6], "secret_env_key": line.split(",")[7].strip("\n")} try: oldone = self.searchCredName(line.split(",")[0])[0] except Exception: oldone = None self.credtable.upsert({"credname": line.split(",")[0], "username": gutils.compare_dicts("username", newone, oldone), "password": gutils.compare_dicts("password", newone, oldone), "key": gutils.compare_dicts("key", newone, oldone), "key_password": gutils.compare_dicts("key_password", newone, oldone), "use": gutils.compare_dicts("use", newone, oldone), "encrypted": gutils.compare_dicts("encrypted", newone, oldone), "secret_env_key": gutils.compare_dicts("secret_env_key", newone, oldone)}, MyCred.credname == line.split(",")[0]) except Exception: self.gLogging.error("cannot open or invalid csv file: " + filename)
def importCsvAsHosts(self, filename): """ This method imports csv file with definitions of hosts. Args: filename (str): path to csv file with hosts definitions **csv file must follow format(no headers):** **hostname,ip/host,port,credential_name*,group** *credential_name - should correspond to defined credentials Examples: format: >>> myhost2,192.168.56.102,22,rootVM,VM execution: >>> importCsvAsHosts("hosts/hosts.csv") """ self.gLogging.debug("importCsvAsHosts invoked") MyHost = Query() try: with open(filename, 'r') as infile: for line in infile: if len(line) > 4: newone = { "group": line.split(",")[4].strip("\n"), "port": line.split(",")[2], "installations": [], "scanned": "no", "def_cred": line.split(",")[3], "host": line.split(",")[1], "hostname": line.split(",")[0] } try: oldone = self.searchHostName(line.split(",")[0])[0] except Exception: oldone = None self.hosttable.upsert( { "group": gutils.compare_dicts("group", newone, oldone), "group_uuid": gutils.uuid_generator( line.split(",")[4].strip("\n")), "group_checked": gutils.compare_checked( "group_checked", self.gConfig['JSON']['pick_yes'], oldone), "host_checked": gutils.compare_checked( "host_checked", self.gConfig['JSON']['pick_yes'], oldone), "port": gutils.compare_dicts("port", newone, oldone), "installations": gutils.compare_dicts("installations", newone, oldone), "scanned": gutils.compare_dicts("scanned", newone, oldone), "def_cred": gutils.compare_dicts("def_cred", newone, oldone), "host": gutils.compare_dicts("host", newone, oldone), "host_uuid": gutils.uuid_generator( line.split(",")[4].strip("\n") + line.split(",")[0]), "hostname": line.split(",")[0] }, MyHost.hostname == line.split(",")[0]) self._indexHosts() except Exception: self.gLogging.error("cannot open or invalid csv file: " + filename)