def find(self, *args, **kwargs): #if it's a single argument and a string, convert it to an objectid if len(args) == 1 and isinstance(args[0], basestring): args = ({u'_id': ObjectId(args[0])},) result = Collection.find(self, *args, **kwargs) return CSCursor(result)
def review_diff(self, new_list: List[JsonType], old_mongo_collection: pymongo_collection.Collection, has_error: bool) -> bool: """Review the difference between an old and new dataset.""" if len(new_list) > 1000: return True old_list = list(old_mongo_collection.find()) diff = _compute_diff(old_list, new_list) if not diff: self._print_in_report('The data is already up to date.') return not has_error if not has_error and self.flag_values.always_accept_diff: return True self._print(json.dumps(diff, indent=2, ensure_ascii=False)) if has_error: return True while True: # Needed as input() prompts to stderr and stderr might be captured, # e.g. by import_status subprocess.run(...). self._print('Do you approve this diff? Y/N ') answer = input('Do you approve this diff? Y/N ').upper() if answer == 'Y': return True if answer == 'N': return False
def _print_diff( self, new_list: list[JsonType], old_mongo_collection: pymongo_collection.Collection ) -> Tuple[bool, bool]: """Print the difference between an old and new dataset. Returns: whether there is a diff to approve, and whether there is a diff to import. """ new_list_len = len(new_list) if new_list_len > 1000: self._print_in_report( f'Too many entries to diff ({new_list_len}).') return False, True old_list = list(old_mongo_collection.find()) diff = _compute_diff(old_list, new_list) if not diff: self._print_in_report('The data is already up to date.') return False, False if not self.flag_values.always_accept_diff: self._print(json.dumps(diff, indent=2, ensure_ascii=False)) return True, True
def GetChangesByName(self, strName): dResult = dict() try: oChangesCollection = PymongoCollection(self.m_oDatabaseMongodb, "changes", False) dResult = oChangesCollection.find({'name': strName}, {'active':1, 'passive':1, '_id':0})[0] except Exception, exc: strErrorMsg = '%s.%s Error: %s - Line: %s' % (self.__class__.__name__, str(exc), stack()[0][3], sys.exc_traceback.tb_lineno) # give a error message Utilities.WriteErrorLog(strErrorMsg, self.m_oConfig)
def SwitchChangesActive(self, strName, strActive, strPassive): bResult = False; try: oChangesCollection = PymongoCollection(self.m_oDatabaseMongodb, "changes", False) if oChangesCollection.find({'name': strName}).count() == 1: oChangesCollection.update({ 'name': strName }, { '$set': {'active': strPassive, 'passive': strActive} }) bResult = True except Exception, exc: strErrorMsg = '%s.%s Error: %s - Line: %s' % (self.__class__.__name__, str(exc), stack()[0][3], sys.exc_traceback.tb_lineno) # give a error message Utilities.WriteErrorLog(strErrorMsg, self.m_oConfig)
def get_vm_by_vm_name_in_range(orch_vm_collection: collection.Collection, names: List, last_seen: datetime.datetime): """ Checks whether an agent is installed on which one of the VMs with the given names. example of last_seen = datetime.datetime(2018, 10, 1, 00, 00, 00) :param orch_vm_collection: orchestration_vm collection :param names: List of names of VMs to check :param last_seen: Datetime object representing the earliest time for the checked vm to be last seen :return: List of Cursors to records in the orchestration_vm collection that match the given criteria """ vm_agent = [] for name in names: res = orch_vm_collection.find( {"name": name, "last_seen": {"$gt": last_seen}}) vm_agent.append(res) return vm_agent
def UpdatePhysicalServerInterface(self): try: dServerInfo = dict() # Connect to CMDBv2 if self.IsConnectedToCMDBv2() is False: return 0 oServerCollection = PymongoCollection(self.m_oDatabaseMongodb, CLT_SERVER, False) if oServerCollection is None: strErrorMsg = "Cannot connect to %s " % self.m_oConfig.CMDBv2Host Utilities.WriteErrorLog(strErrorMsg, self.m_oConfig) return 0 dServerInfo = self.GetPhysicalServerInterfaceFromMDR() if len(dServerInfo) > 0: for strServerKey, dInterfaceInfo in dServerInfo.items(): oDataResult = oServerCollection.find({"code": strServerKey}) if Utilities.CheckExistence(oDataResult) is not False: oServerCollection.update( {"code": strServerKey}, { "$set": { "private_interface": dInterfaceInfo["private"], "public_interface": dInterfaceInfo["public"], } }, ) except Exception, exc: strErrorMsg = "%s.%s Error: %s - Line: %s" % ( self.__class__.__name__, str(exc), stack()[0][3], sys.exc_traceback.tb_lineno, ) # give a error message Utilities.WriteErrorLog(strErrorMsg, self.m_oConfig)
def find_one(self, *args, **kwargs): return _Collection.find(self, *args, **kwargs)
def loadStrategy(col: collection.Collection): records = list(col.find({"strategyId": {"$exists": 1}}, projection={"strategyId": 1, "_id": 0})) return [record["strategyId"] for record in records]