33
44import functools
55from pathlib import Path
6- from typing import Any , List , Optional , Tuple
6+ from typing import Any , List , Optional , Tuple , Union
77
88from gooddata_api_client .exceptions import NotFoundException
99
2929from gooddata_sdk .catalog .entity import TokenCredentialsFromFile
3030from gooddata_sdk .catalog .workspace .declarative_model .workspace .logical_model .ldm import CatalogDeclarativeModel
3131from gooddata_sdk .client import GoodDataApiClient
32- from gooddata_sdk .utils import load_all_entities_dict , read_layout_from_file
32+ from gooddata_sdk .utils import get_ds_credentials , load_all_entities_dict , read_layout_from_file
3333
3434_PDM_DEPRECATION_MSG = "This method is going to be deprecated due to PDM removal."
3535
@@ -173,6 +173,7 @@ def put_declarative_data_sources(
173173 self ,
174174 declarative_data_sources : CatalogDeclarativeDataSources ,
175175 credentials_path : Optional [Path ] = None ,
176+ config_file : Optional [Union [str , Path ]] = None ,
176177 test_data_sources : bool = False ,
177178 ) -> None :
178179 """Set all data sources, including their related physical data model.
@@ -182,16 +183,18 @@ def put_declarative_data_sources(
182183 Declarative Data Source object. Can be retrieved by get_declarative_data_sources.
183184 credentials_path (Optional[Path], optional):
184185 Path to the Credentials. Optional, defaults to None.
186+ config_file (Optional[Union[str, Path]], optional):
187+ Path to the config file. Defaults to None.
185188 test_data_sources (bool, optional):
186189 If True, the connection of data sources is tested. Defaults to False.
187190
188191 Returns:
189192 None
190193 """
191194 if test_data_sources :
192- self .test_data_sources_connection (declarative_data_sources , credentials_path )
195+ self .test_data_sources_connection (declarative_data_sources , credentials_path , config_file )
193196 credentials = self ._credentials_from_file (credentials_path ) if credentials_path is not None else dict ()
194- self ._layout_api .put_data_sources_layout (declarative_data_sources .to_api (credentials ))
197+ self ._layout_api .put_data_sources_layout (declarative_data_sources .to_api (credentials , config_file ))
195198
196199 def store_declarative_data_sources (self , layout_root_path : Path = Path .cwd ()) -> None :
197200 """Store data sources layouts in a directory hierarchy.
@@ -236,6 +239,7 @@ def load_and_put_declarative_data_sources(
236239 self ,
237240 layout_root_path : Path = Path .cwd (),
238241 credentials_path : Optional [Path ] = None ,
242+ config_file : Optional [Union [str , Path ]] = None ,
239243 test_data_sources : bool = False ,
240244 ) -> None :
241245 """Loads and sets layouts stored using `store_declarative_data_sources`.
@@ -247,15 +251,17 @@ def load_and_put_declarative_data_sources(
247251 layout_root_path (Optional[Path], optional):
248252 Path to the root of the layout directory. Defaults to Path.cwd().
249253 credentials_path (Optional[Path], optional):
250- Path to the credentials. Defaults to Path.cwd().
254+ Path to the credentials.
255+ config_file (Optional[Union[str, Path]], optional):
256+ Path to the config file.
251257 test_data_sources (bool, optional):
252258 If True, the connection of data sources is tested. Defaults to False.
253259
254260 Returns:
255261 None
256262 """
257263 data_sources = self .load_declarative_data_sources (layout_root_path )
258- self .put_declarative_data_sources (data_sources , credentials_path , test_data_sources )
264+ self .put_declarative_data_sources (data_sources , credentials_path , config_file , test_data_sources )
259265
260266 @staticmethod
261267 def store_pdm_to_disk (pdm : CatalogDeclarativeTables , path : Path = Path .cwd ()) -> None :
@@ -438,7 +444,10 @@ def scan_sql(self, data_source_id: str, sql_request: ScanSqlRequest) -> ScanSqlR
438444 return ScanSqlResponse .from_api (self ._actions_api .scan_sql (data_source_id , sql_request .to_api ()))
439445
440446 def test_data_sources_connection (
441- self , declarative_data_sources : CatalogDeclarativeDataSources , credentials_path : Optional [Path ] = None
447+ self ,
448+ declarative_data_sources : CatalogDeclarativeDataSources ,
449+ credentials_path : Optional [Path ] = None ,
450+ config_file : Optional [Union [str , Path ]] = None ,
442451 ) -> None :
443452 """Tests connection to declarative data sources.
444453
@@ -453,6 +462,8 @@ def test_data_sources_connection(
453462 Declarative Data Sources object
454463 credentials_path (Optional[Path], optional):
455464 Path to the credentials. Defaults to None.
465+ config_file (Optional[Union[str, Path]], optional):
466+ Path to the config file. Defaults to None.
456467
457468 Raises:
458469 ValueError:
@@ -461,7 +472,15 @@ def test_data_sources_connection(
461472 Returns:
462473 None
463474 """
464- credentials = self ._credentials_from_file (credentials_path ) if credentials_path is not None else dict ()
475+
476+ credentials = dict ()
477+ if credentials_path is not None and config_file is not None :
478+ raise ValueError ("Only one of credentials or config_file should be provided" )
479+ if credentials_path is not None :
480+ credentials = self ._credentials_from_file (credentials_path )
481+ if config_file is not None :
482+ credentials = get_ds_credentials (config_file )
483+
465484 errors : dict [str , str ] = dict ()
466485 for declarative_data_source in declarative_data_sources .data_sources :
467486 if credentials .get (declarative_data_source .id ) is not None :
0 commit comments