Beispiel #1
0
 def execute_with_ctx(cls, cmd: List[str]):
     """
     Executes command with context created by provide_aws_context.
     """
     executor = get_executor()
     with provide_aws_context():
         executor.execute_cmd(cmd=cmd)
 def execute_with_ctx(cls,
                      cmd: List[str],
                      key: str = GCP_GCS_KEY,
                      project_id=None,
                      scopes=None,
                      silent: bool = False):
     """
     Executes command with context created by provide_gcp_context and activated
     service key.
     """
     executor = get_executor()
     current_project_id = project_id or cls._project_id()
     with provide_gcp_context(key,
                              project_id=current_project_id,
                              scopes=scopes):
         executor.execute_cmd(cmd=cmd, silent=silent)
Beispiel #3
0
def provide_gcp_context(
    key_file_path: Optional[str] = None,
    scopes: Optional[Sequence] = None,
    project_id: Optional[str] = None,
):
    """
    Context manager that provides:

    - GCP credentials for application supporting `Application Default Credentials (ADC)
    strategy <https://cloud.google.com/docs/authentication/production>`__.
    - temporary value of :envvar:`AIRFLOW_CONN_GOOGLE_CLOUD_DEFAULT` variable
    - the ``gcloud`` config directory isolated from user configuration

    Moreover it resolves full path to service keys so user can pass ``myservice.json``
    as ``key_file_path``.

    :param key_file_path: Path to file with GCP credentials .json file.
    :type key_file_path: str
    :param scopes: OAuth scopes for the connection
    :type scopes: Sequence
    :param project_id: The id of GCP project for the connection.
        Default: ``os.environ["GCP_PROJECT_ID"]`` or None
    :type project_id: str
    """
    key_file_path = resolve_full_gcp_key_path(key_file_path)  # type: ignore
    if project_id is None:
        project_id = os.environ.get("GCP_PROJECT_ID")
    with provide_gcp_conn_and_credentials(
            key_file_path, scopes, project_id
    ), tempfile.TemporaryDirectory() as gcloud_config_tmp, mock.patch.dict(
            'os.environ', {CLOUD_SDK_CONFIG_DIR: gcloud_config_tmp}):
        executor = get_executor()

        if project_id:
            executor.execute_cmd(
                ["gcloud", "config", "set", "core/project", project_id])
        if key_file_path:
            executor.execute_cmd([
                "gcloud",
                "auth",
                "activate-service-account",
                f"--key-file={key_file_path}",
            ])
        yield
Beispiel #4
0
 def check_output(*args, **kwargs):
     executor = get_executor()
     return executor.check_output(*args, **kwargs)
Beispiel #5
0
 def execute_cmd(*args, **kwargs):
     executor = get_executor()
     return executor.execute_cmd(*args, **kwargs)