def load_policy( neo4j_session: neo4j.Session, policy_id: str, policy_name: str, policy_type: str, principal_arn: str, aws_update_tag: int, ) -> None: neo4j_session.write_transaction(_load_policy_tx, policy_id, policy_name, policy_type, principal_arn, aws_update_tag)
def run(self, session: neo4j.Session) -> None: """ Run the statement. This will execute the query against the graph. """ if self.iterative: self._run_iterative(session) else: session.write_transaction(self._run_noniterative).consume() logger.info( f"Completed {self.parent_job_name} statement #{self.parent_job_sequence_num}" )
def load_ec2_instance_ebs_volumes( neo4j_session: neo4j.Session, ebs_data: List[Dict[str, Any]], current_aws_account_id: str, update_tag: int, ) -> None: neo4j_session.write_transaction( _load_ec2_instance_ebs_tx, ebs_data, update_tag, current_aws_account_id, )
def load_tags( neo4j_session: neo4j.Session, tag_data: Dict, resource_type: str, region: str, current_aws_account_id: str, aws_update_tag: int, ) -> None: for tag_data_batch in batch(tag_data, size=100): neo4j_session.write_transaction( _load_tags_tx, tag_data=tag_data_batch, resource_type=resource_type, region=region, current_aws_account_id=current_aws_account_id, aws_update_tag=aws_update_tag, )
def execute(self, neo4j_session: Session): """Execute the generated neo4j query Arguments: neo4j_session {neo4j.Session} -- The neo4j Session object used for querying the database. """ if self.query == "": raise ValueError( "The query must be generated before this method is called.") try: return neo4j_session.write_transaction( lambda tx: tx.run(self.query)) except: print("Query not defined error") return None
def _run_iterative(self, session: neo4j.Session) -> None: """ Iterative statement execution. Expects the query to return the total number of records updated. """ self.parameters["LIMIT_SIZE"] = self.iterationsize while True: result: neo4j.StatementResult = session.write_transaction( self._run_noniterative) # Exit if we have finished processing all items if not result.summary().counters.contains_updates: # Ensure network buffers are cleared result.consume() break result.consume()
def load_ec2_instance_network_interfaces(neo4j_session: neo4j.Session, instance_data: Dict, update_tag: int) -> None: neo4j_session.write_transaction(_load_ec2_instance_net_if_tx, instance_data, update_tag)
def load_ec2_instances( neo4j_session: neo4j.Session, data: List[Dict], region: str, current_aws_account_id: str, update_tag: int, ) -> None: for reservation in data: reservation_id = reservation["ReservationId"] neo4j_session.write_transaction( _load_ec2_reservation_tx, reservation_id, reservation, current_aws_account_id, region, update_tag, ) for instance in reservation["Instances"]: instanceid = instance["InstanceId"] monitoring_state = instance.get("Monitoring", {}).get("State") instance_state = instance.get("State", {}).get("Name") # NOTE this is a hack because we're using a version of Neo4j that doesn't support temporal data types launch_time = instance.get("LaunchTime") if launch_time: launch_time_unix = str(time.mktime(launch_time.timetuple())) else: launch_time_unix = "" neo4j_session.write_transaction( _load_ec2_instance_tx, instanceid, instance, reservation_id, monitoring_state, launch_time, launch_time_unix, instance_state, current_aws_account_id, region, update_tag, ) # SubnetId can return None intermittently so attach only if non-None. subnet_id = instance.get('SubnetId') if subnet_id: neo4j_session.write_transaction(_load_ec2_subnet_tx, instanceid, subnet_id, region, update_tag) if instance.get("KeyName"): key_name = instance["KeyName"] key_pair_arn = f'arn:aws:ec2:{region}:{current_aws_account_id}:key-pair/{key_name}' neo4j_session.write_transaction( _load_ec2_keypairs_tx, key_pair_arn, key_name, region, instanceid, current_aws_account_id, update_tag, ) if instance.get("SecurityGroups"): for group in instance["SecurityGroups"]: group_id = group["GroupId"] neo4j_session.write_transaction( _load_ec2_security_groups_tx, group_id, group, instanceid, region, current_aws_account_id, update_tag, ) load_ec2_instance_network_interfaces(neo4j_session, instance, update_tag) instance_ebs_volumes_list = get_ec2_instance_ebs_volumes(instance) load_ec2_instance_ebs_volumes(neo4j_session, instance_ebs_volumes_list, current_aws_account_id, update_tag)
def load_ecr_repository_images( neo4j_session: neo4j.Session, repo_images_list: List[Dict], region: str, aws_update_tag: int, ) -> None: logger.info(f"Loading {len(repo_images_list)} ECR repository images in {region} into graph.") neo4j_session.write_transaction(_load_ecr_repo_img_tx, repo_images_list, aws_update_tag, region)