3
3
4
4
import functools
5
5
from pathlib import Path
6
- from typing import Any , List , Optional , Tuple
6
+ from typing import Any , List , Optional , Tuple , Union
7
7
8
8
from gooddata_api_client .exceptions import NotFoundException
9
9
29
29
from gooddata_sdk .catalog .entity import TokenCredentialsFromFile
30
30
from gooddata_sdk .catalog .workspace .declarative_model .workspace .logical_model .ldm import CatalogDeclarativeModel
31
31
from 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
33
33
34
34
_PDM_DEPRECATION_MSG = "This method is going to be deprecated due to PDM removal."
35
35
@@ -173,6 +173,7 @@ def put_declarative_data_sources(
173
173
self ,
174
174
declarative_data_sources : CatalogDeclarativeDataSources ,
175
175
credentials_path : Optional [Path ] = None ,
176
+ config_file : Optional [Union [str , Path ]] = None ,
176
177
test_data_sources : bool = False ,
177
178
) -> None :
178
179
"""Set all data sources, including their related physical data model.
@@ -182,16 +183,18 @@ def put_declarative_data_sources(
182
183
Declarative Data Source object. Can be retrieved by get_declarative_data_sources.
183
184
credentials_path (Optional[Path], optional):
184
185
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.
185
188
test_data_sources (bool, optional):
186
189
If True, the connection of data sources is tested. Defaults to False.
187
190
188
191
Returns:
189
192
None
190
193
"""
191
194
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 )
193
196
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 ))
195
198
196
199
def store_declarative_data_sources (self , layout_root_path : Path = Path .cwd ()) -> None :
197
200
"""Store data sources layouts in a directory hierarchy.
@@ -236,6 +239,7 @@ def load_and_put_declarative_data_sources(
236
239
self ,
237
240
layout_root_path : Path = Path .cwd (),
238
241
credentials_path : Optional [Path ] = None ,
242
+ config_file : Optional [Union [str , Path ]] = None ,
239
243
test_data_sources : bool = False ,
240
244
) -> None :
241
245
"""Loads and sets layouts stored using `store_declarative_data_sources`.
@@ -247,15 +251,17 @@ def load_and_put_declarative_data_sources(
247
251
layout_root_path (Optional[Path], optional):
248
252
Path to the root of the layout directory. Defaults to Path.cwd().
249
253
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.
251
257
test_data_sources (bool, optional):
252
258
If True, the connection of data sources is tested. Defaults to False.
253
259
254
260
Returns:
255
261
None
256
262
"""
257
263
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 )
259
265
260
266
@staticmethod
261
267
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
438
444
return ScanSqlResponse .from_api (self ._actions_api .scan_sql (data_source_id , sql_request .to_api ()))
439
445
440
446
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 ,
442
451
) -> None :
443
452
"""Tests connection to declarative data sources.
444
453
@@ -453,6 +462,8 @@ def test_data_sources_connection(
453
462
Declarative Data Sources object
454
463
credentials_path (Optional[Path], optional):
455
464
Path to the credentials. Defaults to None.
465
+ config_file (Optional[Union[str, Path]], optional):
466
+ Path to the config file. Defaults to None.
456
467
457
468
Raises:
458
469
ValueError:
@@ -461,7 +472,15 @@ def test_data_sources_connection(
461
472
Returns:
462
473
None
463
474
"""
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
+
465
484
errors : dict [str , str ] = dict ()
466
485
for declarative_data_source in declarative_data_sources .data_sources :
467
486
if credentials .get (declarative_data_source .id ) is not None :
0 commit comments