aws_info_helper/ec2.py
def update_collection(self):
        """Update the rh.Collection object if redis-helper installed"""
        if self._collection is None:
            return

        ids_for_profile = set([
            x.get(self._collection._unique_field)
            for x in self._collection.find(
                'profile:{}'.format(self._profile),
                include_meta=False,
                get_fields=self._collection._unique_field,
                limit=self._collection.size
            )
        ])
        ids = set()
        updates = []
        deletes = []
        ip_hash_ids_to_delete = []

        for instance in self.get_all_instances_filtered_data():
            data = ih.cast_keys(instance, **INSTANCE_KEY_VALUE_CASTING)
            data = ih.rename_keys(data, **INSTANCE_KEY_NAME_MAPPING)
            data.update(dict(profile=self._profile))
            old_data = self._collection[data['id']]
            ids.add(data['id'])
            if not old_data:
                updates.append(self._collection.add(**data))
                if data['ip']:
                  updates.append(ah.AWS_IP.add(
                      ip=data['ip'],
                      instance=data['id'],
                      source='ec2',
                      profile=self._profile
                  ))
            else:
                hash_id = old_data['_id']
                try:
                    instance_id = data.pop(self._collection._unique_field)
                except KeyError:
                    pass
                updates.extend(self._collection.update(hash_id, **data))
                if data['ip'] != old_data['ip']:
                    ip_hash_ids_to_delete.extend(ah.AWS_IP.find(
                        'ip:{}, instance:{}'.format(old_data['ip'], old_data['id']),
                        item_format='{_id}'
                    ))
                if data['ip']:
                    existing = ah.AWS_IP.find(
                        'ip:{}, instance:{}'.format(data['ip'], instance_id),
                        include_meta=False
                    )
                    if not existing:
                        updates.append(ah.AWS_IP.add(
                            ip=data['ip'],
                            instance=instance_id,
                            source='ec2',
                            profile=self._profile
                        ))

        for instance_id in ids_for_profile - ids:
            hash_id = self._collection.get_hash_id_for_unique_value(instance_id)
            if hash_id is not None:
                old_data = self._collection.get(hash_id, 'id, ip')
                ip_hash_ids_to_delete.extend(ah.AWS_IP.find(
                    'ip:{}, instance:{}'.format(old_data['ip'], old_data['id']),
                    item_format='{_id}'
                ))
                self._collection.delete(hash_id)
                deletes.append(hash_id)

        if ip_hash_ids_to_delete:
            ah.AWS_IP.delete_many(*ip_hash_ids_to_delete)
            deletes.extend(ip_hash_ids_to_delete)

        for address in self.get_elastic_addresses_filtered_data():
            data = ih.rename_keys(address, **ADDRESS_KEY_NAME_MAPPING)
            existing = ah.AWS_IP.find(
                'ip:{}, source:eip'.format(data['ip']),
                include_meta=False
            )
            if not existing and data['instance']:
                updates.append(ah.AWS_IP.add(
                    ip=data['ip'],
                    instance=data['instance'],
                    source='eip',
                    profile=self._profile
                ))

        return {'updates': updates, 'deletes': deletes}
Update the rh.Collection object if redis-helper installed
Update the rh.Collection object if redis-helper installed
Update the rh . Collection object if redis - helper installed
def update_collection(self):
        if self._collection is None:
            return
        ids_for_profile = set([
            x.get(self._collection._unique_field)
            for x in self._collection.find(
                'profile:{}'.format(self._profile),
                include_meta=False,
                get_fields=self._collection._unique_field,
                limit=self._collection.size
            )
        ])
        ids = set()
        updates = []
        deletes = []
        ip_hash_ids_to_delete = []
        for instance in self.get_all_instances_filtered_data():
            data = ih.cast_keys(instance, **INSTANCE_KEY_VALUE_CASTING)
            data = ih.rename_keys(data, **INSTANCE_KEY_NAME_MAPPING)
            data.update(dict(profile=self._profile))
            old_data = self._collection[data['id']]
            ids.add(data['id'])
            if not old_data:
                updates.append(self._collection.add(**data))
                if data['ip']:
                  updates.append(ah.AWS_IP.add(
                      ip=data['ip'],
                      instance=data['id'],
                      source='ec2',
                      profile=self._profile
                  ))
            else:
                hash_id = old_data['_id']
                try:
                    instance_id = data.pop(self._collection._unique_field)
                except KeyError:
                    pass
                updates.extend(self._collection.update(hash_id, **data))
                if data['ip'] != old_data['ip']:
                    ip_hash_ids_to_delete.extend(ah.AWS_IP.find(
                        'ip:{}, instance:{}'.format(old_data['ip'], old_data['id']),
                        item_format='{_id}'
                    ))
                if data['ip']:
                    existing = ah.AWS_IP.find(
                        'ip:{}, instance:{}'.format(data['ip'], instance_id),
                        include_meta=False
                    )
                    if not existing:
                        updates.append(ah.AWS_IP.add(
                            ip=data['ip'],
                            instance=instance_id,
                            source='ec2',
                            profile=self._profile
                        ))
        for instance_id in ids_for_profile - ids:
            hash_id = self._collection.get_hash_id_for_unique_value(instance_id)
            if hash_id is not None:
                old_data = self._collection.get(hash_id, 'id, ip')
                ip_hash_ids_to_delete.extend(ah.AWS_IP.find(
                    'ip:{}, instance:{}'.format(old_data['ip'], old_data['id']),
                    item_format='{_id}'
                ))
                self._collection.delete(hash_id)
                deletes.append(hash_id)
        if ip_hash_ids_to_delete:
            ah.AWS_IP.delete_many(*ip_hash_ids_to_delete)
            deletes.extend(ip_hash_ids_to_delete)
        for address in self.get_elastic_addresses_filtered_data():
            data = ih.rename_keys(address, **ADDRESS_KEY_NAME_MAPPING)
            existing = ah.AWS_IP.find(
                'ip:{}, source:eip'.format(data['ip']),
                include_meta=False
            )
            if not existing and data['instance']:
                updates.append(ah.AWS_IP.add(
                    ip=data['ip'],
                    instance=data['instance'],
                    source='eip',
                    profile=self._profile
                ))
        return {'updates': updates, 'deletes': deletes}
def update_collection ( self ) : if self . _collection is None : return ids_for_profile = set ( [ x . get ( self . _collection . _unique_field ) for x in self . _collection . find ( 'profile:{}' . format ( self . _profile ) , include_meta = False , get_fields = self . _collection . _unique_field , limit = self . _collection . size ) ] ) ids = set ( ) updates = [ ] deletes = [ ] ip_hash_ids_to_delete = [ ] for instance in self . get_all_instances_filtered_data ( ) : data = ih . cast_keys ( instance , ** INSTANCE_KEY_VALUE_CASTING ) data = ih . rename_keys ( data , ** INSTANCE_KEY_NAME_MAPPING ) data . update ( dict ( profile = self . _profile ) ) old_data = self . _collection [ data [ 'id' ] ] ids . add ( data [ 'id' ] ) if not old_data : updates . append ( self . _collection . add ( ** data ) ) if data [ 'ip' ] : updates . append ( ah . AWS_IP . add ( ip = data [ 'ip' ] , instance = data [ 'id' ] , source = 'ec2' , profile = self . _profile ) ) else : hash_id = old_data [ '_id' ] try : instance_id = data . pop ( self . _collection . _unique_field ) except KeyError : pass updates . extend ( self . _collection . update ( hash_id , ** data ) ) if data [ 'ip' ] != old_data [ 'ip' ] : ip_hash_ids_to_delete . extend ( ah . AWS_IP . find ( 'ip:{}, instance:{}' . format ( old_data [ 'ip' ] , old_data [ 'id' ] ) , item_format = '{_id}' ) ) if data [ 'ip' ] : existing = ah . AWS_IP . find ( 'ip:{}, instance:{}' . format ( data [ 'ip' ] , instance_id ) , include_meta = False ) if not existing : updates . append ( ah . AWS_IP . add ( ip = data [ 'ip' ] , instance = instance_id , source = 'ec2' , profile = self . _profile ) ) for instance_id in ids_for_profile - ids : hash_id = self . _collection . get_hash_id_for_unique_value ( instance_id ) if hash_id is not None : old_data = self . _collection . get ( hash_id , 'id, ip' ) ip_hash_ids_to_delete . extend ( ah . AWS_IP . find ( 'ip:{}, instance:{}' . format ( old_data [ 'ip' ] , old_data [ 'id' ] ) , item_format = '{_id}' ) ) self . _collection . delete ( hash_id ) deletes . append ( hash_id ) if ip_hash_ids_to_delete : ah . AWS_IP . delete_many ( * ip_hash_ids_to_delete ) deletes . extend ( ip_hash_ids_to_delete ) for address in self . get_elastic_addresses_filtered_data ( ) : data = ih . rename_keys ( address , ** ADDRESS_KEY_NAME_MAPPING ) existing = ah . AWS_IP . find ( 'ip:{}, source:eip' . format ( data [ 'ip' ] ) , include_meta = False ) if not existing and data [ 'instance' ] : updates . append ( ah . AWS_IP . add ( ip = data [ 'ip' ] , instance = data [ 'instance' ] , source = 'eip' , profile = self . _profile ) ) return { 'updates' : updates , 'deletes' : deletes }
Update the rh.Collection object if redis-helper installed
Update the rh . Collection object if redis - helper installed
"""Update the rh.Collection object if redis-helper installed"""
self