code: def import_datasource(  # pylint: disable=too-many-arguments
      session: Session,
      i_datasource: Model,
      lookup_database: Callable[[Model], Optional[Model]],
      lookup_datasource: Callable[[Model], Optional[Model]],
      import_time: Optional[int] = None,
      database_id: Optional[int] = None,
  ) -> int:
      """Imports the datasource from the object to the database.
  
      Metrics and columns and datasource will be overridden if exists.
      This function can be used to import/export datasources between multiple
      superset instances. Audit metadata isn't copies over.
      """
      make_transient(i_datasource)
      logger.info(
          "Started import of the datasource: %s", i_datasource.to_json()
      )
  
      i_datasource.id = None
      i_datasource.database_id = (
          database_id
          if database_id
          else getattr(lookup_database(i_datasource), "id", None)
      )
      i_datasource.alter_params(import_time=import_time)
  
      # override the datasource
      datasource = lookup_datasource(i_datasource)
  
      if datasource:
          datasource.override(i_datasource)
          session.flush()
      else:
          datasource = i_datasource.copy()
          session.add(datasource)
          session.flush()
  
      for metric in i_datasource.metrics:
          new_m = metric.copy()
          new_m.table_id = datasource.id
          logger.info(
              "Importing metric %s from the datasource: %s",
              new_m.to_json(),
              i_datasource.full_name,
          )
          imported_m = import_metric(session, new_m)
          if imported_m.metric_name not in [
              m.metric_name for m in datasource.metrics
          ]:
              datasource.metrics.append(imported_m)
  
      for column in i_datasource.columns:
          new_c = column.copy()
          new_c.table_id = datasource.id
          logger.info(
              "Importing column %s from the datasource: %s",
              new_c.to_json(),
              i_datasource.full_name,
          )
          imported_c = import_column(session, new_c)
          if imported_c.column_name not in [
              c.column_name for c in datasource.columns
          ]:
              datasource.columns.append(imported_c)
      session.flush()
      return datasource.id
  
imports: import json
  import logging
  from typing import Any, Callable, Optional
  import yaml
  from flask_appbuilder import Model
  from sqlalchemy.orm import Session
  from sqlalchemy.orm.session import make_transient
  from superset import db
  from superset.commands.base import BaseCommand
  from superset.commands.database.exceptions import DatabaseNotFoundError
  from superset.commands.dataset.exceptions import DatasetInvalidError
  from superset.commands.importers.exceptions import IncorrectVersionError
  from superset.connectors.sqla.models import BaseDatasource, SqlaTable, SqlMetric, TableColumn
  from superset.models.core import Database
  from superset.utils.dict_import_export import DATABASES_KEY
label_desc: Use image node_id 8 for calling a global function with example usage: import_datasource(session, i_datasource, lookup_database, lookup_datasource, import_time, database_id) and returns: datasource